<div dir="ltr">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 play.<div><br></div><div>Here is an example of a callback function that generates random noise:</div><div><br></div><div><div>def generate_samples(in_data, frame_count, time_info, status_flags):</div><div>    out_data = b""</div><div>    for _ in range(frame_count):</div><div>        out_data += pack('h', int((round(random.random() * VOLRANGE) + MINVOL)))</div><div>    return out_data, pyaudio.paContinue</div></div><div><br></div><div>The only thing I care about is "frame_count" which is the requested number of audio bytes to return.</div><div><br></div><div>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.</div><div><br></div><div>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).</div><div><br></div><div>Any thoughts on how I could do this with Python 3.5's ayncio?</div><div><br></div><div>Thanks!</div><div>Eric</div><div><br></div></div>