Exemple #1
0
def get_cpressed_btchroma(path, compression=1):
    """ to easily play with the btchromas we get """
    # MAIN FUNCTION WITH COMPRESSION / STRETCH
    add_loudness = False
    h5 = BAF.GETTERS.open_h5_file_read(path)
    chromas = BAF.GETTERS.get_segments_pitches(h5)
    segstarts = BAF.GETTERS.get_segments_start(h5)
    if compression >= 1:
        btstarts = BAF.GETTERS.get_beats_start(h5)[::compression]
    elif compression == .5:
        btstarts = BAF.GETTERS.get_beats_start(h5)
        interpvals = np.interp(range(1,
                                     len(btstarts) * 2 - 1, 2),
                               range(0,
                                     len(btstarts) * 2, 2), btstarts)
        btstarts = np.array(zip(btstarts, interpvals)).flatten()
    else:
        print 'COMPRESSION OF %d NOT IMPLEMENTED YET' % compression
        raise NotImplementedError
    if add_loudness:  # loudness specific stuff
        btloudmax = BAF.get_btloudnessmax(h5)
        loudnessmax = BAF.GETTERS.get_segments_loudness_max(h5)
    duration = BAF.GETTERS.get_duration(h5)
    h5.close()
    segstarts = np.array(segstarts).flatten()
    btstarts = np.array(btstarts).flatten()
    # add back loudness and aligning
    chromas = chromas.T
    if add_loudness:
        chromas = (chromas) * BAF.idB(loudnessmax)
        #pass
    btchroma = BAF.align_feats(chromas, segstarts, btstarts, duration)
    if btchroma is None:
        return None
    # can we do something with these?
    if add_loudness:
        #btchroma = btchroma * btloudmax
        # normalize
        btchroma = BAF.dB(btchroma + 1e-10)
        btchroma -= btchroma.min()
        #assert not np.isnan(btchroma).any()
        btchroma /= btchroma.max()
        #btchroma = renorm_chroma(btchroma) # EXPERIMENTAL
        # still not correct!
    # Renormalize. Each column max is 1.
    if not add_loudness:
        maxs = btchroma.max(axis=0)
        maxs[np.where(maxs == 0)] = 1.
        btchroma = (btchroma / maxs)
    return btchroma
def get_cpressed_btchroma(path, compression=1):
    """ to easily play with the btchromas we get """
    # MAIN FUNCTION WITH COMPRESSION / STRETCH
    add_loudness = False
    h5 = BAF.GETTERS.open_h5_file_read(path)
    chromas = BAF.GETTERS.get_segments_pitches(h5)
    segstarts = BAF.GETTERS.get_segments_start(h5)
    if compression >= 1:
        btstarts = BAF.GETTERS.get_beats_start(h5)[::compression]
    elif compression == .5:
        btstarts = BAF.GETTERS.get_beats_start(h5)
        interpvals = np.interp(range(1,len(btstarts)*2-1,2),
                               range(0,len(btstarts)*2,2), btstarts)
        btstarts = np.array(zip(btstarts,interpvals)).flatten()
    else:
        print 'COMPRESSION OF %d NOT IMPLEMENTED YET' % compression
        raise NotImplementedError
    if add_loudness: # loudness specific stuff
        btloudmax = BAF.get_btloudnessmax(h5)
        loudnessmax = BAF.GETTERS.get_segments_loudness_max(h5)
    duration = BAF.GETTERS.get_duration(h5)
    h5.close()
    segstarts = np.array(segstarts).flatten()
    btstarts = np.array(btstarts).flatten()
    # add back loudness and aligning
    chromas = chromas.T
    if add_loudness:
        chromas = (chromas) * BAF.idB(loudnessmax)
        #pass
    btchroma = BAF.align_feats(chromas, segstarts, btstarts, duration)
    if btchroma is None:
        return None
    # can we do something with these?
    if add_loudness:
        #btchroma = btchroma * btloudmax
        # normalize
        btchroma = BAF.dB(btchroma+1e-10)
        btchroma -= btchroma.min()
        #assert not np.isnan(btchroma).any()
        btchroma /= btchroma.max()
        #btchroma = renorm_chroma(btchroma) # EXPERIMENTAL
        # still not correct!
    # Renormalize. Each column max is 1.
    if not add_loudness:
        maxs = btchroma.max(axis=0)
        maxs[np.where(maxs == 0)] = 1.
        btchroma = (btchroma / maxs)
    return btchroma
    for filename, start_time, end_time in zip(files, start_times, end_times):
        # Load in MSD hdf5 file
        h5 = hdf5_getters.open_h5_file_read(to_h5_path(filename))
        # Load in beat times from MSD
        beats = hdf5_getters.get_beats_start(h5)
        # Some files have no EN analysis
        if beats.size == 0:
            continue
        # Get indices which fall within the range of correct alignment
        time_mask = np.logical_and(beats > start_time, beats < end_time)
        beats = beats[time_mask]
        # and beat-synchronous feature matrices, within the time range of correct alignment
        chroma = beat_aligned_feats.get_btchromas(h5)[:, time_mask]
        timbre = beat_aligned_feats.get_bttimbre(h5)[:, time_mask]
        loudness = beat_aligned_feats.get_btloudnessmax(h5)[:, time_mask]
        h5.close()
        # Stack it
        msd_features = np.vstack([chroma, timbre, loudness])
        if np.isnan(msd_features).any():
            print filename
            continue
        # Load in pretty midi object
        pm = pretty_midi.PrettyMIDI(midi.read_midifile(to_midi_path(filename)))
        # Construct piano roll, aligned to the msd beat times
        piano_roll = pm.get_piano_roll(times=beats)
        # Ignore notes below 36 and above 84
        piano_roll = piano_roll[36:84, :]
        # Write out
        np.save(os.path.join(output_path, filename.replace('.mp3', '-msd.npy')), msd_features)
        np.save(os.path.join(output_path, filename.replace('.mp3', '-midi.npy')), piano_roll)