revery-ui / revery

:zap: Native, high-performance, cross-platform desktop apps - built with Reason!
https://www.outrunlabs.com/revery/
MIT License
8.06k stars 196 forks source link

feat(audio): Add Wav File Playback to SDL #1043

Closed jozanza closed 3 years ago

jozanza commented 3 years ago

After seeing it mentioned in the roadmap, this issue, and wanting it for my own personal Revery project, I added some WAV file playback support.

The interface exposed is a fairly straightforward mapping to a subset of SDL's Audio API. If we want to expose more of the SDL Audio API or would like to work on a higher-level Revery Audio API, I'd be happy to help out there.

Example Usage:

open Revery.Sdl2.Audio;

/* 1. Load the WAV file spec and audio buffer */
let (desiredSpec, buf, len) =
  switch (Wav.load("audio.wav")) {
  | Ok(data) => data
  | Error(err) => failwith(err)
  };

/* 2. Open the audio device */
let (device, _obtainedSpec) =
  switch(Device.open_(None, false, desiredSpec, Device.AllowedChanges.none)) {
  | Ok(data) => data
  | Error(err) => failwith(err)
  };

/* 3. Play the audio buffer */
switch (queue(device, buf, len)) {
| Ok(_) => Device.pause(device, false)
| Error(err) => failwith(err)
};

I wasn't exactly sure what the best approach for testing and web support was, so I'd appreciate some direction. Cheers! πŸŽ‰

jozanza commented 3 years ago

@zbaylin Sure! Do you want the example as part of this PR or another one after this is merged?

zbaylin commented 3 years ago

@jozanza I think here, if you don't mind! Let me know if you need any assistance! (you seem to know what you're doing though haha)

bryphe commented 3 years ago

Thanks for the awesome contribution, @jozanza - this looks great! Wanted an Audio API for a while 😍

Just some minor feedback:

jozanza commented 3 years ago

@bryphe while working on the example for wav playback, I wound up adding more SDL2 Audio API bindings. So this PR might need a second pass for review. @zbaylin the example is fairly basic, so if you've got any cool ideas, feel free to jump in and add/change whatever you want. It'd probably be good to at least test it out to make sure it also works properly for other people besides me πŸ˜„

bryphe commented 3 years ago

Example looks great @jozanza - thanks for adding the progress bar / start / end times - very nice 😎

Too bad the gif doesn't have sound... but it works well: 2021-01-15 13 32 30

zbaylin commented 3 years ago

The example is awesome! Thanks @jozanza! Looks like @bryphe approved it so I can merge this in!