/
_transform.py
411 lines (352 loc) · 13.3 KB
/
_transform.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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
import numpy as np
import librosa
import cv2
from matchering import Config
from matchering.stages import main
from matchering.saver import save
from matchering.utils import get_temp_folder
from matchering import pcm24
def spectrum_calibration(
target: str,
reference: str,
config: Config = Config(),
save_to_wav: bool = False,
to_mono: bool = True
) -> np.ndarray:
"""Processes the target audio to match the reference audio.
Args:
target: Target audio.
reference: Reference audio.
config: Configuration for the processing.
save_to_wav: Whether to save the processed audio to a wav file.
to_mono: Whether to convert the processed audio to mono.
Returns:
correct_result: Processed audio.
"""
results = [pcm24("result.wav")]
# Get a temporary folder for converting mp3's
temp_folder = config.temp_folder if config.temp_folder \
else get_temp_folder(results)
target = np.vstack((target, target)).T
reference = np.vstack((reference, reference)).T
# Process
result, result_no_limiter, result_no_limiter_normalized = main(
target,
reference,
config,
need_default=any(rr.use_limiter for rr in results),
need_no_limiter=any(
not rr.use_limiter and not rr.normalize for rr in results),
need_no_limiter_normalized=any(
not rr.use_limiter and rr.normalize for rr in results
),
)
del reference
del target
# Output
for required_result in results:
if required_result.use_limiter:
correct_result = result
else:
if required_result.normalize:
correct_result = result_no_limiter_normalized
else:
correct_result = result_no_limiter
if save_to_wav:
save(
required_result.file,
correct_result,
config.internal_sample_rate,
required_result.subtype,
)
# convert to mono if needed
if to_mono:
correct_result = correct_result.mean(axis=1)
return correct_result
def q_transform(
sig: np.ndarray,
sr: int = 44100,
n_bins: int = 84,
hop_length: int = 512,
fmin: int = 55,
norm: int = 1,
bins_per_octave: int = 12,
tuning: int = None,
filter_scale: int = 1,
sparsity: int = 0.01,
window: str = 'hann',
scale: bool = True,
pad_mode: str = 'reflect',
to_db: bool = True,
dsize: tuple = None
) -> np.ndarray:
"""Computes the constant-Q transform of an audio signal.
Args:
sig: Input signal.
sr: Sampling rate of the input signal.
n_bins: Number of frequency bins.
hop_length: Number of samples between successive frames.
fmin: Minimum frequency.
norm: Normalization factor.
bins_per_octave: Number of bins per octave.
tuning: Deviation from A440 tuning in fractional bins.
filter_scale: Filter scale factor. Small values (<1)
use shorter windows for improved
time resolution.
sparsity: Sparsity of the CQT basis.
window: Type of window to use.
scale: If True, scale the magnitude of the CQT by n_bins
pad_mode: If center=True, the padding mode to
use at the edges of the signal.
By default, STFT uses reflection padding.
to_db: Convert the spectrogram to dB scale.
dsize: Size of the output spectrogram :
if None, the output is the raw spectrogram.
Returns:
q_transform: Returns the constant-Q transform of an audio signal.
"""
q_transform = librosa.core.cqt(sig, sr=sr, n_bins=n_bins,
hop_length=hop_length, fmin=fmin,
norm=norm, bins_per_octave=bins_per_octave,
tuning=tuning, filter_scale=filter_scale,
sparsity=sparsity, window=window,
scale=scale, pad_mode=pad_mode)
if to_db is True:
q_transform = librosa.amplitude_to_db(q_transform, ref=np.max)
if dsize is not None:
q_transform = cv2.resize(
q_transform, dsize=dsize, interpolation=cv2.INTER_CUBIC)
return q_transform
def melspectrogram(
sig: np.ndarray,
sr: int = 44100,
n_fft: int = 2048,
hop_length: int = 512,
n_mels: int = 128,
fmin: int = 0.0,
fmax: int = 8000,
power: int = 2,
to_db: bool = True,
dsize: tuple = None
) -> np.ndarray:
"""Computes a mel-scaled spectrogram.
Args:
sig: Input signal.
sr: Sampling rate of the input signal.
n_fft: Length of the FFT window.
hop_length: Number of samples between successive frames.
n_mels: Number of Mel bands to generate.
fmin: Minimum frequency.
fmax: Maximum frequency.
power: Power of the spectrogram.
to_db: Convert the spectrogram to dB scale.
dsize: Size of the output spectrogram :
if None, the output is the raw spectrogram.
Returns:
mel_spectrogram: Returns a mel-scaled spectrogram.
"""
mel_spectrogram = librosa.feature.melspectrogram(
y=sig, sr=sr, n_fft=n_fft, hop_length=hop_length, n_mels=n_mels,
fmin=fmin, fmax=fmax, power=power)
if to_db is True:
mel_spectrogram = librosa.power_to_db(mel_spectrogram, ref=np.max)
if dsize is not None:
mel_spectrogram = cv2.resize(
mel_spectrogram, dsize=dsize, interpolation=cv2.INTER_CUBIC)
return mel_spectrogram
def inverse_melspectrogram(
sig: np.ndarray,
n_fft: int = 2048,
hop_length: int = 512,
win_length: int = None,
window: str = 'hann',
center: bool = True,
pad_mode: str = 'reflect',
power: float = 2.0,
n_iter: int = 32,
length: int = None
) -> np.ndarray:
"""Computes the inverse of a mel-scaled spectrogram.
Args:
sig: Input signal.
n_fft: Length of the FFT window.
hop_length: Number of samples between successive frames.
win_length: Each frame of audio is windowed by window
of length win_length and then padded
with zeroes to match n_fft.
window: Type of window to use.
center: If True, the signal y is padded
so that frame D[:, t] is centered
at y[t * hop_length].
pad_mode: If center=True, the padding mode to use
at the edges of the signal.
By default, STFT uses reflection padding.
power: Power of the spectrogram.
n_iter: Number of inversion iterations.
length: If provided, the output y is zero-padded or clipped
to exactly length samples.
Returns:
inverse_mel_spectrogram : Returns the inverse of
a mel-scaled spectrogram.
"""
inverse_mel_spectrogram = librosa.feature.inverse.mel_to_audio(
M=sig, sr=44100, n_fft=n_fft, hop_length=hop_length,
win_length=win_length, window=window, center=center,
pad_mode=pad_mode, power=power, n_iter=n_iter, length=length)
return inverse_mel_spectrogram
def chroma(
sig: np.ndarray,
sr: int = 44100,
hop_length: int = 512,
fmin: float = None,
norm: int = 1,
threshold: float = 0.0,
tuning: float = None,
n_chroma: int = 12,
n_octaves: int = 7,
window: str = None,
bins_per_octave: int = 36,
cqt_mode: str = 'full',
dsize: tuple = None
) -> np.ndarray:
"""Computes a chromagram from a waveform or power spectrogram.
Args:
sig: Input signal.
sr: Sampling rate of the input signal.
hop_length: Number of samples between successive frames.
fmin: Minimum frequency.
norm: Normalization factor.
threshold: Pre-normalization energy threshold.
Values below the threshold are discarded,
resulting in a sparse chromagram.
tuning: Deviation from A440 tuning in fractional bins.
n_chroma: Number of chroma bins to produce.
n_octaves: Number of octaves to analyze above fmin.
window: Type of window to use.
bins_per_octave: Number of bins per octave.
cqt_mode: Constant-Q transform mode.
dsize: Size of the output spectrogram : if None,
the output is the raw spectrogram.
Returns:
chroma: Returns the chromagram for an audio signal
"""
chroma = librosa.feature.chroma_cqt(y=sig, sr=sr, hop_length=hop_length,
fmin=fmin, norm=norm,
threshold=threshold, tuning=tuning,
n_chroma=n_chroma, n_octaves=n_octaves,
window=window,
bins_per_octave=bins_per_octave,
cqt_mode=cqt_mode)
if dsize is not None:
chroma = cv2.resize(
chroma, dsize=dsize, interpolation=cv2.INTER_CUBIC)
return chroma
def chroma_cens(
sig: np.ndarray,
sr: int,
n_chroma: int = 12,
hop_length: int = 512,
fmin: float = None,
norm: int = 1,
tuning: float = None,
n_octaves: int = 7,
bins_per_octave: int = 36,
cqt_mode: str = 'full',
dsize: tuple = None
) -> np.ndarray:
"""Computes the chroma variant “Chroma Energy Normalized” (CENS).
Args:
sig: Input signal.
sr: Sampling rate of the input signal.
n_chroma: Number of chroma bins to produce.
hop_length: Number of samples between successive frames.
fmin: Minimum frequency.
norm: Normalization factor.
tuning: Deviation from A440 tuning in fractional bins.
n_octaves: Number of octaves to analyze above fmin.
bins_per_octave: Number of bins per octave.
cqt_mode: Constant-Q transform mode.
dsize: Size of the output spectrogram :
if None, the output is the raw spectrogram.
Returns:
chroma_cens: Returns the chroma variant (CENS).
"""
chroma_cens = librosa.feature.chroma_cens(y=sig, sr=sr, n_chroma=n_chroma,
hop_length=hop_length, fmin=fmin,
norm=norm, tuning=tuning,
n_octaves=n_octaves,
bins_per_octave=bins_per_octave,
cqt_mode=cqt_mode)
if dsize is not None:
chroma_cens = cv2.resize(
chroma_cens, dsize=dsize, interpolation=cv2.INTER_CUBIC)
return chroma_cens
def chroma_stft(
sig: np.ndaarray,
sr: int,
n_chroma: int = 12,
hop_length: int = 512,
win_length: int = None,
window: str = 'hann',
center: bool = True,
pad_mode: str = 'reflect',
tuning: float = None,
dsize: tuple = None
) -> np.ndarray:
"""Computes a stft chromagram from a waveform or power spectrogram.
Args:
sig: Input
sr: Sampling rate of the input signal.
n_chroma: Number of chroma bins to produce.
hop_length: Number of samples between successive frames.
win_length: Each frame of audio is windowed by window().
The window will be of length win_length and
then padded with zeros to match n_fft.
window: Type of window to use.
center: If True, the signal y is padded so that
frame D[:, t] is centered at y[t * hop_length].
pad_mode: If center=True, the padding mode to use
at the edges of the signal.
By default, STFT uses reflection padding.
tuning: Deviation from A440 tuning in fractional bins.
dsize: Size of the output spectrogram :
if None, the output is the raw spectrogram.
Returns:
chroma_stft: Returns the chromagram for an audio signal.
"""
chroma_stft = librosa.feature.chroma_stft(y=sig, sr=sr, n_chroma=n_chroma,
hop_length=hop_length,
win_length=win_length,
window=window, center=center,
pad_mode=pad_mode, tuning=tuning)
if dsize is not None:
chroma_stft = cv2.resize(
chroma_stft, dsize=dsize, interpolation=cv2.INTER_CUBIC)
return chroma_stft
def mfcc(
sig: np.ndarray,
sr: int = 22050,
spec: np.ndarray = None,
n_mfcc: int = 20,
dct_type: int = 2,
norm: str = 'ortho',
lifter: int = 0
) -> np.ndarray:
"""
Compute the MFCCs (Mel-frequency cepstral coefficients)
from an audio signal.
Args:
sig: Input signal.
sr: Sampling rate of the input signal.
spec: Pre-computed spectrogram.
n_mfcc: Number of MFCCs to return.
dct_type: Type of DCT (discrete cosine transform) to use.
norm: Type of norm to use.
lifter: Parameter for inversion of MFCCs.
Returns:
mfcc: Mel-frequency cepstral coefficients.
"""
mfcc = librosa.feature.mfcc(
y=sig, sr=sr, S=spec, n_mfcc=n_mfcc, dct_type=dct_type, norm=norm,
lifter=lifter)
return mfcc