Esempio n. 1
0
def test_spectral_bandwidth():
    def __test(infile):
        DATA = load(infile)

        y, sr = librosa.load(DATA['wavfile'][0], sr=None, mono=True)

        n_fft = DATA['nfft'][0, 0].astype(int)
        hop_length = DATA['hop_length'][0, 0].astype(int)

        S = DATA['S']

        # normalization is disabled here, since the precomputed S is already
        # normalized
        # metlab uses p=1, other folks use p=2
        bw = librosa.feature.spectral_bandwidth(S=S,
                                                sr=sr,
                                                n_fft=n_fft,
                                                hop_length=hop_length,
                                                centroid=DATA['centroid'],
                                                norm=False,
                                                p=1)

        # METlab implementation takes the mean, not the sum
        assert np.allclose(bw, S.shape[0] * DATA['bw'])

    for infile in files(os.path.join('data', 'met-bandwidth-*.mat')):
        yield __test, infile
Esempio n. 2
0
def test_spectral_bandwidth():
    def __test(infile):
        DATA = load(infile)

        y, sr = librosa.load(DATA['wavfile'][0], sr=None, mono=True)

        n_fft = DATA['nfft'][0, 0].astype(int)
        hop_length = DATA['hop_length'][0, 0].astype(int)

        S = DATA['S']

        # normalization is disabled here, since the precomputed S is already
        # normalized
        # metlab uses p=1, other folks use p=2
        bw = librosa.feature.spectral_bandwidth(S=S, sr=sr,
                                                n_fft=n_fft,
                                                hop_length=hop_length,
                                                centroid=DATA['centroid'],
                                                norm=False,
                                                p=1)

        # METlab implementation takes the mean, not the sum
        assert np.allclose(bw, S.shape[0] * DATA['bw'])

    for infile in files('data/met-bandwidth-*.mat'):
        yield __test, infile
def test_spectral_rolloff():
    def __test(infile):
        DATA = load(infile)

        y, sr = librosa.load(os.path.join('tests', DATA['wavfile'][0]),
                             sr=None,
                             mono=True)

        n_fft = DATA['nfft'][0, 0].astype(int)
        hop_length = DATA['hop_length'][0, 0].astype(int)
        pct = DATA['pct'][0, 0]

        # spectralRolloff uses normalized spectra
        S = met_stft(y, n_fft, hop_length, n_fft, True)

        rolloff = librosa.feature.spectral_rolloff(S=S,
                                                   sr=sr,
                                                   n_fft=n_fft,
                                                   hop_length=hop_length,
                                                   roll_percent=pct)

        assert np.allclose(rolloff, DATA['rolloff'])

    for infile in files(os.path.join('tests', 'data', 'met-rolloff-*.mat')):
        yield __test, infile
Esempio n. 4
0
def test_tempo():
    def __test(infile):
        DATA = load(infile)

        # Estimate tempo from the given onset envelope
        tempo = librosa.beat.estimate_tempo(DATA['onsetenv'][0],
                                            sr=8000,
                                            hop_length=32,
                                            start_bpm=120.0)

        assert (np.allclose(tempo, DATA['t'][0, 0])
                or np.allclose(tempo, DATA['t'][0, 1]))

    for infile in files('data/beat-tempo-*.mat'):
        yield (__test, infile)
Esempio n. 5
0
def test_tempo():

    def __test(infile):
        DATA = load(infile)

        # Estimate tempo from the given onset envelope
        tempo = librosa.beat.estimate_tempo(DATA['onsetenv'][0],
                                            sr=8000,
                                            hop_length=32,
                                            start_bpm=120.0)

        assert (np.allclose(tempo, DATA['t'][0, 0]) or
                np.allclose(tempo, DATA['t'][0, 1]))

    for infile in files('data/beat-tempo-*.mat'):
        yield (__test, infile)
Esempio n. 6
0
def deprecated_test_beat():

    def __test(infile):

        DATA = load(infile)

        (bpm, beats) = librosa.beat.beat_track(y=None,
                                               sr=8000,
                                               hop_length=32,
                                               onset_envelope=DATA['onsetenv'][0])

        beat_times = librosa.frames_to_time(beats, sr=8000, hop_length=32)
        assert np.allclose(beat_times, DATA['beats'])

    for infile in files('data/beat-beat-*.mat'):
        yield (__test, infile)
Esempio n. 7
0
def deprecated_test_beat():
    def __test(infile):

        DATA = load(infile)

        (bpm,
         beats) = librosa.beat.beat_track(y=None,
                                          sr=8000,
                                          hop_length=32,
                                          onset_envelope=DATA['onsetenv'][0])

        beat_times = librosa.frames_to_time(beats, sr=8000, hop_length=32)
        assert np.allclose(beat_times, DATA['beats'])

    for infile in files(os.path.join('data', 'beat-beat-*.mat')):
        yield (__test, infile)
Esempio n. 8
0
def test_onset_strength():
    def __test(infile):
        DATA = load(infile)

        # Compute onset envelope using the same spectrogram
        onsets = librosa.onset.onset_strength(y=None,
                                              sr=8000,
                                              S=DATA['D'],
                                              centering=False,
                                              detrend=True)

        assert np.allclose(onsets[1:], DATA['onsetenv'][0])

        pass

    for infile in files('data/beat-onset-*.mat'):
        yield (__test, infile)
Esempio n. 9
0
def test_onset_strength():

    def __test(infile):
        DATA = load(infile)

        # Compute onset envelope using the same spectrogram
        onsets = librosa.onset.onset_strength(y=None,
                                              sr=8000,
                                              S=DATA['D'],
                                              centering=False,
                                              detrend=True)

        assert np.allclose(onsets[1:], DATA['onsetenv'][0])

        pass

    for infile in files('data/beat-onset-*.mat'):
        yield (__test, infile)
Esempio n. 10
0
def test_onset_strength():
    def __test(infile):
        DATA = load(infile)

        # Compute onset envelope using the same spectrogram
        onsets = librosa.onset.onset_strength(y=None,
                                              sr=8000,
                                              S=DATA['D'],
                                              lag=1,
                                              max_size=1,
                                              center=False,
                                              detrend=True,
                                              aggregate=np.mean)

        assert np.allclose(onsets[1:], DATA['onsetenv'][0])

        pass

    for infile in files(os.path.join('data', 'beat-onset-*.mat')):
        yield (__test, infile)
Esempio n. 11
0
def test_spectral_centroid():
    def __test(infile):
        DATA = load(infile)

        y, sr = librosa.load(DATA['wavfile'][0], sr=None, mono=True)

        n_fft = DATA['nfft'][0, 0].astype(int)
        hop_length = DATA['hop_length'][0, 0].astype(int)

        # spectralCentroid uses normalized spectra
        S = met_stft(y, n_fft, hop_length, n_fft, True)

        centroid = librosa.feature.spectral_centroid(S=S,
                                                     sr=sr,
                                                     n_fft=n_fft,
                                                     hop_length=hop_length)

        assert np.allclose(centroid, DATA['centroid'])

    for infile in files(os.path.join('data', 'met-centroid-*.mat')):
        yield __test, infile
Esempio n. 12
0
def test_spectral_centroid():
    def __test(infile):
        DATA = load(infile)

        y, sr = librosa.load(DATA['wavfile'][0], sr=None, mono=True)

        n_fft = DATA['nfft'][0, 0].astype(int)
        hop_length = DATA['hop_length'][0, 0].astype(int)

        # spectralCentroid uses normalized spectra
        S = met_stft(y, n_fft, hop_length, n_fft, True)

        centroid = librosa.feature.spectral_centroid(S=S,
                                                     sr=sr,
                                                     n_fft=n_fft,
                                                     hop_length=hop_length)

        assert np.allclose(centroid, DATA['centroid'])

    for infile in files('data/met-centroid-*.mat'):
        yield __test, infile
Esempio n. 13
0
def test_onset_strength():

    def __test(infile):
        DATA = load(infile)

        # Compute onset envelope using the same spectrogram
        onsets = librosa.onset.onset_strength(y=None,
                                              sr=8000,
                                              S=DATA['D'],
                                              lag=1,
                                              max_size=1,
                                              center=False,
                                              detrend=True,
                                              aggregate=np.mean)

        assert np.allclose(onsets[1:], DATA['onsetenv'][0])

        pass

    for infile in files(os.path.join('data','beat-onset-*.mat')):
        yield (__test, infile)
Esempio n. 14
0
def test_spectral_contrast():
    def __test(infile):
        DATA = load(infile)

        y, sr = librosa.load(DATA['wavfile'][0], sr=None, mono=True)

        n_fft = DATA['nfft'][0, 0].astype(int)
        hop_length = DATA['hop_length'][0, 0].astype(int)

        # spectralContrast uses normalized spectra
        S = met_stft(y, n_fft, hop_length, n_fft, True)

        contrast = librosa.feature.spectral_contrast(S=S, sr=sr,
                                                     n_fft=n_fft,
                                                     hop_length=hop_length,
                                                     linear=True)

        assert np.allclose(contrast, DATA['contrast'], rtol=1e-3, atol=1e-2)

    for infile in files('data/met-contrast-*.mat'):
        yield __test, infile
Esempio n. 15
0
def test_spectral_rolloff():
    def __test(infile):
        DATA = load(infile)

        y, sr = librosa.load(DATA['wavfile'][0], sr=None, mono=True)

        n_fft = DATA['nfft'][0, 0].astype(int)
        hop_length = DATA['hop_length'][0, 0].astype(int)
        pct = DATA['pct'][0, 0]

        # spectralRolloff uses normalized spectra
        S = met_stft(y, n_fft, hop_length, n_fft, True)

        rolloff = librosa.feature.spectral_rolloff(S=S, sr=sr,
                                                   n_fft=n_fft,
                                                   hop_length=hop_length,
                                                   roll_percent=pct)

        assert np.allclose(rolloff, DATA['rolloff'])

    for infile in files('data/met-rolloff-*.mat'):
        yield __test, infile
Esempio n. 16
0
def test_spectral_contrast():
    def __test(infile):
        DATA = load(infile)

        y, sr = librosa.load(DATA['wavfile'][0], sr=None, mono=True)

        n_fft = DATA['nfft'][0, 0].astype(int)
        hop_length = DATA['hop_length'][0, 0].astype(int)

        # spectralContrast uses normalized spectra
        S = met_stft(y, n_fft, hop_length, n_fft, True)

        contrast = librosa.feature.spectral_contrast(S=S,
                                                     sr=sr,
                                                     n_fft=n_fft,
                                                     hop_length=hop_length,
                                                     linear=True)

        assert np.allclose(contrast, DATA['contrast'], rtol=1e-3, atol=1e-2)

    for infile in files(os.path.join('data', 'met-contrast-*.mat')):
        yield __test, infile
Esempio n. 17
0
except:
    pass

import pytest

import numpy as np
import scipy.stats
import librosa

from test_core import files, load

__EXAMPLE_FILE = os.path.join('tests', 'data', 'test1_22050.wav')


@pytest.mark.parametrize('infile',
                         files(os.path.join('data', 'beat-onset-*.mat')))
def test_onset_strength(infile):

    DATA = load(infile)

    # Compute onset envelope using the same spectrogram
    onsets = librosa.onset.onset_strength(y=None,
                                          sr=8000,
                                          S=DATA['D'],
                                          lag=1,
                                          max_size=1,
                                          center=False,
                                          detrend=True,
                                          aggregate=np.mean)

    assert np.allclose(onsets[1:], DATA['onsetenv'][0])
Esempio n. 18
0
import numpy as np
import scipy.stats
import librosa

from test_core import files, load

__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("infile",
                         files(os.path.join("data", "beat-onset-*.mat")))
def test_onset_strength(infile):

    DATA = load(infile)

    # Compute onset envelope using the same spectrogram
    onsets = librosa.onset.onset_strength(
        y=None,
        sr=8000,
        S=DATA["D"],
        lag=1,
        max_size=1,
        center=False,
        detrend=True,
        aggregate=np.mean,
    )
Esempio n. 19
0
            n_fft=n_fft,
            hop_length=hop_length,
            win_length=win_length,
            window=scipy.signal.hamming,
            center=False,
        ))

    if normalize:
        S = S / (S[0] + np.sum(2 * S[1:], axis=0))

    return S


@pytest.mark.parametrize("infile",
                         files(
                             os.path.join("tests", "data",
                                          "met-centroid-*.mat")))
def test_spectral_centroid(infile):
    DATA = load(infile)

    y, sr = librosa.load(os.path.join("tests", DATA["wavfile"][0]),
                         sr=None,
                         mono=True)

    n_fft = DATA["nfft"][0, 0].astype(int)
    hop_length = DATA["hop_length"][0, 0].astype(int)

    # spectralCentroid uses normalized spectra
    S = met_stft(y, n_fft, hop_length, n_fft, True)

    centroid = librosa.feature.spectral_centroid(S=S,
Esempio n. 20
0
import numpy as np
import scipy.stats
import librosa

from test_core import files, load

__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("infile", files(os.path.join("data", "beat-onset-*.mat")))
def test_onset_strength(infile):

    DATA = load(infile)

    # Compute onset envelope using the same spectrogram
    onsets = librosa.onset.onset_strength(
        y=None, sr=8000, S=DATA["D"], lag=1, max_size=1, center=False, detrend=True, aggregate=np.mean
    )

    assert np.allclose(onsets[1:], DATA["onsetenv"][0])


@pytest.mark.parametrize("tempo", [60, 80, 110, 160])
@pytest.mark.parametrize("sr", [22050, 44100])
@pytest.mark.parametrize("hop_length", [512, 1024])