Beispiel #1
0
def chroma_cqt(y=None, sr=22050, C=None, hop_length=512, fmin=None,
               norm=np.inf, threshold=0.0, tuning=None, n_chroma=12,
               n_octaves=7, window=None, bins_per_octave=None, cqt_mode='full'):

    cqt_func = {'full': cqt, 'hybrid': hybrid_cqt}

    if bins_per_octave is None:
        bins_per_octave = n_chroma

    # Build the CQT if we don't have one already
    if C is None:
        C = np.abs(cqt_func[cqt_mode](y, sr=sr,
                                      hop_length=hop_length,
                                      fmin=fmin,
                                      n_bins=n_octaves * bins_per_octave,
                                      bins_per_octave=bins_per_octave,
                                      tuning=tuning))

    # Map to chroma
    cq_to_chr = filters.cq_to_chroma(C.shape[0],
                                     bins_per_octave=bins_per_octave,
                                     n_chroma=n_chroma,
                                     fmin=fmin,
                                     window=window)
    chroma = cq_to_chr.dot(C)

    if threshold is not None:
        chroma[chroma < threshold] = 0.0

    # Normalize
    if norm is not None:
        chroma = util.normalize(chroma, norm=norm, axis=0)

    return chroma
def fingerprint_chromagram(audio_signal, audio_parameters):
    # Parameter(s)
    sample_rate = audio_parameters['sample_rate']
    time_resolution = audio_parameters['time_resolution']
    cqt_kernel = audio_parameters['cqt_kernel']
    neighborhood_size = audio_parameters['neighborhood_size']
    frequency_resolution = audio_parameters['frequency_resolution']
    bins_per_octave = 24 * frequency_resolution
    minimum_frequency = audio_parameters['minimum_frequency']
    # Transform the signal into a log-scaled spectrogram using the CQT
    audio_spectrogram = spectrogram(audio_signal, sample_rate, time_resolution,
                                    cqt_kernel)

    # Convert the spectrogram to uint8 (to speed up medfilt2) (in Matlab!)
    audio_spectrogram = np.uint8(
        np.around(255 * audio_spectrogram / np.amax(audio_spectrogram)))
    audio_fingerprint = (audio_spectrogram > ndimage.median_filter(
        audio_spectrogram, neighborhood_size, mode='reflect')).astype(float)

    chroma_filter = cq_to_chroma(audio_fingerprint.shape[0],
                                 bins_per_octave=bins_per_octave,
                                 fmin=minimum_frequency)
    chromagram = chroma_filter.dot(audio_fingerprint)

    return chromagram
Beispiel #3
0
def GetChromaFromPianoroll(arr_pianoroll,bass=False):
    chroma_filter = cq_to_chroma(n_input=128,fmin=midi_to_hz(0))
    chroma = np.dot(chroma_filter,arr_pianoroll.T).T.astype("float32")
    chroma[chroma>0] = 1
    if not bass:
        return chroma
    bass_chroma = np.zeros(shape=chroma.shape,dtype="float32")
    for t in range(bass_chroma.shape[0]):
        notes = arr_pianoroll[t,:]
        if np.sum(notes)>0:
            bass = np.min(np.where(notes>0)[0])
            bass_chroma[i,bass%12] = 1
    return np.concatenate([bass_chroma,chroma],axis=1)
Beispiel #4
0
def GetLabelarrFromPianoroll(arr_pianoroll):
    chroma_filter = cq_to_chroma(n_input = 128, fmin = midi_to_hz(0)).T
    chroma = np.dot(arr_pianoroll,chroma_filter).astype("int32")
    
    lab = np.zeros(chroma.shape[0],dtype="int32")
    for i in range(chroma.shape[0]):
        c = chroma[i,:]
        note = arr_pianoroll[i,:]
        bass = np.where(note>0)[0]
        if bass.size>0:
            c[np.min(bass)%12] = 20     #Force bass note to remain in template
        c_argsort = np.argsort(c,kind="mergesort")
        c[c_argsort[:9]] = 0
        lab[i] = _getlabel(c)
            
    return lab
Beispiel #5
0
def GetTemplateChromaFromPianoroll(arr_pianoroll,bass=False):
    chroma_filter = cq_to_chroma(n_input = 128, fmin = midi_to_hz(0)).T
    chroma = np.dot(arr_pianoroll,chroma_filter).astype("int32")
    chroma_template = np.zeros(chroma.shape,dtype="int32")
    bass_template = np.zeros(chroma.shape,dtype="int32")
    for i in range(chroma.shape[0]):
        c = chroma[i,:]
        note = arr_pianoroll[i,:]
        bass = np.where(note>0)[0]
        if bass.size>0:
            if not bass:
                c[np.min(bass)%12] = 20     #Force bass note to remain in template
            bass_template[i,np.min(bass)] = 1
        c_argsort = np.argsort(c,kind="mergesort")
        c[c_argsort[:9]] = 0
        c[c>0] = 1
        chroma_template[i,:] = c
    if bass:
        return np.concatenate([chroma_template,bass_template],axis=1)
    else:        
        return chroma_template
Beispiel #6
0
def GetTargetsFromPianoroll(arr_pianoroll):
    chroma_filter = cq_to_chroma(n_input=128,fmin=midi_to_hz(0))
    T = arr_pianoroll.shape[0]
    t_bass = np.zeros(T,dtype="int32")
    t_top = np.zeros(T,dtype="int32")
    for t in range(T):
        sum_notes = np.sum(arr_pianoroll[t,:])
        if sum_notes==0:
            continue
        notes = np.where(arr_pianoroll[t,:]>0)[0]
        bassnote = np.min(notes)
        topnote = np.max(notes)
        arr_pianoroll[t,bassnote]=0
        arr_pianoroll[t,topnote]=0
        t_bass[t]=bassnote%12
        t_top[t]=topnote%12
    
    chroma = np.dot(chroma_filter,arr_pianoroll.T).T.astype("float32")
    feature = np.zeros((T,12),dtype="float32")
    feature[chroma>=1] = 1.0

    return t_bass,feature,t_top    
Beispiel #7
0
def GetConvnetTargetFromPianoroll(arr_pianoroll):
    chroma_filter = cq_to_chroma(n_input=128,fmin=midi_to_hz(0))
    T = arr_pianoroll.shape[0]
    bass_chroma = np.zeros((T,12),dtype="float32")
    top_chroma = np.zeros((T,12),dtype="float32")
    for t in range(T):
        sum_notes = np.sum(arr_pianoroll[t,:])
        if sum_notes==0:
            continue
        notes = np.where(arr_pianoroll[t,:]>0)[0]
        bassnote = np.min(notes)
        topnote = np.max(notes)
        arr_pianoroll[t,bassnote]=0
        arr_pianoroll[t,topnote]=0
        bass_chroma[t,bassnote%12]=1
        top_chroma[t,topnote%12]=1
    
    chroma = np.dot(chroma_filter,arr_pianoroll.T).T
    feature = np.zeros((T,12),dtype="float32")
    #feature = normalize(chroma,axis=1,norm=np.inf).astype("float32")
    feature[chroma>=1] = 1.0
    feature = np.concatenate([bass_chroma,feature,top_chroma],axis=1)
    return feature
Beispiel #8
0
temp_C = np.array([[1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0],
                   [1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0],
                   [1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0],
                   [1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0],
                   [1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0],
                   [1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0],
                   [1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0],
                   [1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1]])

qualities = ["", "min", "dim", "dim", "aug", "7", "min7", "maj7"]

notes = ["C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"]

mat_templates = np.zeros((12 * len(qualities), 12))

chromafilter = cq_to_chroma(88)

for q in range(len(qualities)):
    for r in range(12):
        mat_templates[12 * q + r, :] = np.roll(temp_C[q, :], r)


def match_chord(segment, thld=0.2):
    """
    Compare the segment note information with chord templates
    """
    bassnote = np.min(np.nonzero(segment > (segment.max() * thld))[0])
    bassnote_chroma = bassnote % 12
    n_notes = segment.size
    chroma = np.dot(chromafilter[:, :n_notes], segment[:, None])
    chroma /= chroma.max()