summaryrefslogtreecommitdiff
path: root/examples/player/src/main.rs
blob: c52a06c90f71eb51c80b5751e03ebd500256b867 (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
// 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/>.

extern crate ao_rs as ao;
extern crate wildmidi;

use std::env::args;
use std::mem::transmute;
use std::path::Path;
use std::process::exit;

use ao::{Ao, Device, Driver, Format};
use wildmidi::Player;

fn main() {
    let args: Vec<String> = args().collect();

    if args.len() != 2 {
        println!("usage: {} [MIDI]", args[0]);
        exit(1);
    }

    let path = &args[1];

    if !Path::new(&path).exists() {
        println!("{} does not exist.", path);
        exit(1);
    }

    let player = Player::new(44100).unwrap();
    let mut midi = player.load_file(&path).unwrap();

    let _ao = Ao::new();
    let driver = Driver::new().unwrap();
    let format = Format::new();
    let device = Device::new(&driver, &format, None).unwrap();

    loop {
        // It would simply be too slow to do a safe conversion every time we
        // buffer the PCM output.
        let vec = midi.play(4096);
        let pcm = unsafe {
            transmute::<&[u8], &[i8]>(&vec[..])
        };

        if pcm.len() <= 0 {
            break;
        }

        device.play(&pcm[..] as &[i8]);
    }
}