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