From eb261f29d0ae15c2f941c2d84972bd321bf764ac Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Sun, 28 Oct 2018 17:51:39 +0100 Subject: Test of playing an audio file Success! Using the 'rodio' crate to play an audio file. Include the audio file in the binary using `include_bytes!`. This makes it so we get a single self-contained binary. Struggled a bit with getting the reader/array to `Seek`, but finally figured out `Cursor` and got it working. Cool. --- src/lib.rs | 2 ++ src/map.rs | 27 +++++++++++++++++++++++++++ src/sounds.rs | 4 ++++ 3 files changed, 33 insertions(+) create mode 100644 src/sounds.rs (limited to 'src') diff --git a/src/lib.rs b/src/lib.rs index 20f4fb6..f685dfe 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -19,6 +19,7 @@ extern crate magic_crypt; #[macro_use] extern crate quick_error; +extern crate rodio; #[macro_use] extern crate serde_derive; @@ -36,6 +37,7 @@ mod ffi; mod key_code; mod map; mod parser; +mod sounds; mod trial; use parser::{Action, HeadphoneButton, MapAction, MapGroup, MapKind}; diff --git a/src/map.rs b/src/map.rs index 0591db9..750a728 100644 --- a/src/map.rs +++ b/src/map.rs @@ -1,9 +1,13 @@ use std::env; use std::ffi::OsString; +use std::io::BufReader; use std::process::Command; +use rodio::{self, Source}; + use {Action, HeadphoneButton, MapAction, MapKind}; use ffi::State; +use sounds; pub fn run_key_action_for_mode<'a>( state: &mut State, @@ -75,3 +79,26 @@ fn run_action(map_action: &MapAction) { } } +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn play_audio() { + let mode_activated = include_bytes!("../sounds/activ.wav"); + // let mut sound = sounds::MODE_ACTIVATED; + // let mut reader = BufReader::new(sounds::MODE_ACTIVATED); + let file = ::std::fs::File::open("sounds/activ.wav").unwrap(); + // let reader = BufReader::new(&mode_activated[..]); + // let reader = BufReader::new(mode_activated); + // let reader = BufReader::new(file); + let reader = ::std::io::Cursor::new(sounds::MODE_ACTIVATED); +let device = rodio::default_output_device().unwrap(); +// let source = rodio::Decoder::new(reader).unwrap(); +// rodio::play_raw(&device, source.convert_samples()); +// ::std::thread::sleep_ms(2000); +let sink = rodio::play_once(&device, reader).unwrap(); +sink.sleep_until_end(); +sink.play(); + } +} diff --git a/src/sounds.rs b/src/sounds.rs new file mode 100644 index 0000000..8cb6b51 --- /dev/null +++ b/src/sounds.rs @@ -0,0 +1,4 @@ +// const MODE_ACTIVATED = include_bytes!("../sounds/mode_activated.ogg"); +// const MODE_DEACTIVATED = include_bytes!("../sounds/mode_deactivated.ogg"); + +pub const MODE_ACTIVATED: &'static [u8] = include_bytes!("../sounds/activ.wav"); -- cgit v1.2.3