Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

trait Send for *mut libsamplerate_sys::SRC_STATE_tag #9

Open
TomDeRybel opened this issue Aug 19, 2022 · 1 comment
Open

trait Send for *mut libsamplerate_sys::SRC_STATE_tag #9

TomDeRybel opened this issue Aug 19, 2022 · 1 comment

Comments

@TomDeRybel
Copy link

TomDeRybel commented Aug 19, 2022

Hello,

I'm trying to use rust-samplerate inside a CPAL call-back (which lives in it's own thread). The idea is to have the Samplerate object persist between call-back calls, to enable stream processing.

My use case is that the real-time DSP code I'm working on requires a sample rate that the host does not support, and I was going to SRC the data from/to the host to match the required rate.

However, this doesn't seem possible:

error[E0277]: `*mut libsamplerate_sys::SRC_STATE_tag` cannot be sent between threads safely
   --> src/audio_subsystem/cpal_manager.rs:310:52
    |
310 |         self.input_stream = Some(self.input_device.build_input_stream(
    |                                                    ^^^^^^^^^^^^^^^^^^ `*mut libsamplerate_sys::SRC_STATE_tag` cannot be sent between threads safely
    |
    = help: within `CpalManager`, the trait `Send` is not implemented for `*mut libsamplerate_sys::SRC_STATE_tag`
    = note: required because it appears within the type `Samplerate`

Could the Send trait be added to enable this use case? Or am I doing something silly instead?

Update: I've changed the following in "samplerate.rs", and now things compile. But, I'm very uncertain whether or not this is valid...

pub struct Samplerate {
    ptr: *mut SRC_STATE,
    from_rate: u32,
    to_rate: u32,
}
// TODO: Hack to make this work for the CPAL call-back thread in rci-dsp-test.
//       But, is this VALID?????
unsafe impl Send for Samplerate {}

Thank you

@aloiscochard
Copy link

I faced the same design issue while integrating with jack, I could fix it by not using the callback API and then I could initialize Samplerate directly inside the thread where it will be used.

There is no point sending it from thread to thread, in your case it might work fine but it would be unsafe in general AFAIU.

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

No branches or pull requests

2 participants