def test_cq_to_chroma(n_octaves, semitones, n_chroma, fmin, base_c, window): bins_per_octave = 12 * semitones n_bins = n_octaves * bins_per_octave if np.mod(bins_per_octave, n_chroma) != 0: ctx = pytest.raises(librosa.ParameterError) else: ctx = dnr() with ctx: # Fake up a cqt matrix with the corresponding midi notes if fmin is None: midi_base = 24 # C2 else: midi_base = librosa.hz_to_midi(fmin) midi_notes = np.linspace( midi_base, midi_base + n_bins * 12.0 / bins_per_octave, endpoint=False, num=n_bins, ) # We don't care past 2 decimals here. # the log2 inside hz_to_midi can cause problems though. midi_notes = np.around(midi_notes, decimals=2) C = np.diag(midi_notes) cq2chr = librosa.filters.cq_to_chroma( n_input=C.shape[0], bins_per_octave=bins_per_octave, n_chroma=n_chroma, fmin=fmin, base_c=base_c, window=window, ) chroma = cq2chr.dot(C) for i in range(n_chroma): v = chroma[i][chroma[i] != 0] v = np.around(v, decimals=2) if base_c: resid = np.mod(v, 12) else: resid = np.mod(v - 9, 12) resid = np.round(resid * n_chroma / 12.0) assert np.allclose(np.mod(i - resid, 12), 0.0), i - resid
import pytest import librosa __EXAMPLE_FILE = os.path.join("tests", "data", "test1_22050.wav") @pytest.fixture(scope="module", params=[22050, 44100]) def ysr(request): return librosa.load(__EXAMPLE_FILE, sr=request.param) @pytest.mark.parametrize( "rate,ctx", [ (0.25, dnr()), (0.25, dnr()), (1.0, dnr()), (2.0, dnr()), (4.0, dnr()), (-1, pytest.raises(librosa.ParameterError)), (0, pytest.raises(librosa.ParameterError)), ], ) def test_time_stretch(ysr, rate, ctx): with ctx: y, sr = ysr ys = librosa.effects.time_stretch(y, rate) orig_duration = librosa.get_duration(y, sr=sr)
@pytest.mark.parametrize("sr", [4000]) @pytest.mark.parametrize("y", [np.zeros(4000), np.ones(4000), -np.ones(4000)]) @pytest.mark.parametrize("hop_length", [64, 512, 2048]) def test_onset_detect_const(y, sr, hop_length): onsets = librosa.onset.onset_detect(y=y, sr=sr, onset_envelope=None, hop_length=hop_length) assert len(onsets) == 0 @pytest.mark.parametrize( "units, ctx", [("frames", dnr()), ("time", dnr()), ("samples", dnr()), ("bad units", pytest.raises(librosa.ParameterError))], ) @pytest.mark.parametrize("hop_length", [512, 1024]) def test_onset_units(ysr, hop_length, units, ctx): y, sr = ysr with ctx: b1 = librosa.onset.onset_detect(y=y, sr=sr, hop_length=hop_length) b2 = librosa.onset.onset_detect(y=y, sr=sr, hop_length=hop_length, units=units) t1 = librosa.frames_to_time(b1, sr=sr, hop_length=hop_length)
import pytest import librosa __EXAMPLE_FILE = os.path.join("tests", "data", "test1_22050.wav") @pytest.fixture(scope="module", params=[22050, 44100]) def ysr(request): return librosa.load(__EXAMPLE_FILE, sr=request.param) @pytest.mark.parametrize( "rate,ctx", [ (0.25, dnr()), (0.25, dnr()), (1.0, dnr()), (2.0, dnr()), (4.0, dnr()), (-1, pytest.raises(librosa.ParameterError)), (0, pytest.raises(librosa.ParameterError)), ], ) def test_time_stretch(ysr, rate, ctx): with ctx: y, sr = ysr ys = librosa.effects.time_stretch(y, rate) orig_duration = librosa.get_duration(y, sr=sr)
return [(0, 0), (num_samples // 4, num_samples // 4), ((3 * num_samples) // 4, num_samples // 2), (num_samples, (3 * num_samples) // 4)] @pytest.fixture def random_signal(channels, num_samples): if channels is not None: shape = (num_samples, channels) else: shape = (num_samples, ) return np.random.random(shape) @pytest.mark.parametrize("rate,ctx", [ (0.5, dnr()), (1.0, dnr()), (2.0, dnr()), (-1, pytest.raises(ValueError)), (-0.5, pytest.raises(ValueError)), (0, pytest.raises(ValueError)), ]) def test_stretch(sr, random_signal, num_samples, rate, ctx): '''Test shape of random signals with stretching factor of various rate. ''' # input signal of shape (channels, sr * duration) y = random_signal with ctx:
y, sr = ysr librosa.beat.beat_track(y=y, sr=sr, bpm=bpm) @pytest.mark.xfail(raises=librosa.ParameterError) @pytest.mark.parametrize("start_bpm", [-1, -0.5, 0]) def test_beat_bad_start_bpm(ysr, start_bpm): y, sr = ysr librosa.beat.beat_track(y=y, sr=sr, start_bpm=start_bpm) @pytest.mark.parametrize("hop_length", [512, 1024]) @pytest.mark.parametrize( "units,ctx", [ ("frames", dnr()), ("time", dnr()), ("samples", dnr()), ("bad units", pytest.raises(librosa.ParameterError)), ], ) def test_beat_units(ysr, hop_length, units, ctx): y, sr = ysr with ctx: _, b2 = librosa.beat.beat_track(y=y, sr=sr, hop_length=hop_length, units=units) tempo, b1 = librosa.beat.beat_track(y=y, sr=sr, hop_length=hop_length) t1 = librosa.frames_to_time(b1, sr=sr, hop_length=hop_length)
def test_beat_bad_bpm(ysr, bpm): y, sr = ysr librosa.beat.beat_track(y=y, sr=sr, bpm=bpm) @pytest.mark.xfail(raises=librosa.ParameterError) @pytest.mark.parametrize("start_bpm", [-1, -0.5, 0]) def test_beat_bad_start_bpm(ysr, start_bpm): y, sr = ysr librosa.beat.beat_track(y=y, sr=sr, start_bpm=start_bpm) @pytest.mark.parametrize("hop_length", [512, 1024]) @pytest.mark.parametrize( "units,ctx", [("frames", dnr()), ("time", dnr()), ("samples", dnr()), ("bad units", pytest.raises(librosa.ParameterError))], ) def test_beat_units(ysr, hop_length, units, ctx): y, sr = ysr with ctx: _, b2 = librosa.beat.beat_track(y=y, sr=sr, hop_length=hop_length, units=units) tempo, b1 = librosa.beat.beat_track(y=y, sr=sr, hop_length=hop_length) t1 = librosa.frames_to_time(b1, sr=sr, hop_length=hop_length) if units == "time": t2 = b2 elif units == "samples": t2 = librosa.samples_to_time(b2, sr=sr)