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

Way to create new audio sources #13

Closed
ghost opened this issue Jan 23, 2021 · 5 comments
Closed

Way to create new audio sources #13

ghost opened this issue Jan 23, 2021 · 5 comments

Comments

@ghost
Copy link

ghost commented Jan 23, 2021

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

@MoAlyousef
Copy link
Owner

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(())
}

@ghost
Copy link
Author

ghost commented Jan 23, 2021

Ouch, I thought it was as simple as making a superclass of AudioSource on the C++ side storing a trait object and mapping its methods to the trait's methods.

@MoAlyousef
Copy link
Owner

Yeah, all trait implemented methods actually have to be stored as opaque data in the derived class to be called from the C++ side.

@ghost
Copy link
Author

ghost commented Jan 23, 2021

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.

@MoAlyousef
Copy link
Owner

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.

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

1 participant