-
Notifications
You must be signed in to change notification settings - Fork 0
/
crossplatformsound.py
43 lines (37 loc) · 1.26 KB
/
crossplatformsound.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
import threading
from sys import platform
if platform.startswith('win'):
from winsound import PlaySound, SND_FILENAME, SND_ASYNC
else:
from wave import open as waveOpen
from ossaudiodev import open as ossOpen
try:
from ossaudiodev import AFMT_S16_NE
except ImportError:
if byteorder == "little":
AFMT_S16_NE = ossaudiodev.AFMT_S16_LE
else:
AFMT_S16_NE = ossaudiodev.AFMT_S16_BE
class SoundPlayer(object):
def __init__(self):
if platform.find("linux")> -1:
self.dsp = ossOpen('/dev/dsp','w')
def play(self, filename):
thr = threading.Thread(target=self._play, args=(filename,))
thr.run()
def _play(self, filename):
if platform.startswith('win'):
PlaySound(filename, SND_FILENAME|SND_ASYNC)
else:
s = waveOpen(filename,'rb')
(nc,sw,fr,nf,comptype, compname) = s.getparams()
self.dsp.setparameters(AFMT_S16_NE, nc, fr)
data = s.readframes(nf)
s.close()
self.dsp.write(data)
def close(self):
if platform.find("linux") > -1:
self.dsp.close()
if __name__ == "__main__":
x = SoundPlayer()
x.play("sounds/question.wav")