hamel@149
|
1 #!/usr/bin/env python
|
hamel@149
|
2 #
|
hamel@149
|
3 # audio_example.py
|
hamel@149
|
4 #
|
hamel@149
|
5 #
|
hamel@260
|
6 # Created by Philippe Hamel on 10-12-22.
|
hamel@149
|
7 #
|
hamel@149
|
8 # Script to extract SAIs from an audio file using python
|
hamel@149
|
9
|
hamel@149
|
10 # Example wav file
|
hamel@149
|
11 wavfile = '../test_data/short_example.wav'
|
hamel@149
|
12
|
hamel@149
|
13
|
hamel@260
|
14 def process_module(module, input_signal, global_params):
|
hamel@260
|
15 module.Initialize(input_signal,global_params)
|
hamel@260
|
16 module.Process(input_signal)
|
hamel@260
|
17 return module.GetOutputBank()
|
hamel@149
|
18
|
hamel@260
|
19 import aimc
|
hamel@260
|
20 import numpy
|
hamel@260
|
21 #pygmy is available at hg clone https://bitbucket.org/douglaseck/pygmy/
|
hamel@260
|
22 #TODO : find a more general way to open audio file
|
hamel@260
|
23 from pygmy.io.audio import AudioFile
|
hamel@152
|
24
|
hamel@260
|
25 af=AudioFile(wavfile)
|
hamel@260
|
26 x,sr,z=af.read(mono=True)
|
hamel@260
|
27 #x,sr,z=af.read(mono=True,tlen_sec=0.1)
|
hamel@260
|
28 #x=x[:1024]
|
hamel@152
|
29
|
hamel@260
|
30 print len(x)
|
hamel@260
|
31 print sr
|
hamel@149
|
32
|
hamel@149
|
33 sig = aimc.SignalBank()
|
hamel@260
|
34 sig.Initialize(1,len(x),sr)
|
hamel@260
|
35 sig.set_signal(0,x)
|
hamel@149
|
36
|
hamel@260
|
37 pzfc_params = aimc.Parameters()
|
hamel@260
|
38 #pzfc_params.SetString('name','PZFCFilterBank')
|
hamel@260
|
39 #pzfc_params.SetString('child1','NAP')
|
hamel@149
|
40
|
hamel@260
|
41 hcl_params = aimc.Parameters()
|
hamel@260
|
42 #hcl_params.SetString('name','NAP')
|
hamel@260
|
43 #hcl_params.SetString('child1','NAP')
|
hamel@149
|
44
|
hamel@149
|
45 global_params = aimc.Parameters()
|
hamel@260
|
46
|
hamel@260
|
47 pzfc = aimc.ModulePZFC(pzfc_params)
|
hamel@150
|
48 pzfc.Initialize(sig,global_params)
|
hamel@260
|
49 pzfc.Process(sig)
|
hamel@260
|
50 pzfc_output = pzfc.GetOutputBank()
|
hamel@149
|
51
|
hamel@260
|
52 hcl = aimc.ModuleHCL(hcl_params)
|
hamel@260
|
53 hcl_output = process_module(hcl, pzfc_output, global_params)
|
hamel@149
|
54
|
hamel@260
|
55 local_max = aimc.ModuleLocalMax( aimc.Parameters())
|
hamel@260
|
56 local_max_output = process_module(local_max, hcl_output, global_params)
|
hamel@149
|
57
|
hamel@260
|
58 sai = aimc.ModuleSAI( aimc.Parameters())
|
hamel@260
|
59 sai_output = process_module(sai, local_max_output, global_params)
|
hamel@260
|
60
|
hamel@260
|
61
|
hamel@260
|
62 if True:
|
hamel@260
|
63 output_bank=sai_output
|
hamel@149
|
64 n_channel = output_bank.channel_count()
|
hamel@149
|
65 sig_length = output_bank.buffer_length()
|
hamel@149
|
66 output_matrix = numpy.zeros((n_channel,sig_length))
|
hamel@149
|
67 for i in range(n_channel):
|
hamel@149
|
68 output_matrix[i] = numpy.array(output_bank.get_signal(i))
|
hamel@260
|
69 print output_matrix
|
hamel@260
|
70 print output_matrix.shape
|
hamel@260
|
71 print output_matrix.sum()
|
hamel@149
|
72
|
hamel@149
|
73 #output_signal = numpy.array(output_bank.get_signal(0))
|
hamel@149
|
74
|