tomwalters@294: #!/usr/bin/env python
tomwalters@294: # encoding: utf-8
tomwalters@294: #
tomwalters@294: # AIM-C: A C++ implementation of the Auditory Image Model
tomwalters@294: # http://www.acousticscale.org/AIMC
tomwalters@294: #
tomwalters@294: # This program is free software: you can redistribute it and/or modify
tomwalters@294: # it under the terms of the GNU General Public License as published by
tomwalters@294: # the Free Software Foundation, either version 3 of the License, or
tomwalters@294: # (at your option) any later version.
tomwalters@294: #
tomwalters@294: # This program is distributed in the hope that it will be useful,
tomwalters@294: # but WITHOUT ANY WARRANTY; without even the implied warranty of
tomwalters@294: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
tomwalters@294: # GNU General Public License for more details.
tomwalters@294: #
tomwalters@294: # You should have received a copy of the GNU General Public License
tomwalters@294: # along with this program. If not, see .
tomwalters@294: """
tomwalters@294: Profiles_test.py
tomwalters@294:
tomwalters@294: Created by Thomas Walters on 2010-02-22.
tomwalters@294: Copyright 2010 Thomas Walters
tomwalters@294: Test the AIM-C model from filterbank to SSI profiles
tomwalters@294: """
tomwalters@294:
tomwalters@294: import aimc
tomwalters@294: from scipy.io import wavfile
tomwalters@294: from scipy import io
tomwalters@294: import scipy
tomwalters@294: import pylab
tomwalters@294: from itertools import izip, chain, repeat
tomwalters@294:
tomwalters@294: def grouper(n, iterable, padvalue=None):
tomwalters@294: "grouper(3, 'abcdefg', 'x') --> ('a','b','c'), ('d','e','f'), ('g','x','x')"
tomwalters@294: return izip(*[chain(iterable, repeat(padvalue, n-1))]*n)
tomwalters@294:
tomwalters@294: def main():
tomwalters@294: wave_path = "/Users/Tom/Documents/Work/PhD/HTK-AIM/Sounds/"
tomwalters@294: features_path = "/Users/Tom/Documents/Work/PhD/HTK-AIM/work08-jess-original-rec_rubber/features/"
tomwalters@294:
tomwalters@294: file_name = "aa/aa161.1p119.4s100.0t+000itd"
tomwalters@294:
tomwalters@294: wave_suffix = ".wav"
tomwalters@294: features_suffix = ".mat"
tomwalters@294:
tomwalters@294: frame_period_ms = 10;
tomwalters@294:
tomwalters@294: wave_filename = wave_path + file_name + wave_suffix
tomwalters@294: features_filename = features_path + file_name + features_suffix
tomwalters@294:
tomwalters@294: (sample_rate, input_wave) = wavfile.read(wave_filename)
tomwalters@294: wave_length = input_wave.size
tomwalters@294: buffer_length = int(frame_period_ms * sample_rate / 1000)
tomwalters@294:
tomwalters@294: #pylab.plot(input_wave)
tomwalters@294: #pylab.show()
tomwalters@294:
tomwalters@294: input_sig = aimc.SignalBank()
tomwalters@294: input_sig.Initialize(1, buffer_length, sample_rate)
tomwalters@294: parameters = aimc.Parameters()
tomwalters@294: parameters.Load("src/Scripts/profile_features.cfg")
tomwalters@294: mod_gt = aimc.ModuleGammatone(parameters)
tomwalters@294: mod_hl = aimc.ModuleHCL(parameters)
tomwalters@294: mod_profile = aimc.ModuleSlice(parameters)
tomwalters@294: mod_scaler = aimc.ModuleScaler(parameters)
tomwalters@294: mod_gt.AddTarget(mod_hl)
tomwalters@294: mod_hl.AddTarget(mod_profile)
tomwalters@294: mod_profile.AddTarget(mod_scaler)
tomwalters@294: mod_gt.Initialize(input_sig)
tomwalters@294:
tomwalters@294: correct_count = 0;
tomwalters@294: incorrect_count = 0;
tomwalters@294:
tomwalters@294: scaled_wave = []
tomwalters@294: for sample in input_wave:
tomwalters@294: scaled_wave.append(float(sample / float(pow(2,15) - 1)))
tomwalters@294: i = 0
tomwalters@294:
tomwalters@294: wave_chunks = grouper(buffer_length, scaled_wave, 0)
tomwalters@294:
tomwalters@294: out_frames = []
tomwalters@294: for chunk in wave_chunks:
tomwalters@294: i = 0
tomwalters@294: for sample in chunk:
tomwalters@294: input_sig.set_sample(0, i, float(sample))
tomwalters@294: i += 1
tomwalters@294: mod_gt.Process(input_sig)
tomwalters@294: out_sig = mod_scaler.GetOutputBank()
tomwalters@294:
tomwalters@294: channel_count = out_sig.channel_count()
tomwalters@294: out_buffer_length = out_sig.buffer_length()
tomwalters@294: cfs = scipy.zeros((channel_count))
tomwalters@294: out = scipy.zeros((channel_count, out_buffer_length))
tomwalters@294:
tomwalters@294: for ch in range(0, channel_count):
tomwalters@294: for i in range(0, out_buffer_length):
tomwalters@294: out[ch, i] = out_sig.sample(ch, i)
tomwalters@294: out_frames.append(out)
tomwalters@294:
tomwalters@294: outmat = dict(profile_out=out_frames)
tomwalters@294: io.savemat("src/Scripts/profile_out.mat", outmat)
tomwalters@294:
tomwalters@294: pass
tomwalters@294:
tomwalters@294:
tomwalters@294: if __name__ == '__main__':
tomwalters@294: main()