hamel@149: #!/usr/bin/env python hamel@149: # hamel@149: # audio_example.py hamel@149: # hamel@149: # hamel@261: # Created by Philippe Hamel (hamel.phil@gmail.com) on 10-12-22. hamel@149: # hamel@149: # Script to extract SAIs from an audio file using python hamel@149: hamel@261: import aimc hamel@261: import numpy hamel@266: from copy import copy hamel@261: hamel@266: def get_strobe_matrix(strobes,buf_len = 1024): hamel@266: n_channel = len(strobes) hamel@266: strobe_mat = numpy.zeros((n_channel,buf_len)) hamel@266: for ch in range(n_channel): hamel@266: for s in strobes[ch]: hamel@266: strobe_mat[ch,s] = 1. hamel@266: return strobe_mat hamel@266: hamel@266: hamel@266: do_plot=False hamel@149: # Example wav file hamel@149: wavfile = '../test_data/short_example.wav' hamel@149: hamel@261: #Loading audio file hamel@261: try: hamel@261: #pygmy is available at hg clone https://bitbucket.org/douglaseck/pygmy/ hamel@261: from pygmy.io.audio import AudioFile hamel@261: af=AudioFile(wavfile) hamel@261: x,sr,z=af.read(mono=True) hamel@261: except: hamel@261: from scipy.io import wavfile as WF hamel@261: (sr,x) = WF.read(wavfile) hamel@261: x=x.astype('float')/float(pow(2,15) - 1) hamel@149: hamel@261: if x.ndim <= 1: hamel@266: x=numpy.reshape(x,(x.shape[0],1)) hamel@263: sr = 1.*sr hamel@261: hamel@261: nChannels = x.shape[1] hamel@149: hamel@261: nSamples = x.shape[0] hamel@261: buffer_length = 1024 hamel@263: hamel@263: #Zero-padding hamel@263: #zero_pad = numpy.zeros((buffer_length-(nSamples%buffer_length),nChannels)).astype('float') hamel@263: #x = numpy.vstack((x,zero_pad)) hamel@263: hamel@263: #OR hamel@263: hamel@263: #Drop last incomplete frame (this is what happens in the C++ code) hamel@263: nFrames = x.shape[0]/buffer_length hamel@263: x = x[:nFrames*buffer_length] hamel@263: hamel@261: assert(x.shape[0]%buffer_length == 0) hamel@152: hamel@149: hamel@149: sig = aimc.SignalBank() hamel@261: sig.Initialize(nChannels,buffer_length,sr) hamel@149: hamel@261: pzfc = aimc.ModulePZFC(aimc.Parameters()) hamel@261: hcl = aimc.ModuleHCL(aimc.Parameters()) hamel@261: local_max = aimc.ModuleLocalMax(aimc.Parameters()) hamel@261: sai = aimc.ModuleSAI(aimc.Parameters()) hamel@149: hamel@261: pzfc.AddTarget(hcl) hamel@261: hcl.AddTarget(local_max) hamel@261: local_max.AddTarget(sai) hamel@149: hamel@149: global_params = aimc.Parameters() hamel@261: pzfc.Initialize(sig,global_params) hamel@260: hamel@261: output_list = [] hamel@266: strobe_list = [] hamel@266: centre_freq_list=[] hamel@149: hamel@261: for f in range(nFrames): hamel@261: for i in range(nChannels): hamel@261: sig.set_signal(i,x[buffer_length*f:buffer_length*(f+1),i]) hamel@260: hamel@261: pzfc.Process(sig) hamel@261: output_bank = sai.GetOutputBank() hamel@149: n_channel = output_bank.channel_count() hamel@149: sig_length = output_bank.buffer_length() hamel@149: output_matrix = numpy.zeros((n_channel,sig_length)) hamel@266: strobes=[] hamel@266: freqs=[] hamel@149: for i in range(n_channel): hamel@149: output_matrix[i] = numpy.array(output_bank.get_signal(i)) hamel@266: freqs.append(output_bank.centre_frequency(i)) hamel@266: channel_strobes = [] hamel@266: for j in range(output_bank.strobe_count(i)): hamel@266: channel_strobes.append(output_bank.strobe(i,j)) hamel@266: strobes.append(channel_strobes) hamel@266: centre_freq_list.append(freqs) hamel@266: strobe_list.append(strobes) hamel@261: output_list.append(output_matrix) hamel@266: # P.figure() hamel@266: # P.imshow(output_matrix, aspect='auto', origin='lower') hamel@266: hamel@261: hamel@263: print 'nFrames, nChannels, nSamples, sample_rate' hamel@263: print nFrames, n_channel, sig_length, sr hamel@263: hamel@263: hamel@261: if do_plot: hamel@261: import pylab as P hamel@261: P.figure() hamel@261: P.imshow(output_list[0], aspect='auto', origin='lower') hamel@261: hamel@261: hamel@261: ################ DEPRECATED CODE (kept here for reference) ######################### hamel@261: hamel@261: #if True: hamel@261: # output_bank=output_list[0] hamel@261: # n_channel = output_bank.channel_count() hamel@261: # sig_length = output_bank.buffer_length() hamel@261: # output_matrix = numpy.zeros((n_channel,sig_length)) hamel@261: # for i in range(n_channel): hamel@261: # output_matrix[i] = numpy.array(output_bank.get_signal(i)) hamel@261: # print output_matrix hamel@261: # print output_matrix.shape hamel@261: # print output_matrix.sum() hamel@149: hamel@149: #output_signal = numpy.array(output_bank.get_signal(0)) hamel@149: hamel@261: #def process_module(module, input_signal, global_params): hamel@261: # module.Initialize(input_signal,global_params) hamel@261: # module.Process(input_signal) hamel@261: # return module.GetOutputBank() hamel@261: hamel@261: #pzfc_params = aimc.Parameters() hamel@261: ##pzfc_params.SetString('name','PZFCFilterBank') hamel@261: ##pzfc_params.SetString('child1','NAP') hamel@261: # hamel@261: #hcl_params = aimc.Parameters() hamel@261: ##hcl_params.SetString('name','NAP') hamel@261: ##hcl_params.SetString('child1','NAP') hamel@261: # hamel@261: #global_params = aimc.Parameters() hamel@261: # hamel@261: #pzfc = aimc.ModulePZFC(pzfc_params) hamel@261: #pzfc.Initialize(sig,global_params) hamel@261: #pzfc.Process(sig) hamel@261: #pzfc_output = pzfc.GetOutputBank() hamel@261: # hamel@261: #hcl = aimc.ModuleHCL(hcl_params) hamel@261: #hcl_output = process_module(hcl, pzfc_output, global_params) hamel@261: # hamel@261: #local_max = aimc.ModuleLocalMax( aimc.Parameters()) hamel@261: #local_max_output = process_module(local_max, hcl_output, global_params) hamel@261: # hamel@261: #sai = aimc.ModuleSAI( aimc.Parameters()) hamel@266: #sai_output = process_module(sai, local_max_output, global_params)