def load_file(root_path):
    # parser the data
    dir_list = os.listdir(root_path)
    dir_list.sort()
    file_name = []
    file_num = []
    for dir in dir_list:
        tmp_name = os.listdir(os.path.join("./raw_classical_music_data",dir))
        tmp_name.sort()
        file_name.append(tmp_name)
        file_num.append(len(tmp_name))
    print("Num of file in each dir : " ,file_num)

    # Parse a MIDI file to a `pypianoroll.Multitrack` instance
    data_list = []
    for i,dir in enumerate(dir_list):
        print("\rPhraser the data : %d/%d" %(i+1,len(dir_list)), end = "")
        for name in file_name[i]:
            file_path = os.path.join('./raw_classical_music_data',dir,name)
            tmp_pypianoroll = Multitrack(file_path)
            tmp_pypianoroll.remove_empty_tracks()
            tmp_pypianoroll.pad_to_multiple(length_Section*length_tab)
            tmp_np = tmp_pypianoroll.get_stacked_pianoroll()
            data_list.append(tmp_np)
    data_list = np.array(data_list)
    #np.save("all_data.npy",data_list)
    print("")
    print("finish praser the data...")
    return data_list
def test():
    tmp_pypianoroll = Multitrack("./mz_545_1.mid")
    print(tmp_pypianoroll.tracks[0])
    tmp_pypianoroll.remove_empty_tracks()
    tmp_np = tmp_pypianoroll.get_stacked_pianoroll()

    tarck1 = tmp_np[:960,:,0]
    tarck2 = tmp_np[:960,:,1]
    tarck1 = tarck1 > 0.1
    tarck2 = tarck2 > 0.1

    tarck1 = Track(tarck1, program=0, is_drum=False, name='unknown')
    tarck2 = Track(tarck2, program=0, is_drum=False, name='unknown')


    cc = Multitrack(tracks=[tarck1,tarck2], tempo=120.0,beat_resolution=24)

    #print(cc)
    cc.write('test_2.mid')
def midi2Pianoroll(filepath,
                   merge=True,
                   velocity=False,
                   ):
    """Convert a MIDI file to a multi-track piano-roll and save the
    resulting multi-track piano-roll to the destination directory. Return a
    tuple of `midi_md5` and useful information extracted from the MIDI file.
    """
    midi_md5 = os.path.splitext(os.path.basename(filepath))[0]
    # prettyMIDI = PrettyMIDI(filepath)
    # time_signiture = ""
    # if len(prettyMIDI.time_signature_changes)>0:
    #     time_signiture = "{}/{}".format(prettyMIDI.time_signature_changes[0].numerator,
    #                                     prettyMIDI.time_signature_changes[0].denominator)
    # if time_signiture == "":
    #     raise ValueError(midi_md5 + " no timesigniture")

    # if time_signiture != CONFIG['time_signatures']:
    #     warnings.warn(midi_md5 + "'s timesiniture is " + time_signiture + " != " + CONFIG['time_signatures'])
    #     return None

    multitrack = Multitrack(filepath, beat_resolution=CONFIG['beat_resolution'], name=midi_md5)
    if merge:
        result = multitrack.get_merged_pianoroll(mode="max")
    else:
        result = multitrack.get_stacked_pianoroll()
        right = None
        left = None
        for each_track in multitrack.tracks:
            if each_track.name.strip().lower() == 'piano right':
                right = each_track.pianoroll
            if each_track.name.strip().lower() == 'piano left':
                left = each_track.pianoroll
        if right is None or left is None:
            return None
        result = np.stack([right, left], axis=2)

    if not velocity:
        result = np.where(result > 0, 1, 0)
    else:
        result = result / CONFIG['velocity_high']
    return result
Esempio n. 4
0
def midiToPianoroll(filepath,
              merge=True,
              velocity=False,
              ):
    """Convert a MIDI file to a multi-track piano-roll and save the
    resulting multi-track piano-roll to the destination directory. Return a
    tuple of `midi_md5` and useful information extracted from the MIDI file.
    """
    midi_md5 = os.path.splitext(os.path.basename(filepath))[0]
    multitrack = Multitrack(filepath, beat_resolution=CONFIG['beat_resolution'], name=midi_md5)
    if merge:
        result = multitrack.get_merged_pianoroll(mode="max")
    else:
        result = multitrack.get_stacked_pianoroll()

    if not velocity:
        result = np.where(result > 0, 1, 0)
    else:
        result = result / CONFIG['velocity_high']
    return result