def write_midi(filepath, pianorolls, config): is_drums = config['is_drums'] track_names = config['track_names'] tempo = config['tempo'] beat_resolution = config['beat_resolution'] program_nums = config['program_nums'] 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 program_nums is None: program_nums = [0] * len(pianorolls) if is_drums is None: is_drums = [False] * len(pianorolls) multitrack = Multitrack(beat_resolution=beat_resolution, tempo=tempo) for idx in range(pianorolls.shape[2]): if track_names is None: track = Track(pianorolls[..., idx], program_nums[idx], is_drums[idx]) else: track = Track(pianorolls[..., idx], program_nums[idx], is_drums[idx], track_names[idx]) multitrack.append_track(track) multitrack.write(filepath)
def get_merged(multitrack): """Return a `pypianoroll.Multitrack` instance with piano-rolls merged to five tracks (Bass, Drums, Guitar, Piano and Strings)""" category_list = {'Bass': [], 'Drums': [], 'Guitar': [], 'Piano': [], 'Strings': []} program_dict = {'Piano': 0, 'Drums': 0, 'Guitar': 24, 'Bass': 32, 'Strings': 48} multi = Multitrack(tempo=multitrack.tempo, downbeat=multitrack.downbeat, beat_resolution=multitrack.beat_resolution, name=multitrack.name) for idx, track in enumerate(multitrack.tracks): if track.is_drum: category_list['Drums'].append(idx) elif track.program//8 == 0: category_list['Piano'].append(idx) elif track.program//8 == 3: category_list['Guitar'].append(idx) elif track.program//8 == 4: category_list['Bass'].append(idx) else: category_list['Strings'].append(idx) for key in category_list.keys(): is_drum = key == 'Drums' if category_list[key]: merged = multitrack[category_list[key]].get_merged_pianoroll() track = Track(merged, program_dict[key], is_drum=is_drum, name=key) # track.plot() multi.append_track(track) else: track = Track(None, program_dict[key], is_drum=is_drum, name=key) multi.append_track(track) return multi
def notes_to_midi(self, run_folder, output, filename=None): # 배치를 미디로 변환 binarized = output > 0 # 1. 이진화 # 1. reshpae (마디 부분합치기) score = binarized.reshape(-1, binarized.shape[1] * binarized.shape[2], binarized.shape[3], binarized.shape[4]) # 2. 마디 합치기 # 2. pad설정 pad_width = ((0, 0), (0, 0), (24, 20), (0, 0)) # 3. pad적용 score = np.pad(score, pad_width, 'constant') # 3. 패드 적용 # 4. reshape(-1, pitches, Track) score = score.reshape(-1, score.shape[2], score.shape[3]) # 4. reshape ( -1, 피치 수, 트랙) # 5. multitrack 생성 multitrack = Multitrack(beat_resolution=24, tempo=120) # 4.4. Multitrack 생성 ''' for j in range(score.shape[2]) :#4.5. 트랙수만큼 반복한다. track = Track(score[..., j])#4.5.1. Track 생성(painorools[..., idx], program_nums, is_drums) multitrack.append_track(track)#4.5.2. Multitrack 에 추가한다. ''' track = Track(score[..., 0]) multitrack.append_track(track) if run_folder != None: multitrack.write('{}/{}.mid'.format(run_folder, filename)) else: multitrack.write('{}.mid'.format('sample'))
def write_midi(filepath, pianorolls, program_nums=None, is_drums=None, track_names=None, velocity=100, tempo=120.0, beat_resolution=24): """ Write the given piano-roll(s) to a single MIDI file. Arguments --------- filepath : str Path to save the MIDI file. pianorolls : np.array, ndim=3 The piano-roll array to be written to the MIDI file. Shape is (num_timestep, num_pitch, num_track). 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`. is_drums : list of bool Drum indicator(s) to be assigned to the MIDI track(s). True for drums. False for other instruments. Must have the same length as `pianorolls`. track_names : list of str Track name(s) to be assigned to the MIDI track(s). """ 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(beat_resolution=beat_resolution, tempo=tempo) for idx in range(pianorolls.shape[2]): if track_names is None: track = Track(pianorolls[..., idx], program_nums[idx], is_drums[idx]) else: track = Track(pianorolls[..., idx], program_nums[idx], is_drums[idx], track_names[idx]) multitrack.append_track(track) multitrack.write(filepath)
def save_midi(file_path, stacked_piano_rolls, program_nums=None, is_drums=None, track_names=None, tempo=80.0, beat_resolution=24): """ Write the given piano-roll(s) to a single MIDI file. :param file_path: :param stacked_piano_rolls: np.ndarray, shape=(num_time_step, 128, num_track) :param program_nums: :param is_drums: :param track_names: :param tempo: :param beat_resolution: :return: """ # Check arguments # if not np.issubdtype(stacked_piano_rolls.dtype, np.bool_): # raise TypeError("Support only binary-valued piano-rolls") if stacked_piano_rolls.shape[2] != len(program_nums): raise ValueError( "`stacked_piano_rolls.shape[2]` and `program_nums` must have be the same" ) if stacked_piano_rolls.shape[2] != len(is_drums): raise ValueError( "`stacked_piano_rolls.shape[2]` and `is_drums` must have be the same" ) if isinstance(program_nums, int): program_nums = [program_nums] if isinstance(is_drums, int): is_drums = [is_drums] if program_nums is None: program_nums = [0] * len(stacked_piano_rolls) if is_drums is None: is_drums = [False] * len(stacked_piano_rolls) # Write midi multitrack = Multitrack(tempo=tempo, beat_resolution=beat_resolution) for idx in range(stacked_piano_rolls.shape[2]): if track_names is None: track = Track(stacked_piano_rolls[..., idx], program_nums[idx], is_drums[idx]) else: track = Track(stacked_piano_rolls[..., idx], program_nums[idx], is_drums[idx], track_names[idx]) multitrack.append_track(track) multitrack.write(file_path)
def write_midi(filepath, pianorolls, program_nums=None, is_drums=None, track_names=None, velocity=100, tempo=120.0, beat_resolution=24): """ Write the given piano-roll(s) to a single MIDI file. Arguments --------- filepath : str Path to save the MIDI file. pianorolls : np.array, ndim=3 The piano-roll array to be written to the MIDI file. Shape is (num_timestep, num_pitch, num_track). 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`. is_drums : list of bool Drum indicator(s) to be assigned to the MIDI track(s). True for drums. False for other instruments. Must have the same length as `pianorolls`. track_names : list of str Track name(s) to be assigned to the MIDI track(s). """ 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(beat_resolution=beat_resolution, tempo=tempo) for idx in range(pianorolls.shape[2]): if track_names is None: track = Track(pianorolls[..., idx], program_nums[idx], is_drums[idx]) else: track = Track(pianorolls[..., idx], program_nums[idx], is_drums[idx], track_names[idx]) multitrack.append_track(track) multitrack.write(filepath)
def write_midi(filepath, pianorolls, program_nums=None, is_drums=None, track_names=None, velocity=100, tempo=162.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(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 = Track(pianorolls[..., idx], program_nums[idx], is_drums[idx]) else: track = Track(pianorolls[..., idx], program_nums[idx], is_drums[idx], track_names[idx]) multitrack.append_track(track) # plt.savefig(cf.MP3Name) multitrack.write(filepath)