annotate swig/audio_example.py @ 149:a61f41b221e1

corrected a bug in the swig example and added a new more complex example
author hamel.phil
date Wed, 05 Jan 2011 18:48:02 +0000
parents
children 9425901c60a6
rev   line source
hamel@149 1 #!/usr/bin/env python
hamel@149 2 #
hamel@149 3 # audio_example.py
hamel@149 4 #
hamel@149 5 #
hamel@149 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@149 14 def process_module(module, input_signal, global_params):
hamel@149 15 module.Initialize(input_signal,global_params)
hamel@149 16 module.Process(input_signal)
hamel@149 17 return module.GetOutputBank()
hamel@149 18
hamel@149 19 import aimc
hamel@149 20 import numpy
hamel@149 21 #pygmy is available at hg clone https://bitbucket.org/douglaseck/pygmy/
hamel@149 22 #TODO : find a more general way to open audio file
hamel@149 23 from pygmy.io.audio import AudioFile
hamel@149 24
hamel@149 25 af=AudioFile(wavfile)
hamel@149 26 x,sr,z=af.read(mono=True)
hamel@149 27 #x,sr,z=af.read(mono=True,tlen_sec=0.1)
hamel@149 28 #x=x[:1024]
hamel@149 29
hamel@149 30 print len(x)
hamel@149 31 print sr
hamel@149 32
hamel@149 33 sig = aimc.SignalBank()
hamel@149 34 sig.Initialize(1,len(x),sr)
hamel@149 35 sig.set_signal(0,x)
hamel@149 36
hamel@149 37 pzfc_params = aimc.Parameters()
hamel@149 38 #pzfc_params.SetString('name','PZFCFilterBank')
hamel@149 39 #pzfc_params.SetString('child1','NAP')
hamel@149 40
hamel@149 41 hcl_params = aimc.Parameters()
hamel@149 42 #hcl_params.SetString('name','NAP')
hamel@149 43 #hcl_params.SetString('child1','NAP')
hamel@149 44
hamel@149 45 global_params = aimc.Parameters()
hamel@149 46
hamel@149 47 pzfc = aimc.ModulePZFC(pzfc_params)
hamel@149 48 pzfc.Initialize(sig,global_params)
hamel@149 49 pzfc.Process(sig)
hamel@149 50 pzfc_output = pzfc.GetOutputBank()
hamel@149 51
hamel@149 52 hcl = aimc.ModuleHCL(hcl_params)
hamel@149 53 hcl_output = process_module(hcl, pzfc_output, global_params)
hamel@149 54
hamel@149 55 local_max = aimc.ModuleLocalMax( aimc.Parameters())
hamel@149 56 local_max_output = process_module(local_max, hcl_output, global_params)
hamel@149 57
hamel@149 58 sai = aimc.ModuleSAI( aimc.Parameters())
hamel@149 59 sai_output = process_module(sai, local_max_output, global_params)
hamel@149 60
hamel@149 61
hamel@149 62 if True:
hamel@149 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@149 69 print output_matrix
hamel@149 70 print output_matrix.shape
hamel@149 71 print output_matrix.sum()
hamel@149 72
hamel@149 73 #output_signal = numpy.array(output_bank.get_signal(0))
hamel@149 74