3 use sdl2::audio::{AudioCallback, AudioDevice, AudioSpecDesired};
12 impl AudioCallback for XmCallback {
15 fn callback(&mut self, out: &mut [f32]) {
16 self.xm.generate_samples(out);
20 pub struct SoundPlayer {
21 _device: Option<AudioDevice<XmCallback>>,
24 fn play_xm(raw_xm: &[u8]) -> SoundPlayer {
25 let sdl_context = sdl2::init().unwrap();
26 let sdl_audio = sdl_context.audio().unwrap();
28 let sample_rate = 48000u32;
29 let desired_spec = AudioSpecDesired {
30 freq: Some(sample_rate as i32),
34 let device = sdl_audio.open_playback(None, &desired_spec, |actual_spec| {
35 let xm = XMContext::new(&raw_xm, actual_spec.freq as u32).unwrap();
45 _device: Some(device),
49 pub fn start() -> SoundPlayer {
50 let filename = "flora.xm";
51 match File::open(filename) {
52 Result::Ok(mut f) => {
53 let mut xm = Vec::new();
54 f.read_to_end(&mut xm).unwrap();
58 println!("Couldn't open module {}: {:?}", filename, err);
61 return SoundPlayer { _device: None };