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

How to play a WAV file? #458

Closed
xaviershay opened this issue Oct 4, 2015 · 3 comments
Closed

How to play a WAV file? #458

xaviershay opened this issue Oct 4, 2015 · 3 comments

Comments

@xaviershay
Copy link
Contributor

I found AudioSpecWAV, but none of the audio initialisation methods seem to take it as a type, and it doesn't implement AudioCallback. I tried implementing this myself with my own callback looking something like:

struct MyWav {
    wav: AudioSpecWAV,
    volume: f32,
    pos: usize,
}

impl AudioCallback for MyWav {
    type Channel = f32;

    fn callback(&mut self, out: &mut [f32]) {
        for x in out.iter_mut() {
            *x = match self.wav.buffer().get(self.pos) {
                Some(v) => { self.pos += 1; v as f32 },
                None => { 0.0 }
            }
        }
    }
}

... but I don't know how to work around the following error I get:

the traitcore::marker::Syncis not implemented for the type*mut u8``

@xaviershay
Copy link
Contributor Author

Moved to stackoverflow with a little more detail: http://stackoverflow.com/questions/33057198/how-to-play-a-wav-file-with-rust-sdl2

@nukep
Copy link
Contributor

nukep commented Oct 11, 2015

There's no interface for playing back a WAV file, or any buffer with an audio format determined at runtime. The issue is that AudioCallback defines a fixed type (compile-time), whereas a loaded WAV could be any audio format (run-time).

AudioSpecWAV doesn't currently implement Send or Sync, although it really should, as it's an immutable data type. Using the type in an AudioCallback won't work right now without an unsafe somewhere. :(

I may introduce a PR to allow dynamically specifying an audio format for playback, but for the meanwhile, you can convert the WAV to a target audio format using AudioCVT. I can provide an example when I have the time.

@xaviershay
Copy link
Contributor Author

hrm ok, thanks for the pointers.

xaviershay added a commit to xaviershay/rust-sdl2 that referenced this issue Oct 11, 2015
xaviershay added a commit to xaviershay/rust-sdl2 that referenced this issue Oct 11, 2015
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