tomwalters@305
|
1 #!/usr/bin/env python
|
tomwalters@305
|
2 # encoding: utf-8
|
tomwalters@305
|
3 #
|
tomwalters@305
|
4 # AIM-C: A C++ implementation of the Auditory Image Model
|
tomwalters@305
|
5 # http://www.acousticscale.org/AIMC
|
tomwalters@305
|
6 #
|
tomwalters@305
|
7 # This program is free software: you can redistribute it and/or modify
|
tomwalters@305
|
8 # it under the terms of the GNU General Public License as published by
|
tomwalters@305
|
9 # the Free Software Foundation, either version 3 of the License, or
|
tomwalters@305
|
10 # (at your option) any later version.
|
tomwalters@305
|
11 #
|
tomwalters@305
|
12 # This program is distributed in the hope that it will be useful,
|
tomwalters@305
|
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
tomwalters@305
|
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
tomwalters@305
|
15 # GNU General Public License for more details.
|
tomwalters@305
|
16 #
|
tomwalters@305
|
17 # You should have received a copy of the GNU General Public License
|
tomwalters@305
|
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
|
tomwalters@305
|
19 """
|
tomwalters@305
|
20 Profiles_test.py
|
tomwalters@305
|
21
|
tomwalters@305
|
22 Created by Thomas Walters on 2010-02-22.
|
tomwalters@305
|
23 Copyright 2010 Thomas Walters <tom@acousticscale.org>
|
tomwalters@305
|
24 Test the AIM-C model from filterbank to SSI profiles
|
tomwalters@305
|
25 """
|
tomwalters@305
|
26
|
tomwalters@305
|
27 import aimc
|
tomwalters@305
|
28 from scipy.io import wavfile
|
tomwalters@305
|
29 from scipy import io
|
tomwalters@305
|
30 import scipy
|
tomwalters@305
|
31 import pylab
|
tomwalters@305
|
32 import numpy
|
tomwalters@305
|
33 from itertools import izip, chain, repeat
|
tomwalters@305
|
34
|
tomwalters@305
|
35 def grouper(n, iterable, padvalue=None):
|
tomwalters@305
|
36 "grouper(3, 'abcdefg', 'x') --> ('a','b','c'), ('d','e','f'), ('g','x','x')"
|
tomwalters@305
|
37 return izip(*[chain(iterable, repeat(padvalue, n-1))]*n)
|
tomwalters@305
|
38
|
tomwalters@305
|
39 def BankToArray(out_bank):
|
tomwalters@305
|
40 channel_count = out_bank.channel_count()
|
tomwalters@305
|
41 out_buffer_length = out_bank.buffer_length()
|
tomwalters@305
|
42 out = scipy.zeros((channel_count,out_buffer_length))
|
tomwalters@305
|
43 for ch in range(0, channel_count):
|
tomwalters@305
|
44 for i in range(0, out_buffer_length):
|
tomwalters@305
|
45 out[ch, i] = out_bank.sample(ch, i)
|
tomwalters@305
|
46 return out
|
tomwalters@305
|
47
|
tomwalters@305
|
48 def StrobesToList(bank):
|
tomwalters@305
|
49 channel_count = bank.channel_count()
|
tomwalters@305
|
50 out = scipy.zeros((channel_count,), dtype=numpy.object)
|
tomwalters@305
|
51 for ch in range(0, channel_count):
|
tomwalters@305
|
52 s = []
|
tomwalters@305
|
53 for i in range(0, bank.strobe_count(ch)):
|
tomwalters@305
|
54 s.append(bank.strobe(ch, i))
|
tomwalters@305
|
55 out[ch] = s
|
tomwalters@305
|
56 return out
|
tomwalters@305
|
57
|
tomwalters@305
|
58 def main():
|
tomwalters@305
|
59 wave_path = "/Users/Tom/Documents/Work/PhD/HTK-AIM/Sounds/"
|
tomwalters@305
|
60
|
tomwalters@305
|
61 file_name = "ii/ii172.5p112.5s100.0t+000itd"
|
tomwalters@305
|
62
|
tomwalters@305
|
63 wave_suffix = ".wav"
|
tomwalters@305
|
64
|
tomwalters@305
|
65 frame_period_ms = 10;
|
tomwalters@305
|
66
|
tomwalters@305
|
67 wave_filename = wave_path + file_name + wave_suffix
|
tomwalters@305
|
68
|
tomwalters@305
|
69 (sample_rate, input_wave) = wavfile.read(wave_filename)
|
tomwalters@305
|
70 wave_length = input_wave.size
|
tomwalters@305
|
71 buffer_length = int(frame_period_ms * sample_rate / 1000)
|
tomwalters@305
|
72
|
tomwalters@305
|
73
|
tomwalters@305
|
74 input_sig = aimc.SignalBank()
|
tomwalters@305
|
75 input_sig.Initialize(1, buffer_length, sample_rate)
|
tomwalters@305
|
76 parameters = aimc.Parameters()
|
tomwalters@305
|
77 parameters.SetInt("input.buffersize", 480)
|
tomwalters@305
|
78
|
tomwalters@305
|
79 mod_gt = aimc.ModuleGammatone(parameters)
|
tomwalters@305
|
80 mod_hl = aimc.ModuleHCL(parameters)
|
tomwalters@305
|
81 mod_strobes = aimc.ModuleLocalMax(parameters)
|
tomwalters@305
|
82
|
tomwalters@305
|
83 mod_gt.AddTarget(mod_hl)
|
tomwalters@305
|
84 mod_hl.AddTarget(mod_strobes)
|
tomwalters@305
|
85
|
tomwalters@305
|
86 mod_gt.Initialize(input_sig)
|
tomwalters@305
|
87
|
tomwalters@305
|
88 correct_count = 0;
|
tomwalters@305
|
89 incorrect_count = 0;
|
tomwalters@305
|
90
|
tomwalters@305
|
91 scaled_wave = []
|
tomwalters@305
|
92 for sample in input_wave:
|
tomwalters@305
|
93 scaled_wave.append(float(sample / float(pow(2,15) - 1)))
|
tomwalters@305
|
94 i = 0
|
tomwalters@305
|
95
|
tomwalters@305
|
96 wave_chunks = grouper(buffer_length, scaled_wave, 0)
|
tomwalters@305
|
97
|
tomwalters@305
|
98 out_nap = []
|
tomwalters@305
|
99 out_strobes = []
|
tomwalters@305
|
100
|
tomwalters@305
|
101 for chunk in wave_chunks:
|
tomwalters@305
|
102 i = 0
|
tomwalters@305
|
103 for sample in chunk:
|
tomwalters@305
|
104 input_sig.set_sample(0, i, float(sample))
|
tomwalters@305
|
105 i += 1
|
tomwalters@305
|
106 mod_gt.Process(input_sig)
|
tomwalters@305
|
107 out_nap.append(BankToArray(mod_hl.GetOutputBank()))
|
tomwalters@305
|
108 out_strobes.append(StrobesToList(mod_strobes.GetOutputBank()))
|
tomwalters@305
|
109
|
tomwalters@305
|
110 outmat = dict(nap=out_nap, strobes=out_strobes)
|
tomwalters@305
|
111 io.savemat("src/Scripts/strobes_out.mat", outmat, oned_as='column')
|
tomwalters@305
|
112
|
tomwalters@305
|
113 pass
|
tomwalters@305
|
114
|
tomwalters@305
|
115
|
tomwalters@305
|
116 if __name__ == '__main__':
|
tomwalters@305
|
117 main()
|