-
Notifications
You must be signed in to change notification settings - Fork 844
/
audio.py
372 lines (280 loc) · 10.1 KB
/
audio.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
# -*- coding: utf-8 -*-
# !/usr/bin/env python
from scipy import signal
from pydub import AudioSegment
import os
import librosa
import soundfile as sf
import numpy as np
def read_wav(path, sr, duration=None, mono=True):
wav, _ = librosa.load(path, mono=mono, sr=sr, duration=duration)
return wav
def write_wav(wav, sr, path, format='wav', subtype='PCM_16'):
sf.write(path, wav, sr, format=format, subtype=subtype)
def read_mfcc(prefix):
filename = '{}.mfcc.npy'.format(prefix)
mfcc = np.load(filename)
return mfcc
def write_mfcc(prefix, mfcc):
filename = '{}.mfcc'.format(prefix)
np.save(filename, mfcc)
def read_spectrogram(prefix):
filename = '{}.spec.npy'.format(prefix)
spec = np.load(filename)
return spec
def write_spectrogram(prefix, spec):
filename = '{}.spec'.format(prefix)
np.save(filename, spec)
def split_wav(wav, top_db):
intervals = librosa.effects.split(wav, top_db=top_db)
wavs = map(lambda i: wav[i[0]: i[1]], intervals)
return wavs
def trim_wav(wav):
wav, _ = librosa.effects.trim(wav)
return wav
def fix_length(wav, length):
if len(wav) != length:
wav = librosa.util.fix_length(wav, length)
return wav
def crop_random_wav(wav, length):
"""
Randomly cropped a part in a wav file.
:param wav: a waveform
:param length: length to be randomly cropped.
:return: a randomly cropped part of wav.
"""
assert (wav.ndim <= 2)
assert (type(length) == int)
wav_len = wav.shape[-1]
start = np.random.choice(range(np.maximum(1, wav_len - length)), 1)[0]
end = start + length
if wav.ndim == 1:
wav = wav[start:end]
else:
wav = wav[:, start:end]
return wav
def mp3_to_wav(src_path, tar_path):
"""
Read mp3 file from source path, convert it to wav and write it to target path.
Necessary libraries: ffmpeg, libav.
:param src_path: source mp3 file path
:param tar_path: target wav file path
"""
basepath, filename = os.path.split(src_path)
os.chdir(basepath)
AudioSegment.from_mp3(src_path).export(tar_path, format='wav')
def prepro_audio(source_path, target_path, format=None, sr=None, db=None):
"""
Read a wav, change sample rate, format, and average decibel and write to target path.
:param source_path: source wav file path
:param target_path: target wav file path
:param sr: sample rate.
:param format: output audio format.
:param db: decibel.
"""
sound = AudioSegment.from_file(source_path, format)
if sr:
sound = sound.set_frame_rate(sr)
if db:
change_dBFS = db - sound.dBFS
sound = sound.apply_gain(change_dBFS)
sound.export(target_path, 'wav')
def _split_path(path):
"""
Split path to basename, filename and extension. For example, 'a/b/c.wav' => ('a/b', 'c', 'wav')
:param path: file path
:return: basename, filename, and extension
"""
basepath, filename = os.path.split(path)
filename, extension = os.path.splitext(filename)
return basepath, filename, extension
def wav2spec(wav, n_fft, win_length, hop_length, time_first=True):
"""
Get magnitude and phase spectrogram from waveforms.
Parameters
----------
wav : np.ndarray [shape=(n,)]
The real-valued waveform.
n_fft : int > 0 [scalar]
FFT window size.
win_length : int <= n_fft [scalar]
The window will be of length `win_length` and then padded
with zeros to match `n_fft`.
hop_length : int > 0 [scalar]
Number audio of frames between STFT columns.
time_first : boolean. optional.
if True, time axis is followed by bin axis. In this case, shape of returns is (t, 1 + n_fft/2)
Returns
-------
mag : np.ndarray [shape=(t, 1 + n_fft/2) or (1 + n_fft/2, t)]
Magnitude spectrogram.
phase : np.ndarray [shape=(t, 1 + n_fft/2) or (1 + n_fft/2, t)]
Phase spectrogram.
"""
stft = librosa.stft(y=wav, n_fft=n_fft, hop_length=hop_length, win_length=win_length)
mag = np.abs(stft)
phase = np.angle(stft)
if time_first:
mag = mag.T
phase = phase.T
return mag, phase
def spec2wav(mag, n_fft, win_length, hop_length, num_iters=30, phase=None):
"""
Get a waveform from the magnitude spectrogram by Griffin-Lim Algorithm.
Parameters
----------
mag : np.ndarray [shape=(1 + n_fft/2, t)]
Magnitude spectrogram.
n_fft : int > 0 [scalar]
FFT window size.
win_length : int <= n_fft [scalar]
The window will be of length `win_length` and then padded
with zeros to match `n_fft`.
hop_length : int > 0 [scalar]
Number audio of frames between STFT columns.
num_iters: int > 0 [scalar]
Number of iterations of Griffin-Lim Algorithm.
phase : np.ndarray [shape=(1 + n_fft/2, t)]
Initial phase spectrogram.
Returns
-------
wav : np.ndarray [shape=(n,)]
The real-valued waveform.
"""
assert (num_iters > 0)
if phase is None:
phase = np.pi * np.random.rand(*mag.shape)
stft = mag * np.exp(1.j * phase)
wav = None
for i in range(num_iters):
wav = librosa.istft(stft, win_length=win_length, hop_length=hop_length)
if i != num_iters - 1:
stft = librosa.stft(wav, n_fft=n_fft, win_length=win_length, hop_length=hop_length)
_, phase = librosa.magphase(stft)
phase = np.angle(phase)
stft = mag * np.exp(1.j * phase)
return wav
def preemphasis(wav, coeff=0.97):
"""
Emphasize high frequency range of the waveform by increasing power(squared amplitude).
Parameters
----------
wav : np.ndarray [shape=(n,)]
Real-valued the waveform.
coeff: float <= 1 [scalar]
Coefficient of pre-emphasis.
Returns
-------
preem_wav : np.ndarray [shape=(n,)]
The pre-emphasized waveform.
"""
preem_wav = signal.lfilter([1, -coeff], [1], wav)
return preem_wav
def inv_preemphasis(preem_wav, coeff=0.97):
"""
Invert the pre-emphasized waveform to the original waveform.
Parameters
----------
preem_wav : np.ndarray [shape=(n,)]
The pre-emphasized waveform.
coeff: float <= 1 [scalar]
Coefficient of pre-emphasis.
Returns
-------
wav : np.ndarray [shape=(n,)]
Real-valued the waveform.
"""
wav = signal.lfilter([1], [1, -coeff], preem_wav)
return wav
def linear_to_mel(linear, sr, n_fft, n_mels, **kwargs):
"""
Convert a linear-spectrogram to mel-spectrogram.
:param linear: Linear-spectrogram.
:param sr: Sample rate.
:param n_fft: FFT window size.
:param n_mels: Number of mel filters.
:return: Mel-spectrogram.
"""
mel_basis = librosa.filters.mel(sr, n_fft, n_mels, **kwargs) # (n_mels, 1+n_fft//2)
mel = np.dot(mel_basis, linear) # (n_mels, t) # mel spectrogram
return mel
def amp2db(amp):
return librosa.amplitude_to_db(amp)
def db2amp(db):
return librosa.db_to_amplitude(db)
def normalize_db(db, max_db, min_db):
"""
Normalize dB-scaled spectrogram values to be in range of 0~1.
:param db: Decibel-scaled spectrogram.
:param max_db: Maximum dB.
:param min_db: Minimum dB.
:return: Normalized spectrogram.
"""
norm_db = np.clip((db - min_db) / (max_db - min_db), 0, 1)
return norm_db
def denormalize_db(norm_db, max_db, min_db):
"""
Denormalize the normalized values to be original dB-scaled value.
:param norm_db: Normalized spectrogram.
:param max_db: Maximum dB.
:param min_db: Minimum dB.
:return: Decibel-scaled spectrogram.
"""
db = np.clip(norm_db, 0, 1) * (max_db - min_db) + min_db
return db
def dynamic_range_compression(db, threshold, ratio, method='downward'):
"""
Execute dynamic range compression(https://en.wikipedia.org/wiki/Dynamic_range_compression) to dB.
:param db: Decibel-scaled magnitudes
:param threshold: Threshold dB
:param ratio: Compression ratio.
:param method: Downward or upward.
:return: Range compressed dB-scaled magnitudes
"""
if method is 'downward':
db[db > threshold] = (db[db > threshold] - threshold) / ratio + threshold
elif method is 'upward':
db[db < threshold] = threshold - ((threshold - db[db < threshold]) / ratio)
return db
def emphasize_magnitude(mag, power=1.2):
"""
Emphasize a magnitude spectrogram by applying power function. This is used for removing noise.
:param mag: magnitude spectrogram.
:param power: exponent.
:return: emphasized magnitude spectrogram.
"""
emphasized_mag = np.power(mag, power)
return emphasized_mag
def wav2melspec(wav, sr, n_fft, win_length, hop_length, n_mels, time_first=True, **kwargs):
# Linear spectrogram
mag_spec, phase_spec = wav2spec(wav, n_fft, win_length, hop_length, time_first=False)
# Mel-spectrogram
mel_spec = linear_to_mel(mag_spec, sr, n_fft, n_mels, **kwargs)
# Time-axis first
if time_first:
mel_spec = mel_spec.T # (t, n_mels)
return mel_spec
def wav2melspec_db(wav, sr, n_fft, win_length, hop_length, n_mels, normalize=False, max_db=None, min_db=None,
time_first=True, **kwargs):
# Mel-spectrogram
mel_spec = wav2melspec(wav, sr, n_fft, win_length, hop_length, n_mels, time_first=False, **kwargs)
# Decibel
mel_db = librosa.amplitude_to_db(mel_spec)
# Normalization
mel_db = normalize_db(mel_db, max_db, min_db) if normalize else mel_db
# Time-axis first
if time_first:
mel_db = mel_db.T # (t, n_mels)
return mel_db
def wav2mfcc(wav, sr, n_fft, win_length, hop_length, n_mels, n_mfccs, preemphasis_coeff=0.97, time_first=True,
**kwargs):
# Pre-emphasis
wav_preem = preemphasis(wav, coeff=preemphasis_coeff)
# Decibel-scaled mel-spectrogram
mel_db = wav2melspec_db(wav_preem, sr, n_fft, win_length, hop_length, n_mels, time_first=False, **kwargs)
# MFCCs
mfccs = np.dot(librosa.filters.dct(n_mfccs, mel_db.shape[0]), mel_db)
# Time-axis first
if time_first:
mfccs = mfccs.T # (t, n_mfccs)
return mfccs