Datasets:
File size: 8,211 Bytes
b03e08e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
#!/usr/bin/env python
import numpy as np
# https://github.com/librosa/librosa
import librosa
import librosa.display
import argparse
import os
from PIL import Image
from PIL import PngImagePlugin
import json
import math
FLAGS = None
# ------------------------------------------------------
# get any args provided on the command line
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('mode', type=str, help='Choices: folder | single. folder mode for wav files in directory structure, single mode for single files', default='folder')
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('--sr', type=int, help='Samplerate', default=None)
parser.add_argument('--fftsize', type=int, help='Size of fft window in samples', default=1024)
parser.add_argument('--hopsize', type=int, help='Size of frame hop through sample file', default=256)
parser.add_argument('--dur', type=int, help='Make files this duration in sec. If unspecified keep original duration', default=None)
FLAGS, unparsed = parser.parse_known_args()
print('\n FLAGS parsed : {0}'.format(FLAGS))
#filetypes = ['.wav','.mp3'] not yet implimented. manually change fileExtList in listDirectory
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 wav2stft(fname, srate, fftSize, fftHop, dur) :
try:
audiodata, samplerate = librosa.load(fname, sr=srate, mono=True, duration=dur)
#print(np.amax(np.abs(audiodata)))
#print(np.amin(np.abs(audiodata)))
#print(audiodata[50:70])
except:
print('can not read ' + fname)
return
if srate == None:
print('Using native samplerate of ' + str(samplerate))
S = np.abs(librosa.stft(audiodata, n_fft=fftSize, hop_length=fftHop, win_length=fftSize, center=True))
#print(audiodata.shape)
#print(S.shape)
return S
def log_scale(x):
output = np.log1p(x)
return output
def findMinMax(img):
return np.amin(img),np.amax(img)
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)) #info required to reverse scaling
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 checkScaling(topdir, outdir, srate, fftSize, fftHop, dur):
"""
Returns the max and min values of the log magnitude after STFT in the whole dataset.
This is to provide a known and standardized mapping from [min,max] -> [0,255] when saving as a png image.
Parameters
topdir - the dir containing class folders containing wav files.
outdir - the top level directory to write wave files to (written in to class subfolders)
dur - (in seconds) all files will be truncated or zeropadded to have this duration given the srate
srate - input files will be resampled to srate as they are read in before being saved as wav files
"""
print("Now determining maximum log magnitude value in dataset for scaling to png...")
subdirs = get_subdirs(topdir)
max_mag = 0
min_mag = 0
for subdir in subdirs:
fullpaths, _ = listDirectory(topdir + '/' + subdir, '.wav')
for idx in range(len(fullpaths)) :
fname = os.path.basename(fullpaths[idx])
D = wav2stft(fullpaths[idx], srate, fftSize, fftHop, dur)
D = log_scale(D)
minM,maxM = findMinMax(D)
if minM > min_mag:
min_mag = minM
if maxM > max_mag:
max_mag = maxM
print("Dataset: min magnitude=",min_mag,"max magnitude=",max_mag)
minScale = int(math.floor(min_mag))
maxScale = int(math.ceil(max_mag))
#print("New scale: min magnitude=",minScale,"max magnitude=",maxScale)
print("Using [{0},{1}] -> [0,255] for png conversion".format(minScale,maxScale))
pnginfo = {}
pnginfo['datasetMin'] = str(min_mag)
pnginfo['datasetMax'] = str(max_mag)
pnginfo['scaleMin'] = str(minScale)
pnginfo['scaleMax'] = str(maxScale)
return pnginfo
def wav2Spect(topdir, outdir, srate, fftSize, fftHop, dur, pnginfo) :
"""
Creates spectrograms for subfolder-labeled wavfiles.
Creates class folders for the spectrogram files in outdir with the same structure found in topdir.
Parameters
topdir - the dir containing class folders containing wav files.
outdir - the top level directory to write wave files to (written in to class subfolders)
dur - (in seconds) all files will be truncated or zeropadded to have this duration given the srate
srate - input files will be resampled to srate as they are read in before being saved as wav files
"""
subdirs = get_subdirs(topdir)
count = 0
for subdir in subdirs:
fullpaths, _ = listDirectory(topdir + '/' + subdir, '.wav')
for idx in range(len(fullpaths)) :
fname = os.path.basename(fullpaths[idx])
D = wav2stft(fullpaths[idx], srate, fftSize, fftHop, dur)
D = log_scale(D)
try:
os.stat(outdir + '/' + subdir) # test for existence
except:
os.makedirs(outdir + '/' + subdir) # create if necessary
print(str(count) + ': ' + subdir + '/' + os.path.splitext(fname)[0])
logSpect2PNG(D, outdir+'/'+subdir+'/'+os.path.splitext(fname)[0]+'.png', lwinfo=pnginfo)
count +=1
print("COMPLETE")
def wav2Spect_single(filename, srate, fftSize, fftHop, dur) :
"""
Creates spectrograms from single wavfiles.
"""
D = wav2stft(filename, srate, fftSize, fftHop, dur)
D = log_scale(D)
minM,maxM = findMinMax(D)
print("Dataset: min magnitude=",minM,"max magnitude=",maxM)
minScale = int(math.floor(minM))
maxScale = int(math.ceil(maxM))
print("Using [{0},{1}] -> [0,255] for png conversion".format(minScale,maxScale))
pnginfo = {}
pnginfo['datasetMin'] = str(minM)
pnginfo['datasetMax'] = str(maxM)
pnginfo['scaleMin'] = str(minScale)
pnginfo['scaleMax'] = str(maxScale)
print(str(0) + ': ' + os.path.splitext(filename)[0])
logSpect2PNG(D, os.path.splitext(filename)[0] +'.png',pnginfo)
print("COMPLETE")
if FLAGS.mode == 'folder':
pnginfo = checkScaling(FLAGS.rootdir,FLAGS.outdir,FLAGS.sr,FLAGS.fftsize,FLAGS.hopsize,FLAGS.dur)
wav2Spect(FLAGS.rootdir,FLAGS.outdir,FLAGS.sr,FLAGS.fftsize,FLAGS.hopsize,FLAGS.dur,pnginfo)
elif FLAGS.mode == 'single':
wav2Spect_single(FLAGS.filename,FLAGS.sr,FLAGS.fftsize,FLAGS.hopsize,FLAGS.dur)
else:
raise ValueError("Not an acceptable mode! Choices: folder | single. folder mode for wav files in directory structure, single mode for single files")
|