Skip to content
Extraits de code Groupes Projets
segment.py 1,74 Kio
import librosa
import numpy as np
# import matplotlib.pyplot as plt
import sys



class Segment:

    def __init__(self, file):
        self.file = file


    def onsets(self):
        '''
        Use librosa's onset detection to detect syllable start times
        '''

        y, sr = librosa.load(self.file)

        o_env = librosa.onset.onset_strength(y=y, sr=sr)
        times = librosa.times_like(o_env, sr=sr)
        onset_raw = librosa.onset.onset_detect(onset_envelope=o_env, sr=sr)
        onset_bt = librosa.onset.onset_backtrack(onset_raw, o_env)

        S = np.abs(librosa.stft(y=y))
        rms = librosa.feature.rms(S=S)
        onset_bt_rms = librosa.onset.onset_backtrack(onset_raw, rms[0])

        onset_bt_times = librosa.frames_to_time(onset_bt, sr=sr)
        onset_bt_rms_times = librosa.frames_to_time(onset_bt_rms, sr=sr)

        onset_raw_times = librosa.frames_to_time(onset_raw, sr=sr)

        # print(onset_bt_rms_times)

        '''
        fig, ax = plt.subplots(nrows=3, sharex=True)
        librosa.display.specshow(librosa.amplitude_to_db(S, ref=np.max),y_axis='log', x_axis='time', ax=ax[0])
        ax[0].label_outer()
        ax[1].plot(times, o_env, label='Onset strength')
        ax[1].vlines(librosa.frames_to_time(onset_raw), 0, o_env.max(), label='Raw onsets')
        ax[1].vlines(librosa.frames_to_time(onset_bt), 0, o_env.max(), label='Backtracked', color='r')
        ax[1].legend()
        ax[1].label_outer()
        ax[2].plot(times, rms[0], label='RMS')
        ax[2].vlines(librosa.frames_to_time(onset_bt_rms), 0, rms.max(), label='Backtracked (RMS)', color='r')
        ax[2].legend()

        plt.show()
        '''

        return onset_raw_times


if __name__ == "__main__":
    seg = Segment(sys.argv[1])
    seg.onsets()