aboutsummaryrefslogtreecommitdiffstats
path: root/src/sounds.rs
blob: 152e7b26f146063a4fd0b0f86ad882bfd857ec97 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
use std::io::Cursor;

use rodio;

use errors::*;

const MODE_ACTIVATED: &'static [u8] = include_bytes!("../sounds/mode_activated.wav");
const MODE_DEACTIVATED: &'static [u8] = include_bytes!("../sounds/mode_deactivated.wav");

pub fn play_mode_activated() -> Result<()> {
    play_audio(MODE_ACTIVATED)
}

pub fn play_mode_deactivated() -> Result<()> {
    play_audio(MODE_DEACTIVATED)
}

fn play_audio<R>(r: R) -> Result<()>
where R: AsRef<[u8]> + Send + 'static {
    let device = rodio::default_output_device()
        .chain_err(|| "could not find an audio output device")?;

    let reader = Cursor::new(r);
    let sink = rodio::play_once(&device, reader)
        .chain_err(|| "error playing audio")?;

    sink.sleep_until_end();
    sink.play();

    Ok(())
}


#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn play_audio_plays_audio() {
        play_mode_activated().unwrap();
        play_mode_deactivated().unwrap();
    }
}