forked from qqueing/DeepSpeaker-pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
audio_processing.py
214 lines (162 loc) · 7.15 KB
/
audio_processing.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
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import numpy as np
from python_speech_features import fbank, delta
import constants as c
import librosa
def mk_MFB(filename, sample_rate=c.SAMPLE_RATE,use_delta = c.USE_DELTA,use_scale = c.USE_SCALE,use_logscale = c.USE_LOGSCALE):
audio, sr = librosa.load(filename, sr=sample_rate, mono=True)
#audio = audio.flatten()
filter_banks, energies = fbank(audio, samplerate=sample_rate, nfilt=c.FILTER_BANK, winlen=0.025)
if use_logscale:
filter_banks = 20 * np.log10(np.maximum(filter_banks,1e-5))
if use_delta:
delta_1 = delta(filter_banks, N=1)
delta_2 = delta(delta_1, N=1)
filter_banks = normalize_frames(filter_banks, Scale=use_scale)
delta_1 = normalize_frames(delta_1, Scale=use_scale)
delta_2 = normalize_frames(delta_2, Scale=use_scale)
frames_features = np.hstack([filter_banks, delta_1, delta_2])
else:
filter_banks = normalize_frames(filter_banks, Scale=use_scale)
frames_features = filter_banks
np.save(filename.replace('.wav', '.npy'),frames_features)
return
def read_MFB(filename):
#audio, sr = librosa.load(filename, sr=sample_rate, mono=True)
#audio = audio.flatten()
audio = np.load(filename.replace('.wav', '.npy'))
return audio
class truncatedinputfromMFB(object):
"""Rescales the input PIL.Image to the given 'size'.
If 'size' is a 2-element tuple or list in the order of (width, height), it will be the exactly size to scale.
If 'size' is a number, it will indicate the size of the smaller edge.
For example, if height > width, then image will be
rescaled to (size * height / width, size)
size: size of the exactly size or the smaller edge
interpolation: Default: PIL.Image.BILINEAR
"""
def __init__(self, input_per_file=1):
super(truncatedinputfromMFB, self).__init__()
self.input_per_file = input_per_file
def __call__(self, frames_features):
network_inputs = []
num_frames = len(frames_features)
import random
for i in range(self.input_per_file):
j = random.randrange(c.NUM_PREVIOUS_FRAME, num_frames - c.NUM_NEXT_FRAME)
if not j:
frames_slice = np.zeros(c.NUM_FRAMES, c.FILTER_BANK, 'float64')
frames_slice[0:(frames_features.shape)[0]] = frames_features.shape
else:
frames_slice = frames_features[j - c.NUM_PREVIOUS_FRAME:j + c.NUM_NEXT_FRAME]
network_inputs.append(frames_slice)
return np.array(network_inputs)
def read_audio(filename, sample_rate=c.SAMPLE_RATE):
audio, sr = librosa.load(filename, sr=sample_rate, mono=True)
audio = audio.flatten()
return audio
#this is not good
#def normalize_frames(m):
# return [(v - np.mean(v)) / (np.std(v) + 2e-12) for v in m]
def normalize_frames(m,Scale=True):
if Scale:
return (m - np.mean(m, axis=0)) / (np.std(m, axis=0) + 2e-12)
else:
return (m - np.mean(m, axis=0))
def pre_process_inputs(signal=np.random.uniform(size=32000), target_sample_rate=8000,use_delta = c.USE_DELTA):
filter_banks, energies = fbank(signal, samplerate=target_sample_rate, nfilt=c.FILTER_BANK, winlen=0.025)
delta_1 = delta(filter_banks, N=1)
delta_2 = delta(delta_1, N=1)
filter_banks = normalize_frames(filter_banks)
delta_1 = normalize_frames(delta_1)
delta_2 = normalize_frames(delta_2)
if use_delta:
frames_features = np.hstack([filter_banks, delta_1, delta_2])
else:
frames_features = filter_banks
num_frames = len(frames_features)
network_inputs = []
"""Too complicated
for j in range(c.NUM_PREVIOUS_FRAME, num_frames - c.NUM_NEXT_FRAME):
frames_slice = frames_features[j - c.NUM_PREVIOUS_FRAME:j + c.NUM_NEXT_FRAME]
#network_inputs.append(np.reshape(frames_slice, (32, 20, 3)))
network_inputs.append(frames_slice)
"""
import random
j = random.randrange(c.NUM_PREVIOUS_FRAME, num_frames - c.NUM_NEXT_FRAME)
frames_slice = frames_features[j - c.NUM_PREVIOUS_FRAME:j + c.NUM_NEXT_FRAME]
network_inputs.append(frames_slice)
return np.array(network_inputs)
class truncatedinput(object):
"""Rescales the input PIL.Image to the given 'size'.
If 'size' is a 2-element tuple or list in the order of (width, height), it will be the exactly size to scale.
If 'size' is a number, it will indicate the size of the smaller edge.
For example, if height > width, then image will be
rescaled to (size * height / width, size)
size: size of the exactly size or the smaller edge
interpolation: Default: PIL.Image.BILINEAR
"""
def __call__(self, input):
#min_existing_frames = min(self.libri_batch['raw_audio'].apply(lambda x: len(x)).values)
want_size = int(c.TRUNCATE_SOUND_FIRST_SECONDS * c.SAMPLE_RATE)
if want_size > len(input):
output = np.zeros((want_size,))
output[0:len(input)] = input
#print("biho check")
return output
else:
return input[0:want_size]
class toMFB(object):
"""Rescales the input PIL.Image to the given 'size'.
If 'size' is a 2-element tuple or list in the order of (width, height), it will be the exactly size to scale.
If 'size' is a number, it will indicate the size of the smaller edge.
For example, if height > width, then image will be
rescaled to (size * height / width, size)
size: size of the exactly size or the smaller edge
interpolation: Default: PIL.Image.BILINEAR
"""
def __call__(self, input):
output = pre_process_inputs(input, target_sample_rate=c.SAMPLE_RATE)
return output
import torch
class totensor(object):
"""Rescales the input PIL.Image to the given 'size'.
If 'size' is a 2-element tuple or list in the order of (width, height), it will be the exactly size to scale.
If 'size' is a number, it will indicate the size of the smaller edge.
For example, if height > width, then image will be
rescaled to (size * height / width, size)
size: size of the exactly size or the smaller edge
interpolation: Default: PIL.Image.BILINEAR
"""
def __call__(self, pic):
"""
Args:
pic (PIL.Image or numpy.ndarray): Image to be converted to tensor.
Returns:
Tensor: Converted image.
"""
if isinstance(pic, np.ndarray):
# handle numpy array
#img = torch.from_numpy(pic.transpose((0, 2, 1)))
#return img.float()
img = torch.FloatTensor(pic.transpose((0, 2, 1)))
#img = np.float32(pic.transpose((0, 2, 1)))
return img
#img = torch.from_numpy(pic)
# backward compatibility
class tonormal(object):
def __init__(self):
self.mean = 0.013987
self.var = 1.008
def __call__(self, tensor):
"""
Args:
tensor (Tensor): Tensor image of size (C, H, W) to be normalized.
Returns:
Tensor: Normalized image.
"""
# TODO: make efficient
print(self.mean)
self.mean+=1
#for t, m, s in zip(tensor, self.mean, self.std):
# t.sub_(m).div_(s)
return tensor