/
music.py
68 lines (61 loc) · 1.69 KB
/
music.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
from threading import Thread
from wave import open as waveOpen
from audioop import add
from subprocess import Popen, PIPE
try:
from subprocess import DEVNULL
except ImportError: #Python 2.7
DEVNULL = open("/dev/null")
class Music(Thread):
def __init__(self, what, soundout):
self.what = what
Thread.__init__(self)
if self.what == "None":
return
self.scheduled = None
if soundout == "pulse":
self.soundout = Popen(["pacat", "--latency-msec=1000", "--client=LokoPizza"], stdin=PIPE, stdout=DEVNULL, stderr=DEVNULL)
elif soundout == "alsa":
self.soundout = Popen(["aplay", "-t", "wav", "-f", "cd", "-"], stdin=PIPE, stdout=DEVNULL, stderr=DEVNULL)
def run(self):
if self.what != "None":
bgm = waveOpen("bgm_mouthmoney.wav", "rb")
self.keeprunning = True
reses = []
while(self.keeprunning):
if len(reses) >= 128: #TODO
#Abspielen
self.soundout.stdin.write(b"".join(reses))
reses = []
if self.scheduled and self.what != "bgm":
scheduled = self.scheduled
read0 = bgm.readframes(4)
if not read0:
bgm.rewind()
continue
read1 = scheduled.readframes(4)
if not read1:
self.scheduled = None
reses.append(read0)
continue
try:
reses.append(add(read0, read1, 4))
except:
pass #really?
else:
read = bgm.readframes(256)
if not read:
bgm.rewind()
continue
reses.append(read)
def reset(self):
self.scheduled = None
def play(self, filename):
self.scheduled = waveOpen(filename, "rb")
def explosion(self):
self.play("explosion.wav")
def train(self):
if self.what == "train":
self.play("train.wav")
elif self.what == "NootNoot":
self.play("NootNoot.wav")