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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
extern crate rodio;

use rodio::{ Endpoint, Sink };
use rodio::Source;
use std::collections::HashMap;
use std::io::{ BufReader, SeekFrom, Seek, Cursor };
use std::fs::File;

/// Enum to denote how the audio is stored
enum AudioType {
    /// The Audio is packed into the binary
    Packed(Vec<u8>),
    /// The Audio is loose on the filesystem
    Loose(File),
}

/// Struct representing the Audio system
pub struct Audio {
    /// The endpoint to play the audio from
    endpoint: Endpoint,
    /// HashMap of available audio clips
    audio: HashMap<&'static str, AudioType>,
    /// HashMap of the playback channels that are associated with audio clips
    channels: HashMap<&'static str, Sink>,
}

impl Audio {
    /// Creates a new instance of the Audio system
    pub fn new() -> Audio {
        Audio {
            endpoint: rodio::get_default_endpoint().unwrap(),
            audio: HashMap::new(),
            channels: HashMap::new(),
        }
    }

    /// Adds a piece of audio to the system
    pub fn add_audio(&mut self, name: &'static str, path: &'static str) {
        let file = File::open(path).unwrap();
        self.audio.insert(name, AudioType::Loose(file));
        self.channels.insert(name, Sink::new(&self.endpoint));
    }

    /// Add audio that is packed into the binary
    pub fn add_packed_audio(&mut self, name: &'static str, bytes: Vec<u8>) {
        self.audio.insert(name, AudioType::Packed(bytes));
        self.channels.insert(name, Sink::new(&self.endpoint));
    }

    /// Plays an piece of Audio that has been loaded into the system
    pub fn play(&mut self, name: &'static str) {
        let audio = self.audio.get(name).unwrap();

        match *audio {
            AudioType::Packed(ref audio) => {
                let audio = audio.clone();
                let cursor = Cursor::new(audio);
                self.channels.get(name).unwrap().append(rodio::Decoder::new(BufReader::new(cursor)).unwrap());
            },
            AudioType::Loose(ref audio) => {
                let mut audio = audio.try_clone().unwrap();
                audio.seek(SeekFrom::Start(0u64)).unwrap();
                self.channels.get(name).unwrap().append(rodio::Decoder::new(BufReader::new(audio)).unwrap());
            },
        }
    }

    /// Loops a piece of Audio that has been loaded into the system
    pub fn repeat(&mut self, name: &'static str) {
        let audio = self.audio.get(name).unwrap();

        match *audio {
            AudioType::Packed(ref audio) => {
                let audio = audio.clone();
                let cursor = Cursor::new(audio);
                let decoder = rodio::Decoder::new(BufReader::new(cursor)).unwrap();
                self.channels.get(name).unwrap().append(decoder.repeat_infinite());
            },
            AudioType::Loose(ref audio) => {
                let mut audio = audio.try_clone().unwrap();
                audio.seek(SeekFrom::Start(0u64)).unwrap();
                let decoder = rodio::Decoder::new(BufReader::new(audio)).unwrap();
                self.channels.get(name).unwrap().append(decoder.repeat_infinite());
            },
        }
    }
}