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
#!/usr/bin/env python3
#
# Copyright (c)  2022  Xiaomi Corporation (authors: Fangjun Kuang)
 
import pickle
 
import kaldi_native_fbank as knf
 
 
def test_default():
    opts = knf.FrameExtractionOptions()
    assert opts.samp_freq == 16000
    assert opts.frame_shift_ms == 10.0
    assert opts.frame_length_ms == 25.0
    assert opts.dither == 1.0
    assert abs(opts.preemph_coeff - 0.97) < 1e-6
    assert opts.remove_dc_offset is True
    assert opts.window_type == "povey"
    assert opts.round_to_power_of_two is True
    assert abs(opts.blackman_coeff - 0.42) < 1e-6
    assert opts.snip_edges is True
 
 
def test_set_get():
    opts = knf.FrameExtractionOptions()
    opts.samp_freq = 44100
    assert opts.samp_freq == 44100
 
    opts.frame_shift_ms = 20.5
    assert opts.frame_shift_ms == 20.5
 
    opts.frame_length_ms = 1
    assert opts.frame_length_ms == 1
 
    opts.dither = 0.5
    assert opts.dither == 0.5
 
    opts.preemph_coeff = 0.25
    assert opts.preemph_coeff == 0.25
 
    opts.remove_dc_offset = False
    assert opts.remove_dc_offset is False
 
    opts.window_type = "hanning"
    assert opts.window_type == "hanning"
 
    opts.round_to_power_of_two = False
    assert opts.round_to_power_of_two is False
 
    opts.blackman_coeff = 0.25
    assert opts.blackman_coeff == 0.25
 
    opts.snip_edges = False
    assert opts.snip_edges is False
 
 
def test_from_empty_dict():
    opts = knf.FrameExtractionOptions.from_dict({})
    opts2 = knf.FrameExtractionOptions()
 
    assert str(opts) == str(opts2)
 
 
def test_from_dict_partial():
    d = {"samp_freq": 10, "frame_shift_ms": 2}
 
    opts = knf.FrameExtractionOptions.from_dict(d)
 
    opts2 = knf.FrameExtractionOptions()
    assert str(opts) != str(opts2)
 
    opts2.samp_freq = 10
    assert str(opts) != str(opts2)
 
    opts2.frame_shift_ms = 2
    assert str(opts) == str(opts2)
 
    opts2.frame_shift_ms = 3
    assert str(opts) != str(opts2)
 
 
def test_from_dict_full_and_as_dict():
    opts = knf.FrameExtractionOptions()
    opts.samp_freq = 20
    opts.frame_length_ms = 100
 
    d = opts.as_dict()
    for key, value in d.items():
        assert value == getattr(opts, key)
 
    opts2 = knf.FrameExtractionOptions.from_dict(d)
    assert str(opts2) == str(opts)
 
    d["window_type"] = "hanning"
    opts3 = knf.FrameExtractionOptions.from_dict(d)
    assert opts3.window_type == "hanning"
 
 
def test_pickle():
    opts = knf.FrameExtractionOptions()
    opts.samp_freq = 44100
    opts.dither = 5.5
    data = pickle.dumps(opts)
 
    opts2 = pickle.loads(data)
    assert str(opts) == str(opts2)
 
 
def main():
    test_default()
    test_set_get()
    test_from_empty_dict()
    test_from_dict_partial()
    test_from_dict_full_and_as_dict()
    test_pickle()
 
 
if __name__ == "__main__":
    main()