tomwalters@22: #!/usr/bin/env python tomwalters@22: # encoding: utf-8 tomwalters@22: # tomwalters@22: # AIM-C: A C++ implementation of the Auditory Image Model tomwalters@22: # http://www.acousticscale.org/AIMC tomwalters@22: # tomwalters@45: # Licensed under the Apache License, Version 2.0 (the "License"); tomwalters@45: # you may not use this file except in compliance with the License. tomwalters@45: # You may obtain a copy of the License at tomwalters@22: # tomwalters@45: # http://www.apache.org/licenses/LICENSE-2.0 tomwalters@22: # tomwalters@45: # Unless required by applicable law or agreed to in writing, software tomwalters@45: # distributed under the License is distributed on an "AS IS" BASIS, tomwalters@45: # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. tomwalters@45: # See the License for the specific language governing permissions and tomwalters@45: # limitations under the License. tomwalters@22: """ tomwalters@22: Profiles_test.py tomwalters@22: tomwalters@22: Created by Thomas Walters on 2010-02-22. tomwalters@22: Copyright 2010 Thomas Walters tomwalters@22: Test the AIM-C model from filterbank to SSI profiles tomwalters@22: """ tomwalters@22: tomwalters@22: import aimc tomwalters@22: from scipy.io import wavfile tomwalters@22: from scipy import io tomwalters@22: import scipy tomwalters@22: import pylab tomwalters@22: from itertools import izip, chain, repeat tomwalters@22: tomwalters@22: def grouper(n, iterable, padvalue=None): tomwalters@22: "grouper(3, 'abcdefg', 'x') --> ('a','b','c'), ('d','e','f'), ('g','x','x')" tomwalters@22: return izip(*[chain(iterable, repeat(padvalue, n-1))]*n) tomwalters@22: tomwalters@22: def main(): tomwalters@22: wave_path = "/Users/Tom/Documents/Work/PhD/HTK-AIM/Sounds/" tomwalters@22: features_path = "/Users/Tom/Documents/Work/PhD/HTK-AIM/work08-jess-original-rec_rubber/features/" tomwalters@22: tomwalters@22: file_name = "aa/aa161.1p119.4s100.0t+000itd" tomwalters@22: tomwalters@22: wave_suffix = ".wav" tomwalters@22: features_suffix = ".mat" tomwalters@22: tomwalters@22: frame_period_ms = 10; tomwalters@22: tomwalters@22: wave_filename = wave_path + file_name + wave_suffix tomwalters@22: features_filename = features_path + file_name + features_suffix tomwalters@22: tomwalters@22: (sample_rate, input_wave) = wavfile.read(wave_filename) tomwalters@22: wave_length = input_wave.size tomwalters@22: buffer_length = int(frame_period_ms * sample_rate / 1000) tomwalters@22: tomwalters@22: #pylab.plot(input_wave) tomwalters@22: #pylab.show() tomwalters@22: tomwalters@22: input_sig = aimc.SignalBank() tomwalters@22: input_sig.Initialize(1, buffer_length, sample_rate) tomwalters@22: parameters = aimc.Parameters() tomwalters@22: parameters.Load("src/Scripts/profile_features.cfg") tomwalters@22: mod_gt = aimc.ModuleGammatone(parameters) tomwalters@22: mod_hl = aimc.ModuleHCL(parameters) tomwalters@22: mod_profile = aimc.ModuleSlice(parameters) tomwalters@22: mod_scaler = aimc.ModuleScaler(parameters) tomwalters@22: mod_gt.AddTarget(mod_hl) tomwalters@22: mod_hl.AddTarget(mod_profile) tomwalters@22: mod_profile.AddTarget(mod_scaler) tomwalters@22: mod_gt.Initialize(input_sig) tomwalters@22: tomwalters@22: correct_count = 0; tomwalters@22: incorrect_count = 0; tomwalters@22: tomwalters@22: scaled_wave = [] tomwalters@22: for sample in input_wave: tomwalters@22: scaled_wave.append(float(sample / float(pow(2,15) - 1))) tomwalters@22: i = 0 tomwalters@22: tomwalters@22: wave_chunks = grouper(buffer_length, scaled_wave, 0) tomwalters@22: tomwalters@22: out_frames = [] tomwalters@22: for chunk in wave_chunks: tomwalters@22: i = 0 tomwalters@22: for sample in chunk: tomwalters@22: input_sig.set_sample(0, i, float(sample)) tomwalters@22: i += 1 tomwalters@22: mod_gt.Process(input_sig) tomwalters@22: out_sig = mod_scaler.GetOutputBank() tomwalters@22: tomwalters@22: channel_count = out_sig.channel_count() tomwalters@22: out_buffer_length = out_sig.buffer_length() tomwalters@22: cfs = scipy.zeros((channel_count)) tomwalters@22: out = scipy.zeros((channel_count, out_buffer_length)) tomwalters@22: tomwalters@22: for ch in range(0, channel_count): tomwalters@22: for i in range(0, out_buffer_length): tomwalters@22: out[ch, i] = out_sig.sample(ch, i) tomwalters@22: out_frames.append(out) tomwalters@22: tomwalters@22: outmat = dict(profile_out=out_frames) tomwalters@22: io.savemat("src/Scripts/profile_out.mat", outmat) tomwalters@22: tomwalters@22: pass tomwalters@22: tomwalters@22: tomwalters@22: if __name__ == '__main__': tomwalters@22: main()