Yabin Li
2023-04-24 7ab2e5cf22bbb31808bcacf84c054c710e4e6a93
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#!/usr/bin/env python3
#
# Copyright (c)  2021  Xiaomi Corporation (authors: Fangjun Kuang)
 
 
import pickle
 
import kaldi_native_fbank as knf
 
 
def test_default():
    opts = knf.FbankOptions()
    assert opts.frame_opts.samp_freq == 16000
    assert opts.frame_opts.frame_shift_ms == 10.0
    assert opts.frame_opts.frame_length_ms == 25.0
    assert opts.frame_opts.dither == 1.0
    assert abs(opts.frame_opts.preemph_coeff - 0.97) < 1e-6
    assert opts.frame_opts.remove_dc_offset is True
    assert opts.frame_opts.window_type == "povey"
    assert opts.frame_opts.round_to_power_of_two is True
    assert abs(opts.frame_opts.blackman_coeff - 0.42) < 1e-6
    assert opts.frame_opts.snip_edges is True
 
    assert opts.mel_opts.num_bins == 23
    assert opts.mel_opts.low_freq == 20
    assert opts.mel_opts.high_freq == 0
    assert opts.mel_opts.vtln_low == 100
    assert opts.mel_opts.vtln_high == -500
    assert opts.mel_opts.debug_mel is False
    assert opts.mel_opts.htk_mode is False
 
    assert opts.use_energy is False
    assert opts.energy_floor == 0.0
    assert opts.raw_energy is True
    assert opts.htk_compat is False
    assert opts.use_log_fbank is True
    assert opts.use_power is True
 
 
def test_set_get():
    opts = knf.FbankOptions()
    opts.use_energy = True
    assert opts.use_energy is True
 
    opts.energy_floor = 1
    assert opts.energy_floor == 1
 
    opts.raw_energy = False
    assert opts.raw_energy is False
 
    opts.htk_compat = True
    assert opts.htk_compat is True
 
    opts.use_log_fbank = False
    assert opts.use_log_fbank is False
 
    opts.use_power = False
    assert opts.use_power is False
 
 
def test_set_get_frame_opts():
    opts = knf.FbankOptions()
 
    opts.frame_opts.samp_freq = 44100
    assert opts.frame_opts.samp_freq == 44100
 
    opts.frame_opts.frame_shift_ms = 20.5
    assert opts.frame_opts.frame_shift_ms == 20.5
 
    opts.frame_opts.frame_length_ms = 1
    assert opts.frame_opts.frame_length_ms == 1
 
    opts.frame_opts.dither = 0.5
    assert opts.frame_opts.dither == 0.5
 
    opts.frame_opts.preemph_coeff = 0.25
    assert opts.frame_opts.preemph_coeff == 0.25
 
    opts.frame_opts.remove_dc_offset = False
    assert opts.frame_opts.remove_dc_offset is False
 
    opts.frame_opts.window_type = "hanning"
    assert opts.frame_opts.window_type == "hanning"
 
    opts.frame_opts.round_to_power_of_two = False
    assert opts.frame_opts.round_to_power_of_two is False
 
    opts.frame_opts.blackman_coeff = 0.25
    assert opts.frame_opts.blackman_coeff == 0.25
 
    opts.frame_opts.snip_edges = False
    assert opts.frame_opts.snip_edges is False
 
 
def test_set_get_mel_opts():
    opts = knf.FbankOptions()
 
    opts.mel_opts.num_bins = 100
    assert opts.mel_opts.num_bins == 100
 
    opts.mel_opts.low_freq = 22
    assert opts.mel_opts.low_freq == 22
 
    opts.mel_opts.high_freq = 1
    assert opts.mel_opts.high_freq == 1
 
    opts.mel_opts.vtln_low = 101
    assert opts.mel_opts.vtln_low == 101
 
    opts.mel_opts.vtln_high = -100
    assert opts.mel_opts.vtln_high == -100
 
    opts.mel_opts.debug_mel = True
    assert opts.mel_opts.debug_mel is True
 
    opts.mel_opts.htk_mode = True
    assert opts.mel_opts.htk_mode is True
 
 
def test_from_empty_dict():
    opts = knf.FbankOptions.from_dict({})
    opts2 = knf.FbankOptions()
 
    assert str(opts) == str(opts2)
 
 
def test_from_dict_partial():
    d = {
        "energy_floor": 10.5,
        "htk_compat": True,
        "mel_opts": {"num_bins": 80, "vtln_low": 1},
        "frame_opts": {"window_type": "hanning"},
    }
    opts = knf.FbankOptions.from_dict(d)
    assert opts.energy_floor == 10.5
    assert opts.htk_compat is True
    assert opts.mel_opts.num_bins == 80
    assert opts.mel_opts.vtln_low == 1
    assert opts.frame_opts.window_type == "hanning"
 
    mel_opts = knf.MelBanksOptions.from_dict(d["mel_opts"])
    assert str(opts.mel_opts) == str(mel_opts)
 
 
def test_from_dict_full_and_as_dict():
    opts = knf.FbankOptions()
    opts.htk_compat = True
    opts.mel_opts.num_bins = 80
    opts.frame_opts.samp_freq = 10
 
    d = opts.as_dict()
    assert d["htk_compat"] is True
    assert d["mel_opts"]["num_bins"] == 80
    assert d["frame_opts"]["samp_freq"] == 10
 
    mel_opts = knf.MelBanksOptions()
    mel_opts.num_bins = 80
    assert d["mel_opts"] == mel_opts.as_dict()
 
    frame_opts = knf.FrameExtractionOptions()
    frame_opts.samp_freq = 10
    assert d["frame_opts"] == frame_opts.as_dict()
 
    opts2 = knf.FbankOptions.from_dict(d)
    assert str(opts2) == str(opts)
 
    d["htk_compat"] = False
    opts3 = knf.FbankOptions.from_dict(d)
    assert opts3.htk_compat is False
 
 
def test_pickle():
    opts = knf.FbankOptions()
    opts.use_energy = True
    opts.use_power = False
 
    opts.frame_opts.samp_freq = 44100
    opts.mel_opts.num_bins = 100
 
    data = pickle.dumps(opts)
 
    opts2 = pickle.loads(data)
    assert str(opts) == str(opts2)
 
 
def main():
    test_default()
    test_set_get()
    test_set_get_frame_opts()
    test_set_get_mel_opts()
    test_from_empty_dict()
    test_from_dict_partial()
    test_from_dict_full_and_as_dict()
    test_pickle()
 
 
if __name__ == "__main__":
    main()