/
test_time_frequency.py
589 lines (421 loc) · 18.8 KB
/
test_time_frequency.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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
# CREATED:2015-02-14 19:13:49 by Brian McFee <brian.mcfee@nyu.edu>
"""Unit tests for time and frequency conversion"""
import os
try:
os.environ.pop("LIBROSA_CACHE_DIR")
except KeyError:
pass
import warnings
import librosa
import numpy as np
import pytest
@pytest.mark.parametrize("frames", [100, np.arange(10.0), np.ones((3, 3))], ids=["0d", "1d", "2d"])
@pytest.mark.parametrize("hop_length", [512, 1024])
@pytest.mark.parametrize("n_fft", [None, 1024])
def test_frames_to_samples(frames, hop_length, n_fft):
samples = librosa.frames_to_samples(frames, hop_length=hop_length, n_fft=n_fft)
frames = np.asanyarray(frames)
assert frames.shape == samples.shape
assert frames.ndim == samples.ndim
if n_fft is None:
assert np.allclose(samples, frames * hop_length)
else:
assert np.allclose((samples - n_fft // 2) // hop_length, frames)
@pytest.mark.parametrize(
"samples", [1024 * 100, 1024 * np.arange(10.0), 1024 * np.ones((3, 3))], ids=["0d", "1d", "2d"]
)
@pytest.mark.parametrize("hop_length", [512, 1024])
@pytest.mark.parametrize("n_fft", [None, 1024])
def test_samples_to_frames(samples, hop_length, n_fft):
frames = librosa.samples_to_frames(samples, hop_length=hop_length, n_fft=n_fft)
samples = np.asanyarray(samples)
assert frames.shape == samples.shape
assert frames.ndim == samples.ndim
if n_fft is None:
assert np.allclose(samples, frames * hop_length)
else:
assert np.allclose((samples - n_fft // 2) // hop_length, frames)
@pytest.mark.parametrize("sr", [22050, 44100])
@pytest.mark.parametrize("hop_length", [256, 512])
@pytest.mark.parametrize("n_fft", [None, 2048])
def test_frames_to_time(sr, hop_length, n_fft):
# Generate frames at times 0s, 1s, 2s
frames = np.arange(3) * sr // hop_length
if n_fft:
frames -= n_fft // (2 * hop_length)
times = librosa.frames_to_time(frames, sr=sr, hop_length=hop_length, n_fft=n_fft)
# we need to be within one frame
assert np.all(np.abs(times - np.asarray([0, 1, 2])) * sr < hop_length)
@pytest.mark.parametrize("sr", [22050, 44100])
def test_time_to_samples(sr):
assert np.allclose(librosa.time_to_samples([0, 1, 2], sr=sr), [0, sr, 2 * sr])
@pytest.mark.parametrize("sr", [22050, 44100])
def test_samples_to_time(sr):
assert np.allclose(librosa.samples_to_time([0, sr, 2 * sr], sr=sr), [0, 1, 2])
@pytest.mark.parametrize("sr", [22050, 44100])
@pytest.mark.parametrize("hop_length", [256, 512])
@pytest.mark.parametrize("n_fft", [None, 2048])
def test_time_to_frames(sr, hop_length, n_fft):
# Generate frames at times 0s, 1s, 2s
times = np.arange(3)
frames = librosa.time_to_frames(times, sr=sr, hop_length=hop_length, n_fft=n_fft)
if n_fft:
frames -= n_fft // (2 * hop_length)
# we need to be within one frame
assert np.all(np.abs(times - np.asarray([0, 1, 2])) * sr < hop_length)
@pytest.mark.parametrize("tuning", [0.0, -0.2, 0.1])
@pytest.mark.parametrize("bins_per_octave", [12, 24, 36])
def test_octs_to_hz(tuning, bins_per_octave):
freq = np.asarray([55, 110, 220, 440]) * (2.0 ** (tuning / bins_per_octave))
freq_out = librosa.octs_to_hz([1, 2, 3, 4], tuning=tuning, bins_per_octave=bins_per_octave)
assert np.allclose(freq, freq_out)
@pytest.mark.parametrize("tuning", [0.0, -0.2, 0.1])
@pytest.mark.parametrize("bins_per_octave", [12, 24, 36])
def test_hz_to_octs(tuning, bins_per_octave):
freq = np.asarray([55, 110, 220, 440]) * (2.0 ** (tuning / bins_per_octave))
octs = [1, 2, 3, 4]
oct_out = librosa.hz_to_octs(freq, tuning=tuning, bins_per_octave=bins_per_octave)
assert np.allclose(octs, oct_out)
@pytest.mark.parametrize(
"A4,bins_per_octave,tuning",
[
(440.0, 12, 0.0),
([440.0, 444.0], 24, [0.0, 0.31335]),
([432.0], 12, [-0.317667]),
(432.0, 36, -0.953)
],
)
def test_A4_to_tuning(A4, bins_per_octave, tuning):
tuning_out = librosa.A4_to_tuning(A4=A4, bins_per_octave=bins_per_octave)
assert np.allclose(np.asarray(tuning), tuning_out)
@pytest.mark.parametrize(
"tuning,bins_per_octave,A4",
[
(0.0, 12, 440.0),
([-0.2], 24, [437.466]),
([0.1, 0.9], 36, [440.848, 447.691]),
(0.0, 24, 440.0)
],
)
def test_tuning_to_A4(tuning, bins_per_octave, A4):
A4_out = librosa.tuning_to_A4(tuning=tuning, bins_per_octave=bins_per_octave)
assert np.allclose(np.asarray(A4), A4_out)
@pytest.mark.parametrize(
"tuning,octave",
[(None, None), (None, 1), (None, 2), (None, 3), (-25, 1), (-25, 2), (-25, 3), (0, 1), (0, 2), (0, 3)],
)
@pytest.mark.parametrize("accidental", ["", "#", "b", "!"])
@pytest.mark.parametrize("round_midi", [False, True])
def test_note_to_midi(tuning, accidental, octave, round_midi):
note = "C{:s}".format(accidental)
if octave is not None:
note = "{:s}{:d}".format(note, octave)
else:
octave = 0
if tuning is not None:
note = "{:s}{:+d}".format(note, tuning)
else:
tuning = 0
midi_true = 12 * (octave + 1) + tuning * 0.01
if accidental == "#":
midi_true += 1
elif accidental in list("b!"):
midi_true -= 1
midi = librosa.note_to_midi(note, round_midi=round_midi)
if round_midi:
midi_true = np.round(midi_true)
assert midi == midi_true
midi = librosa.note_to_midi([note], round_midi=round_midi)
assert midi[0] == midi_true
@pytest.mark.xfail(raises=librosa.ParameterError)
def test_note_to_midi_badnote():
librosa.note_to_midi("does not pass")
@pytest.mark.parametrize(
"tuning,octave",
[(None, None), (None, 1), (None, 2), (None, 3), (-25, 1), (-25, 2), (-25, 3), (0, 1), (0, 2), (0, 3)],
)
@pytest.mark.parametrize("accidental", ["", "#", "b", "!"])
@pytest.mark.parametrize("round_midi", [False, True])
def test_note_to_hz(tuning, octave, accidental, round_midi):
note = "A{:s}".format(accidental)
if octave is not None:
note = "{:s}{:d}".format(note, octave)
else:
octave = 0
if tuning is not None:
note = "{:s}{:+d}".format(note, tuning)
else:
tuning = 0
if round_midi:
tuning = np.around(tuning, -2)
hz_true = 440.0 * (2.0 ** (tuning * 0.01 / 12)) * (2.0 ** (octave - 4))
if accidental == "#":
hz_true *= 2.0 ** (1.0 / 12)
elif accidental in list("b!"):
hz_true /= 2.0 ** (1.0 / 12)
hz = librosa.note_to_hz(note, round_midi=round_midi)
assert np.allclose(hz, hz_true)
hz = librosa.note_to_hz([note], round_midi=round_midi)
assert np.allclose(hz[0], hz_true)
@pytest.mark.xfail(raises=librosa.ParameterError)
def test_note_to_hz_badnote():
librosa.note_to_hz("does not pass")
@pytest.mark.parametrize(
"midi_num,note,octave,cents",
[
(24.25, "C", False, False),
(24.25, "C1", True, False),
(24.25, "C1+25", True, True),
([24.25], ["C"], False, False),
],
)
def test_midi_to_note(midi_num, note, octave, cents):
note_out = librosa.midi_to_note(midi_num, octave=octave, cents=cents)
assert note_out == note
@pytest.mark.xfail(raises=librosa.ParameterError)
def test_midi_to_note_cents_nooctave():
librosa.midi_to_note(24.25, octave=False, cents=True)
def test_midi_to_hz():
assert np.allclose(librosa.midi_to_hz([33, 45, 57, 69]), [55, 110, 220, 440])
def test_hz_to_midi():
assert np.allclose(librosa.hz_to_midi(55), 33)
assert np.allclose(librosa.hz_to_midi([55, 110, 220, 440]), [33, 45, 57, 69])
@pytest.mark.parametrize(
"hz,note,octave,cents",
[
(440, "A", False, False),
(440, "A4", True, False),
(440, "A4+0", True, True),
([440, 880], ["A4+0", "A5+0"], True, True),
],
)
def test_hz_to_note(hz, note, octave, cents):
note_out = librosa.hz_to_note(hz, octave=octave, cents=cents)
assert note_out == note
@pytest.mark.xfail(raises=librosa.ParameterError)
def test_hz_to_note_cents_nooctave():
librosa.hz_to_note(440, octave=False, cents=True)
@pytest.mark.parametrize("sr", [8000, 22050])
@pytest.mark.parametrize("n_fft", [1024, 2048])
def test_fft_frequencies(sr, n_fft):
freqs = librosa.fft_frequencies(sr=sr, n_fft=n_fft)
# DC
assert freqs[0] == 0
# Nyquist, positive here for more convenient display purposes
assert freqs[-1] == sr / 2.0
# Ensure that the frequencies increase linearly
dels = np.diff(freqs)
assert np.allclose(dels, dels[0])
@pytest.mark.parametrize("n_bins", [12, 24, 36])
@pytest.mark.parametrize("fmin", [440.0])
@pytest.mark.parametrize("bins_per_octave", [12, 24, 36])
@pytest.mark.parametrize("tuning", [-0.25, 0.0, 0.25])
def test_cqt_frequencies(n_bins, fmin, bins_per_octave, tuning):
freqs = librosa.cqt_frequencies(n_bins, fmin, bins_per_octave=bins_per_octave, tuning=tuning)
# Make sure we get the right number of bins
assert len(freqs) == n_bins
# And that the first bin matches fmin by tuning
assert np.allclose(freqs[0], fmin * 2.0 ** (float(tuning) / bins_per_octave))
# And that we have constant Q
Q = np.diff(np.log2(freqs))
assert np.allclose(Q, 1.0 / bins_per_octave)
@pytest.mark.parametrize("n_bins", [1, 16, 128])
@pytest.mark.parametrize("hop_length", [256, 512])
@pytest.mark.parametrize("sr", [11025, 22050])
def test_tempo_frequencies(n_bins, hop_length, sr):
freqs = librosa.tempo_frequencies(n_bins, hop_length=hop_length, sr=sr)
# Verify the length
assert len(freqs) == n_bins
# 0-bin should be infinite
assert not np.isfinite(freqs[0])
# remaining bins should be spaced by 1/hop_length
if n_bins > 1:
invdiff = (freqs[1:] ** -1) * (60.0 * sr)
assert np.allclose(invdiff[0], hop_length)
assert np.allclose(np.diff(invdiff), np.asarray(hop_length)), np.diff(invdiff)
@pytest.mark.parametrize("sr", [8000, 22050])
@pytest.mark.parametrize("hop_length", [256, 512])
@pytest.mark.parametrize("win_length", [192, 384])
def test_fourier_tempo_frequencies(sr, hop_length, win_length):
freqs = librosa.fourier_tempo_frequencies(sr=sr, hop_length=hop_length, win_length=win_length)
# DC
assert freqs[0] == 0
# Nyquist, positive here for more convenient display purposes
assert freqs[-1] == sr * 60 / 2.0 / hop_length
# Ensure that the frequencies increase linearly
dels = np.diff(freqs)
assert np.allclose(dels, dels[0])
@pytest.mark.parametrize("min_db", [None, -40, -80])
def test_A_weighting(min_db):
# Check that 1KHz is around 0dB
a_khz = librosa.A_weighting(1000.0, min_db=min_db)
assert np.allclose(a_khz, 0, atol=1e-3)
a_range = librosa.A_weighting(np.linspace(2e1, 2e4), min_db=min_db)
# Check that the db cap works
if min_db is not None:
assert not np.any(a_range < min_db)
@pytest.mark.parametrize("min_db", [None, -40, -80])
def test_B_weighting(min_db):
# Check that 1KHz is around 0dB
b_khz = librosa.B_weighting(1000.0, min_db=min_db)
assert np.allclose(b_khz, 0, atol=1e-3)
b_range = librosa.B_weighting(np.linspace(2e1, 2e4), min_db=min_db)
# Check that the db cap works
if min_db is not None:
assert not np.any(b_range < min_db)
@pytest.mark.parametrize("min_db", [None, -40, -80])
def test_C_weighting(min_db):
# Check that 1KHz is around 0dB
c_khz = librosa.C_weighting(1000.0, min_db=min_db)
assert np.allclose(c_khz, 0, atol=1e-3)
c_range = librosa.B_weighting(np.linspace(2e1, 2e4), min_db=min_db)
# Check that the db cap works
if min_db is not None:
assert not np.any(c_range < min_db)
@pytest.mark.parametrize("min_db", [None, -40, -80])
def test_D_weighting(min_db):
# Check that 1KHz is around 0dB
d_khz = librosa.D_weighting(1000.0, min_db=min_db)
assert np.allclose(d_khz, 0, atol=1e-3)
d_range = librosa.D_weighting(np.linspace(2e1, 2e4), min_db=min_db)
# Check that the db cap works
if min_db is not None:
assert not np.any(d_range < min_db)
@pytest.mark.parametrize("min_db", [None, -40, -80])
def test_Z_weighting(min_db):
# Check that 1KHz is around 0dB
d_khz = librosa.Z_weighting(np.linspace(2e1, 2e4), min_db=min_db)
assert np.allclose(d_khz, 0, atol=1e-3)
@pytest.mark.parametrize(
"kind", list(librosa.core.time_frequency.WEIGHTING_FUNCTIONS))
def test_frequency_weighting(kind):
freq = np.linspace(2e1, 2e4)
assert np.allclose(
librosa.frequency_weighting(freq, kind),
librosa.core.time_frequency.WEIGHTING_FUNCTIONS[kind](freq),
0, atol=1e-3)
@pytest.mark.parametrize(
"kinds", ['AZC', ['A', 'Z', 'C']])
def test_multi_frequency_weighting(kinds):
freq = np.linspace(2e1, 2e4)
assert np.allclose(
librosa.multi_frequency_weighting(freq, kinds),
np.stack([
librosa.A_weighting(freq),
librosa.Z_weighting(freq),
librosa.C_weighting(freq),
]),
0, atol=1e-3)
def test_samples_like():
X = np.ones((3, 4, 5))
hop_length = 512
for axis in (0, 1, 2, -1):
samples = librosa.samples_like(X, hop_length=hop_length, axis=axis)
expected_samples = np.arange(X.shape[axis]) * hop_length
assert np.allclose(samples, expected_samples)
def test_samples_like_scalar():
X = 7
hop_length = 512
samples = librosa.samples_like(X, hop_length=hop_length)
expected_samples = np.arange(7) * hop_length
assert np.allclose(samples, expected_samples)
def test_times_like():
X = np.ones((3, 4, 5))
sr = 22050
hop_length = 512
for axis in (0, 1, 2, -1):
times = librosa.times_like(X, sr=sr, hop_length=hop_length, axis=axis)
expected_times = np.arange(X.shape[axis]) * hop_length / float(sr)
assert np.allclose(times, expected_times)
def test_times_like_scalar():
X = 7
sr = 22050
hop_length = 512
times = librosa.times_like(X, sr=sr, hop_length=hop_length)
expected_times = np.arange(7) * hop_length / float(sr)
assert np.allclose(times, expected_times)
@pytest.mark.parametrize("blocks", [0, 1, [10, 20]])
@pytest.mark.parametrize("block_length", [1, 4, 8])
def test_blocks_to_frames(blocks, block_length):
frames = librosa.blocks_to_frames(blocks, block_length)
# Check shape
assert frames.ndim == np.asarray(blocks).ndim
assert frames.size == np.asarray(blocks).size
# Check values
assert np.allclose(frames, block_length * np.asanyarray(blocks))
# Check dtype
assert np.issubdtype(frames.dtype, np.int)
@pytest.mark.parametrize("blocks", [0, 1, [10, 20]])
@pytest.mark.parametrize("block_length", [1, 4, 8])
@pytest.mark.parametrize("hop_length", [1, 512])
def test_blocks_to_samples(blocks, block_length, hop_length):
samples = librosa.blocks_to_samples(blocks, block_length, hop_length)
# Check shape
assert samples.ndim == np.asarray(blocks).ndim
assert samples.size == np.asarray(blocks).size
# Check values
assert np.allclose(samples, np.asanyarray(blocks) * hop_length * block_length)
# Check dtype
assert np.issubdtype(samples.dtype, np.int)
@pytest.mark.parametrize("blocks", [0, 1, [10, 20]])
@pytest.mark.parametrize("block_length", [1, 4, 8])
@pytest.mark.parametrize("hop_length", [1, 512])
@pytest.mark.parametrize("sr", [22050, 44100])
def test_blocks_to_time(blocks, block_length, hop_length, sr):
times = librosa.blocks_to_time(blocks, block_length, hop_length, sr)
# Check shape
assert times.ndim == np.asarray(blocks).ndim
assert times.size == np.asarray(blocks).size
# Check values
assert np.allclose(times, np.asanyarray(blocks) * hop_length * block_length / float(sr))
# Check dtype
assert np.issubdtype(times.dtype, np.float)
@pytest.mark.xfail(raises=librosa.ParameterError)
def test_key_to_notes_badkey():
librosa.key_to_notes('not a key')
@pytest.mark.parametrize('key,ref_notes', [
# Test for implicit accidentals, ties
('C:maj', ['C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#', 'A', 'A#', 'B']),
('A:min', ['C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#', 'A', 'A#', 'B']),
# Test for implicit accidentals, unambiguous
('D:maj', ['C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#', 'A', 'A#', 'B']),
('F:min', ['C', 'Db', 'D', 'Eb', 'E', 'F', 'Gb', 'G', 'Ab', 'A', 'Bb', 'B']),
# Test for proper enharmonics with ties
('Eb:min', ['C', 'Db', 'D', 'Eb', 'E', 'F', 'Gb', 'G', 'Ab', 'A', 'Bb', 'Cb']),
('D#:min', ['C', 'C#', 'D', 'D#', 'E', 'E#', 'F#', 'G', 'G#', 'A', 'A#', 'B']),
('Gb:maj', ['C', 'Db', 'D', 'Eb', 'E', 'F', 'Gb', 'G', 'Ab', 'A', 'Bb', 'Cb']),
('F#:maj', ['C', 'C#', 'D', 'D#', 'E', 'E#', 'F#', 'G', 'G#', 'A', 'A#', 'B']),
# Test for theoretical keys
('G#:maj', ['B#', 'C#', 'D', 'D#', 'E', 'E#', 'F#', 'F##', 'G#', 'A', 'A#', 'B']),
('Cb:min', ['C', 'Db', 'Ebb', 'Eb', 'Fb', 'F', 'Gb', 'Abb', 'Ab', 'Bbb', 'Bb', 'Cb']),
# Test the edge case of theoretical sharps
('B#:maj', ['B#', 'C#', 'C##', 'D#', 'D##', 'E#', 'F#', 'F##', 'G#', 'G##', 'A#', 'A##']),
])
def test_key_to_notes(key, ref_notes):
notes = librosa.key_to_notes(key, unicode=False)
assert len(notes) == len(ref_notes)
for (n, rn) in zip(notes, ref_notes):
assert n == rn
@pytest.mark.parametrize('key,ref_notes', [
('G#:maj', ['B♯', 'C♯', 'D', 'D♯', 'E', 'E♯', 'F♯', 'F𝄪', 'G♯', 'A', 'A♯', 'B']),
('Cb:min', ['C', 'D♭', 'E𝄫', 'E♭', 'F♭', 'F', 'G♭', 'A𝄫', 'A♭', 'B𝄫', 'B♭', 'C♭'])
])
def test_key_to_notes_unicode(key, ref_notes):
notes = librosa.key_to_notes(key, unicode=True)
assert len(notes) == len(ref_notes)
for (n, rn) in zip(notes, ref_notes):
assert n == rn
@pytest.mark.xfail(raises=librosa.ParameterError)
def test_key_to_degrees_badkey():
librosa.key_to_degrees('not a key')
@pytest.mark.parametrize('key,ref_degrees', [('C:maj', [0, 2, 4, 5, 7, 9, 11]),
('C:min', [0, 2, 3, 5, 7, 8, 10]),
('A:min', [ 9, 11, 0, 2, 4, 5, 7]),
('Gb:maj', [ 6, 8, 10, 11, 1, 3, 5])])
def test_key_to_degrees(key, ref_degrees):
degrees = librosa.key_to_degrees(key)
assert len(degrees) == len(ref_degrees)
for (d, rd) in zip(degrees, ref_degrees):
assert d == rd