-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathplayer.py
44 lines (37 loc) · 1.16 KB
/
player.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
import pyaudio
from contextlib import contextmanager
from config import BUFFER_SIZE
class Player(object):
def __init__(
self,
generate_data_callback,
sample_width,
number_of_channels,
sample_rate,
*a,
**k
):
self._generate_data = generate_data_callback
self._sample_width = sample_width
self._number_of_channels = number_of_channels
self._sample_rate = sample_rate
def _get_next_buffer(self, in_data, frame_count, time_info, status):
return (self._generate_data(), pyaudio.paContinue)
@contextmanager
def stream_audio(self):
pya = pyaudio.PyAudio()
audio_stream = pya.open(
format=pyaudio.get_format_from_width(self._sample_width),
channels=self._number_of_channels,
rate=self._sample_rate,
output=True,
stream_callback=self._get_next_buffer,
frames_per_buffer=BUFFER_SIZE,
)
audio_stream.start_stream()
try:
yield audio_stream
finally:
audio_stream.stop_stream()
audio_stream.close()
pya.terminate()