hamel@149
|
1 #!/usr/bin/env python
|
hamel@149
|
2 #
|
hamel@149
|
3 # audio_example.py
|
hamel@149
|
4 #
|
hamel@149
|
5 #
|
hamel@261
|
6 # Created by Philippe Hamel (hamel.phil@gmail.com) on 10-12-22.
|
hamel@149
|
7 #
|
hamel@149
|
8 # Script to extract SAIs from an audio file using python
|
hamel@149
|
9
|
hamel@261
|
10 import aimc
|
hamel@261
|
11 import numpy
|
hamel@261
|
12
|
hamel@261
|
13 do_plot=True
|
hamel@149
|
14 # Example wav file
|
hamel@149
|
15 wavfile = '../test_data/short_example.wav'
|
hamel@149
|
16
|
hamel@261
|
17 #Loading audio file
|
hamel@261
|
18 try:
|
hamel@261
|
19 #pygmy is available at hg clone https://bitbucket.org/douglaseck/pygmy/
|
hamel@261
|
20 from pygmy.io.audio import AudioFile
|
hamel@261
|
21 af=AudioFile(wavfile)
|
hamel@261
|
22 x,sr,z=af.read(mono=True)
|
hamel@261
|
23 except:
|
hamel@261
|
24 from scipy.io import wavfile as WF
|
hamel@261
|
25 (sr,x) = WF.read(wavfile)
|
hamel@261
|
26 x=x.astype('float')/float(pow(2,15) - 1)
|
hamel@149
|
27
|
hamel@261
|
28 if x.ndim <= 1:
|
hamel@261
|
29 x=reshape(x,(x.shape[0],1))
|
hamel@261
|
30
|
hamel@261
|
31 nChannels = x.shape[1]
|
hamel@149
|
32
|
hamel@261
|
33 nSamples = x.shape[0]
|
hamel@261
|
34 buffer_length = 1024
|
hamel@261
|
35 zero_pad = numpy.zeros((buffer_length-(nSamples%buffer_length),nChannels)).astype('float')
|
hamel@261
|
36 print x.shape, zero_pad.shape
|
hamel@261
|
37 x = numpy.vstack((x,zero_pad))
|
hamel@261
|
38 print x.shape
|
hamel@261
|
39 assert(x.shape[0]%buffer_length == 0)
|
hamel@152
|
40
|
hamel@261
|
41 nFrames = x.shape[0]/buffer_length
|
hamel@261
|
42 print nFrames
|
hamel@149
|
43
|
hamel@149
|
44 sig = aimc.SignalBank()
|
hamel@261
|
45 sig.Initialize(nChannels,buffer_length,sr)
|
hamel@149
|
46
|
hamel@261
|
47 pzfc = aimc.ModulePZFC(aimc.Parameters())
|
hamel@261
|
48 hcl = aimc.ModuleHCL(aimc.Parameters())
|
hamel@261
|
49 local_max = aimc.ModuleLocalMax(aimc.Parameters())
|
hamel@261
|
50 sai = aimc.ModuleSAI(aimc.Parameters())
|
hamel@149
|
51
|
hamel@261
|
52 pzfc.AddTarget(hcl)
|
hamel@261
|
53 hcl.AddTarget(local_max)
|
hamel@261
|
54 local_max.AddTarget(sai)
|
hamel@149
|
55
|
hamel@149
|
56 global_params = aimc.Parameters()
|
hamel@261
|
57 pzfc.Initialize(sig,global_params)
|
hamel@260
|
58
|
hamel@149
|
59
|
hamel@261
|
60 ##One chunk only
|
hamel@261
|
61 #chunk=x[:buffer_length]
|
hamel@149
|
62
|
hamel@261
|
63 output_list = []
|
hamel@149
|
64
|
hamel@261
|
65 for f in range(nFrames):
|
hamel@261
|
66 for i in range(nChannels):
|
hamel@261
|
67 sig.set_signal(i,x[buffer_length*f:buffer_length*(f+1),i])
|
hamel@260
|
68
|
hamel@261
|
69 pzfc.Process(sig)
|
hamel@261
|
70 output_bank = sai.GetOutputBank()
|
hamel@149
|
71 n_channel = output_bank.channel_count()
|
hamel@149
|
72 sig_length = output_bank.buffer_length()
|
hamel@149
|
73 output_matrix = numpy.zeros((n_channel,sig_length))
|
hamel@149
|
74 for i in range(n_channel):
|
hamel@149
|
75 output_matrix[i] = numpy.array(output_bank.get_signal(i))
|
hamel@261
|
76 output_list.append(output_matrix)
|
hamel@261
|
77
|
hamel@261
|
78 if do_plot:
|
hamel@261
|
79 import pylab as P
|
hamel@261
|
80 P.figure()
|
hamel@261
|
81 P.imshow(output_list[0], aspect='auto', origin='lower')
|
hamel@261
|
82
|
hamel@261
|
83
|
hamel@261
|
84 ################ DEPRECATED CODE (kept here for reference) #########################
|
hamel@261
|
85
|
hamel@261
|
86 #if True:
|
hamel@261
|
87 # output_bank=output_list[0]
|
hamel@261
|
88 # n_channel = output_bank.channel_count()
|
hamel@261
|
89 # sig_length = output_bank.buffer_length()
|
hamel@261
|
90 # output_matrix = numpy.zeros((n_channel,sig_length))
|
hamel@261
|
91 # for i in range(n_channel):
|
hamel@261
|
92 # output_matrix[i] = numpy.array(output_bank.get_signal(i))
|
hamel@261
|
93 # print output_matrix
|
hamel@261
|
94 # print output_matrix.shape
|
hamel@261
|
95 # print output_matrix.sum()
|
hamel@149
|
96
|
hamel@149
|
97 #output_signal = numpy.array(output_bank.get_signal(0))
|
hamel@149
|
98
|
hamel@261
|
99 #def process_module(module, input_signal, global_params):
|
hamel@261
|
100 # module.Initialize(input_signal,global_params)
|
hamel@261
|
101 # module.Process(input_signal)
|
hamel@261
|
102 # return module.GetOutputBank()
|
hamel@261
|
103
|
hamel@261
|
104 #pzfc_params = aimc.Parameters()
|
hamel@261
|
105 ##pzfc_params.SetString('name','PZFCFilterBank')
|
hamel@261
|
106 ##pzfc_params.SetString('child1','NAP')
|
hamel@261
|
107 #
|
hamel@261
|
108 #hcl_params = aimc.Parameters()
|
hamel@261
|
109 ##hcl_params.SetString('name','NAP')
|
hamel@261
|
110 ##hcl_params.SetString('child1','NAP')
|
hamel@261
|
111 #
|
hamel@261
|
112 #global_params = aimc.Parameters()
|
hamel@261
|
113 #
|
hamel@261
|
114 #pzfc = aimc.ModulePZFC(pzfc_params)
|
hamel@261
|
115 #pzfc.Initialize(sig,global_params)
|
hamel@261
|
116 #pzfc.Process(sig)
|
hamel@261
|
117 #pzfc_output = pzfc.GetOutputBank()
|
hamel@261
|
118 #
|
hamel@261
|
119 #hcl = aimc.ModuleHCL(hcl_params)
|
hamel@261
|
120 #hcl_output = process_module(hcl, pzfc_output, global_params)
|
hamel@261
|
121 #
|
hamel@261
|
122 #local_max = aimc.ModuleLocalMax( aimc.Parameters())
|
hamel@261
|
123 #local_max_output = process_module(local_max, hcl_output, global_params)
|
hamel@261
|
124 #
|
hamel@261
|
125 #sai = aimc.ModuleSAI( aimc.Parameters())
|
hamel@261
|
126 #sai_output = process_module(sai, local_max_output, global_params) |