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@318
|
7 # Licensed under the Apache License, Version 2.0 (the "License");
|
tomwalters@318
|
8 # you may not use this file except in compliance with the License.
|
tomwalters@318
|
9 # You may obtain a copy of the License at
|
tomwalters@305
|
10 #
|
tomwalters@318
|
11 # http://www.apache.org/licenses/LICENSE-2.0
|
tomwalters@305
|
12 #
|
tomwalters@318
|
13 # Unless required by applicable law or agreed to in writing, software
|
tomwalters@318
|
14 # distributed under the License is distributed on an "AS IS" BASIS,
|
tomwalters@318
|
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
tomwalters@318
|
16 # See the License for the specific language governing permissions and
|
tomwalters@318
|
17 # limitations under the License.
|
tomwalters@305
|
18 """
|
tomwalters@305
|
19 Profiles_test.py
|
tomwalters@305
|
20
|
tomwalters@305
|
21 Created by Thomas Walters on 2010-02-22.
|
tomwalters@305
|
22 Copyright 2010 Thomas Walters <tom@acousticscale.org>
|
tomwalters@305
|
23 Test the AIM-C model from filterbank to SSI profiles
|
tomwalters@305
|
24 """
|
tomwalters@305
|
25
|
tomwalters@305
|
26 import aimc
|
tomwalters@305
|
27 from scipy.io import wavfile
|
tomwalters@305
|
28 from scipy import io
|
tomwalters@305
|
29 import scipy
|
tomwalters@305
|
30 import pylab
|
tomwalters@305
|
31 import numpy
|
tomwalters@305
|
32 from itertools import izip, chain, repeat
|
tomwalters@305
|
33
|
tomwalters@305
|
34 def grouper(n, iterable, padvalue=None):
|
tomwalters@305
|
35 "grouper(3, 'abcdefg', 'x') --> ('a','b','c'), ('d','e','f'), ('g','x','x')"
|
tomwalters@305
|
36 return izip(*[chain(iterable, repeat(padvalue, n-1))]*n)
|
tomwalters@305
|
37
|
tomwalters@305
|
38 def BankToArray(out_bank):
|
tomwalters@305
|
39 channel_count = out_bank.channel_count()
|
tomwalters@305
|
40 out_buffer_length = out_bank.buffer_length()
|
tomwalters@305
|
41 out = scipy.zeros((channel_count,out_buffer_length))
|
tomwalters@305
|
42 for ch in range(0, channel_count):
|
tomwalters@305
|
43 for i in range(0, out_buffer_length):
|
tomwalters@305
|
44 out[ch, i] = out_bank.sample(ch, i)
|
tomwalters@305
|
45 return out
|
tomwalters@305
|
46
|
tomwalters@305
|
47 def StrobesToList(bank):
|
tomwalters@305
|
48 channel_count = bank.channel_count()
|
tomwalters@305
|
49 out = scipy.zeros((channel_count,), dtype=numpy.object)
|
tomwalters@305
|
50 for ch in range(0, channel_count):
|
tomwalters@305
|
51 s = []
|
tomwalters@305
|
52 for i in range(0, bank.strobe_count(ch)):
|
tomwalters@305
|
53 s.append(bank.strobe(ch, i))
|
tomwalters@305
|
54 out[ch] = s
|
tomwalters@305
|
55 return out
|
tomwalters@305
|
56
|
tomwalters@305
|
57 def main():
|
tomwalters@305
|
58 wave_path = "/Users/Tom/Documents/Work/PhD/HTK-AIM/Sounds/"
|
tomwalters@305
|
59
|
tomwalters@305
|
60 file_name = "ii/ii172.5p112.5s100.0t+000itd"
|
tomwalters@305
|
61
|
tomwalters@305
|
62 wave_suffix = ".wav"
|
tomwalters@305
|
63
|
tomwalters@305
|
64 frame_period_ms = 10;
|
tomwalters@305
|
65
|
tomwalters@305
|
66 wave_filename = wave_path + file_name + wave_suffix
|
tomwalters@305
|
67
|
tomwalters@305
|
68 (sample_rate, input_wave) = wavfile.read(wave_filename)
|
tomwalters@305
|
69 wave_length = input_wave.size
|
tomwalters@305
|
70 buffer_length = int(frame_period_ms * sample_rate / 1000)
|
tomwalters@305
|
71
|
tomwalters@305
|
72
|
tomwalters@305
|
73 input_sig = aimc.SignalBank()
|
tomwalters@305
|
74 input_sig.Initialize(1, buffer_length, sample_rate)
|
tomwalters@305
|
75 parameters = aimc.Parameters()
|
tomwalters@305
|
76 parameters.SetInt("input.buffersize", 480)
|
tomwalters@305
|
77
|
tomwalters@305
|
78 mod_gt = aimc.ModuleGammatone(parameters)
|
tomwalters@305
|
79 mod_hl = aimc.ModuleHCL(parameters)
|
tomwalters@305
|
80 mod_strobes = aimc.ModuleLocalMax(parameters)
|
tomwalters@305
|
81
|
tomwalters@305
|
82 mod_gt.AddTarget(mod_hl)
|
tomwalters@305
|
83 mod_hl.AddTarget(mod_strobes)
|
tomwalters@305
|
84
|
tomwalters@305
|
85 mod_gt.Initialize(input_sig)
|
tomwalters@305
|
86
|
tomwalters@305
|
87 correct_count = 0;
|
tomwalters@305
|
88 incorrect_count = 0;
|
tomwalters@305
|
89
|
tomwalters@305
|
90 scaled_wave = []
|
tomwalters@305
|
91 for sample in input_wave:
|
tomwalters@305
|
92 scaled_wave.append(float(sample / float(pow(2,15) - 1)))
|
tomwalters@305
|
93 i = 0
|
tomwalters@305
|
94
|
tomwalters@305
|
95 wave_chunks = grouper(buffer_length, scaled_wave, 0)
|
tomwalters@305
|
96
|
tomwalters@305
|
97 out_nap = []
|
tomwalters@305
|
98 out_strobes = []
|
tomwalters@305
|
99
|
tomwalters@305
|
100 for chunk in wave_chunks:
|
tomwalters@305
|
101 i = 0
|
tomwalters@305
|
102 for sample in chunk:
|
tomwalters@305
|
103 input_sig.set_sample(0, i, float(sample))
|
tomwalters@305
|
104 i += 1
|
tomwalters@305
|
105 mod_gt.Process(input_sig)
|
tomwalters@305
|
106 out_nap.append(BankToArray(mod_hl.GetOutputBank()))
|
tomwalters@305
|
107 out_strobes.append(StrobesToList(mod_strobes.GetOutputBank()))
|
tomwalters@305
|
108
|
tomwalters@305
|
109 outmat = dict(nap=out_nap, strobes=out_strobes)
|
tomwalters@305
|
110 io.savemat("src/Scripts/strobes_out.mat", outmat, oned_as='column')
|
tomwalters@305
|
111
|
tomwalters@305
|
112 pass
|
tomwalters@305
|
113
|
tomwalters@305
|
114
|
tomwalters@305
|
115 if __name__ == '__main__':
|
tomwalters@305
|
116 main()
|