More examples
Hi! For the MultiDSP project, I would like to rely on this crate for most things in order to participate and contribute to the Rust audio effort. Looking into how to use ring_buffer to write a simple threaded ALSA loopback: My first goal is to copy the input buffer filled by ALSA capture into the ring buffer in one thread, and read from it to write to ALSA playback in another thread.
However I find difficult to get started. May I ask if the authors could write more examples, especially involving threads? Thanks!
Here is something close to what I am trying to do, from the rb crate: https://docs.rs/rb/0.3.1/rb/struct.SpscRb.html
Sorry it took a while for one of us to get back to you.
The ring buffer you're looking for isn't really in this crate. You probably want something like bounded_spsc_queue, which is pretty much exclusively my solution for sending audio frames from a synth to a gui thread. You could also use channels (in std) but that could run the risk of blocking.
That said, if you want to create a PR to allow the user to split the ring buffer in sample into a Producer and Consumer (similar to bounded_spsc_queue) I'm sure it would be considered as a lighter-weight alternative. bounded_spsc_queue is fast, but relies on a custom allocator and therefore only runs on nightly.
Could you be more specific about the types of examples you'd like to see? This crate is pretty normal as far as Rust goes, in that types just auto-implement Send or Sync, and there's really nothing fancy going on that would warrant threading-specific examples as far as I can tell. But I'd be happy to look at some code if you have questions about getting something to work.
I have a similar problems in writing a realtime streaming server. I get packets in with chunks of audio data that I want to compose together into a continuous signal. However I encounter a couple of (maybe related) problems:
- Making a signal over ringbuffer using a
.drain()iterator, means I'm borrowing the buffer, so once I processed one packet, I must destroy the signal again. The producer consumer pattern you describe above sounds exactly like what I want. - I want to convert the sample rate, however both
ConverterandInterpolatorholds state that are gone once the signal is destroyed. I've resorted to copy-paste the Converter code to be able to be able to extract theInterpolatorinstance as well as theinterpolation_valuebetween creations of theConverter.