def plot_static_beat(tempo, onset_env, sampling_rate):
    # Convert to scalar
    tempo = np.asscalar(tempo)
    # Compute 2-second windowed autocorrelation
    hop_length = 512
    auto_correlation = librosa.autocorrelate(onset_env,
                                             2 * sampling_rate // hop_length)
    freqs = librosa.tempo_frequencies(len(auto_correlation),
                                      sr=sampling_rate,
                                      hop_length=hop_length)

    # Plot on a BPM axis.  We skip the first (0-lag) bin.
    fig = plt.figure(figsize=(8, 4))
    ax = fig.add_subplot(111)
    ax.semilogx(freqs[1:],
                librosa.util.normalize(auto_correlation)[1:],
                label='Onset autocorrelation',
                basex=2)
    ax.axvline(tempo,
               0,
               1,
               color='r',
               alpha=0.75,
               linestyle='--',
               label='Tempo: {:.2f} BPM'.format(tempo))
    ax.grid()
    ax.axis('tight')
    return fig
Esempio n. 2
0
    def getVisualTempogram(self, window_length=None, force_recompute=None, norm_columns=None, **kwargs):
        """

        :param self:
        :param window_length: in seconds
        :param force_recompute:
        :param kwargs:
        :return:
        """
        feature_name = 'visual_tempogram';
        if ((not self.hasFeature(feature_name)) or force_recompute):
            if (window_length is None):
                window_length = DEFAULT_TEMPOGRAM_WINDOW_SECONDS;
            params = kwargs;
            params.update({'force_recompute': force_recompute});
            vbe = self.computeImpactEnvelope(cut_suppression_seconds=None);
            onset_envelope = vbe;
            win_length = int(round(window_length * self.sampling_rate));
            sr = self.sampling_rate;
            hop_length = 1;

            center = kwargs.get('center');
            if (center is None):
                center = True;
            window = 'hann'
            norm = np.inf;
            ac_window = librosa.filters.get_window(window, win_length, fftbins=True)

            # Center the autocorrelation windows
            n = len(onset_envelope)

            if center:
                onset_envelope = np.pad(onset_envelope, int(win_length // 2),
                                        mode='linear_ramp', end_values=[0, 0])
            # Carve onset envelope into frames
            odf_frame = librosa.util.frame(onset_envelope,
                                           frame_length=win_length,
                                           hop_length=hop_length)
            # Truncate to the length of the original signal
            if center:
                odf_frame = odf_frame[:, :n]

            odf_frame = librosa.util.normalize(odf_frame, axis=0, norm=norm)
            if (norm_columns is None):
                norm_columns = True;

            if (norm_columns):
                # Window, autocorrelate, and normalize
                result = librosa.util.normalize(
                    librosa.core.audio.autocorrelate(odf_frame * ac_window[:, np.newaxis], axis=0), norm=norm, axis=0);
            else:
                result = librosa.core.audio.autocorrelate(odf_frame * ac_window[:, np.newaxis], axis=0);
                result = np.true_divide(result, np.max(result.ravel()));

            tempo_bpms = librosa.tempo_frequencies(result.shape[0], hop_length=hop_length, sr=sr)
            self.setFeature(name='tempogram_bpms', value=tempo_bpms);
            ###########
            self.setFeature(name=feature_name, value=result, params=params);

        return self.getFeature(feature_name);
Esempio n. 3
0
 def plot_correlation1(self, onset_env, sr):
     hop_length = 512
     ac = librosa.autocorrelate(onset_env, 2 * sr // hop_length)
     freqs = librosa.tempo_frequencies(len(ac),
                                       sr=sr,
                                       hop_length=hop_length)
     self.li.set_xdata(freqs[1:])
     self.li.set_ydata(librosa.util.normalize(ac)[1:])
     plt.pause(0.001)
Esempio n. 4
0
    def getTempogram(self,
                     window_length=None,
                     force_recompute=None,
                     frame_rate=None,
                     resample_rate=None,
                     **kwargs):
        """

        :param self:
        :param window_length: in seconds
        :param force_recompute:
        :param kwargs:
        :return:
        """
        feature_name = 'tempogram'
        if ((not self.hasFeature(feature_name)) or force_recompute):
            if (window_length is None):
                window_length = DEFAULT_TEMPOGRAM_WINDOW_SECONDS
            tpgparams = {}
            tpgparams.update(kwargs)

            sr = self.sampling_rate
            y = self.getSignal()
            if (resample_rate is None):
                resample_rate = 22050
            if (sr > resample_rate):
                print(("resampling {}Hz to {}Hz".format(sr, resample_rate)))
                y = librosa.core.resample(y,
                                          orig_sr=sr,
                                          target_sr=resample_rate)
                sr = resample_rate
                print("resampled")

            if (frame_rate is None):
                frame_rate = 30
            hop_length = int(round(truediv(sr, frame_rate)))
            win_length = int(round(window_length * frame_rate))

            tparams = dict(y=y,
                           sr=sr,
                           hop_length=hop_length,
                           win_length=win_length,
                           **kwargs)
            tpgparams.update(
                dict(sr=sr, hop_length=hop_length, win_length=win_length))
            result = librosa.feature.tempogram(**tparams)
            ###########
            tempo_bpms = librosa.tempo_frequencies(result.shape[0],
                                                   hop_length=hop_length,
                                                   sr=sr)
            self.setFeature(name='tempogram_bpms', value=tempo_bpms)
            self.setFeature(name=feature_name, value=result, params=tpgparams)
            self.setInfo(label='tempogram_params', value=tpgparams)

        return self.getFeature(feature_name)
Esempio n. 5
0
    def __test(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)
Esempio n. 6
0
    def __test(n_bins, hop_length, sr):

        freqs = librosa.tempo_frequencies(n_bins, hop_length=hop_length, sr=sr)

        # Verify the length
        eq_(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)
Esempio n. 7
0
def tempoVSautoCorrelation(tempo, hop_length):
    tempo = np.asscalar(tempo)
    ac = librosa.autocorrelate(onset_env, 2 * sr // hop_length)
    freqs = librosa.tempo_frequencies(len(ac), sr=sr, hop_length=hop_length)
    plt.figure(figsize=(8, 4))
    plt.semilogx(freqs[1:],
                 librosa.util.normalize(ac)[1:],
                 label='Onset autocorrelation',
                 basex=2)
    plt.axvline(tempo,
                0,
                1,
                color='r',
                alpha=0.75,
                linestyle='--',
                label='Tempo: {:.2f} BPM'.format(tempo))
    plt.xlabel('Tempo (BPM)')
    plt.grid()
    plt.title('Static tempo estimation')
    plt.legend(frameon=True)
    plt.axis('tight')
Esempio n. 8
0
def ac_peaks(data, rate, plot=False):
    """Return the three highest peaks in the autocorrelation (tempo) array.  Plot if needed."""

    # Get the onset strength envelope (deals with lag, spectral flux, but the main idea is that it can give us info about lag/variation in the audio, so we can use it to get tempo information)
    oenv = librosa.onset.onset_strength(y=data, sr=rate)

    # Compute the tempogram and truncate at time (index) 1000
    gram = tempogram(data, rate)
    gram = gram[:, :1000]

    # Get the global autocorrelation and the frequencies (in this case, freqs indicate BPM estimates)
    ac_global = librosa.autocorrelate(oenv, max_size=gram.shape[0])
    freqs = librosa.tempo_frequencies(gram.shape[0], sr=rate)

    # Find the peaks of the autocorrelation plot, sort them, and keep only the three highest peaks
    peaks, _ = find_peaks(ac_global)
    sorting = np.argsort(ac_global[peaks])
    peaks = peaks[sorting][-3:]

    # Plot the stuff if requested
    if plot:
        plt.semilogx(freqs, ac_global, ':', base=2)
        plt.semilogx(freqs[peaks],
                     ac_global[peaks],
                     marker='o',
                     linestyle='',
                     base=2)
        plt.xlabel('BPM')
        plt.ylabel('Autocorrelation')
        plt.legend(['Global Autocorrelation', 'Three Highest Peaks'])
        plt.show()

    # Return the frequencies with the three highest autocorrelation value as an array
    if len(freqs[peaks]) == 3:
        return np.array(freqs[peaks])[::-1]
    else:
        return np.array([float('NaN'), float('NaN'), float('NaN')])
Esempio n. 9
0
import numpy as np
import matplotlib.pyplot as plt
from glob import glob
import librosa as lr

audio = 'arabic6'
y, sr = lr.load('./{}.wav'.format(audio))
onset_env = lr.onset.onset_strength(y, sr=sr)
tempo = lr.beat.tempo(onset_envelope=onset_env, sr=sr)
print(tempo)
tempo = np.asscalar(tempo)
# Compute 2-second windowed autocorrelation
hop_length = 512
ac = lr.autocorrelate(onset_env, 2 * sr // hop_length)
freqs = lr.tempo_frequencies(len(ac), sr=sr, hop_length=hop_length)
# Plot on a BPM axis.  We skip the first (0-lag) bin.
plt.figure(figsize=(8, 4))
plt.semilogx(freqs[1:],
             lr.util.normalize(ac)[1:],
             label='Onset autocorrelation',
             basex=2)
plt.axvline(tempo,
            0,
            1,
            color='r',
            alpha=0.75,
            linestyle='--',
            label='Tempo: {:.2f} BPM'.format(tempo))
plt.xlabel('Tempo (BPM)')
plt.grid()
plt.title('Static tempo estimation')
Esempio n. 10
0
                label='Estimated tempo={:g}'.format(tempo))
    plt.legend(frameon=True, framealpha=0.75)
    plt.subplot(4, 1, 3)
    x = np.linspace(0,
                    tempogram.shape[0] * float(hop_length) / sr,
                    num=tempogram.shape[0])
    plt.plot(x, np.mean(tempogram, axis=1), label='Mean local autocorrelation')
    plt.plot(x, ac_global, '--', alpha=0.75, label='Global autocorrelation')
    plt.xlabel('Lag (seconds)')
    plt.axis('tight')
    plt.legend(frameon=True)
    plt.subplot(4, 1, 4)

    # We can also plot on a BPM axis
    freqs = librosa.tempo_frequencies(tempogram.shape[0],
                                      hop_length=hop_length,
                                      sr=sr)
    plt.semilogx(freqs[1:],
                 np.mean(tempogram[1:], axis=1),
                 label='Mean local autocorrelation',
                 basex=2)
    plt.semilogx(freqs[1:],
                 ac_global[1:],
                 '--',
                 alpha=0.75,
                 label='Global autocorrelation',
                 basex=2)
    plt.axvline(tempo,
                color='black',
                linestyle='--',
                alpha=.8,
def plot_tempogram(number):
    example_mp3, sr, song_name = load_music.load_song(number)
    hop_length = 512
    oenv = librosa.onset.onset_strength(y=example_mp3,
                                        sr=sr,
                                        hop_length=hop_length)
    tempogram = librosa.feature.tempogram(onset_envelope=oenv,
                                          sr=sr,
                                          hop_length=hop_length)
    # Compute global onset autocorrelation
    ac_global = librosa.autocorrelate(oenv, max_size=tempogram.shape[0])
    ac_global = librosa.util.normalize(ac_global)
    # Estimate the global tempo for display purposes
    tempo = librosa.beat.tempo(onset_envelope=oenv,
                               sr=sr,
                               hop_length=hop_length)[0]

    fig, ax = plt.subplots(nrows=4, figsize=(10, 10))
    times = librosa.times_like(oenv, sr=sr, hop_length=hop_length)
    ax[0].plot(times, oenv, label='Onset strength')
    ax[0].label_outer()
    ax[0].legend(frameon=True)
    librosa.display.specshow(tempogram,
                             sr=sr,
                             hop_length=hop_length,
                             x_axis='time',
                             y_axis='tempo',
                             cmap='magma',
                             ax=ax[1])
    ax[1].axhline(tempo,
                  color='w',
                  linestyle='--',
                  alpha=1,
                  label='Estimated tempo={:g}'.format(tempo))
    ax[1].legend(loc='upper right')
    ax[1].set(title='Tempogram')
    x = np.linspace(0,
                    tempogram.shape[0] * float(hop_length) / sr,
                    num=tempogram.shape[0])
    ax[2].plot(x,
               np.mean(tempogram, axis=1),
               label='Mean local autocorrelation')
    ax[2].plot(x, ac_global, '--', alpha=0.75, label='Global autocorrelation')
    ax[2].set(xlabel='Lag (seconds)')
    ax[2].legend(frameon=True)
    freqs = librosa.tempo_frequencies(tempogram.shape[0],
                                      hop_length=hop_length,
                                      sr=sr)
    ax[3].semilogx(freqs[1:],
                   np.mean(tempogram[1:], axis=1),
                   label='Mean local autocorrelation',
                   basex=2)
    ax[3].semilogx(freqs[1:],
                   ac_global[1:],
                   '--',
                   alpha=0.75,
                   label='Global autocorrelation',
                   basex=2)
    ax[3].axvline(tempo,
                  color='black',
                  linestyle='--',
                  alpha=.8,
                  label='Estimated tempo={:g}'.format(tempo))
    ax[3].legend(frameon=True)
    ax[3].set(xlabel='BPM')
    ax[3].grid(True)
    fig.suptitle('Tempogram on ' + song_name, fontsize=8)

    plt.show()
    def Tempogram_Show2(self, show=True):
        hop_length = 512

        tempogram_record = librosa.feature.tempogram(
            onset_envelope=self.__onset_env_record,
            sr=self.__sr_record,
            hop_length=hop_length)

        tempo_record = librosa.beat.tempo(
            onset_envelope=self.__onset_env_record,
            sr=self.__sr_record,
            hop_length=hop_length)[0]

        ac_global_record = librosa.autocorrelate(
            self.__onset_env_record, max_size=tempogram_record.shape[0])
        ac_global_record = librosa.util.normalize(ac_global_record)

        x_record = np.linspace(0,
                               tempogram_record.shape[0] * float(hop_length) /
                               self.__sr_record,
                               num=tempogram_record.shape[0])

        tempogram_music = librosa.feature.tempogram(
            onset_envelope=self.__onset_env_music,
            sr=self.__sr_music,
            hop_length=hop_length)

        tempo_music = librosa.beat.tempo(onset_envelope=self.__onset_env_music,
                                         sr=self.__sr_music,
                                         hop_length=hop_length)[0]

        ac_global_music = librosa.autocorrelate(
            self.__onset_env_music, max_size=tempogram_music.shape[0])
        ac_global_music = librosa.util.normalize(ac_global_music)

        x_music = np.linspace(0,
                              tempogram_music.shape[0] * float(hop_length) /
                              self.__sr_music,
                              num=tempogram_music.shape[0])

        if show == True:
            fig, ax = plt.subplots(nrows=4)

            plt.title('Recording:')
            ax[0].plot(x_record,
                       np.mean(tempogram_record, axis=1),
                       label='Mean local autocorrelation')
            ax[0].plot(x_record,
                       ac_global_record,
                       '--',
                       alpha=0.75,
                       label='Global autocorrelation')

            ax[0].set(xlabel='Record Lag (seconds)')
            ax[0].legend(title='Recording:', frameon=True)

            ax[1].plot(x_music,
                       np.mean(tempogram_music, axis=1),
                       label='Mean local autocorrelation')
            ax[1].plot(x_music,
                       ac_global_music,
                       '--',
                       alpha=0.75,
                       label='Global autocorrelation')
            ax[1].set(xlabel='Music Lag (seconds)')
            ax[1].legend(title='Music:', frameon=True)

            freqs_record = librosa.tempo_frequencies(tempogram_record.shape[0],
                                                     hop_length=hop_length,
                                                     sr=self.__sr_record)
            freqs_music = librosa.tempo_frequencies(tempogram_music.shape[0],
                                                    hop_length=hop_length,
                                                    sr=self.__sr_music)

            ax[2].semilogx(freqs_record[1:],
                           np.mean(tempogram_record[1:], axis=1),
                           label='Mean local autocorrelation',
                           basex=2)
            ax[2].semilogx(freqs_record[1:],
                           ac_global_record[1:],
                           '--',
                           alpha=0.75,
                           label='Global autocorrelation',
                           basex=2)
            ax[2].axvline(tempo_record,
                          color='black',
                          linestyle='--',
                          alpha=.8,
                          label='Estimated tempo={:g}'.format(tempo_record))
            ax[2].legend(title='Recording:', frameon=True)
            ax[2].set(xlabel='BPM')
            ax[2].grid(True)

            ax[3].semilogx(freqs_music[1:],
                           np.mean(tempogram_music[1:], axis=1),
                           label='Mean local autocorrelation',
                           basex=2)
            ax[3].semilogx(freqs_music[1:],
                           ac_global_music[1:],
                           '--',
                           alpha=0.75,
                           label='Global autocorrelation',
                           basex=2)
            ax[3].axvline(tempo_music,
                          color='black',
                          linestyle='--',
                          alpha=.8,
                          label='Estimated tempo={:g}'.format(tempo_music))
            ax[3].legend(title='Music:', frameon=True)
            ax[3].set(xlabel='BPM')
            ax[3].grid(True)
            plt.show()
        return format(tempo_music), format(tempo_record)
    def Tempogram_Show(self):
        tempogram = librosa.feature.tempogram(onset_envelope=self.__onset_env,
                                              sr=self.__sr,
                                              hop_length=self.__hop_length)
        ac_global = librosa.autocorrelate(self.__onset_env,
                                          max_size=tempogram.shape[0])
        ac_global = librosa.util.normalize(ac_global)

        tempo = librosa.beat.tempo(onset_envelope=self.__onset_env,
                                   sr=self.__sr,
                                   hop_length=self.__hop_length)[0]

        fig, ax = plt.subplots(nrows=4, figsize=(10, 10))
        times = librosa.times_like(self.__onset_env,
                                   sr=self.__sr,
                                   hop_length=self.__hop_length)
        ax[0].plot(times, self.__onset_env, label='Onset strength')
        ax[0].label_outer()
        ax[0].legend(frameon=True)
        librosa.display.specshow(tempogram,
                                 sr=self.__sr,
                                 hop_length=self.__hop_length,
                                 x_axis='time',
                                 y_axis='tempo',
                                 cmap='magma',
                                 ax=ax[1])
        ax[1].axhline(tempo,
                      color='w',
                      linestyle='--',
                      alpha=1,
                      label='Estimated tempo={:g}'.format(tempo))
        ax[1].legend(loc='upper right')
        ax[1].set(title='Tempogram')
        x = np.linspace(0,
                        tempogram.shape[0] * float(self.__hop_length) /
                        self.__sr,
                        num=tempogram.shape[0])
        ax[2].plot(x,
                   np.mean(tempogram, axis=1),
                   label='Mean local autocorrelation')
        ax[2].plot(x,
                   ac_global,
                   '--',
                   alpha=0.75,
                   label='Global autocorrelation')
        ax[2].set(xlabel='Lag (seconds)')
        ax[2].legend(frameon=True)
        freqs = librosa.tempo_frequencies(tempogram.shape[0],
                                          hop_length=self.__hop_length,
                                          sr=self.__sr)
        ax[3].semilogx(freqs[1:],
                       np.mean(tempogram[1:], axis=1),
                       label='Mean local autocorrelation',
                       basex=2)
        ax[3].semilogx(freqs[1:],
                       ac_global[1:],
                       '--',
                       alpha=0.75,
                       label='Global autocorrelation',
                       basex=2)
        ax[3].axvline(tempo,
                      color='black',
                      linestyle='--',
                      alpha=.8,
                      label='Estimated tempo={:g}'.format(tempo))
        ax[3].legend(frameon=True)
        ax[3].set(xlabel='BPM')
        ax[3].grid(True)
        plt.show()