-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsoundplayer.py
43 lines (30 loc) · 936 Bytes
/
soundplayer.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 pyaudio
import alsaaudio
RATE = 44100
CHANNELS = 1
BUFFER = 1024
class Audio(object):
def __init__(self):
self.p = pyaudio.PyAudio()
self.stream = self.p.open(format=pyaudio.paInt16, channels=CHANNELS, rate=RATE, output=True, frames_per_buffer=BUFFER)
def play(self, signal, loop=1):
for i in range(loop):
self.stream.write(signal, len(signal))
#self.stream.close()
#self.p.terminate()
# alsaaudio version
def alsaplay(signal, channels, rate):
#device = alsaaudio.PCM(mode=alsaaudio.PCM_PLAYBACK)
#device = alsaaudio.PCM(type=alsaaudio.PCM_PLAYBACK, mode=alsaaudio.PCM_NORMAL)
device = alsaaudio.PCM(mode=alsaaudio.PCM_NORMAL)
try:
device.setchannels(channels)
device.setrate(rate)
device.setformat(alsaaudio.PCM_FORMAT_S16_LE)
except ValueError:
pass
else:
device.setchannels(CHANNELS)
device.setrate(RATE)
device.setformat(alsaaudio.PCM_FORMAT_S16_LE)
device.write(signal)