示例#1
0
def write_midi(filepath, pianorolls, program_nums=None, is_drums=None,
               track_names=None, velocity=100, tempo=170.0, beat_resolution=16):
    
    #if not os.path.exists(filepath):
    #    os.makedirs(filepath)

    if not np.issubdtype(pianorolls.dtype, np.bool_):
        raise TypeError("Support only binary-valued piano-rolls")
    if isinstance(program_nums, int):
        program_nums = [program_nums]
    if isinstance(is_drums, int):
        is_drums = [is_drums]
    if pianorolls.shape[2] != len(program_nums):
        raise ValueError("`pianorolls` and `program_nums` must have the same"
                         "length")
    if pianorolls.shape[2] != len(is_drums):
        raise ValueError("`pianorolls` and `is_drums` must have the same"
                         "length")
    if program_nums is None:
        program_nums = [0] * len(pianorolls)
    if is_drums is None:
        is_drums = [False] * len(pianorolls)

    multitrack = Multitrack(resolution=beat_resolution, tempo=np.array([tempo])) # beat_resolution=beat_resolution, tempo=tempo
    for idx in range(pianorolls.shape[2]):
        #plt.subplot(10,1,idx+1)
        #plt.imshow(pianorolls[..., idx].T,cmap=plt.cm.binary, interpolation='nearest', aspect='auto')
        if track_names is None:
            track = BinaryTrack(pianoroll=pianorolls[..., idx], program=program_nums[idx],
                          is_drum=is_drums[idx]) # Track(...)
        else:
            track = BinaryTrack(pianoroll=pianorolls[..., idx], program=program_nums[idx],
                          is_drum=is_drums[idx], name=track_names[idx]) # Track(...)
        multitrack.append(track) # multitrack.append_track(track)
    #plt.savefig(cf.MP3Name)
    print(multitrack.tracks)
    multitrack.write(filepath)
示例#2
0
def write_midi(filepath,
               pianorolls,
               program_nums=None,
               is_drums=None,
               track_names=None,
               velocity=100,
               tempo=120.0,
               beat_resolution=4):
    """
    Write the given piano-roll(s) to a single MIDI file.

    Arguments
    ---------
    1.filepath : str
        Path to save the MIDI file.
    2.pianorolls : np.array, ndim=3
        The piano-roll array to be written to the MIDI file. Shape is
        (num_timestep, num_pitch, num_track).
    3.program_nums : int or list of int
        MIDI program number(s) to be assigned to the MIDI track(s). Available
        values are 0 to 127. Must have the same length as `pianorolls`.
    4.is_drums : list of bool
        True for drums. False for other instruments. Must have the same length as
        `pianorolls`.
    5.track_names : str
    """
    if not np.issubdtype(pianorolls.dtype, np.bool_):
        raise TypeError("Support only binary-valued piano-rolls")
    if isinstance(program_nums, int):
        program_nums = [program_nums]
    if isinstance(is_drums, int):
        is_drums = [is_drums]

    if pianorolls.shape[2] != len(program_nums):
        raise ValueError("`pianorolls` and `program_nums` must have the same"
                         "length")
    if pianorolls.shape[2] != len(is_drums):
        raise ValueError("`pianorolls` and `is_drums` must have the same"
                         "length")
    if program_nums is None:
        program_nums = [0] * len(pianorolls)
    if is_drums is None:
        is_drums = [False] * len(pianorolls)

    multitrack = Multitrack(resolution=beat_resolution,
                            tempo=np.array([tempo * 1.0] *
                                           pianorolls.shape[0]))
    for idx in range(pianorolls.shape[2]):
        if track_names is None:
            tmp = pianorolls[..., idx]
            below_pad = (128 - tmp.shape[1]) // 2
            fore_pad = 128 - below_pad - tmp.shape[1]
            tmp = np.pad(tmp, ((0, 0), (below_pad, fore_pad)), "constant")
            track = BinaryTrack(pianoroll=tmp,
                                program=int(program_nums[idx]),
                                is_drum=is_drums[idx])

        else:
            track = BinaryTrack(pianoroll=pianorolls[:, :, idx],
                                program=program_nums[idx],
                                is_drum=is_drums[idx],
                                name=track_names[idx])
        multitrack.append(track)
    multitrack.write(filepath)
    return multitrack