Mercurial > hg > aimc
changeset 448:eba6d914f82c
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 | 60ca68aee3c7 |
children | 91f3d4287ac2 |
files | trunk/src/Modules/Input/ModuleFileInput.cc trunk/swig/aimc_data_io.py trunk/swig/audio_example.py trunk/test_data/short_example_mono.wav |
diffstat | 4 files changed, 142 insertions(+), 46 deletions(-) [+] |
line wrap: on
line diff
--- a/trunk/src/Modules/Input/ModuleFileInput.cc Wed Jan 05 18:48:02 2011 +0000 +++ b/trunk/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); } }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/trunk/swig/aimc_data_io.py Thu Jan 06 03:33:11 2011 +0000 @@ -0,0 +1,42 @@ +#!/usr/bin/env python +# +# audio_example.py +# +# +# Created by Philippe Hamel (hamel.phil@gmail.com) on 11-01-05. +# +# methods to read and write aimc data files + +from struct import * +import numpy as N + +#int_size = calcsize('i') +#float_size = calcsize('d') + +def readbin(type,file) : + """ + used to read binary data from a file + """ + return unpack(type,file.read(calcsize(type))) + +def read_aimc_data(filename): + file = open(filename,'rb') + + nFrames = readbin('i',file)[0] + period = readbin( 'f', file)[0] # Frame period in ms + nChannels = readbin( 'i', file)[0] # vertical axis of an AI + nSamples = readbin( 'i', file)[0] # horizontal axis of an AI + sample_rate = readbin('f', file)[0] # sample rate of each channel in Hz + + print 'nFrames, period, nChannels, nSamples, sample_rate' + print nFrames, period, nChannels, nSamples, sample_rate + + nData = nFrames * nChannels * nSamples + print nData + vec_data = readbin('%if'%nData,file) + + file.close() + data = N.reshape(vec_data,(nFrames, nChannels, nSamples)) + return data, nFrames, period, nChannels, nSamples, sample_rate + +#TODO write_aimc_data(data, nFrames, period, nChannels, nSamples, sample_rate) \ No newline at end of file
--- a/trunk/swig/audio_example.py Wed Jan 05 18:48:02 2011 +0000 +++ b/trunk/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