-
Notifications
You must be signed in to change notification settings - Fork 0
/
audio.py
56 lines (43 loc) · 1.43 KB
/
audio.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""A simple sound interface.
There are some problems with module pyaudio on Linux.
This module should be replaced with a better library in the future.
"""
import wave
import thread
import pyaudio
class AudioManager(object):
"""A simple class to interface pyaudio."""
def __init__(self):
self.p = pyaudio.PyAudio()
self.chunk = 1024
def play(self, filename, loop=False):
"""Play a wave track."""
def run():
stream = None
try:
wf = wave.open(filename, 'rb')
# Open stream
p = self.p
stream = p.open(format =
p.get_format_from_width(wf.getsampwidth()),
channels = wf.getnchannels(),
rate = wf.getframerate(),
output = True)
while True:
# Read data
data = wf.readframes(self.chunk)
# Play stream
while data != '':
stream.write(data)
data = wf.readframes(self.chunk)
if loop:
wf.rewind()
else:
break
except Exception:
pass
if stream:
stream.close()
thread.start_new_thread(run, ())