我正在尝试使用 pyaudio 播放流式音频
我正在获取一些数据,对其进行处理,然后将它们放入音频流。
数据速率相当稳定,有一些抖动。
我可以在日志中看到数据速率是恒定的并且处理是实时的。
Demodulation ended after 0.0877389907837 seconds
demodulation tooked 0.0463800430298 seconds
Data is coming with period -0.150208950043 seconds
所以我有 0.150 秒的音频,我在 0.08 秒处理了它。
然后在播放器程序日志中,我看到起初一切正常。实际播放数据的时间几乎等于应有的时间(例如 150 毫秒)。然后在某个时候这个时间减少,我看到这个缓冲区溢出错误。就像数据不能按时到达一样。但正如我在日志中看到的那样,数据的处理仍然是实时的。所以我不知道为什么会这样。
这是我的多处理音频播放器代码。
class MultiprocessedAudioPlayer(object):
def __init__(self, sampling_frequency, min_buffer_size=1, max_buffer_size=10, sample_width=2):
self.p = PyAudio()
self.stream = self.p.open(format=self.p.get_format_from_width(width=sample_width), rate=sampling_frequency,
output=True, channels=1)
self.sub = ZmqSubscriber(host='tcp://localhost', port='8888', on_receive_callback=self.on_frame_received)
self.buffer = deque(maxlen=max_buffer_size)
def on_frame_received(self, frame):
self.play(blosc.unpack_array(frame[0]))
def play(self, frame):
print('started playing frame at {}'.format(datetime.now()))
print('frame length is {}'.format(len(frame)))
self.stream.write(frame, num_frames=len(frame))
print('stopped playing frame at {}'.format(datetime.now()))
def close(self):
self.stream.stop_stream()
self.stream.close()
self.p.terminate()