|
|
|
import numpy as np |
|
|
|
import librosa |
|
import librosa.display |
|
import argparse |
|
import os |
|
from PIL import Image |
|
from PIL import PngImagePlugin |
|
import json |
|
import scipy |
|
|
|
|
|
FLAGS = None |
|
|
|
|
|
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter) |
|
parser.add_argument('--conversion', type=str, choices=['spec2cqt','cqt2spec'], help='direction of conversion', default='spec2cqt') |
|
parser.add_argument('--filename', type=str, help='For single mode, enter filename', default=None) |
|
parser.add_argument('--rootdir', type=str, help='Roots folder where class folders containing audio files are kept', default='./') |
|
parser.add_argument('--outdir', type=str, help='Output directory', default='./output') |
|
|
|
parser.add_argument('--fftsize', type=int, help='Size of fft window in samples', default=1024) |
|
|
|
|
|
FLAGS, unparsed = parser.parse_known_args() |
|
print('\n FLAGS parsed : {0}'.format(FLAGS)) |
|
|
|
|
|
|
|
def get_subdirs(a_dir): |
|
""" Returns a list of sub directory names in a_dir """ |
|
return [name for name in os.listdir(a_dir) |
|
if (os.path.isdir(os.path.join(a_dir, name)) and not (name.startswith('.')))] |
|
|
|
|
|
def listDirectory(directory, fileExtList): |
|
"""Returns a list of file info objects in directory that extension in the list fileExtList - include the . in your extension string""" |
|
fnameList = [os.path.normcase(f) |
|
for f in os.listdir(directory) |
|
if (not(f.startswith('.')))] |
|
fileList = [os.path.join(directory, f) |
|
for f in fnameList |
|
if os.path.splitext(f)[1] in fileExtList] |
|
return fileList , fnameList |
|
|
|
|
|
def logSpect2PNG(outimg, fname, lwinfo=None) : |
|
|
|
info = PngImagePlugin.PngInfo() |
|
lwinfo = lwinfo or {} |
|
lwinfo['fileMin'] = str(np.amin(outimg)) |
|
lwinfo['fileMax'] = str(np.amax(outimg)) |
|
info.add_text('meta',json.dumps(lwinfo)) |
|
|
|
shift = int(lwinfo['scaleMax']) - int(lwinfo['scaleMin']) |
|
SC2 = 255*(outimg-int(lwinfo['scaleMin']))/shift |
|
savimg = Image.fromarray(np.flipud(SC2)) |
|
|
|
pngimg = savimg.convert('L') |
|
pngimg.save(fname,pnginfo=info) |
|
|
|
|
|
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 |
|
|
|
|
|
def logfmap(I, L, H) : |
|
""" |
|
% [M,N] = logfmap(I,L,H) |
|
I - number of rows in the original spectrogram |
|
L - low bin to preserve |
|
H - high bin to preserve |
|
|
|
% Return a maxtrix for premultiplying spectrograms to map |
|
% the rows into a log frequency space. |
|
% Output map covers bins L to H of input |
|
% L must be larger than 1, since the lowest bin of the FFT |
|
% (corresponding to 0 Hz) cannot be represented on a |
|
% log frequency axis. Including bins close to 1 makes |
|
% the number of output rows exponentially larger. |
|
% N returns the recovery matrix such that N*M is approximately I |
|
% (for dimensions L to H). |
|
% |
|
% Ported from MATLAB code written by Dan Ellis: |
|
% 2004-05-21 dpwe@ee.columbia.edu |
|
""" |
|
ratio = (H-1)/H; |
|
opr = np.int(np.round(np.log(L/H)/np.log(ratio))) |
|
print('opr is ' + str(opr)) |
|
ibin = L*np.exp(list(range(0,opr)*-np.log(ratio))) |
|
|
|
M = np.zeros((opr,I)) |
|
eps=np.finfo(float).eps |
|
|
|
for i in range(0, opr) : |
|
|
|
|
|
|
|
|
|
|
|
tt = np.multiply(np.pi, (list(range(0,I))-ibin[i])) |
|
M[i,:] = np.divide((np.sin(tt)+eps) , (tt+eps)); |
|
|
|
|
|
G = np.ones((I)); |
|
print ('H is ' + str(H)) |
|
G[0:H] = np.divide(list(range(0,H)), H) |
|
|
|
N = np.transpose(np.multiply(M,np.matlib.repmat(G,opr,1))) |
|
|
|
return M,N |
|
|
|
def spect2CQT(topdir, outdir, fftSize, lowRow=1): |
|
""" |
|
Creates psuedo constant-Q spectrograms from linear frequency spectrograms. |
|
Creates class folders in outdir with the same structure found in topdir. |
|
|
|
Parameters |
|
topdir - the dir containing class folders containing png (log magnitude) spectrogram files. |
|
outdir - the top level directory to write psuedo constantQ files to (written in to class subfolders) |
|
lowRow is the lowest row in the FFT that you want to include in the psuedo constant Q spectrogram |
|
""" |
|
|
|
|
|
LIN_FREQ_BINS = int(fftSize/2+1) |
|
LOW_ROW = lowRow |
|
LOG_FREQ_BINS = int(fftSize/2+1) |
|
M,N = logfmap(LIN_FREQ_BINS,LOW_ROW,LOG_FREQ_BINS) |
|
|
|
|
|
subdirs = get_subdirs(topdir) |
|
count = 0 |
|
for subdir in subdirs: |
|
|
|
fullpaths, _ = listDirectory(topdir + '/' + subdir, '.png') |
|
|
|
for idx in range(len(fullpaths)) : |
|
fname = os.path.basename(fullpaths[idx]) |
|
D, pnginfo = PNG2LogSpect(fullpaths[idx],None,None) |
|
|
|
|
|
MD = np.dot(M,D) |
|
MD = scipy.signal.resample(MD, LIN_FREQ_BINS) |
|
|
|
|
|
|
|
pnginfo["linFreqBins"] = LIN_FREQ_BINS |
|
pnginfo["lowRow"] = LOW_ROW |
|
pnginfo["logFreqBins"] = LOG_FREQ_BINS |
|
|
|
try: |
|
os.stat(outdir + '/' + subdir) |
|
except: |
|
os.makedirs(outdir + '/' + subdir) |
|
|
|
print(str(count) + ': ' + subdir + '/' + os.path.splitext(fname)[0]) |
|
logSpect2PNG(MD, outdir+'/'+subdir+'/'+os.path.splitext(fname)[0]+'.png',lwinfo=pnginfo) |
|
|
|
count +=1 |
|
print("COMPLETE") |
|
|
|
def CQT2spec(topdir,outdir): |
|
|
|
subdirs = get_subdirs(topdir) |
|
count = 0 |
|
for subdir in subdirs: |
|
|
|
fullpaths, _ = listDirectory(topdir + '/' + subdir, '.png') |
|
|
|
for idx in range(len(fullpaths)) : |
|
fname = os.path.basename(fullpaths[idx]) |
|
D, pnginfo = PNG2LogSpect(fullpaths[idx],None,None) |
|
M,N = logfmap(pnginfo["linFreqBins"],pnginfo["lowRow"],pnginfo["logFreqBins"]) |
|
resampledD = scipy.signal.resample(D, M.shape[0]) |
|
|
|
|
|
ND = np.dot(N,resampledD) |
|
|
|
try: |
|
os.stat(outdir + '/' + subdir) |
|
except: |
|
os.makedirs(outdir + '/' + subdir) |
|
|
|
print(str(count) + ': ' + subdir + '/' + os.path.splitext(fname)[0]) |
|
logSpect2PNG(ND, outdir+'/'+subdir+'/'+os.path.splitext(fname)[0]+'.png',lwinfo=pnginfo) |
|
|
|
count +=1 |
|
print("COMPLETE") |
|
|
|
if FLAGS.conversion == 'spec2cqt': |
|
spect2CQT(FLAGS.rootdir,FLAGS.outdir,FLAGS.fftsize,lowRow=10) |
|
elif FLAGS.conversion == 'cqt2spec': |
|
CQT2spec(FLAGS.rootdir,FLAGS.outdir) |