Skip to content

Conversation

@faressc
Copy link

@faressc faressc commented Feb 20, 2025

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::StreamOptions struct 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 RtApiStream gets two new members, bufferSizeCallbackInfo and sampleRateCallbackInfo similar to the existing callbackInfo member. The BufferSizeCallbackInfo and SampleRateCallbackInfo structs are populated when the RtAudio::openStream function is called. The user can specify the respective callback functions pointers (RtAudioBufferSizeCallback or/and an RtAudioSampleRateCallback) and user data pointers (void*) in the RtAudio::StreamOptions struct. The RtAudio::StreamOptions are therefore extended with the following members:

  • bufferSizeCallback of type RtAudioBufferSizeCallback
  • bufferSizeCallbackUserData of type void*
  • sampleRateCallback of type RtAudioSampleRateCallback
  • sampleRateCallbackUserData of type void*

When specified, the respective callback functions are called, when the buffer size or sample rate changes. Additionally, the following internal RtApiStream members are updated when the buffer size changes:

  • bufferSize
  • userBuffer is reallocated with the new buffer size
  • deviceBuffer is reallocated with the new buffer size

When the sample rate changes, only the sampleRate member 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.

@faressc
Copy link
Author

faressc commented Apr 19, 2025

Hello there,

Any feedback would be highly appreciated. I am very open to change the code in order to better fit your guidelines.

Best
Fares

@garyscavone
Copy link
Contributor

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.

@faressc
Copy link
Author

faressc commented Apr 23, 2025

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.

@mattbeghin
Copy link

This would be interesting in CoreAudio (macOS) since when another app opens the same audio device, it might change sample rate and buffer size

@faressc
Copy link
Author

faressc commented Dec 16, 2025

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants