我正在尝试实现一个程序来同时触发多个Midi音符。每个音符都在一个单独的线程中触发,还有另一个线程在处理乐句的播放时间。
我正在使用MIDO发送MIDI消息。
# Threading function for triggering multiple notes at the same time
def play_thread(self, note, offset):
thread_handler = handler(self.tempo, self.debug)
# print("printing solves the problem")
time.sleep(240* offset /self.tempo) # Wait until it's time to play
thread_handler.play_note(note)
def clock(self, length):
time.sleep(240 * length / self.tempo)
# Utility method to play phrase
def play(self):
"""Utility method to play Phrase.
:return: Plays Phrase via MIDIHandler
:rtype: None
"""
# work clock:
clock = threading.Thread(target=self.clock, args=(self.length,))
thread_list = []
for note, start in self.phrase.items():
this_thread = threading.Thread(target=self.play_thread, args=(note,
start))
thread_list.append(this_thread)
# start all threads
clock.start()
for thread in thread_list:
thread.start()
# handle endless
if self.endless:
clock.join()
play()
else:
for thread in thread_list:
thread.join()
clock.join()
print("Phrase has stopped playing")