-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplay.py
65 lines (53 loc) · 2.11 KB
/
play.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
import pyaudio
import math
import scipy.io.wavfile
import numpy as np
class Player:
def __init__(self, sr, buffer_size, input_op, all_output_operators):
self.input_op = input_op
self.all_output_operators = all_output_operators
self.current_offset = 0
self.stream = None
self.buffer_size = buffer_size
self.sr = sr
def callback(self, in_data, frame_count, time_info, flag):
if flag:
print("Playback Error: %i" % flag)
assert(frame_count == self.buffer_size)
self.current_offset += 1
for op in self.all_output_operators:
op.step(self.current_offset + 1)
result = self.input_op[0].output_buffers[self.input_op[1]]
return result.tobytes(), pyaudio.paContinue
def play_non_blocking(self):
pa = pyaudio.PyAudio()
self.stream = pa.open(format=pyaudio.paInt16,
channels=2,
rate=self.sr,
output=True,
frames_per_buffer=self.buffer_size,
stream_callback=self.callback)
# while stream.is_active():
# time.sleep(0.1)
#
# stream.close()
# pa.terminate()
def play(self):
pa = pyaudio.PyAudio()
stream = pa.open(format=pyaudio.paInt16,
channels=2,
rate=self.sr,
output=True)
data, state = self.callback(None, self.buffer_size, 0, None)
while state == pyaudio.paContinue:
stream.write(data)
data, state = self.callback(None, self.buffer_size, 0, None)
stream.close()
pa.terminate()
def save(self, output_file, length):
buffer_count = int(math.ceil(length * self.sr / self.buffer_size))
result = np.array([], dtype='float')
for i in range(buffer_count):
self.input_op[0].step(i + 1)
result = np.array([*result, *self.input_op[0].output_buffers[self.input_op[1]]])
scipy.io.wavfile.write(output_file, self.sr, result)