Open mrzapp opened 1 year ago
Thanks for this suggestion!
There is only one thing really wrong with your code: you should always write to the output buffer(s). If you don't have anything to write to them, write zeros. I just tried your code (on Linux) and it worked as expected until playback was finished, but then the last block was repeated indefinitely (because I guess the buffer was simply re-used and still contained the signal of the last block). The annoying thing is that on other systems (and maybe on your system) it might just work, because the last block might not be re-used for some reason.
Other than that, it really depends on your assumptions for this example and what use case you want to illustrate with it.
If the duration of the recording is actually known in advance, it might make more sense to create a buffer of the full length and then write to it (instead of repeatedly using append()
).
However, we might want to pretend that the recording duration is not yet known when starting the recording?
In this case there is still the question whether we only want to use entire blocks of audio.
Because if yes, it might make more sense to store the blocks in a list
(or maybe a collections.deque
) of NumPy arrays?
I think the whole playback_position
thing would only make sense if we would allow the signal to start/stop mid-block.
If you want to add this as an "official" example, feel free to create a PR with it, then we can discuss further details about it.
It would also probably be more interesting (and realistic?) to stop the recording and start playback from the non-audio thread?
This way, the example could show some inter-thread communication which might be helpful for some applications?
It would be nice with an example program that demonstrates in the simplest way possible to record and play audio. Like the
thru_client.py
, but with record/playback implemented.This is how far I got with my limited understanding of both numpy and the python JACK client.
EDIT: This updated example actually seems to work. Is this in your opinion how this should be done? If so, it could be very helpful for newcomers to have it on the project page along with the other examples.