New Feature: Buffer size and sample rate callback functions #454
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.