Reputation: 6121
I'm trying to use pyaudio to play streaming audio that is
I'm getting some data, processing it and then put them to audio stream.
The data rate is pretty constant with some jitter.
I can see in logs that data rate is constant and that the processing is real time.
Demodulation ended after 0.0877389907837 seconds
demodulation tooked 0.0463800430298 seconds
Data is coming with period -0.150208950043 seconds
So I've got 0.150 sec of audio and I've processed it at 0.08 seconds.
Then in player programm logs I see that at first everything is ok. Time of actuall playing of the data is pretty much equal to what it should be (150 ms for example). Then at some point this time decreases and I see this buffer overrun errors. Like it would be if data wouldn't arrive at time. But as I can see in logs the processing of data is still real time. So I don't know why is this happening.
This is my code for multiprocessed audio player.
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()
Upvotes: 1
Views: 1765
Reputation: 78
Your problem sounds similar to one I had playing back using the blocking mode for pyaudio.
My playback also finished before the audio duration could have possibly completed. I suspect that I was supplying audio faster than pyaudio was able to play it back. I never managed to solve the problem in blocking mode.
The way I solved the problem was to use a callback instead of trying to use blocking mode.
An example of how to do that is Here
Upvotes: 1