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@448
|
12
|
hamel@448
|
13 do_plot=True
|
hamel@447
|
14 # Example wav file
|
hamel@447
|
15 wavfile = '../test_data/short_example.wav'
|
hamel@447
|
16
|
hamel@448
|
17 #Loading audio file
|
hamel@448
|
18 try:
|
hamel@448
|
19 #pygmy is available at hg clone https://bitbucket.org/douglaseck/pygmy/
|
hamel@448
|
20 from pygmy.io.audio import AudioFile
|
hamel@448
|
21 af=AudioFile(wavfile)
|
hamel@448
|
22 x,sr,z=af.read(mono=True)
|
hamel@448
|
23 except:
|
hamel@448
|
24 from scipy.io import wavfile as WF
|
hamel@448
|
25 (sr,x) = WF.read(wavfile)
|
hamel@448
|
26 x=x.astype('float')/float(pow(2,15) - 1)
|
hamel@447
|
27
|
hamel@448
|
28 if x.ndim <= 1:
|
hamel@448
|
29 x=reshape(x,(x.shape[0],1))
|
hamel@450
|
30 sr = 1.*sr
|
hamel@448
|
31
|
hamel@448
|
32 nChannels = x.shape[1]
|
hamel@447
|
33
|
hamel@448
|
34 nSamples = x.shape[0]
|
hamel@448
|
35 buffer_length = 1024
|
hamel@450
|
36
|
hamel@450
|
37 #Zero-padding
|
hamel@450
|
38 #zero_pad = numpy.zeros((buffer_length-(nSamples%buffer_length),nChannels)).astype('float')
|
hamel@450
|
39 #x = numpy.vstack((x,zero_pad))
|
hamel@450
|
40
|
hamel@450
|
41 #OR
|
hamel@450
|
42
|
hamel@450
|
43 #Drop last incomplete frame (this is what happens in the C++ code)
|
hamel@450
|
44 nFrames = x.shape[0]/buffer_length
|
hamel@450
|
45 x = x[:nFrames*buffer_length]
|
hamel@450
|
46
|
hamel@448
|
47 assert(x.shape[0]%buffer_length == 0)
|
hamel@447
|
48
|
hamel@447
|
49
|
hamel@447
|
50 sig = aimc.SignalBank()
|
hamel@448
|
51 sig.Initialize(nChannels,buffer_length,sr)
|
hamel@447
|
52
|
hamel@448
|
53 pzfc = aimc.ModulePZFC(aimc.Parameters())
|
hamel@448
|
54 hcl = aimc.ModuleHCL(aimc.Parameters())
|
hamel@448
|
55 local_max = aimc.ModuleLocalMax(aimc.Parameters())
|
hamel@448
|
56 sai = aimc.ModuleSAI(aimc.Parameters())
|
hamel@447
|
57
|
hamel@448
|
58 pzfc.AddTarget(hcl)
|
hamel@448
|
59 hcl.AddTarget(local_max)
|
hamel@448
|
60 local_max.AddTarget(sai)
|
hamel@447
|
61
|
hamel@447
|
62 global_params = aimc.Parameters()
|
hamel@448
|
63 pzfc.Initialize(sig,global_params)
|
hamel@447
|
64
|
hamel@448
|
65 output_list = []
|
hamel@450
|
66 bank_list =[]
|
hamel@447
|
67
|
hamel@448
|
68 for f in range(nFrames):
|
hamel@448
|
69 for i in range(nChannels):
|
hamel@448
|
70 sig.set_signal(i,x[buffer_length*f:buffer_length*(f+1),i])
|
hamel@447
|
71
|
hamel@448
|
72 pzfc.Process(sig)
|
hamel@448
|
73 output_bank = sai.GetOutputBank()
|
hamel@450
|
74 bank_list.append(output_bank)
|
hamel@447
|
75 n_channel = output_bank.channel_count()
|
hamel@447
|
76 sig_length = output_bank.buffer_length()
|
hamel@447
|
77 output_matrix = numpy.zeros((n_channel,sig_length))
|
hamel@447
|
78 for i in range(n_channel):
|
hamel@447
|
79 output_matrix[i] = numpy.array(output_bank.get_signal(i))
|
hamel@448
|
80 output_list.append(output_matrix)
|
hamel@448
|
81
|
hamel@450
|
82 print 'nFrames, nChannels, nSamples, sample_rate'
|
hamel@450
|
83 print nFrames, n_channel, sig_length, sr
|
hamel@450
|
84
|
hamel@450
|
85
|
hamel@448
|
86 if do_plot:
|
hamel@448
|
87 import pylab as P
|
hamel@448
|
88 P.figure()
|
hamel@448
|
89 P.imshow(output_list[0], aspect='auto', origin='lower')
|
hamel@448
|
90
|
hamel@448
|
91
|
hamel@448
|
92 ################ DEPRECATED CODE (kept here for reference) #########################
|
hamel@448
|
93
|
hamel@448
|
94 #if True:
|
hamel@448
|
95 # output_bank=output_list[0]
|
hamel@448
|
96 # n_channel = output_bank.channel_count()
|
hamel@448
|
97 # sig_length = output_bank.buffer_length()
|
hamel@448
|
98 # output_matrix = numpy.zeros((n_channel,sig_length))
|
hamel@448
|
99 # for i in range(n_channel):
|
hamel@448
|
100 # output_matrix[i] = numpy.array(output_bank.get_signal(i))
|
hamel@448
|
101 # print output_matrix
|
hamel@448
|
102 # print output_matrix.shape
|
hamel@448
|
103 # print output_matrix.sum()
|
hamel@447
|
104
|
hamel@447
|
105 #output_signal = numpy.array(output_bank.get_signal(0))
|
hamel@447
|
106
|
hamel@448
|
107 #def process_module(module, input_signal, global_params):
|
hamel@448
|
108 # module.Initialize(input_signal,global_params)
|
hamel@448
|
109 # module.Process(input_signal)
|
hamel@448
|
110 # return module.GetOutputBank()
|
hamel@448
|
111
|
hamel@448
|
112 #pzfc_params = aimc.Parameters()
|
hamel@448
|
113 ##pzfc_params.SetString('name','PZFCFilterBank')
|
hamel@448
|
114 ##pzfc_params.SetString('child1','NAP')
|
hamel@448
|
115 #
|
hamel@448
|
116 #hcl_params = aimc.Parameters()
|
hamel@448
|
117 ##hcl_params.SetString('name','NAP')
|
hamel@448
|
118 ##hcl_params.SetString('child1','NAP')
|
hamel@448
|
119 #
|
hamel@448
|
120 #global_params = aimc.Parameters()
|
hamel@448
|
121 #
|
hamel@448
|
122 #pzfc = aimc.ModulePZFC(pzfc_params)
|
hamel@448
|
123 #pzfc.Initialize(sig,global_params)
|
hamel@448
|
124 #pzfc.Process(sig)
|
hamel@448
|
125 #pzfc_output = pzfc.GetOutputBank()
|
hamel@448
|
126 #
|
hamel@448
|
127 #hcl = aimc.ModuleHCL(hcl_params)
|
hamel@448
|
128 #hcl_output = process_module(hcl, pzfc_output, global_params)
|
hamel@448
|
129 #
|
hamel@448
|
130 #local_max = aimc.ModuleLocalMax( aimc.Parameters())
|
hamel@448
|
131 #local_max_output = process_module(local_max, hcl_output, global_params)
|
hamel@448
|
132 #
|
hamel@448
|
133 #sai = aimc.ModuleSAI( aimc.Parameters())
|
hamel@448
|
134 #sai_output = process_module(sai, local_max_output, global_params) |