-
Notifications
You must be signed in to change notification settings - Fork 1
/
spectral_ops.py
361 lines (294 loc) · 11.5 KB
/
spectral_ops.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
# Copyright 2020 The DDSP Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# This file has been modified from the original
# Lint as: python3
"""Library of FFT operations for loss functions and conditioning."""
import crepe
import gin
import librosa
import numpy as np
import tensorflow.compat.v2 as tf
_CREPE_SAMPLE_RATE = 16000
_CREPE_FRAME_SIZE = 1024
F0_RANGE = 127.0 # MIDI
LD_RANGE = 120.0 # dB
def safe_log(x, eps=1e-5):
return tf.math.log(x + eps)
def stft(audio, frame_size=6144, overlap=0.75, pad_end=True):
"""Differentiable stft in tensorflow, computed in batch."""
audio = tf_float32(audio)
assert frame_size * overlap % 2.0 == 0.0
s = tf.signal.stft(
signals=audio,
frame_length=int(frame_size),
frame_step=int(frame_size * (1.0 - overlap)),
fft_length=int(frame_size),
pad_end=pad_end)
return s
def stft_np(audio, frame_size=6144, overlap=0.75, pad_end=True):
"""Non-differentiable stft using librosa, one example at a time."""
assert frame_size * overlap % 2.0 == 0.0
hop_size = int(frame_size * (1.0 - overlap))
is_2d = (len(audio.shape) == 2)
if pad_end:
n_samples_initial = int(audio.shape[-1])
n_frames = int(np.ceil(n_samples_initial / hop_size))
n_samples_final = (n_frames - 1) * hop_size + frame_size
pad = n_samples_final - n_samples_initial
padding = ((0, 0), (0, pad)) if is_2d else ((0, pad),)
audio = np.pad(audio, padding, 'constant')
def stft_fn(y):
return librosa.stft(y=y,
n_fft=int(frame_size),
hop_length=hop_size,
center=False).T
s = np.stack([stft_fn(a) for a in audio]) if is_2d else stft_fn(audio)
return s
@gin.register
def compute_mag(audio, size=6144, overlap=0.75, pad_end=True):
mag = tf.abs(stft(audio, frame_size=size, overlap=overlap, pad_end=pad_end))
return tf_float32(mag)
@gin.register
def compute_mel(audio,
lo_hz=0.0,
hi_hz=24000.0,
bins=64,
fft_size=2048,
overlap=0.75,
pad_end=True):
"""Calculate Mel Spectrogram."""
mag = compute_mag(audio, fft_size, overlap, pad_end)
num_spectrogram_bins = int(mag.shape[-1])
linear_to_mel_matrix = tf.signal.linear_to_mel_weight_matrix(
bins, num_spectrogram_bins, 48000, lo_hz, hi_hz)
mel = tf.tensordot(mag, linear_to_mel_matrix, 1)
mel.set_shape(mag.shape[:-1].concatenate(linear_to_mel_matrix.shape[-1:]))
return mel
@gin.register
def compute_logmag(audio, size=6144, overlap=0.75, pad_end=True):
return safe_log(compute_mag(audio, size, overlap, pad_end))
@gin.register
def compute_logmel(audio,
lo_hz=80.0,
hi_hz=22800.0,
bins=64,
fft_size=2048,
overlap=0.75,
pad_end=True):
mel = compute_mel(audio, lo_hz, hi_hz, bins, fft_size, overlap, pad_end)
return safe_log(mel)
@gin.register
def compute_mfcc(audio,
lo_hz=20.0,
hi_hz=24000.0,
fft_size=1024,
mel_bins=128,
mfcc_bins=13,
overlap=0.75,
pad_end=True):
"""Calculate Mel-frequency Cepstral Coefficients."""
logmel = compute_logmel(
audio,
lo_hz=lo_hz,
hi_hz=hi_hz,
bins=mel_bins,
fft_size=fft_size,
overlap=overlap,
pad_end=pad_end)
mfccs = tf.signal.mfccs_from_log_mel_spectrograms(logmel)
return mfccs[..., :mfcc_bins]
def diff(x, axis=-1):
"""Take the finite difference of a tensor along an axis.
Args:
x: Input tensor of any dimension.
axis: Axis on which to take the finite difference.
Returns:
d: Tensor with size less than x by 1 along the difference dimension.
Raises:
ValueError: Axis out of range for tensor.
"""
shape = x.shape.as_list()
if axis >= len(shape):
raise ValueError('Invalid axis index: %d for tensor with only %d axes.' %
(axis, len(shape)))
begin_back = [0 for _ in range(len(shape))]
begin_front = [0 for _ in range(len(shape))]
begin_front[axis] = 1
shape[axis] -= 1
slice_front = tf.slice(x, begin_front, shape)
slice_back = tf.slice(x, begin_back, shape)
d = slice_front - slice_back
return d
@gin.register
def compute_loudness(audio,
sample_rate=48000,
frame_rate=250,
n_fft=6144,
range_db=LD_RANGE,
ref_db=20.7,
use_tf=False):
"""Perceptual loudness in dB, relative to white noise, amplitude=1.
Function is differentiable if use_tf=True.
Args:
audio: Numpy ndarray or tensor. Shape [batch_size, audio_length] or
[batch_size,].
sample_rate: Audio sample rate in Hz.
frame_rate: Rate of loudness frames in Hz.
n_fft: Fft window size.
range_db: Sets the dynamic range of loudness in decibles. The minimum
loudness (per a frequency bin) corresponds to -range_db.
ref_db: Sets the reference maximum perceptual loudness as given by
(A_weighting + 10 * log10(abs(stft(audio))**2.0). The default value
corresponds to white noise with amplitude=1.0 and n_fft=2048. There is a
slight dependence on fft_size due to different granularity of perceptual
weighting.
use_tf: Make function differentiable by using tensorflow.
Returns:
Loudness in decibels. Shape [batch_size, n_frames] or [n_frames,].
"""
if sample_rate % frame_rate != 0:
raise ValueError(
'frame_rate: {} must evenly divide sample_rate: {}.'
'For default frame_rate: 250Hz, suggested sample_rate: 16kHz or 48kHz'
.format(frame_rate, sample_rate))
# Pick tensorflow or numpy.
lib = tf if use_tf else np
# Make inputs tensors for tensorflow.
audio = tf_float32(audio) if use_tf else audio
# Temporarily a batch dimension for single examples.
is_1d = (len(audio.shape) == 1)
audio = audio[lib.newaxis, :] if is_1d else audio
# Take STFT.
hop_size = sample_rate // frame_rate
overlap = 1 - hop_size / n_fft
stft_fn = stft if use_tf else stft_np
s = stft_fn(audio, frame_size=n_fft, overlap=overlap, pad_end=True)
# Compute power
amplitude = lib.abs(s)
log10 = (lambda x: tf.math.log(x) / tf.math.log(10.0)) if use_tf else np.log10
amin = 1e-20 # Avoid log(0) instabilities.
power_db = log10(lib.maximum(amin, amplitude))
power_db *= 20.0
# Perceptual weighting.
frequencies = librosa.fft_frequencies(sr=sample_rate, n_fft=n_fft)
a_weighting = librosa.A_weighting(frequencies)[lib.newaxis, lib.newaxis, :]
loudness = power_db + a_weighting
# Set dynamic range.
loudness -= ref_db
loudness = lib.maximum(loudness, -range_db)
mean = tf.reduce_mean if use_tf else np.mean
# Average over frequency bins.
loudness = mean(loudness, axis=-1)
# Remove temporary batch dimension.
loudness = loudness[0] if is_1d else loudness
# Compute expected length of loudness vector
n_secs = audio.shape[-1] / float(
sample_rate) # `n_secs` can have milliseconds
expected_len = int(n_secs * frame_rate)
# Pad with `-range_db` noise floor or trim vector
loudness = pad_or_trim_to_expected_length(
loudness, expected_len, -range_db, use_tf=use_tf)
return loudness
@gin.register
def compute_f0(audio, sample_rate, frame_rate, viterbi=True):
"""Fundamental frequency (f0) estimate using CREPE.
This function is non-differentiable and takes input as a numpy array.
Args:
audio: Numpy ndarray of single audio example. Shape [audio_length,].
sample_rate: Sample rate in Hz.
frame_rate: Rate of f0 frames in Hz.
viterbi: Use Viterbi decoding to estimate f0.
Returns:
f0_hz: Fundamental frequency in Hz. Shape [n_frames,].
f0_confidence: Confidence in Hz estimate (scaled [0, 1]). Shape [n_frames,].
"""
n_secs = len(audio) / float(sample_rate) # `n_secs` can have milliseconds
crepe_step_size = 1000 / frame_rate # milliseconds
expected_len = int(n_secs * frame_rate)
audio = np.asarray(audio)
# Compute f0 with crepe.
_, f0_hz, f0_confidence, _ = crepe.predict(
audio,
sr=sample_rate,
viterbi=viterbi,
step_size=crepe_step_size,
center=False,
verbose=0)
# Postprocessing on f0_hz
f0_hz = pad_or_trim_to_expected_length(f0_hz, expected_len, 0) # pad with 0
f0_hz = f0_hz.astype(np.float32)
# Postprocessing on f0_confidence
f0_confidence = pad_or_trim_to_expected_length(f0_confidence, expected_len, 1)
f0_confidence = np.nan_to_num(f0_confidence) # Set nans to 0 in confidence
f0_confidence = f0_confidence.astype(np.float32)
return f0_hz, f0_confidence
def pad_or_trim_to_expected_length(vector,
expected_len,
pad_value=0,
len_tolerance=20,
use_tf=False):
"""Make vector equal to the expected length.
Feature extraction functions like `compute_loudness()` or `compute_f0` produce
feature vectors that vary in length depending on factors such as `sample_rate`
or `hop_size`. This function corrects vectors to the expected length, warning
the user if the difference between the vector and expected length was
unusually high to begin with.
Args:
vector: Numpy 1D ndarray. Shape [vector_length,]
expected_len: Expected length of vector.
pad_value: Value to pad at end of vector.
len_tolerance: Tolerance of difference between original and desired vector
length.
use_tf: Make function differentiable by using tensorflow.
Returns:
vector: Vector with corrected length.
Raises:
ValueError: if `len(vector)` is different from `expected_len` beyond
`len_tolerance` to begin with.
"""
expected_len = int(expected_len)
vector_len = int(vector.shape[-1])
if abs(vector_len - expected_len) > len_tolerance:
# Ensure vector was close to expected length to begin with
raise ValueError('Vector length: {} differs from expected length: {} '
'beyond tolerance of : {}'.format(vector_len,
expected_len,
len_tolerance))
# Pick tensorflow or numpy.
lib = tf if use_tf else np
is_1d = (len(vector.shape) == 1)
vector = vector[lib.newaxis, :] if is_1d else vector
# Pad missing samples
if vector_len < expected_len:
n_padding = expected_len - vector_len
vector = lib.pad(
vector, ((0, 0), (0, n_padding)),
mode='constant',
constant_values=pad_value)
# Trim samples
elif vector_len > expected_len:
vector = vector[..., :expected_len]
# Remove temporary batch dimension.
vector = vector[0] if is_1d else vector
return vector
def reset_crepe():
"""Reset the global state of CREPE to force model re-building."""
for k in crepe.core.models:
crepe.core.models[k] = None
def tf_float32(x):
"""Ensure array/tensor is a float32 tf.Tensor."""
if isinstance(x, tf.Tensor):
return tf.cast(x, dtype=tf.float32) # This is a no-op if x is float32.
else:
return tf.convert_to_tensor(x, tf.float32)