From c54226120bdc9277f9ab6e0e54dae9062550702d Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Sun, 28 Oct 2018 23:53:10 +0100 Subject: Add mode activated and deactivated sounds * Remove test "activ.wav" file * Add mode activated and deactivated audio files * Add two new methods to play these two sounds without having to pass in `MODE_ACTIVATED` or `MODE_DEACTIVATED` from outside the module * Make `play_audio()` private The 'rodio' crate supports WAV, FLAC, MP3, and Vorbis file types. Tried using OGG and MP3 versions of the audio, which sound effectively the same played in an audio player, and have much better compression, but 'rodio' kept playing them choppily, in a weird staccato. Maybe the sample rate wasn't right? No idea what the problem was. Didn't really have the same problem with the WAV files. Unfortunate because the OGG files were only 16K, and the MP3 files were 32K, while the WAV files are 348K. Would much rather have smaller size files, but if the sounds play incorrectly, there's no easy way around it. --- sounds/activ.wav | Bin 382190 -> 0 bytes sounds/mode_activated.wav | Bin 0 -> 352844 bytes sounds/mode_deactivated.wav | Bin 0 -> 352844 bytes src/sounds.rs | 17 ++++++++++++----- 4 files changed, 12 insertions(+), 5 deletions(-) delete mode 100644 sounds/activ.wav create mode 100644 sounds/mode_activated.wav create mode 100644 sounds/mode_deactivated.wav diff --git a/sounds/activ.wav b/sounds/activ.wav deleted file mode 100644 index d2a05c9..0000000 Binary files a/sounds/activ.wav and /dev/null differ diff --git a/sounds/mode_activated.wav b/sounds/mode_activated.wav new file mode 100644 index 0000000..0a8cf29 Binary files /dev/null and b/sounds/mode_activated.wav differ diff --git a/sounds/mode_deactivated.wav b/sounds/mode_deactivated.wav new file mode 100644 index 0000000..1cd1c10 Binary files /dev/null and b/sounds/mode_deactivated.wav differ diff --git a/src/sounds.rs b/src/sounds.rs index e0c1093..152e7b2 100644 --- a/src/sounds.rs +++ b/src/sounds.rs @@ -4,12 +4,18 @@ use rodio; use errors::*; -// const MODE_ACTIVATED = include_bytes!("../sounds/mode_activated.ogg"); -// const MODE_DEACTIVATED = include_bytes!("../sounds/mode_deactivated.ogg"); +const MODE_ACTIVATED: &'static [u8] = include_bytes!("../sounds/mode_activated.wav"); +const MODE_DEACTIVATED: &'static [u8] = include_bytes!("../sounds/mode_deactivated.wav"); -pub const MODE_ACTIVATED: &'static [u8] = include_bytes!("../sounds/activ.wav"); +pub fn play_mode_activated() -> Result<()> { + play_audio(MODE_ACTIVATED) +} + +pub fn play_mode_deactivated() -> Result<()> { + play_audio(MODE_DEACTIVATED) +} -pub fn play_audio(r: R) -> Result<()> +fn play_audio(r: R) -> Result<()> where R: AsRef<[u8]> + Send + 'static { let device = rodio::default_output_device() .chain_err(|| "could not find an audio output device")?; @@ -31,6 +37,7 @@ mod tests { #[test] fn play_audio_plays_audio() { - play_audio(MODE_ACTIVATED).unwrap(); + play_mode_activated().unwrap(); + play_mode_deactivated().unwrap(); } } -- cgit v1.2.3