forked from csukuangfj/kaldifeat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_mfcc.py
executable file
·141 lines (105 loc) · 3.8 KB
/
test_mfcc.py
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
#!/usr/bin/env python3
# Copyright 2021 Xiaomi Corporation (authors: Fangjun Kuang)
import pickle
from pathlib import Path
import torch
from utils import get_devices, read_ark_txt, read_wave
import kaldifeat
cur_dir = Path(__file__).resolve().parent
def test_online_mfcc(
opts: kaldifeat.MfccOptions,
wave: torch.Tensor,
cpu_features: torch.Tensor,
):
"""
Args:
opts:
The options to create the online mfcc extractor.
wave:
The input 1-D waveform.
cpu_features:
The groud truth features that are computed offline
"""
online_mfcc = kaldifeat.OnlineMfcc(opts)
num_processed_frames = 0
i = 0 # current sample index to feed
while not online_mfcc.is_last_frame(num_processed_frames - 1):
while num_processed_frames < online_mfcc.num_frames_ready:
# There are new frames to be processed
frame = online_mfcc.get_frame(num_processed_frames)
assert torch.allclose(
frame.squeeze(0), cpu_features[num_processed_frames], atol=1e-3
)
num_processed_frames += 1
# Simulate streaming . Send a random number of audio samples
# to the extractor
num_samples = torch.randint(300, 1000, (1,)).item()
samples = wave[i : (i + num_samples)] # noqa
i += num_samples
if len(samples) == 0:
online_mfcc.input_finished()
continue
online_mfcc.accept_waveform(16000, samples)
assert num_processed_frames == online_mfcc.num_frames_ready
assert num_processed_frames == cpu_features.size(0)
def test_mfcc_default():
print("=====test_mfcc_default=====")
filename = cur_dir / "test_data/test.wav"
wave = read_wave(filename)
gt = read_ark_txt(cur_dir / "test_data/test-mfcc.txt")
cpu_features = None
for device in get_devices():
print("device", device)
opts = kaldifeat.MfccOptions()
opts.device = device
opts.frame_opts.dither = 0
mfcc = kaldifeat.Mfcc(opts)
features = mfcc(wave.to(device))
if device.type == "cpu":
cpu_features = features
assert torch.allclose(features.cpu(), gt, atol=1e-1)
opts = kaldifeat.MfccOptions()
opts.frame_opts.dither = 0
test_online_mfcc(opts, wave, cpu_features)
def test_mfcc_no_snip_edges():
print("=====test_mfcc_no_snip_edges=====")
filename = cur_dir / "test_data/test.wav"
wave = read_wave(filename)
gt = read_ark_txt(cur_dir / "test_data/test-mfcc-no-snip-edges.txt")
cpu_features = None
for device in get_devices():
print("device", device)
opts = kaldifeat.MfccOptions()
opts.device = device
opts.frame_opts.dither = 0
opts.frame_opts.snip_edges = False
mfcc = kaldifeat.Mfcc(opts)
features = mfcc(wave.to(device))
if device.type == "cpu":
cpu_features = features
assert torch.allclose(features.cpu(), gt, rtol=1e-1)
opts = kaldifeat.MfccOptions()
opts.frame_opts.dither = 0
opts.frame_opts.snip_edges = False
test_online_mfcc(opts, wave, cpu_features)
def test_pickle():
for device in get_devices():
opts = kaldifeat.MfccOptions()
opts.device = device
opts.frame_opts.dither = 0
opts.frame_opts.snip_edges = False
mfcc = kaldifeat.Mfcc(opts)
data = pickle.dumps(mfcc)
mfcc2 = pickle.loads(data)
assert str(mfcc.opts) == str(mfcc2.opts)
opts = kaldifeat.MfccOptions()
opts.frame_opts.dither = 0
opts.frame_opts.snip_edges = False
mfcc = kaldifeat.OnlineMfcc(opts)
data = pickle.dumps(mfcc)
mfcc2 = pickle.loads(data)
assert str(mfcc.opts) == str(mfcc2.opts)
if __name__ == "__main__":
test_mfcc_default()
test_mfcc_no_snip_edges()
test_pickle()