-
Notifications
You must be signed in to change notification settings - Fork 0
/
concurrent_play_tabs.py
123 lines (107 loc) · 2.43 KB
/
concurrent_play_tabs.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# Matthew Yaspan
# Rock Band Assignment
import time
import pyglet
import sys
import threading
from pyglet import media
numThreads = 0
threadCount = 0
bootstrap = 0
numThreadsMutex = threading.Semaphore(1)
playMutex = threading.Semaphore(1)
mutex = threading.Semaphore(1)
turnstile_1 = threading.Semaphore(0)
turnstile_2 = threading.Semaphore(1)
def arrive():
global mutex
global threadCount
global turnstile_1
global turnstile_2
global numThreads
mutex.acquire()
threadCount += 1
if threadCount == numThreads:
turnstile_2.acquire()
turnstile_1.release()
mutex.release()
return
def waitToPlay():
global mutex
global threadCount
global turnstile_1
global turnstile_2
global numThreads
turnstile_1.acquire()
turnstile_1.release()
def waitForFinish():
global mutex
global threadCount
global turnstile_1
global turnstile_2
global numThreads
mutex.acquire()
threadCount -= 1
if threadCount == 0:
turnstile_1.acquire()
turnstile_2.release()
mutex.release()
turnstile_2.acquire()
turnstile_2.release()
def playInstrument(audio, pitches):
global numThreads
# arrive()
#waitToPlay()
#waitForFinish()
player = media.Player()
count = [0]
while count[0] < len(pitches) :
arrive()
waitToPlay()
playNote(audio, pitches, count, player)
if count[0] == len(pitches):
numThreadsMutex.acquire()
numThreads -= 1
numThreadsMutex.release()
waitForFinish()
return 0
def playNote( audio, pitches, count, player):
sound = pyglet.media.load(audio, streaming=False)
if pitches[count[0]].isdigit():
pitch = pitches[count[0]]
player.next_source()
player.queue(sound)
player.pitch = 2.0**(float(pitch)/12.0)
#playMutex.acquire()
player.play()
#playMutex.release()
time.sleep(0.2)
#player.pause()
else:
time.sleep(0.2)
count[0] += 1
return
def main(argv):
global threadCount
global numThreads
files = []
tabs = []
players = []
threads = []
tab_file = open(argv[1])
lines = tab_file.readlines()
for line in lines:
temp =line.split(' ')
if len(temp) == 2:
files.append(temp[0])
tabs.append(temp[1])
for i in range(0, len(tabs)):
threads.append(threading.Thread(target=playInstrument,
args=[files[i], tabs[i]]))
numThreads += 1;
for thread in threads:
thread.start()
for thread in threads:
thread.join()
if __name__ == "__main__":
main(sys.argv)