summaryrefslogtreecommitdiff
path: root/src/lib.rs
blob: 436bcbef615aac81c2f653e87b8b9c0b273a0a4e (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
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// Copyright (C) 2018 Jakob L. Kreuze, All Rights Reserved.
//
// This file is part of wildmidi.
//
// wildmidi is free software: you can redistribute it and/or modify it under the
// terms of the GNU Lesser General Public License as published by the Free
// Software Foundation, either version 3 of the License, or (at your option) any
// later version.
//
// wildmidi is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
// A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
// details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with wildmidi. If not, see <http://www.gnu.org/licenses/>.

#[macro_use]
extern crate simple_error;

use std::error::Error;
use std::ffi::CString;
use std::os::raw::{c_char, c_uchar, c_ushort, c_int, c_ulong, c_void};
use std::path::Path;

extern "C" {
    // "Methods" of the Midi player.
    fn WildMidi_Init(cfg: *const c_char, rate: c_ushort, flags: c_ushort) -> c_int;
    fn WildMidi_Open(path: *const c_char) -> *const c_void;
    fn WildMidi_OpenBuffer(data: *const c_uchar, size: c_ulong) -> *const c_void;
    // fn WildMidi_SetOption();
    fn WildMidi_MasterVolume(volume: c_uchar) -> c_int;
    fn WildMidi_Shutdown();

    // "Methods" of the individual Midi handles.
    fn WildMidi_Close(ptr: *const c_void) -> c_int;
    fn WildMidi_FastSeek(ptr: *const c_void, pos: c_ushort) -> c_int;
    fn WildMidi_GetOutput(ptr: *const c_void, buf: *mut c_uchar, len: c_ulong) -> c_int;
}

/// Loader for the Midi format.
pub struct Player;

impl Player {
    fn locate_cfg() -> Option<&'static str> {
        let paths = vec![
            "/etc/wildmidi/wildmidi.cfg",
            "/etc/wildmidi.cfg"
        ];

        for path in paths.iter() {
            if Path::new(path).exists() {
                return Some(path);
            }
        }

        None
    }

    /// Create a new Player with the given sample rate, using the default
    /// configuration file.
    ///
    /// # Errors
    ///
    /// Will fail if 'rate' is not on the interval [11025,65535], or if neither
    /// of the default configuration files exist ('/etc/wildmidi/wildmidi.cfg',
    /// '/etc/wildmidi.cfg').
    pub fn new(rate: u16) -> Result<Player, Box<Error>> {
        let cfg = match Player::locate_cfg() {
            Some(cfg) => cfg,
            None => bail!("No valid configuration file found"),
        };

        Player::with_cfg(cfg, rate)
    }

    /// Create a new Player with the given config path and sample rate.
    ///
    /// # Errors
    ///
    /// Will fail if 'rate' is not on the interval [11025,65535].
    pub fn with_cfg(cfg: &str, rate: u16) -> Result<Player, Box<Error>> {
        let cfg = CString::new(cfg)?;

        unsafe {
            // WildMidi_Shutdown();
            if WildMidi_Init(cfg.as_ptr(), rate, 0) != 0 {
                bail!("Couldn't initialize WildMidi.");
            }
        }

        Ok(Player { })
    }

    // TODO: Document this.
    pub fn volume(&mut self, volume: u8) -> Result<(), Box<Error>> {
        unsafe {
            if WildMidi_MasterVolume(volume) != 0 {
                bail!("Couldn't set volume.");
            }
        }

        Ok(())
    }

    /// Loads a Midi file from memory.
    ///
    /// # Errors
    ///
    /// Will fail if an internal error occurs in WildMidi (such as a parse
    /// error).
    pub fn load(&self, data: &[u8]) -> Result<Midi, Box<Error>> {
        unsafe {
            let len = data.len() as c_ulong;
            let ptr = WildMidi_OpenBuffer(data.as_ptr(), len);

            if !ptr.is_null() {
                return Ok(Midi::new(ptr));
            }
        }

        bail!("Failed to open Midi file.")
    }

    /// Loads a Midi file from disk.
    ///
    /// # Errors
    ///
    /// Will fail if the file does not exist, or if an internal error occurs in
    /// WildMidi (such as a parse error).
    pub fn load_file(&self, path: &str) -> Result<Midi, Box<Error>> {
        if !Path::new(path).exists() {
            bail!("File does not exist");
        }

        let path = CString::new(path)?;

        unsafe {
            let ptr = WildMidi_Open(path.as_ptr());

            if !ptr.is_null() {
                return Ok(Midi::new(ptr));
            }
        }

        bail!("Failed to open Midi file.")
    }
}

impl Drop for Player {
    fn drop(&mut self) {
        unsafe {
            WildMidi_Shutdown();
        }
    }
}

/// An actual Midi file, capable of producing a PCM output.
pub struct Midi {
    ptr: *const c_void,
}

impl Midi {
    fn new(ptr: *const c_void) -> Midi {
        Midi { ptr }
    }

    /// Returns a Vec<u8> containing 'len' bytes of PCM data.
    pub fn play(&mut self, len: usize) -> Vec<u8> {
        let mut vec = vec![0;len];

        unsafe {
            let buf = vec.as_mut_ptr();
            let handle = self.ptr;
            let read = WildMidi_GetOutput(handle, buf, len as c_ulong) as usize;

            if read < len {
                vec.resize(read, 0);
            }
        }

        vec
    }

    // TODO: Document this.
    pub fn seek(&mut self, pos: u32) {
        unsafe {
            // FIXME: Doesn't check return value.
            WildMidi_FastSeek(self.ptr, pos as c_ushort);
        }
    }
}

impl Drop for Midi {
    fn drop(&mut self) {
        unsafe {
            // There isn't much of a point in handling errors here.
            WildMidi_Close(self.ptr);
        }
    }
}

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

    #[test]
    fn create() {
        if let Err(e) = Player::new(44100) {
            panic!("{}", e);
        }
    }

    #[test]
    fn invalid_rate() {
        if let Ok(_) = Player::new(0) {
            panic!("Allowed player to be created with invalid rate.");
        }
    }
}