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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*!
    Minimalist audio lib based on [rodio](https://github.com/tomaka/rodio).
*/

#![deny(missing_docs)]

extern crate rodio;

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

/// 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: Device,
    /// 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>,
}

/// Implementation of Default trait for Audio
impl Default for Audio {
    /// Creates a default instance of Audio
    fn default() -> Self {
        Self::new()
    }
}

impl Audio {
    /// Creates a new instance of the Audio system
    pub fn new() -> Audio {
        Audio {
            endpoint: rodio::default_output_device().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[name];

        match audio {
            &AudioType::Packed(ref audio) => {
                let audio = audio.clone();
                let cursor = Cursor::new(audio);
                self.channels[name].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[name].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[name];

        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[name].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[name].append(decoder.repeat_infinite());
            }
        }
    }
}