def file_reader(filename, chan): file_base, file_ext = os.path.splitext(filename) if file_ext.lower() == '.aup': import audacity aud = audacity.Aup(filename) w = aud.get_channel_data(chan) sr = aud.rate else: sys.stderr.write("Format not recognized: %s" % file_ext) return return (sr, w)
sep.append([inputfile, float(t0), float(t1)]) outpath = "data_split/" os.makedirs(outpath, exist_ok=True) def get_out_wav_filename(aupfilename): fname, ext = os.path.splitext(filename) wavfilename = fname + ".wav" wavbasename = os.path.basename(wavfilename) out = outpath + "_".join(wavbasename.split(" ")) return out for filename in sys.argv[1:]: aup = audacity.Aup(filename) out = get_out_wav_filename(filename) labels = [] for label in aup.get_labels(0): t0 = label[0] t1 = label[1] l = label[2] interval = t1 - t0 if interval <= 0.0: print("[ERROR]", out, filename) continue count = 0 for s in sep: if s[0].startswith(out): #if not (s[2]<t0 or t1<s[1]):#overlap if (t0 <= s[1] and s[2] <= t1): #including
def get_fsdata(aupfile, *args, nfft=1024): """ Pads the data and utilizes the numpy.fft package to: - get the discrete fourier transforms of the (raw) audio - get the corresponding frequency axes Input ----- aupfile : audacity project file nfft : number of samples to use for fft calculation (power of 2 recommended - defaults to 1024) *args: (optional) channel numbers starting with 0 in the following order... internal, external, labium, infrared defaults are 0, 1, 2, 3 Output (numpy array format) ------ returns a dictionary of the data with the following keys: 'int_fs' : internal fourier spectrum data 'ext_fs' : external fourier spectrum data 'lab_fs' : labium fourier spectrum data 'int_wf', 'ext_wf', 'lab_wf' : corresponding waveforms optional keys returned only if infrared channel is given: 'ir' : infrared sensor RMS amplitude """ auf = audacity.Aup(aupfile) print(aupfile) sr = auf.rate rawdata = [] chnums = [] maxlen = 0 for chno in range(auf.nchannels): chnums.append(chno) rawdata.append(auf.get_channel_data(chno)) maxlen = max(maxlen, len(rawdata[-1])) data = np.zeros((len(rawdata), maxlen)) for chno, chdata in enumerate(rawdata): data[chno, :len(chdata)] = chdata chtags = [] if len(args) == 0: if len(data) < 2 or len(data) > 4: raise ValueError("Invalid number of data channels.", "Use *args to specify channel numbers") else: chtags = chnums else: chtags = list(args) chdict = {} chnames = ['int', 'ext', 'lab', 'ir'] for tag, num in zip(chtags, chnums): chdict[chnames[num]] = data[tag] outdict = {} for chname, chdata in chdict.items(): if chname == 'ir': outdict['ir'] = np.sqrt(np.mean((chdata - np.mean(chdata))**2)) else: fourier = np.fft.rfft(chdata, nfft) outdict[chname + '_fs'] = 20 * np.log10(np.abs(fourier)) outdict[chname + '_wf'] = chdata return outdict
def get_tfdata(aupfile, *args, nfft, testing=False): """ Utilizes the TransferFunctions package to: - pad the data and remove audio delays - estimate the transfer function from an external to an internal receiver Input ----- aupfile : audacity project file nfft : window length for transfer function calculation testing : (optional) boolean, if True returns additional transfer functions from source to receivers *args: (optional) channel numbers starting with 0 in the following order... internal, external, source, infrared defaults are 0, 1, 2, 3 Output (numpy array format) ------ returns a dictionary of the data with the following keys: 'tf' : transfer function amplitude data 'f' : transfer function frequency data optional keys returned only if infrared channel is given: 'ir' : infrared sensor RMS amplitude optional keys returned only if testing==True: 'tf_src_int' : transfer function from source to internal 'tf_src_ext' : transfer function from source to external 'f_int', 'f_ext' : corresponding frequency values """ auf = audacity.Aup(aupfile) print(aupfile) sr = auf.rate rawdata = [] chnums = [] maxlen = 0 for chno in range(auf.nchannels): chnums.append(chno) rawdata.append(auf.get_channel_data(chno)) maxlen = max(maxlen, len(rawdata[-1])) data = np.zeros((len(rawdata), maxlen)) for chno, chdata in enumerate(rawdata): data[chno, :len(chdata)] = chdata chtags = [] if len(args) == 0: if len(data) < 2 or len(data) > 4: raise ValueError("Unrecognised number of data channels.", "Use *args to specify channel numbers") else: chtags = chnums else: chtags = list(args) chdict = {} chnames = ['int', 'ext', 'src', 'ir'] for tag, num in zip(chtags, chnums): chdict[chnames[num]] = data[tag] outdict = {} tfxy, ff = tf.tfe(chdict['int'], chdict['ext'], Fs=sr, NFFT=nfft) outdict['tf'] = np.array(tfxy) outdict['f'] = np.array(ff) for chname, chdata in chdict.items(): if chname == 'ir': outdict['ir'] = np.sqrt(np.mean((chdata - np.mean(chdata))**2)) if testing == True: delay = tf.determineDelay(chdict['src'] / np.mean(chdict['src']), chdict['ext'] / np.mean(chdict['ext']), maxdel=2**15) print("Delay: %d samples" % delay) chdict['src'] = np.roll(chdict['src'], delay) for chname, chdata in zip(chnames[0:2], [chdict['int'], chdict['ext']]): tfxy, ff = tf.tfe(chdata, chdict['src'], Fs=sr, NFFT=nfft) outdict['tf_src_%s' % chname] = np.array(tfxy) outdict['f_%s' % chname] = np.array(ff) return outdict