annotate trunk/swig/audio_example.py @ 706:f8e90b5d85fd tip

Delete CARFAC code from this repository. It has been moved to https://github.com/google/carfac Please email me with your github username to get access. I've also created a new mailing list to discuss CARFAC development: https://groups.google.com/forum/#!forum/carfac-dev
author ronw@google.com
date Thu, 18 Jul 2013 20:56:51 +0000
parents 649c3390301e
children
rev   line source
hamel@447 1 #!/usr/bin/env python
hamel@447 2 #
hamel@447 3 # audio_example.py
hamel@447 4 #
hamel@447 5 #
hamel@448 6 # Created by Philippe Hamel (hamel.phil@gmail.com) on 10-12-22.
hamel@447 7 #
hamel@447 8 # Script to extract SAIs from an audio file using python
hamel@447 9
hamel@448 10 import aimc
hamel@448 11 import numpy
hamel@453 12 from copy import copy
hamel@448 13
hamel@453 14 def get_strobe_matrix(strobes,buf_len = 1024):
hamel@453 15 n_channel = len(strobes)
hamel@453 16 strobe_mat = numpy.zeros((n_channel,buf_len))
hamel@453 17 for ch in range(n_channel):
hamel@453 18 for s in strobes[ch]:
hamel@453 19 strobe_mat[ch,s] = 1.
hamel@453 20 return strobe_mat
hamel@453 21
hamel@453 22
hamel@453 23 do_plot=False
hamel@447 24 # Example wav file
hamel@447 25 wavfile = '../test_data/short_example.wav'
hamel@447 26
hamel@448 27 #Loading audio file
hamel@448 28 try:
hamel@448 29 #pygmy is available at hg clone https://bitbucket.org/douglaseck/pygmy/
hamel@448 30 from pygmy.io.audio import AudioFile
hamel@448 31 af=AudioFile(wavfile)
hamel@448 32 x,sr,z=af.read(mono=True)
hamel@448 33 except:
hamel@448 34 from scipy.io import wavfile as WF
hamel@448 35 (sr,x) = WF.read(wavfile)
hamel@448 36 x=x.astype('float')/float(pow(2,15) - 1)
hamel@447 37
hamel@448 38 if x.ndim <= 1:
hamel@453 39 x=numpy.reshape(x,(x.shape[0],1))
hamel@450 40 sr = 1.*sr
hamel@448 41
hamel@448 42 nChannels = x.shape[1]
hamel@447 43
hamel@448 44 nSamples = x.shape[0]
hamel@448 45 buffer_length = 1024
hamel@450 46
hamel@450 47 #Zero-padding
hamel@450 48 #zero_pad = numpy.zeros((buffer_length-(nSamples%buffer_length),nChannels)).astype('float')
hamel@450 49 #x = numpy.vstack((x,zero_pad))
hamel@450 50
hamel@450 51 #OR
hamel@450 52
hamel@450 53 #Drop last incomplete frame (this is what happens in the C++ code)
hamel@450 54 nFrames = x.shape[0]/buffer_length
hamel@450 55 x = x[:nFrames*buffer_length]
hamel@450 56
hamel@448 57 assert(x.shape[0]%buffer_length == 0)
hamel@447 58
hamel@447 59
hamel@447 60 sig = aimc.SignalBank()
hamel@448 61 sig.Initialize(nChannels,buffer_length,sr)
hamel@447 62
hamel@448 63 pzfc = aimc.ModulePZFC(aimc.Parameters())
hamel@448 64 hcl = aimc.ModuleHCL(aimc.Parameters())
hamel@448 65 local_max = aimc.ModuleLocalMax(aimc.Parameters())
hamel@448 66 sai = aimc.ModuleSAI(aimc.Parameters())
hamel@447 67
hamel@448 68 pzfc.AddTarget(hcl)
hamel@448 69 hcl.AddTarget(local_max)
hamel@448 70 local_max.AddTarget(sai)
hamel@447 71
hamel@447 72 global_params = aimc.Parameters()
hamel@448 73 pzfc.Initialize(sig,global_params)
hamel@447 74
hamel@448 75 output_list = []
hamel@453 76 strobe_list = []
hamel@453 77 centre_freq_list=[]
hamel@447 78
hamel@448 79 for f in range(nFrames):
hamel@448 80 for i in range(nChannels):
hamel@448 81 sig.set_signal(i,x[buffer_length*f:buffer_length*(f+1),i])
hamel@447 82
hamel@448 83 pzfc.Process(sig)
hamel@448 84 output_bank = sai.GetOutputBank()
hamel@447 85 n_channel = output_bank.channel_count()
hamel@447 86 sig_length = output_bank.buffer_length()
hamel@447 87 output_matrix = numpy.zeros((n_channel,sig_length))
hamel@453 88 strobes=[]
hamel@453 89 freqs=[]
hamel@447 90 for i in range(n_channel):
hamel@447 91 output_matrix[i] = numpy.array(output_bank.get_signal(i))
hamel@453 92 freqs.append(output_bank.centre_frequency(i))
hamel@453 93 channel_strobes = []
hamel@453 94 for j in range(output_bank.strobe_count(i)):
hamel@453 95 channel_strobes.append(output_bank.strobe(i,j))
hamel@453 96 strobes.append(channel_strobes)
hamel@453 97 centre_freq_list.append(freqs)
hamel@453 98 strobe_list.append(strobes)
hamel@448 99 output_list.append(output_matrix)
hamel@453 100 # P.figure()
hamel@453 101 # P.imshow(output_matrix, aspect='auto', origin='lower')
hamel@453 102
hamel@448 103
hamel@450 104 print 'nFrames, nChannels, nSamples, sample_rate'
hamel@450 105 print nFrames, n_channel, sig_length, sr
hamel@450 106
hamel@450 107
hamel@448 108 if do_plot:
hamel@448 109 import pylab as P
hamel@448 110 P.figure()
hamel@448 111 P.imshow(output_list[0], aspect='auto', origin='lower')
hamel@448 112
hamel@448 113
hamel@448 114 ################ DEPRECATED CODE (kept here for reference) #########################
hamel@448 115
hamel@448 116 #if True:
hamel@448 117 # output_bank=output_list[0]
hamel@448 118 # n_channel = output_bank.channel_count()
hamel@448 119 # sig_length = output_bank.buffer_length()
hamel@448 120 # output_matrix = numpy.zeros((n_channel,sig_length))
hamel@448 121 # for i in range(n_channel):
hamel@448 122 # output_matrix[i] = numpy.array(output_bank.get_signal(i))
hamel@448 123 # print output_matrix
hamel@448 124 # print output_matrix.shape
hamel@448 125 # print output_matrix.sum()
hamel@447 126
hamel@447 127 #output_signal = numpy.array(output_bank.get_signal(0))
hamel@447 128
hamel@448 129 #def process_module(module, input_signal, global_params):
hamel@448 130 # module.Initialize(input_signal,global_params)
hamel@448 131 # module.Process(input_signal)
hamel@448 132 # return module.GetOutputBank()
hamel@448 133
hamel@448 134 #pzfc_params = aimc.Parameters()
hamel@448 135 ##pzfc_params.SetString('name','PZFCFilterBank')
hamel@448 136 ##pzfc_params.SetString('child1','NAP')
hamel@448 137 #
hamel@448 138 #hcl_params = aimc.Parameters()
hamel@448 139 ##hcl_params.SetString('name','NAP')
hamel@448 140 ##hcl_params.SetString('child1','NAP')
hamel@448 141 #
hamel@448 142 #global_params = aimc.Parameters()
hamel@448 143 #
hamel@448 144 #pzfc = aimc.ModulePZFC(pzfc_params)
hamel@448 145 #pzfc.Initialize(sig,global_params)
hamel@448 146 #pzfc.Process(sig)
hamel@448 147 #pzfc_output = pzfc.GetOutputBank()
hamel@448 148 #
hamel@448 149 #hcl = aimc.ModuleHCL(hcl_params)
hamel@448 150 #hcl_output = process_module(hcl, pzfc_output, global_params)
hamel@448 151 #
hamel@448 152 #local_max = aimc.ModuleLocalMax( aimc.Parameters())
hamel@448 153 #local_max_output = process_module(local_max, hcl_output, global_params)
hamel@448 154 #
hamel@448 155 #sai = aimc.ModuleSAI( aimc.Parameters())
hamel@453 156 #sai_output = process_module(sai, local_max_output, global_params)