|
|
|
import numpy as np |
|
|
|
import librosa |
|
import librosa.display |
|
import argparse |
|
import os |
|
from PIL import Image |
|
from PIL import PngImagePlugin |
|
import json |
|
|
|
from spsi import spsi |
|
|
|
FLAGS = None |
|
|
|
|
|
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter) |
|
parser.add_argument('filename', type=str, help='Name of log mag spectrogram. Include extension') |
|
parser.add_argument('--outdir', type=str, help='Output directory', default='./output') |
|
parser.add_argument('--scalemax', type=int, help='Value to use as the max when scaling from png [0,255] to original [min,max]', default=None) |
|
parser.add_argument('--scalemin', type=int, help='Value to use as the min when scaling from png [0,255] to original [min,max]', default=None) |
|
parser.add_argument('--sr', type=int, help='Samplerate', default=22050) |
|
parser.add_argument('--hopsize', type=int, help='Size of frame hop through sample file', default=256) |
|
parser.add_argument('--glsteps', type=int, help='Number of Griffin&Lim iterations following SPSI', default=50 ) |
|
parser.add_argument('--wavfile', type=str, help='Optional name for output audio file. Unspecified means use the png filename', default=None) |
|
|
|
FLAGS, unparsed = parser.parse_known_args() |
|
print('\n FLAGS parsed : {0}'.format(FLAGS)) |
|
|
|
|
|
def inv_log(img): |
|
img = np.exp(img) - 1. |
|
return img |
|
|
|
|
|
def PNG2LogSpect(fname,scalemin,scalemax): |
|
|
|
""" |
|
Read png spectrograms, expand to original scale and return numpy array. |
|
If not stored in one of png metadata, the values needed to undo previous scaling are required to be specified. |
|
""" |
|
img = Image.open(fname) |
|
|
|
|
|
try: |
|
img.text = img.text |
|
lwinfo = json.loads(img.text['meta']) |
|
except: |
|
print('PNG2LogSpect: no img.text, using user specified values!') |
|
lwinfo = {} |
|
lwinfo['scaleMin'] = scalemin |
|
lwinfo['scaleMax'] = scalemax |
|
|
|
|
|
minx, maxx = float(lwinfo['scaleMin']), float(lwinfo['scaleMax']) |
|
|
|
|
|
img = img.convert('L') |
|
outimg = np.asarray(img, dtype=np.float32) |
|
outimg = (outimg - 0)/(255-0)*(maxx-minx) + minx |
|
|
|
return np.flipud(outimg), lwinfo |
|
|
|
|
|
D,_ = PNG2LogSpect(FLAGS.filename,FLAGS.scalemin,FLAGS.scalemax) |
|
Dsize, _ = D.shape |
|
fftsize = 2*(Dsize-1) |
|
|
|
magD = inv_log(D) |
|
y_out = spsi(magD, fftsize=fftsize, hop_length=FLAGS.hopsize) |
|
|
|
|
|
|
|
if FLAGS.glsteps != 0 : |
|
x = librosa.stft(y_out, fftsize, FLAGS.hopsize, center=False) |
|
p = np.angle(x) |
|
|
|
for i in range(FLAGS.glsteps): |
|
S = magD * np.exp(1j*p) |
|
y_out = librosa.istft(S, FLAGS.hopsize, center=True) |
|
p = np.angle(librosa.stft(y_out, fftsize, FLAGS.hopsize, center=True)) |
|
|
|
scalefactor = np.amax(np.abs(y_out)) |
|
|
|
|
|
print('scaling peak sample, ' + str(scalefactor) + ' to 1') |
|
|
|
|
|
if FLAGS.wavfile == None: |
|
librosa.output.write_wav(os.path.splitext(FLAGS.filename)[0]+'.wav', y_out, FLAGS.sr) |
|
else: |
|
librosa.output.write_wav(FLAGS.wavfile, y_out, FLAGS.sr) |
|
|
|
|
|
|
|
|