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
Way to create new audio sources #13
Comments
Currently it's not possible to create an audio source on the Rust side. Making it possible would require a lot of work on the wrapper side since it would require making both the Audiosource and AudiosourceInstance class extensible, along with all their public members (via getter/setter functions) and their methods (via function pointers). At the moment, the closest you can get to creating an audio source is through composition using one of the available sources and implementing Deref and DerefMut: struct MyWav {
w: audio::Wav,
some_data: i32,
other_data: usize,
}
impl MyWav {
pub fn new(arg1: i32, arg2: usize) -> MyWav {
let w = audio::Wav::default();
MyWav {
w,
some_data: arg1,
other_data: arg2
}
}
}
impl std::ops::Deref for MyWav {
type Target = audio::Wav;
fn deref(&self) -> &Self::Target {
&self.w
}
}
impl std::ops::DerefMut for MyWav {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.w
}
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
let sl = Soloud::default()?;
let mut wav = MyWav::new(2, 3);
wav.load("someaudio.mp3")?;
sl.play(&*wav); // notice the deref here
// wait
Ok(())
} |
Ouch, I thought it was as simple as making a superclass of |
Yeah, all trait implemented methods actually have to be stored as opaque data in the derived class to be called from the C++ side. |
Would having a non-generic Rust-side struct store the trait object and have it map methods instead work? I'm still wondering if we can simply store function pointers in the C++ side class - that we can just give out the pointers to the non-generic struct's methods. |
Sorry I missed your question. Soloud calls createInstance internally on an audio source, so theses methods have to be passed/stored at the moment of initialization. I currently can’t think of a clean way to do it. It would require initializing a Rust side audio source, storing the necessary methods and then calling the soloud constructor of a derived class which will store these as function pointers. However just initializing the derived class would already create the instance with nulls and soloud doesn’t offer a way to get audio source instances. |
I can't seem to find a way to create new audio sources in the documentation or in the source code. It is possible to do so from the Rust side, like a trait to implement of some sort?
I think this chapter from the SoLoud article is relevant: https://sol.gfxile.net/soloud/newsoundsources.html
The text was updated successfully, but these errors were encountered: