-
Notifications
You must be signed in to change notification settings - Fork 2
/
midi_song.py
63 lines (62 loc) · 2.17 KB
/
midi_song.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import mido
from mido import MidiFile
import threading
import traceback
class MidiSong:
def __init__(self,filename,output):
self.filename = filename
self.output = output
self.midifile = None
self.midifile = MidiFile(self.filename)
self.playing = threading.Event()
self.playing.clear()
self.stopped = threading.Event()
self.stopped.set()
self.thread = None
self.open()
def open(self):
if self.thread is not None:
self.stop()
self.midifile = MidiFile(self.filename)
def play(self):
#print("1: n:{0},p:{1},s:{2}".format(self.filename,self.playing.is_set(), self.stopped.is_set()))
if self.playing.is_set():
self.pause()
else:
if not self.stopped.is_set():
self.stop()
self.thread = threading.Thread(target=self.runnable)
self.thread.setName("MIDI song: {0}".format(self.filename))
self.thread.start()
#print("2: n:{0},p:{1},s:{2}".format(self.filename,self.playing.is_set(), self.stopped.is_set()))
def pause(self):
if self.playing.is_set():
self.playing.clear()
self.output.reset()
else:
self.playing.set()
def stop(self):
#print("1: n:{0},p:{1},s:{2}".format(self.filename,self.playing.is_set(), self.stopped.is_set()))
self.stopped.set()
if self.thread is not None:
self.playing.set()
self.thread.join()
self.playing.clear()
self.output.reset()
#print("2: n:{0},p:{1},s:{2}".format(self.filename,self.playing.is_set(), self.stopped.is_set()))
def wait(self, timeout=None):
self.stopped.wait(timeout)
def close(self):
self.stop()
#this doesn't work for some reason... #self.midifile.close()
def runnable(self):
self.stopped.clear()
self.playing.set()
for message in self.midifile.play():
self.playing.wait()
self.output.send(message)
if self.stopped.is_set():
break;
self.output.reset()
self.playing.clear()
self.stopped.set()