New Feature: Buffer size and sample rate callback functions#454
New Feature: Buffer size and sample rate callback functions#454faressc wants to merge 4 commits intothestk:masterfrom
Conversation
|
Hello there, Any feedback would be highly appreciated. I am very open to change the code in order to better fit your guidelines. Best |
|
Would you consider writing a small test program for the tests directory to test this functionality? The added functionality seems great but could be challenging to test if added incrementally in other APIs. |
|
Thanks for the answer! I'd be happy to provide some test cases for the feature! It will take me a few days as I'm on vacation this week. |
|
This would be interesting in CoreAudio (macOS) since when another app opens the same audio device, it might change sample rate and buffer size |
|
Oh, this has completely escaped my scope! Sorry! Unfortunately, I'm too involved these days to implement the tests. But if you need a more detailed explanation of the suggestion, let me know! I'd also be happy to help with the CoreAudio implementation. |
Some native APIs allow for dynamic buffer size and sample rate changes (i.e. JACK/Pipewire-JACK). This PR adds the ability to set callback functions for buffer size and sample rate changes that occur during stream operation. The callback functions are specified in the
RtAudio::StreamOptionsstruct and are called when the buffer size or sample rate changes. The callback functions are backend agnostic and are implemented for the JACK backend in this PR.Internally the
RtApiStreamgets two new members,bufferSizeCallbackInfoandsampleRateCallbackInfosimilar to the existingcallbackInfomember. TheBufferSizeCallbackInfoandSampleRateCallbackInfostructs are populated when theRtAudio::openStreamfunction is called. The user can specify the respective callback functions pointers (RtAudioBufferSizeCallbackor/and anRtAudioSampleRateCallback) and user data pointers (void*) in theRtAudio::StreamOptionsstruct. TheRtAudio::StreamOptionsare therefore extended with the following members:bufferSizeCallbackof typeRtAudioBufferSizeCallbackbufferSizeCallbackUserDataof typevoid*sampleRateCallbackof typeRtAudioSampleRateCallbacksampleRateCallbackUserDataof typevoid*When specified, the respective callback functions are called, when the buffer size or sample rate changes. Additionally, the following internal
RtApiStreammembers are updated when the buffer size changes:bufferSizeuserBufferis reallocated with the new buffer sizedeviceBufferis reallocated with the new buffer sizeWhen the sample rate changes, only the
sampleRatemember is updated.This is all implemented with the JACK backend, but the changes are designed to be backend agnostic, so that other backends can be extended in the future.
I would appreciate feedback and would be happy to make any necessary changes to get this PR merged.
Cheers,
Fares
PS: I have also added the ability for the JACK backend to force a buffer size on the JACK server with
jack_set_buffer_size()when the stream is opened and the requested buffer size from the stream is different from the current JACK server buffer size. This happens automatically, but a warning is printed to the console. To me this seems better than the current behavior where the buffer size of the JACK server is queried and the stream is simply opened with the JACK server buffer size.