Open tommy-fox opened 4 years ago
I've seen that you suggest creating a custom callback with a stream object as the proper way to handle this
Exactly. Don't use sd.play()
for this.
Also, sd.play()
is not await
able.
Did you have a look at the two asyncio
examples?
Do they help?
asyncio_generators.py shows that you need a thread-safe queue (e.g. Python's queue.Queue
) to get data in the audio callback. An async queue won't work.
I don't know what a "trio memory channel" is, but probably it's not thread-safe?
And does math.inf
specify the channel's capacity?
That's probably not a good idea ... but let's worry about that later ...
Anyway, then you need to write to this thread-safe queue from your coroutine. When the queue is full (because I guess it should have a finite capacity), you'll have to wait a bit before trying again. Normally that's easier when using threads, because then you can simply block on the queue, which won't work in an async context.
If you read overlapping blocks, you will also have to overlap the resulting blocks on playback, right?
Anyway, I strongly suggest to forget all the processing for the moment and just implement block-wise reading from a file and playback of those blocks. Once that works, you can add all the processing you like.
BTW, is using async
an external requirement?
Things might be easier if you just use threads like we did in the good old days.
@tommy-fox Any news? Have you been successful?
Hi @tommy-fox, I currently have the exact same issue/requirement. Did you find a workaround you could share?
Hello! Firstly, thanks for all your work and examples.
I'm working on a project that takes audio from a file, segments it into overlapping blocks, then feeds those blocks through source separation processing, and finally to loud speakers. I'm using the trio library for asynchronous IO and 2 queues: one for feeding audio into the source separation and one to take audio from source separation to playback.
Right now I am looping over the queue that provides audio blocks from the source separation and calling sd.play() with those blocks. This is producing a bit of latency at each call to sd.play(). I've seen that you suggest creating a custom callback with a stream object as the proper way to handle this but I'm having trouble getting it to work in my set up. I'll include some code below that hopefully gives you a better idea of what is happening.
The second to last code block is where I call sd.play() and is the source of my troubles.
Do you have any suggestions as to how I should implement the callback function and start the stream?
Thank you in advance!