tomwalters@32: #!/usr/bin/env python tomwalters@32: # encoding: utf-8 tomwalters@32: # tomwalters@32: # AIM-C: A C++ implementation of the Auditory Image Model tomwalters@32: # http://www.acousticscale.org/AIMC tomwalters@32: # 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@32: # tomwalters@45: # http://www.apache.org/licenses/LICENSE-2.0 tomwalters@32: # 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@32: """ tomwalters@32: Profiles_test.py tomwalters@32: tomwalters@32: Created by Thomas Walters on 2010-02-22. tomwalters@32: Copyright 2010 Thomas Walters tomwalters@32: Test the AIM-C model from filterbank to SSI profiles tomwalters@32: """ tomwalters@32: tomwalters@32: import aimc tomwalters@32: from scipy.io import wavfile tomwalters@32: from scipy import io tomwalters@32: import scipy tomwalters@32: import pylab tomwalters@32: import numpy tomwalters@32: from itertools import izip, chain, repeat tomwalters@32: tomwalters@32: def grouper(n, iterable, padvalue=None): tomwalters@32: "grouper(3, 'abcdefg', 'x') --> ('a','b','c'), ('d','e','f'), ('g','x','x')" tomwalters@32: return izip(*[chain(iterable, repeat(padvalue, n-1))]*n) tomwalters@32: tomwalters@32: def BankToArray(out_bank): tomwalters@32: channel_count = out_bank.channel_count() tomwalters@32: out_buffer_length = out_bank.buffer_length() tomwalters@32: out = scipy.zeros((channel_count,out_buffer_length)) tomwalters@32: for ch in range(0, channel_count): tomwalters@32: for i in range(0, out_buffer_length): tomwalters@32: out[ch, i] = out_bank.sample(ch, i) tomwalters@32: return out tomwalters@32: tomwalters@32: def StrobesToList(bank): tomwalters@32: channel_count = bank.channel_count() tomwalters@32: out = scipy.zeros((channel_count,), dtype=numpy.object) tomwalters@32: for ch in range(0, channel_count): tomwalters@32: s = [] tomwalters@32: for i in range(0, bank.strobe_count(ch)): tomwalters@32: s.append(bank.strobe(ch, i)) tomwalters@32: out[ch] = s tomwalters@32: return out tomwalters@32: tomwalters@32: def main(): tomwalters@32: wave_path = "/Users/Tom/Documents/Work/PhD/HTK-AIM/Sounds/" tomwalters@32: tomwalters@32: file_name = "ii/ii172.5p112.5s100.0t+000itd" tomwalters@32: tomwalters@32: wave_suffix = ".wav" tomwalters@32: tomwalters@32: frame_period_ms = 10; tomwalters@32: tomwalters@32: wave_filename = wave_path + file_name + wave_suffix tomwalters@32: tomwalters@32: (sample_rate, input_wave) = wavfile.read(wave_filename) tomwalters@32: wave_length = input_wave.size tomwalters@32: buffer_length = int(frame_period_ms * sample_rate / 1000) tomwalters@32: tomwalters@32: tomwalters@32: input_sig = aimc.SignalBank() tomwalters@32: input_sig.Initialize(1, buffer_length, sample_rate) tomwalters@32: parameters = aimc.Parameters() tomwalters@32: parameters.SetInt("input.buffersize", 480) tomwalters@32: tomwalters@32: mod_gt = aimc.ModuleGammatone(parameters) tomwalters@32: mod_hl = aimc.ModuleHCL(parameters) tomwalters@32: mod_strobes = aimc.ModuleLocalMax(parameters) tomwalters@32: tomwalters@32: mod_gt.AddTarget(mod_hl) tomwalters@32: mod_hl.AddTarget(mod_strobes) tomwalters@32: tomwalters@32: mod_gt.Initialize(input_sig) tomwalters@32: tomwalters@32: correct_count = 0; tomwalters@32: incorrect_count = 0; tomwalters@32: tomwalters@32: scaled_wave = [] tomwalters@32: for sample in input_wave: tomwalters@32: scaled_wave.append(float(sample / float(pow(2,15) - 1))) tomwalters@32: i = 0 tomwalters@32: tomwalters@32: wave_chunks = grouper(buffer_length, scaled_wave, 0) tomwalters@32: tomwalters@32: out_nap = [] tomwalters@32: out_strobes = [] tomwalters@32: tomwalters@32: for chunk in wave_chunks: tomwalters@32: i = 0 tomwalters@32: for sample in chunk: tomwalters@32: input_sig.set_sample(0, i, float(sample)) tomwalters@32: i += 1 tomwalters@32: mod_gt.Process(input_sig) tomwalters@32: out_nap.append(BankToArray(mod_hl.GetOutputBank())) tomwalters@32: out_strobes.append(StrobesToList(mod_strobes.GetOutputBank())) tomwalters@32: tomwalters@32: outmat = dict(nap=out_nap, strobes=out_strobes) tomwalters@32: io.savemat("src/Scripts/strobes_out.mat", outmat, oned_as='column') tomwalters@32: tomwalters@32: pass tomwalters@32: tomwalters@32: tomwalters@32: if __name__ == '__main__': tomwalters@32: main()