[CentralOH] Async buffer generation

Eric Floehr eric at intellovations.com
Sun Jun 19 11:53:49 EDT 2016

I am using pyaudio to generate real-time audio. Pyaudio allows you to pass
in a callback function so that it can request some amount of audio bytes to

Here is an example of a callback function that generates random noise:

def generate_samples(in_data, frame_count, time_info, status_flags):
    out_data = b""
    for _ in range(frame_count):
        out_data += pack('h', int((round(random.random() * VOLRANGE) +
    return out_data, pyaudio.paContinue

The only thing I care about is "frame_count" which is the requested number
of audio bytes to return.

The generation/creation of those audio bytes is occurring synchronously in
the callback however. What I'd really like to do is generate the audio
bytes asynchronously and then just have the callback just drain however
much of the buffer is requested.

I've been going through Python asyncio tutorials but haven't groked this
use-case (most of the tutorials are network/HTTP/etc. based and I know its
the same concept but it's different enough that I haven't been able to
latch on to how to do it in my case).

Any thoughts on how I could do this with Python 3.5's ayncio?

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/centraloh/attachments/20160619/a1a4fd4f/attachment.html>

More information about the CentralOH mailing list