本文整理汇总了Python中mido.MidiTrack方法的典型用法代码示例。如果您正苦于以下问题:Python mido.MidiTrack方法的具体用法?Python mido.MidiTrack怎么用?Python mido.MidiTrack使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mido
的用法示例。
在下文中一共展示了mido.MidiTrack方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: change_tempo
# 需要导入模块: import mido [as 别名]
# 或者: from mido import MidiTrack [as 别名]
def change_tempo(filename, data_path, target_path):
mid = mido.MidiFile(data_path + filename)
new_mid = mido.MidiFile()
new_mid.ticks_per_beat = mid.ticks_per_beat
for track in mid.tracks:
new_track = mido.MidiTrack()
for msg in track:
new_msg = msg.copy()
if new_msg.type == 'set_tempo':
new_msg.tempo = 500000
# if msg.type == 'note_on' or msg.type == 'note_off':
if discretize_time:
print(msg.time)
new_msg.time = myround(msg.time, base=mid.ticks_per_beat/(discritezition/4) )
# msg.time = myround(msg.time, base=mid.ticks_per_beat/(discritezition/4) )
if offset_time:
# print('first:', time)
print((mid.ticks_per_beat/(offset/4)))
new_msg.time = int(msg.time + mid.ticks_per_beat/(offset))
# print('second:', new_time)
# print('diff:',time )
# msg.time = time
new_track.append(new_msg)
new_mid.tracks.append(new_track)
new_mid.save(target_path + filename)
示例2: change_tempo2
# 需要导入模块: import mido [as 别名]
# 或者: from mido import MidiTrack [as 别名]
def change_tempo2(filename, data_path, target_path):
mid = mido.MidiFile(data_path + filename)
new_mid = mido.MidiFile()
new_mid.ticks_per_beat = mid.ticks_per_beat
for track in mid.tracks:
new_track = mido.MidiTrack()
new_mid.tracks.append(new_track)
for msg in track:
if msg.type == 'set_tempo':
print(msg)
msg.tempo = 500000
print(msg)
new_track.append(msg)
new_mid.save(target_path + filename)
示例3: start_recording
# 需要导入模块: import mido [as 别名]
# 或者: from mido import MidiTrack [as 别名]
def start_recording(self):
self.mid = MidiFile(None, None, 0, 20000) #10000 is a ticks_per_beat value
self.track = MidiTrack()
self.mid.tracks.append(self.track)
self.isrecording = True
menu.render_message("Recording started", "", 1000)
self.restart_time()
self.messages_to_save = []
示例4: pianorollToMidi
# 需要导入模块: import mido [as 别名]
# 或者: from mido import MidiTrack [as 别名]
def pianorollToMidi(piano_roll, filepath):
#ensure that resolution is an integer
ticks_per_time_slice=1 # hard-coded, arbitrary but needs to be >= 1 and an integer to avoid distortion
tempo = 1/time_per_time_slice
resolution = 60*ticks_per_time_slice/(tempo*time_per_time_slice)
mid = MidiFile(ticks_per_beat = int(resolution))
track = MidiTrack()
mid.tracks.append(track)
track.append(MetaMessage('set_tempo', tempo = int(MICROSECONDS_PER_MINUTE/tempo), time =0))
current_state = np.zeros(input_dim)
index_of_last_event = 0
for slice_index, time_slice in enumerate(np.concatenate((piano_roll, np.zeros((1, input_dim))), axis =0)):
note_changes = time_slice - current_state
for note_idx, note in enumerate(note_changes):
if note == 1:
note_event = Message('note_on', time = (slice_index - index_of_last_event)*ticks_per_time_slice, velocity = 65, note = note_idx + lowest_note )
track.append(note_event)
index_of_last_event = slice_index
elif note == -1:
note_event = Message('note_off', time = (slice_index - index_of_last_event)*ticks_per_time_slice, velocity = 65, note = note_idx + lowest_note )
track.append(note_event)
index_of_last_event = slice_index
current_state = time_slice
eot = MetaMessage('end_of_track', time=1)
track.append(eot)
mid.save(filepath)
示例5: samples_to_midi
# 需要导入模块: import mido [as 别名]
# 或者: from mido import MidiTrack [as 别名]
def samples_to_midi(samples, file, ticks_per_beat=48, thresh=0.5):
mid = MidiFile()
track = MidiTrack()
mid.tracks.append(track)
track.append(Message('program_change', program=4))
mid.ticks_per_beat = ticks_per_beat
ticks_per_measure = 4 * ticks_per_beat
ticks_per_sample = ticks_per_measure / samples_per_measure
# note_on channel=1 note=44 velocity=127 time=816
# note_off channel=1 note=44 velocity=64 time=24
abs_time = 0
last_time = 0
for sample in samples:
for y in range(sample.shape[0]):
abs_time += ticks_per_sample
for x in range(sample.shape[1]):
note = int(x + (128 - num_notes) / 2)
if sample[y, x] >= thresh and (y == 0 or sample[y - 1, x] < thresh):
delta_time = abs_time - last_time
track.append(Message('note_on', note=note, velocity=int(sample[y,x]*127), time=int(delta_time)))
last_time = abs_time
elif sample[y, x] < thresh and (y == sample.shape[0] - 1 or sample[y - 1, x] > thresh):
delta_time = abs_time - last_time
track.append(Message('note_off', note=note, velocity=int(sample[y,x]*127), time=int(delta_time)))
last_time = abs_time
mid.save(file)
示例6: save_midi
# 需要导入模块: import mido [as 别名]
# 或者: from mido import MidiTrack [as 别名]
def save_midi(path, pitches, intervals, velocities):
"""
Save extracted notes as a MIDI file
Parameters
----------
path: the path to save the MIDI file
pitches: np.ndarray of bin_indices
intervals: list of (onset_index, offset_index)
velocities: list of velocity values
"""
file = MidiFile()
track = MidiTrack()
file.tracks.append(track)
ticks_per_second = file.ticks_per_beat * 2.0
events = []
for i in range(len(pitches)):
events.append(dict(type='on', pitch=pitches[i], time=intervals[i][0], velocity=velocities[i]))
events.append(dict(type='off', pitch=pitches[i], time=intervals[i][1], velocity=velocities[i]))
events.sort(key=lambda row: row['time'])
last_tick = 0
for event in events:
current_tick = int(event['time'] * ticks_per_second)
velocity = int(event['velocity'] * 127)
if velocity > 127:
velocity = 127
pitch = int(round(hz_to_midi(event['pitch'])))
track.append(Message('note_' + event['type'], note=pitch, velocity=velocity, time=current_tick - last_tick))
last_tick = current_tick
file.save(path)
示例7: multi_pianoroll_to_midi
# 需要导入模块: import mido [as 别名]
# 或者: from mido import MidiTrack [as 别名]
def multi_pianoroll_to_midi(file_name, bpm, pianoroll_dic):
# 1.初始化
mid = mido.MidiFile()
tracks = {} # 要保存的音轨信息
first_track = True
midi_tempo = round(60000000 / bpm) # 这首歌的速度(每一拍多少微秒)
# 2.保存音符
for key in pianoroll_dic:
# 2.1.定义音轨名称/使用乐器等
tracks[key] = mido.MidiTrack() # 定义新的音轨
mid.tracks.append(tracks[key]) # 在midi中添加这个音轨
if first_track:
tracks[key].append(mido.MetaMessage('set_tempo', tempo=midi_tempo, time=0)) # 设置歌曲的速度
first_track = False
tracks[key].append(mido.MetaMessage('track_name', name=pianoroll_dic[key]['name'], time=0)) # 这个音轨的名称
tracks[key].append(mido.Message('program_change', program=pianoroll_dic[key]['program'], time=0, channel=key)) # 这个音轨使用的乐器
# 2.2.从piano_dict中获取音符列表并转化为midi message的形式
note_list = []
for note_it in pianoroll_dic[key]['note']:
note_list.append(['on', note_it[0], note_it[1], note_it[2]])
note_list.append(['off', note_it[0] + note_it[3], note_it[1], note_it[2]])
note_list = sorted(note_list, key=lambda item: item[1]) # 按照音符的时间排序
# 2.3.往tracks中保存这些音符
current_note_time = 0
for note_it in note_list:
if note_it[0] == 'on':
tracks[key].append(mido.Message('note_on', note=note_it[2], velocity=note_it[3], time=round(480 * (note_it[1] - current_note_time)), channel=key))
elif note_it[0] == 'off':
tracks[key].append(mido.Message('note_off', note=note_it[2], velocity=note_it[3], time=round(480 * (note_it[1] - current_note_time)), channel=key))
current_note_time = note_it[1]
# 3.保存这个midi文件
mid.save(file_name)