changeset 261:b8f16e8acf7b

corrected a bug with stereo files in FileInput, and some more stuff
author hamel.phil
date Thu, 06 Jan 2011 03:33:11 +0000
parents 0a8fac5bbfeb
children d129539a7cc2
files src/Modules/Input/ModuleFileInput.cc swig/aimc_data_io.py swig/audio_example.py
diffstat 3 files changed, 101 insertions(+), 64 deletions(-) [+]
line wrap: on
line diff
--- a/src/Modules/Input/ModuleFileInput.cc	Wed Jan 05 18:48:02 2011 +0000
+++ b/src/Modules/Input/ModuleFileInput.cc	Thu Jan 06 03:33:11 2011 +0000
@@ -103,11 +103,13 @@
 
   // Read buffersize bytes into buffer
   read = sf_readf_float(file_handle_, &buffer[0], buffer_length_);
-    
+  for (int i = 0; i < 10; i++) { 
+	  LOG_INFO(_T("%i: %f, %f"),i,buffer[2*i],buffer[2*i+1]);
+  }
   // Place the contents of the buffer into the signal bank
   int counter = 0;
-  for (int c = 0; c < audio_channels_; ++c) {
-    for (int i = 0; i < read; ++i) {
+  for (int i = 0; i < read; ++i) {
+	for (int c = 0; c < audio_channels_; ++c) {
       output_.set_sample(c, i, buffer[counter]);
       ++counter;
     }
@@ -117,8 +119,8 @@
   // of the file has been reached.
   if (read < buffer_length_) {
     // Zero samples at end
-    for (int c = 0; c < audio_channels_; ++c) {
-      for (int i = read; i < buffer_length_; ++i) {
+	for (int i = read; i < buffer_length_; ++i) {
+	  for (int c = 0; c < audio_channels_; ++c) {
         output_.set_sample(c, i, 0.0f);
       }
     }
--- a/swig/aimc_data_io.py	Wed Jan 05 18:48:02 2011 +0000
+++ b/swig/aimc_data_io.py	Thu Jan 06 03:33:11 2011 +0000
@@ -39,21 +39,4 @@
     data = N.reshape(vec_data,(nFrames, nChannels, nSamples))
     return data, nFrames, period, nChannels, nSamples, sample_rate
 
-def write_aimc_data(filename, data, sample_rate, period = 0.0):
-
-    nFrames, nChannels, nSamples = data.shape
-    
-    file = open(filename,'wb')
-    
-    file.write(pack('i',nFrames))
-    file.write(pack('f',period)) #Not correctly implemented yet
-    file.write(pack('i',nChannels))
-    file.write(pack('i',nSamples))
-    file.write(pack('f',sample_rate))
-    
-    vec_data = data.flatten()
-    for elem in vec_data:
-        file.write(pack('f',elem))
-
-    file.close()
-    
+#TODO write_aimc_data(data, nFrames, period, nChannels, nSamples, sample_rate)
\ No newline at end of file
--- a/swig/audio_example.py	Wed Jan 05 18:48:02 2011 +0000
+++ b/swig/audio_example.py	Thu Jan 06 03:33:11 2011 +0000
@@ -3,72 +3,124 @@
 #  audio_example.py
 #  
 #
-#  Created by Philippe Hamel on 10-12-22.
+#  Created by Philippe Hamel (hamel.phil@gmail.com) on 10-12-22.
 #
 # Script to extract SAIs from an audio file using python
 
+import aimc
+import numpy
+
+do_plot=True
 # Example wav file
 wavfile = '../test_data/short_example.wav'
 
+#Loading audio file
+try:
+	#pygmy is available at hg clone https://bitbucket.org/douglaseck/pygmy/
+	from pygmy.io.audio import AudioFile
+	af=AudioFile(wavfile)
+	x,sr,z=af.read(mono=True)
+except:
+	from scipy.io import wavfile as WF
+	(sr,x) = WF.read(wavfile)
+	x=x.astype('float')/float(pow(2,15) - 1)
 
-def process_module(module, input_signal, global_params):
-	module.Initialize(input_signal,global_params)
-	module.Process(input_signal)
-	return module.GetOutputBank()
+if x.ndim <= 1:
+	x=reshape(x,(x.shape[0],1))
+	
+nChannels = x.shape[1]
 
-import aimc
-import numpy
-#pygmy is available at hg clone https://bitbucket.org/douglaseck/pygmy/
-#TODO : find a more general way to open audio file
-from pygmy.io.audio import AudioFile
+nSamples = x.shape[0]
+buffer_length = 1024
+zero_pad = numpy.zeros((buffer_length-(nSamples%buffer_length),nChannels)).astype('float')
+print x.shape, zero_pad.shape
+x = numpy.vstack((x,zero_pad))
+print x.shape
+assert(x.shape[0]%buffer_length == 0)
 
-af=AudioFile(wavfile)
-x,sr,z=af.read(mono=True)
-#x,sr,z=af.read(mono=True,tlen_sec=0.1)
-#x=x[:1024]
-
-print len(x)
-print sr
+nFrames = x.shape[0]/buffer_length
+print nFrames
 
 sig = aimc.SignalBank()
-sig.Initialize(1,len(x),sr)
-sig.set_signal(0,x)
+sig.Initialize(nChannels,buffer_length,sr)
 
-pzfc_params = aimc.Parameters()
-#pzfc_params.SetString('name','PZFCFilterBank')
-#pzfc_params.SetString('child1','NAP')
+pzfc = aimc.ModulePZFC(aimc.Parameters())
+hcl = aimc.ModuleHCL(aimc.Parameters())
+local_max = aimc.ModuleLocalMax(aimc.Parameters())
+sai = aimc.ModuleSAI(aimc.Parameters())
 
-hcl_params = aimc.Parameters()
-#hcl_params.SetString('name','NAP')
-#hcl_params.SetString('child1','NAP')
+pzfc.AddTarget(hcl)
+hcl.AddTarget(local_max)
+local_max.AddTarget(sai)
 
 global_params = aimc.Parameters()
+pzfc.Initialize(sig,global_params)
 
-pzfc = aimc.ModulePZFC(pzfc_params)
-pzfc.Initialize(sig,global_params)
-pzfc.Process(sig)
-pzfc_output = pzfc.GetOutputBank()
 
-hcl = aimc.ModuleHCL(hcl_params)
-hcl_output = process_module(hcl, pzfc_output, global_params)
+##One chunk only
+#chunk=x[:buffer_length]
 
-local_max = aimc.ModuleLocalMax( aimc.Parameters())
-local_max_output = process_module(local_max, hcl_output, global_params)
+output_list = []
 
-sai = aimc.ModuleSAI( aimc.Parameters())
-sai_output = process_module(sai, local_max_output, global_params)
+for f in range(nFrames):
+	for i in range(nChannels):
+		sig.set_signal(i,x[buffer_length*f:buffer_length*(f+1),i])
 
-
-if True:
-	output_bank=sai_output
+	pzfc.Process(sig)
+	output_bank = sai.GetOutputBank()
 	n_channel = output_bank.channel_count()
 	sig_length = output_bank.buffer_length()
 	output_matrix = numpy.zeros((n_channel,sig_length))
 	for i in range(n_channel):
 		output_matrix[i] = numpy.array(output_bank.get_signal(i))
-	print output_matrix
-	print output_matrix.shape
-	print output_matrix.sum()
+	output_list.append(output_matrix)
+
+if do_plot:
+	import pylab as P
+	P.figure()
+	P.imshow(output_list[0], aspect='auto', origin='lower')
+
+
+################ DEPRECATED CODE (kept here for reference) #########################
+
+#if True:
+#	output_bank=output_list[0]
+#	n_channel = output_bank.channel_count()
+#	sig_length = output_bank.buffer_length()
+#	output_matrix = numpy.zeros((n_channel,sig_length))
+#	for i in range(n_channel):
+#		output_matrix[i] = numpy.array(output_bank.get_signal(i))
+#	print output_matrix
+#	print output_matrix.shape
+#	print output_matrix.sum()
 
 #output_signal = numpy.array(output_bank.get_signal(0))
 
+#def process_module(module, input_signal, global_params):
+#	module.Initialize(input_signal,global_params)
+#	module.Process(input_signal)
+#	return module.GetOutputBank()
+
+#pzfc_params = aimc.Parameters()
+##pzfc_params.SetString('name','PZFCFilterBank')
+##pzfc_params.SetString('child1','NAP')
+#
+#hcl_params = aimc.Parameters()
+##hcl_params.SetString('name','NAP')
+##hcl_params.SetString('child1','NAP')
+#
+#global_params = aimc.Parameters()
+#
+#pzfc = aimc.ModulePZFC(pzfc_params)
+#pzfc.Initialize(sig,global_params)
+#pzfc.Process(sig)
+#pzfc_output = pzfc.GetOutputBank()
+#
+#hcl = aimc.ModuleHCL(hcl_params)
+#hcl_output = process_module(hcl, pzfc_output, global_params)
+#
+#local_max = aimc.ModuleLocalMax( aimc.Parameters())
+#local_max_output = process_module(local_max, hcl_output, global_params)
+#
+#sai = aimc.ModuleSAI( aimc.Parameters())
+#sai_output = process_module(sai, local_max_output, global_params)
\ No newline at end of file