Esempio n. 1
0
def generate_rhythm_overlay(rhythm, measure_samples, steps_per_measure, sr):
    # Calculate click interval
    measure_length = measure_samples[1] - measure_samples[0]
    # click_tempo = tempo * (steps_per_measure/float(beats_per_measure))
    # click_interval = 60.0/click_tempo
    measure_length_seconds = librosa.samples_to_time(measure_length, sr=sr)
    click_interval = measure_length_seconds / float(steps_per_measure)

    # Generate click times for single measure
    pulse_times_measure, step_times_measure = generate_rhythm_times(
        rhythm, click_interval)

    # Generate clicks for single measure
    pulse_clicks_measure, step_clicks_measure = generate_rhythm_clicks(
        rhythm, click_interval, sr=sr)

    # Concatenate clicks and click times for all measures
    pulse_times, step_times, pulse_clicks, step_clicks = np.array(
        []), np.array([]), np.array([]), np.array([])
    for s in measure_samples:
        t = float(librosa.samples_to_time(s, sr=sr))
        pulse_clicks = np.hstack((pulse_clicks, pulse_clicks_measure))
        step_clicks = np.hstack((step_clicks, step_clicks_measure))
        pulse_times = np.hstack((pulse_times, pulse_times_measure + t))
        step_times = np.hstack((step_times, step_times_measure + t))

    # Offset clicks by first onset
    pulse_clicks = np.hstack((np.zeros(measure_samples[0]), pulse_clicks))
    step_clicks = np.hstack((np.zeros(measure_samples[0]), step_clicks))

    return (pulse_times, step_times, pulse_clicks, step_clicks)
def get_df_audio(sep, div, bmp, sr):
    audio_list = []
    for i in range(0, len(sep)):
        duration = librosa.core.get_duration(sep[i])
        start = librosa.samples_to_time(div[i, 0])
        end = librosa.samples_to_time(div[i, 1])
        pitch = estimate_pitch_fft(sep[i], sr)
        #pitch = estimate_pitch(sep[i], sr)
        midi = librosa.hz_to_midi(pitch)
        audio_list.append([start, end, duration, pitch, midi, bmp])
    df = pd.DataFrame(
        audio_list,
        columns=['start', 'end', 'duration', 'pitch', 'midi', 'tempo'])
    return df
Esempio n. 3
0
def get_one_audio_info(audio_path):
    audio_split = audio_path.split(os.path.sep)
    csv_target_name = audio_path
    print(audio_split)
    csv_subject = audio_split[2]
    csv_label = audio_split[-1][:2]

    audio,sr = load(audio_path)
    csv_time = librosa.get_duration(audio)
    L,R = trim_silence(audio)
    csv_available_time_L = librosa.samples_to_time(L)[0]
    csv_available_time_R = librosa.samples_to_time(R)[0]
    csv_data = [csv_target_name,csv_subject,csv_label,csv_time,
                        csv_available_time_L,csv_available_time_R]
    return csv_data
Esempio n. 4
0
def get_audio_length(signal=None, frames=None, config=dict()):
    '''
    Computes the audio length in seconds from given either signal or frames vector.

    Parameters
    ----------
    signal : np.ndarray [shape=(n_signal,)] or None
        Signal vector.

    frames : np.ndarray [shape=(n_frames,)] or None
        Frames vector.

    config : dict
        Configuration dictionary. For full list of parameters with their description, see README file. Following
        parameters are used in this function:

        * 'audio.sampling_rate'
        * 'spectrum.hop_length'

    Returns
    -------
    time : int
        Number of seconds.
    '''

    sampling_rate = get(config, 'audio.sampling_rate')
    hop_length = get(config, 'spectrum.hop_length')

    if signal is not None:
        return int(np.round(np.asscalar(librosa.samples_to_time(len(signal), sr=sampling_rate))))
    elif frames is not None:
        return int(np.round(np.asscalar(librosa.frames_to_time(len(frames), sr=sampling_rate, hop_length=hop_length))))
    else:
        raise ValueError('Either `signal` or `frames` parameter must be passed.')
def generation_dict_to_audio(packed_paths, sr, gen_dict, out_path,
                             meta_out_path, max_samples, augmentations, fold,
                             folds):
    sound_factory = SoundFactory(packed_paths, sr, augmentations=augmentations)

    meta_list = []
    audio_array = np.zeros(max_samples)
    for key_idx, key in enumerate(gen_dict.keys()):
        for interval_idx, interval in enumerate(gen_dict[key]):
            # print(
            #     f"Parsing key: {key}, {int(100 * key_idx / len(list(gen_dict.keys())))}%, interval: {interval}, {int(100 * interval_idx / len(gen_dict[key]))}%")
            print(
                f"Parsing: {fold + 1/folds * 100}%, {int(100 * (key_idx + 1) / len(list(gen_dict.keys())))}%, {int(100 * (interval_idx + 1) / len(gen_dict[key]))}%"
            )

            y, relative_time_interval = sound_factory.get_note(key, interval)
            interval = (int(interval[0]), int(interval[1]))
            audio_array_temp = audio_array[slice(*interval)]
            (audio_array_temp, y) = match_lists_by_len([audio_array_temp, y])
            added = np.add(audio_array_temp, y)
            normal = librosa.util.normalize(added)
            try:
                audio_array[slice(*interval)] = normal
                current_time = librosa.samples_to_time(interval[0], sr=sr)
                meta_list = append_to_meta(meta_list, key, out_path,
                                           current_time,
                                           relative_time_interval, key)
            except:
                pass

    audio_array = 0.3 * audio_array
    librosa.output.write_wav(out_path, audio_array, sr)
    df = pd.DataFrame(meta_list)
    df.to_csv(meta_out_path, sep='\t', index=False, header=False)
    print(f"saving to :{out_path}")
Esempio n. 6
0
def build_song_segments(
    target_song, segments_df, songs_df
):  # segments_df = all of tailor swift scenes, songs_df = a list of all of tailor's songs in db
    matches = []
    wave_segments = []
    target_remaining_samples = target_song
    while len(target_remaining_samples) > 10000:
        # TODO: only necessary to calculate onsets up to max length of segment, not entire remaining song
        # fix if too slow
        target_onset_envelope = librosa.onset.onset_strength(
            target_remaining_samples)
        # dot prod - correlate segments, penalize by sqrt of length, which makes longer segments better
        onset_silly_match = segments_df.onset_envelope.map(
            silly_segment_matcher(target_onset_envelope))
        best_match_index = onset_silly_match.idxmax()
        best_match = segments_df.loc[best_match_index]
        matching_song = songs_df[songs_df['name'] == best_match['name']]
        wave = matching_song.song_wave.values[0]
        start_times = matching_song.scene_start_times_sec.values[0]
        start_time = start_times[best_match.index_in_song]
        # end of song if index_in_song is the last one
        end_time = start_times[best_match.index_in_song + 1] if best_match.index_in_song + 1 < len(start_times) \
                    else librosa.samples_to_time(len(wave))
        start_sample = librosa.time_to_samples(start_time)
        end_sample = librosa.time_to_samples(end_time)

        matches.append((best_match['name'], start_time, end_time))
        consumed_samples = end_sample - start_sample
        target_remaining_samples = target_remaining_samples[consumed_samples:]
        # don't reuse samples
        segments_df = segments_df.drop(best_match_index)
        wave_segments.append(wave[start_sample:end_sample])
    return matches, np.concatenate(wave_segments)
Esempio n. 7
0
def test_time_delta(process_base_song, start, frames, expected):
    if frames < 0:
        start = 0
        frames = len(process_base_song.time_series) - 1
        expected = librosa.samples_to_time(
            numpy.array([frames]), process_base_song.sampling_rate)[0]
    assert process_base_song.time_delta(start, start + frames) == expected
def gen_hihat(all_data, fs, fps, cand):
    fps = librosa.samples_to_frames(fs, hop_length=hop_len, n_fft=win_len)
    fps = 100
    print(cand)
    proc = BeatTrackingProcessor(look_aside=0.2, fps=fps)
    act = RNNBeatProcessor()(all_data)
    beat_times = proc(act)

    song_len = librosa.samples_to_time(data.shape, sr=fs)[0]
    hihat = np.zeros(all_data.shape)
    idx = np.where(beat_times <= song_len)[0]
    new_beat_times = np.zeros(idx.shape)
    new_beat_times[idx] = beat_times[idx]
    beat_samples = librosa.time_to_samples(new_beat_times, sr=fs)
    start = librosa.frames_to_samples(cand[0], hop_len, n_fft=win_len)
    end = librosa.frames_to_samples(cand[-1], hop_len, n_fft=win_len)
    cand_len = end - start

    i = 3
    is_hihat = np.zeros(beat_samples.shape)
    while i < len(beat_samples):
        is_hihat[i] = 1
        i = i + 4
    for i, s in enumerate(beat_samples):
        if is_hihat[i] == 1:
            if s + cand_len > hihat.shape:
                break
            hihat[s:s + cand_len] = data[start:end]

    return hihat, new_beat_times, beat_samples
Esempio n. 9
0
 def get_start_time(self) -> float:
     if Beat.INDEX_VALUE == 'time':
         return self.index
     elif Beat.INDEX_VALUE == 'samples':
         return librosa.samples_to_time(self.index, sr=util.SAMPLE_RATE)
     else:
         raise NotImplementedError("Only samples and time are supported")
Esempio n. 10
0
def dub2(songid1, songid2, dist_value, posi, var):
    out = dsp.buffer()
    dubhead = 0
    #filename = Song.query.filter_by(id=songid1).first().filename
    #audio = dsp.read(os.path.join(app.instance_path, filename))
    labels2 = [i.note for i in Beat.query.filter_by(song_id=songid1)]
    ar = dist(dist_value, posi)

    for e, i in enumerate(labels2):
        while dubhead < 60:
            rstart = [s.start for s in Beat.query.filter_by(note=i)]
            rend = [s.end for s in Beat.query.filter_by(note=i)]
            source = [s.song_id for s in Beat.query.filter_by(note=i)]
            rpool = [(rstart[i], rend[i], source[i])
                     for i in range(0, len(rstart))]

            sl = random.choice(rpool)
            bl = int(sl[1] - sl[0])
            l = (sl[1] + (bl * np.random.choice(16, p=ar)))
            filename = Song.query.filter_by(id=sl[2]).first().filename
            audio = dsp.read(os.path.join(app.instance_path, filename))
            a = audio[sl[0]:l]
            stime = librosa.samples_to_time(len(a), sr=44100)
            #var = 0.5
            a = a.taper((stime / 2) * var)
            out.dub(a, dubhead)
            dubhead += stime - ((stime / 2) * var)
    return out
Esempio n. 11
0
    def get_sheet(self):
        y, sr = st.read_wav('temp.wav')
        onset_boundaries = st.get_onsetboundaries(y, sr)

        chords = []
        for i in range(len(onset_boundaries) - 1):
            p0 = onset_boundaries[i]
            p1 = onset_boundaries[i + 1]
            pitch = st.get_pitch(y[p0:p1], sr)
            chords.append([p for p in pitch])

        # test music sheet
        times = st.set_duration(
            librosa.samples_to_time(onset_boundaries, sr=sr))
        self.music_sheet = st.init_stream()
        for notes, length in zip(chords, times):
            #if notes == []:
            #    continue
            if notes == []:
                n = st.set_rest(length)
                self.music_sheet.append(n)
                continue
            n = st.set_chrod(notes)
            n.quarterLength = length
            self.music_sheet.append(n)
Esempio n. 12
0
    def play(self, dur):
        times = sm.waves.tspan(dur)
        waves = []

        for start, wdur, lf, hf, amp, amp_freq, amp_phase in zip(
            self.starts, self.durs, self.low_freqs, self.high_freqs, self.amps, self.amp_freqs, self.amp_phases
        ):
            ss = lr.time_to_samples(start, sm.sound.SAMPLE_RATE)
            es = lr.time_to_samples(start + wdur, sm.sound.SAMPLE_RATE)
            es = min(es, self.so.samples.size - 1)
            samps = self.so.samples[ss:es]
            if samps.size <= 0:
                continue
            if hf < lf:
                hf, lf = lf, hf
            if es - ss > 0.1 * sm.sound.SAMPLE_RATE:
                samps = sm.effects.band_pass(SoundObject(samps), lf, hf).samples
            peak = np.max(np.abs(samps))
            if peak > 0.0:
                samps /= peak
            samp_dur = lr.samples_to_time(samps.size, sm.sound.SAMPLE_RATE)
            wave = np.interp(
                times, 
                np.linspace(0, samp_dur, samps.size),
                samps,
                period=samp_dur
            ) * amp
            mod = sm.waves.sin(times, amp_freq, 1.0, amp_phase)
            wave *= mod
            waves.append(SoundObject(wave))

        return sm.sound.join(waves)
def note_by_num_samples(freq, num_samples, amp, rate):
    data = np.array([
        np.sin(2 * np.pi * freq * librosa.samples_to_time(sample, rate)) * amp
        for sample in range(num_samples)
    ],
                    dtype=np.float32)
    return data
Esempio n. 14
0
def get_sheet():
    y, sr = read_wav('C:/mkyu/test4.wav')
    onset_boundaries = get_onsetboundaries(y, sr)

    chords = []
    for i in range(len(onset_boundaries) - 1):
        p0 = onset_boundaries[i]
        p1 = onset_boundaries[i + 1]
        pitch = get_pitch(y[p0:p1], sr)
        chords.append([p for p in pitch])

    # test music sheet
    times = set_duration(librosa.samples_to_time(onset_boundaries, sr=sr))
    music_sheet = init_stream()
    for notes, length in zip(chords, times):
        #        print(notes, length)
        if notes:
            n = set_chrod(notes)
            n.quarterLength = length
            music_sheet.append(n)
        else:  # 코드없이 길이만 있을경우에는 쉼표를 넣어야겠죵?
            music_sheet.append(note.Rest(quarterLength=length))

    music_sheet.show()
    music_sheet.show('text')
    music_sheet.write('midi', fp='result.mid')
def get_sheet(filename='temp/temp.wav'):
    y, sr = read_wav(filename)
    onset_boundaries = get_onsetboundaries(y, sr)

    chords = []
    for i in range(len(onset_boundaries) - 1):
        p0 = onset_boundaries[i]
        p1 = onset_boundaries[i + 1]
        pitch = get_pitch(y[p0:p1], sr)
        chords.append([p for p in pitch])

    times = set_duration(librosa.samples_to_time(onset_boundaries, sr=sr))
    music_sheet = init_stream()
    for notes, length in zip(chords, times):
        if notes == []:
            n = set_rest(length)
            music_sheet.append(n)
            continue
        n = set_chrod(notes)
        n.quarterLength = length
        music_sheet.append(n)

    #music_sheet.write('midi', fp='instrument.mid')
    #music_sheet.show('text')
    #music_sheet.write('midi', fp='result2.mid')
    return music_sheet
Esempio n. 16
0
def strikes_and_notes(path):
    y, fs = librosa.core.load(path, offset=0.0, duration=None)
    t = librosa.times_like(y, sr=fs)

    strikes = librosa.onset.onset_detect(y, sr=fs, units='samples')

    played_times = []
    played_notes = []

    for i in range(len(strikes)):
        if i == len(strikes) - 1:
            window = y[strikes[i]:min(len(y), 2 * strikes[i] - strikes[i - 1])]
        else:
            window = y[strikes[i]:strikes[i + 1]]

        f0, voiced_flag, voiced_probs = librosa.pyin(
            window,
            fmin=librosa.note_to_hz('C2'),
            fmax=librosa.note_to_hz('C7'),
            fill_na=None)
        f0_est = np.median(f0[~np.isnan(f0)])

        if ~np.isnan(f0_est):
            played_notes.append(f0_est)
            played_times.append(librosa.samples_to_time(strikes, sr=fs))

    return played_times, played_notes
Esempio n. 17
0
def get_phrase_intervals(file_id, y,sr, r, w, w_p_ratio,  period_threshold, peak_window, tempo):
    #param:
    #   y = waveform
    #   sr = sample rate
    #   r = radius/resolution of diagonal cut
    #   w = checkerboard window size in seconds,  w<=r
    #   w_p_ratio = ratio used for peak picking, unknown
    #   period_threshold = threshold for filtering by period
    #   fpb = frames per beat for phrase search
    
    #TEST CONSTANTS
    if (w>r):
        sys.exit('Window Resolution Mismatch')
    sample_length = librosa.samples_to_time([len(y)],sr)[0] #s
    w_f = librosa.time_to_frames([w],hop_length=256)[0]
    f  = extract_features(y)
    #frames per beat
    fpb = librosa.time_to_frames([1/(tempo/60)],hop_length=256)[0]
    #LOAD OR CREATE S-MATRIX & NOVELTY VECTOR
    s_matrix = init_smatrix(file_id,f,r,  sample_length)
    novelty = init_novelty_vector(file_id, w, w_f,  sample_length, s_matrix)
    
    w_p = w_f/w_p_ratio
    peaks = librosa.util.peak_pick(novelty, w_p, w_p, w_p, w_p, peak_window, w_p)
    
    return filter_by_period(peaks, period_threshold, fpb)
Esempio n. 18
0
def librosa_beat_tracking(config, signal, song, entry_point):
    sample_rate = song['frame_rate']

    # create signal partition based on given entry point
    entry_point_index = round(len(signal) * entry_point)
    if entry_point > 0.5:
        signal_part = signal[entry_point_index:]
    else:
        signal_part = signal[:entry_point_index]

    # check if beat tracking was done already and take saved status
    beat_tracking_path = f"{config['song_analysis_path']}/{song['name']}_{song['bpm']}.json"
    if path.exists(beat_tracking_path):
        tempo, beats = get_beat_tracking_from_file(beat_tracking_path)
        print(f"\t\t Read tempo & beats from file. BPM of song: {tempo}, amount of beats found: {len(beats)}")
    else:
        # compute onset envelopes
        onset_env = librosa.onset.onset_strength(y=signal_part, sr=sample_rate, aggregate=numpy.median)

        # compute beats using librosa beat tracking
        tempo, beats = librosa.beat.beat_track(onset_envelope=onset_env, sr=sample_rate)

        # save beat tracking
        save_beat_tracking_to_file(beat_tracking_path, tempo, beats)

        print(f"\t\t Librosa beat detection finished. BPM of song: {tempo}, amount of beats found: {len(beats)}")

    # create onset sample matrix from tracked beats
    onset_samples = list(librosa.frames_to_samples(beats))
    onset_samples = numpy.concatenate(onset_samples, len(signal_part))

    # derive frame index of beat starts/stops from onset sample matrix
    starts = onset_samples[0:-1]
    stops = onset_samples[1:]

    times_starts = librosa.samples_to_time(starts, sr=sample_rate)
    times_stops = librosa.samples_to_time(stops, sr=sample_rate)

    # offset times values based on entry_point if we aim to find exit segments
    if entry_point > 0.5:
        times_offset = (len(signal)/sample_rate)*entry_point
        times_starts = [t + times_offset for t in times_starts]
        times_stops = [t + times_offset for t in times_stops]

    return times_starts, times_stops
def CalcDownbeat(y, sr, duration, **args):
    # calc downbeat entertime
    analysisLength = args['-duration']
    minimumMusicLength = 165
    maximumMusicLength = 360
    numThread = args['-thread']
    threhold = args['-threhold']
    abThrehold = args['-abThrehold']

    startTime = time.time()

    if duration < minimumMusicLength:
        logger.error(MUSIC_TOO_SHORT, 'music is too short! duration:',
                     duration)
        return 0, 0

    if duration > maximumMusicLength:
        logger.error(MUSIC_TOO_LONG, 'music is too long! duration:', duration)
        return 0, 0

    start = int(duration * 0.3)
    clipTime = np.array([start, start + analysisLength])
    # logger.info('duration', duration, 'start', start)
    clip = librosa.time_to_samples(clipTime, sr=sr)
    # logger.info('total', y.shape, 'clip', clip)
    yy = y[clip[0]:clip[1]]

    processer = RNNDownBeatProcessor(num_threads=numThread)
    downbeatTracking = DBNDownBeatTrackingProcessor(beats_per_bar=4,
                                                    transition_lambda=1000,
                                                    fps=100)
    beatProba = processer(yy)

    beatIndex = downbeatTracking(beatProba)

    firstBeat, lastBeat = NormalizeInterval(beatIndex,
                                            threhold=threhold,
                                            abThrehold=abThrehold)
    if firstBeat == -1:
        logger.error(
            PROBABLY_NOT_MUSIC, 'generate error,numbeat %d, abnormal rate %f' %
            (len(beatIndex), lastBeat))
        return 0, 0

    newBeat = beatIndex[firstBeat:lastBeat]
    downbeat = madmom.features.downbeats.filter_downbeats(newBeat)
    downbeat = downbeat + librosa.samples_to_time(clip, sr=sr)[0]

    barInter, etAuto = CalcBarInterval(downbeat)

    # enter time is less than a bar interval
    etAuto = etAuto % barInter

    bpm = 240.0 / barInter

    return bpm, etAuto
def saveAudioWithFormBeeps(audio, time, sr=22050, folder_name=''):

    #check if folder exists or make it
    checkMakeFolder(folder_name)

    # generating a track with clicks at the start of forms
    clicks = lr.clicks(times=lr.samples_to_time(time), length=len(audio))

    # adsd the clicks to original audio and save it
    sf.write(folder_name + 'audio_beeped.wav', audio + clicks, int(sr))
Esempio n. 21
0
def trim(filename, output, SHOW_PLOTS=False):
    y, sr = librosa.load(filename)

    FOLGA = int(sr)
    N_SLICES = 4

    y = librosa.to_mono(y)
    y = librosa.util.normalize(y)
    y, indexes = librosa.effects.trim(y, top_db=24, frame_length=2)
    fxy = fx(y)
    fxy[np.abs(fxy) < 0.5] = 0

    peaks, _ = find_peaks(fxy, height=0.5, distance=sr)
    peaks = _remove_until(peaks, N_SLICES)

    if SHOW_PLOTS:
        peak_times = librosa.samples_to_time(peaks)
        plt.title(filename)
        librosa.display.waveplot(fxy, color='m')
        librosa.display.waveplot(y, color='orange')
        plt.vlines(peak_times,
                   -1,
                   1,
                   color='k',
                   linestyle='--',
                   linewidth=6,
                   alpha=0.9,
                   label='Segment boundaries')
        plt.show()
        return

    labels = _extract_labels(filename)

    if not os.path.exists('%s/%s' % (output, labels)):
        os.mkdir('%s/%s' % (output, labels))

    for i in range(N_SLICES):
        if (i >= len(peaks)):
            continue
        p = peaks[i]
        left = int(round(max(0, p - FOLGA)))
        right = int(round(min(p + FOLGA, len(y) - 1)))
        audio = y[left:right]
        if (np.any(audio)):
            _, [left, right] = librosa.effects.trim(audio,
                                                    top_db=12,
                                                    frame_length=2)
            left = int(round(max(0, left - FOLGA // 4)))
            right = int(round(min(right + FOLGA // 4, len(y) - 1)))
            audio_trim = audio[left:right]
            audio_trim = librosa.util.normalize(audio_trim)
            librosa.output.write_wav('%s/%s/%d-%s.wav' %
                                     (output, labels, i, labels[i]),
                                     audio_trim,
                                     sr=sr)
    def sound_data(self):
        (sound_data, sr) = self.recording.data
        if self.start_t is None:
            self.start_t = 0
        if self.end_t is None:
            self.end_t = librosa.samples_to_time(sound_data.size, sr)

        start_i = librosa.time_to_samples(self.start_t, sr)
        end_i = librosa.time_to_samples(self.end_t, sr)

        return sound_data[start_i:end_i]
Esempio n. 23
0
 def _detect_onset(self, y):
     try:
         frame_len = 2048
         if len(y) > frame_len:
             onset_sample = librosa.effects.split(
                 y, 50, frame_length=frame_len)[0][0]
         else:
             onset_sample = len(y) - 1
     except:
         pass
         print("Error")
     return librosa.samples_to_time(onset_sample, self.sr)
Esempio n. 24
0
def compute_buffers(y, beat_frames):
    int_max = numpy.iinfo(numpy.int16).max
    raw = (y * int_max).astype(numpy.int16).T.copy(order='C')

    buffers = []
    for start, end in iter_beat_slices(raw, beat_frames):
        samples = raw[start:end]
        data = struct.pack("h" * len(samples), *samples)
        duration = librosa.samples_to_time(end - start)
        buffers.append((data, duration))

    return buffers
Esempio n. 25
0
def formFixedBeatBins(wf, thesnd, limitpeaks=False):
    strsnd = os.path.basename(thesnd).split('.')[0]
    beat_bins = 0
    #amp_peaks, _ = find_peaks(wf, height=0.25, distance=sr/3.0) # get amplitude envelope and return peaks
    amp_peaks, _ = find_peaks(wf, height=0.4, distance=sr /
                              2.0)  # get amplitude envelope and return peaks
    #print 'numpks %r = %r'%(strsnd, len(amp_peaks))
    avg_int_bb = librosa.samples_to_time(np.average(np.diff(amp_peaks)))
    idealperiods[strsnd] = avg_int_bb

    # get the ideal period from the sound file path
    idealperiod = 60. / float(
        os.path.basename(snd).split('.')[0].split('_')[1])
    fixed_bb = [avg_int_bb * i for i in range(len(amp_peaks))]

    # for vweak case, amp env doesn't work very well
    # none of the audio should have more than 15 'beats' therefore if find_peaks
    # returns too many peaks, use the 'idealperiod' to create the fixed beat window array
    if limitpeaks == True:
        if len(amp_peaks) > 15:
            print('too many amp peaks')
            fixed_bb = [idealperiod * i for i in range(14)]

    avg_bpm = 60. / avg_int_bb  # save the avg bpm depending on avg period
    beat_bins = librosa.samples_to_time(amp_peaks)  # convert to samples

    # shift over fixed beat window depending on if its > or < first amplitude env peak
    if fixed_bb[0] < beat_bins[0]:
        fixed_bb += (beat_bins[0] - fixed_bb[0])
    if fixed_bb[0] > beat_bins[0]:
        fixed_bb -= (fixed_bb[0] - beat_bins[0])
    # shift over half window (makes the "GT beat" at 180 deg in phase coherence plots )
    for i in range(1, len(fixed_bb)):
        fixed_bb[i - 1] = fixed_bb[i - 1] + (fixed_bb[i] - fixed_bb[i - 1]) / 2
    fixed_bb[-1] = fixed_bb[-1] + avg_int_bb / 2.  # last in array

    if fixed_bb[0] >= avg_int_bb:
        fixed_bb = np.insert(fixed_bb, 0, fixed_bb[0] - avg_int_bb)
    return fixed_bb, avg_bpm, amp_peaks
Esempio n. 26
0
    def __init__(self,file_path,n_mfcc=12):
        self.file_path = ""

        if kstd.checkPathExist(file_path) == kstd.NORMAL_CODE:
            self.file_path = file_path
            y,sr = librosa.audio.load(file_path)
            
            self.raw_data      = y
            self.sampling_rate = sr
            self.audio_frame   = len(self.raw_data)
            self.recorded_time = librosa.samples_to_time(self.audio_frame,self.sampling_rate)
            self.mfcc          = librosa.feature.mfcc(y,sr=sr,n_mfcc=n_mfcc)
            self.mfcc_length   = self.mfcc.shape[1]
Esempio n. 27
0
    def time_delta(self, start_frame, end_frame):
        """Find the time delta between two given sample indices.

        Find the time delta (in seconds) between two given sample indices. That
        is the difference in time (seconds) between the given sample indices.

        :param int start_frame: The index of the first sample.
        :param int end_frame: The index of the last sample.
        :returns: The difference in seconds between both given samples.
        :rtype: int
        """
        d1, d2 = librosa.samples_to_time(np.array([start_frame, end_frame]),
                                         self.sampling_rate)
        return d2 - d1
def plot_similarity(signal):
    f_fundamental = 523
    phase = 0.57
    t = librosa.samples_to_time(samples=range(len(signal)), sr=sr)
    sin_wave = 0.1 * np.sin(2 * np.pi * (f_fundamental * t - phase))
    plt.figure(figsize=(18, 8))
    plt.plot(t[10000:10400], sin_wave[10000:10400], color="r")
    plt.plot(t[10000:10400], signal[10000:10400], color="y")
    plt.fill_between(t[10000:10400],
                     sin_wave[10000:10400] * signal[10000:10400],
                     color="b")
    plt.xlabel("Time (s)")
    plt.ylabel("Amplitude")
    plt.savefig('result/Similarity.png')
Esempio n. 29
0
def get_single_onsets(filename):
    y, sr = librosa.load(filename)
    #librosa.display.waveplot(y, sr=sr)
    #onset_frames = librosa.onset.onset_detect(y=y, sr=sr)
    onset_frames,onsets_frames_strength = get_onsets_by_all(y,sr)
    onset_times = librosa.frames_to_time(onset_frames, sr=sr)
    #plt.vlines(onset_times, 0, y.max(), color='r', linestyle='--')
    onset_samples = librosa.time_to_samples(onset_times)
    #print(onset_samples)
    #plt.subplot(len(onset_times),1,1)
    #plt.show()
    curr_num = 1
    for i in range(0, len(onset_times)):
        start = onset_samples[i] - sr/2
        if start < 0:
            start =0
        end = onset_samples[i] + sr/2
        #y2 = [x if i> start and i<end else 0 for i,x in enumerate(y)]
        tmp = [x if i > start and i < end else 0 for i, x in enumerate(y)]
        y2 = np.zeros(len(tmp))
        middle = int(len(y2) / 2)
        offset = middle - onset_samples[i]
        for j in range(len(tmp)):
            if tmp[j] > 0:
                y2[j + offset] = tmp[j]
        # y2 = [tmp[i + offset] for i in range(len(tmp)) if tmp[i]>0]
        # y2 = [0.03 if i> start and i<end else 0.02 for i,x in enumerate(y)]
        # y2[int(len(y2) / 2)] = np.max(y)  # 让图片展示归一化
        t = librosa.samples_to_time([middle], sr=sr)
        plt.vlines(t, -1 * np.max(y), np.max(y), color='r', linestyle='--')  # 标出节拍位置
        y2 = np.array(y2)
        #print("len(y2) is {}".format(len(y2)))

        #print("(end - start)*sr is {}".format((end - start)*sr))
        #plt.subplot(len(onset_times),1,i+1)
        #y, sr = librosa.load(filename, offset=2.0, duration=3.0)
        librosa.display.waveplot(y2, sr=sr)
        fig = matplotlib.pyplot.gcf()
        fig.set_size_inches(4, 4)
        if "." in filename:
            Filename = filename.split(".")[0]
        plt.axis('off')
        plt.axes().get_xaxis().set_visible(False)
        plt.axes().get_yaxis().set_visible(False)
        plt.savefig(savepath + str(curr_num) + '.png', bbox_inches='tight', pad_inches=0)
        plt.clf()
        curr_num += 1
    #plt.show()
    return onset_frames,onsets_frames_strength
Esempio n. 30
0
 def calc_onset_values(self, y, sr, hop_length):
     onset_samples = librosa.onset.onset_detect(y,
                                                sr=sr,
                                                units='samples',
                                                hop_length=hop_length,
                                                backtrack=False,
                                                pre_max=20,
                                                post_max=20,
                                                pre_avg=100,
                                                post_avg=100,
                                                delta=0.2,
                                                wait=0)
     onset_boundaries = np.concatenate([[0], onset_samples, [len(y)]])
     onset_times = librosa.samples_to_time(onset_boundaries)
     return onset_samples, onset_boundaries, onset_times
Esempio n. 31
0
def plotwaverform(sound):
    y, sr = lb.load(sound)
    m = lb.samples_to_time(lb.effects.split(y=y, top_db=12))
    print(m)
    plt.figure()
    plt.subplot(2, 1, 1)
    librosa.display.waveplot(y, sr, color='black')
    plt.title("Waveform")
    for line in m:
        plt.axvspan(line[0],
                    line[1],
                    color='green',
                    alpha=0.5,
                    ymin=0.1,
                    ymax=0.9)
    plt.show()
Esempio n. 32
0
    def __test(units, hop_length, y, sr):

        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)

        if units == 'time':
            t2 = b2

        elif units == 'samples':
            t2 = librosa.samples_to_time(b2, sr=sr)

        elif units == 'frames':
            t2 = librosa.frames_to_time(b2, sr=sr, hop_length=hop_length)

        assert np.allclose(t1, t2)
Esempio n. 33
0
    def __test(units, hop_length, y, sr):

        tempo, b1 = librosa.beat.beat_track(y=y, sr=sr, hop_length=hop_length)
        _, b2 = librosa.beat.beat_track(y=y, sr=sr, hop_length=hop_length,
                                        units=units)

        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)

        elif units == 'frames':
            t2 = librosa.frames_to_time(b2, sr=sr, hop_length=hop_length)

        assert np.allclose(t1, t2)
Esempio n. 34
0
def test_beat_units(sr, hop_length, units):

    y, sr = librosa.load(__EXAMPLE_FILE, sr=sr)

    tempo, b1 = librosa.beat.beat_track(y=y, sr=sr, hop_length=hop_length)
    _, b2 = librosa.beat.beat_track(y=y, sr=sr, hop_length=hop_length,
                                    units=units)

    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)

    elif units == 'frames':
        t2 = librosa.frames_to_time(b2, sr=sr, hop_length=hop_length)

    assert np.allclose(t1, t2)
 def __test(sr):
     assert np.allclose(librosa.samples_to_time([0, sr, 2 * sr], sr=sr),
                        [0, 1, 2])