From: Bernie Innocenti Date: Sat, 8 Apr 2017 14:29:56 +0000 (-0400) Subject: Play some music! X-Git-Tag: v0.4.0~9 X-Git-Url: https://codewiz.org/gitweb?p=mandelwow.git;a=commitdiff_plain;h=c08c0c194d16312d4ad6005513d001aab941142b Play some music! --- diff --git a/Cargo.toml b/Cargo.toml index 58e3bbc..cada459 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,6 +9,7 @@ glutin = "*" genmesh = "0.4.1" obj = { version = "0.5", features = ["usegenmesh"] } libxm = "1.0.0" +sdl2 = "*" [[bin]] name = "mandelwow" diff --git a/mandelwow.rs b/mandelwow.rs index f760a26..72e3f86 100644 --- a/mandelwow.rs +++ b/mandelwow.rs @@ -5,6 +5,7 @@ extern crate glium; extern crate glutin; extern crate libxm; +extern crate sdl2; use glium::{DisplayBuild, Surface}; use glium::index::{IndexBuffer, PrimitiveType}; @@ -12,10 +13,10 @@ use glutin::ElementState::Pressed; use glutin::Event::KeyboardInput; use glutin::VirtualKeyCode; use libxm::XMContext; +use sdl2::audio::{AudioCallback, AudioDevice, AudioSpecDesired}; use std::fs::File; use std::io::Read; - mod support; #[derive(Copy, Clone)] @@ -273,15 +274,52 @@ fn mandelwow(display: &glium::Display, mandel(&display, &mut frame, &program, &uniforms, bounds, z0); } } -fn play_xm(raw_xm: &[u8]) { - let freq = 48000u32; - let mut xm = XMContext::new(&raw_xm, freq).unwrap(); + +struct XmCallback { + xm: XMContext, +} + +impl AudioCallback for XmCallback { + type Channel = f32; + + fn callback(&mut self, out: &mut [f32]) { + self.xm.generate_samples(out); + } +} + +struct SoundPlayer { + _device: AudioDevice, +} + +fn play_xm(raw_xm: &[u8]) -> SoundPlayer { + let sdl_context = sdl2::init().unwrap(); + let sdl_audio = sdl_context.audio().unwrap(); + + let sample_rate = 48000u32; + let desired_spec = AudioSpecDesired { + freq: Some(sample_rate as i32), + channels: Some(2u8), + samples: None, + }; + let device = sdl_audio.open_playback(None, &desired_spec, |actual_spec| { + let xm = XMContext::new(&raw_xm, actual_spec.freq as u32).unwrap(); + + XmCallback { + xm: xm, + } + }).unwrap(); + + device.resume(); + + SoundPlayer { + _device: device, + } } fn main() { let mut xm = Vec::new(); File::open("flora.xm").unwrap().read_to_end(&mut xm).unwrap(); - play_xm(&xm); + let _sound_player = play_xm(&xm); let display = glium::glutin::WindowBuilder::new() //.with_dimensions(1024, 768)