예제 #1
0
def getStartPoint(input_phrase, match):
    proc = dbt.DBNDownBeatTrackingProcessor(beats_per_bar=[4, 4], fps=100)
    act = dbt.RNNDownBeatProcessor()(input_phrase)

    in_down_index = np.argmin(proc(act)[:, 1])
    #print(proc(act))
    print("input downbeat:{}".format(in_down_index))
    in_down_time = proc(act)[in_down_index, 0] * 1000

    act2 = dbt.RNNDownBeatProcessor()(match)
    match_down_index = np.argmin(proc(act2)[:, 1])
    #print(proc(act2))
    print("match downbeat:{}".format(match_down_index))
    match_down_time = proc(act2)[match_down_index, 0] * 1000

    return in_down_time, match_down_time
예제 #2
0
def get_bars_from_audio(song):
    """
    Returns the bars of a song, directly from its audio signal.
    Encapsulates the downbeat estimator from the madmom toolbox [1].

    Parameters
    ----------
    song : String
        Path to the desired song.

    Returns
    -------
    downbeats_times : list of tuple of float
        List of the estimated bars, as (start, end) times.
        
    References
    ----------
    [1] Böck, S., Korzeniowski, F., Schlüter, J., Krebs, F., & Widmer, G. (2016, October). 
    Madmom: A new python audio and music signal processing library. 
    In Proceedings of the 24th ACM international conference on Multimedia (pp. 1174-1178).

    """
    act = dbt.RNNDownBeatProcessor()(song)
    proc = dbt.DBNDownBeatTrackingProcessor(beats_per_bar=4, fps=100)
    song_beats = proc(act)
    downbeats_times = []
    if song_beats[0][1] != 1:
        downbeats_times.append(0.1)
    for beat in song_beats:
        if beat[1] == 1:
            downbeats_times.append(beat[0])
    mean_bar = np.mean([
        downbeats_times[i + 1] - downbeats_times[i]
        for i in range(len(downbeats_times) - 1)
    ])
    signal_length = act.shape[0] / 100
    while downbeats_times[-1] + 1.1 * mean_bar < signal_length:
        downbeats_times.append(round(downbeats_times[-1] + mean_bar, 2))
    downbeats_times.append(signal_length)
    return frontiers_to_segments(downbeats_times)
def chroma_and_spectral(loop):
    ######## madmom

    # downbeat
    proc = dbt.DBNDownBeatTrackingProcessor(beats_per_bar=[4, 4], fps=100)
    act = dbt.RNNDownBeatProcessor()(loop)
    #print(proc(act)[:,0]) #time v.s. index in a bar
    beat_frames = librosa.time_to_frames(proc(act)[:, 0],
                                         sr=44100,
                                         hop_length=512)

    # to get downbeat index and segment num
    # filter music with few bar
    if np.sum(proc(act)[:, 1] == 1) < 3:
        return False, 0, 0, 0, 0, 0

    downbeat_first = np.argmin(proc(act)[:, 1])
    #bug for [1,5,9....] there is a beat before
    if (beat_frames[0] != 0):
        downbeat_first = downbeat_first + 1
    bar_num = math.floor((len(beat_frames) - downbeat_first) / 4)
    '''
    # chroma
    pcp=ch.CLPChromaProcessor(fps=100)
    chroma=pcp(loop)
    chroma=chroma.T #madmom form is different from librosa (frames,12(madmom)) <-> (12,frames(lib))
    '''
    y, sr = librosa.load(loop, sr=44100)  # sr=sample rate
    y_harmonic, _ = librosa.effects.hpss(y)
    # beatsynchronous chromagrams.
    chroma = librosa.feature.chroma_cqt(y=y_harmonic, sr=sr, n_chroma=12)
    beat_chroma = librosa.util.sync(chroma, beat_frames, aggregate=np.median)
    #tempo
    tempo = get_tempo(loop)

    #spec
    beat_spectral = 0
    '''
    librosa.display.specshow(beat_chroma,x_axis='chroma')
    plt.title('chromagram(madmom)')
    plt.savefig("chroma(madmom).png")
    '''
    '''
    ######## librosa
    y, sr = librosa.load(loop, sr=44100) # sr=sample rate
    #print(sr)
    
    y_harmonic, y_percussive = librosa.effects.hpss(y)
    tempo, beat_frames = librosa.beat.beat_track(y=y_percussive ,sr=sr)
    print(beat_frames.shape,tempo)
    
    # beatsynchronous chromagrams.
    chromagram = librosa.feature.chroma_cqt(y=y_harmonic,sr=sr,n_chroma=12)
    # We'll use the median value of each feature between beat frames  
             
    beat_chroma = librosa.util.sync(chromagram,
                                beat_frames,
                                aggregate=np.median)

    # beatsynchronous spectralgrams.
    #n_mel?????    
    S=librosa.feature.melspectrogram(y=y,sr=sr,n_mels=7)                         
    beat_spectral = librosa.util.sync(S,
                                beat_frames,
                                aggregate=np.median)
    
    
    librosa.display.specshow(beat_spectral,y_axis='mel')
    plt.xlabel('beat')
    #plt.colorbar(format='%+2.0f dB')
    #plt.colorbar()
    plt.title('Spectrogram')
    plt.savefig(loop+"_spectral_12.png")
    
    librosa.display.specshow(beat_chroma,y_axis='chroma')
    plt.xlabel('beat')
    plt.colorbar()
    plt.title('Chromagram')
    plt.savefig(loop+"_chroma_12.png")
    
    librosa.display.specshow(chromagram,y_axis='chroma')
    plt.xlabel('frame')
    plt.title('Chromagram(no_beat_sync)')
    plt.savefig(loop+"_chroma_nosyc.png")
    '''
    return True, beat_chroma, beat_spectral, tempo, downbeat_first, bar_num
                               hop_length=hop_length)
plt.plot(times, librosa.util.normalize(onset_env), label='Onset strength')
plt.vlines(times[beats],
           0,
           1,
           alpha=0.5,
           color='r',
           linestyle='--',
           label='Beats')
print(times[beats])

#### madmom
#proc = BeatTrackingProcessor(fps=100)
#proc = bt.DBNBeatTrackingProcessor(fps=100)
#act = bt.RNNBeatProcessor()(sample2)
proc = dbt.DBNDownBeatTrackingProcessor(beats_per_bar=[4, 4], fps=120)
act = dbt.RNNDownBeatProcessor()(sample)
plt.vlines(proc(act),
           0,
           1,
           alpha=0.5,
           color='g',
           linestyle='--',
           label='Beats_2')
print(proc(act))

plt.legend(frameon=True, framealpha=0.75)
# Limit the plot to a 15-second window
plt.xlim(0, 15)
plt.gca().xaxis.set_major_formatter(librosa.display.TimeFormatter())
plt.tight_layout()