Move sound code to sound.rs
authorBernie Innocenti <bernie@codewiz.org>
Sat, 8 Apr 2017 16:33:31 +0000 (12:33 -0400)
committerBernie Innocenti <bernie@codewiz.org>
Sat, 8 Apr 2017 16:33:31 +0000 (12:33 -0400)
mandelwow.rs
sound.rs [new file with mode: 0644]

index e2de8b3490cdc1e701571558f6c2479e757cbe42..4c5d25e74bdd7fb7098a185287ab57952a74c1ed 100644 (file)
@@ -12,11 +12,8 @@ use glium::index::{IndexBuffer, PrimitiveType};
 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 sound;
 mod support;
 
 #[derive(Copy, Clone)]
@@ -275,51 +272,8 @@ fn mandelwow(display: &glium::Display,
     }
 }
 
-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<XmCallback>,
-}
-
-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();
-    let _sound_player = play_xm(&xm);
+    sound::start();
 
     let display = glium::glutin::WindowBuilder::new()
         //.with_dimensions(1024, 768)
@@ -376,8 +330,8 @@ fn main() {
             [     0.0,  0.0,      z_trans, 1.0f32]
         ];
 
-        // Draw the bounding box before the fractal, when the Z-buffer is still clear, so the lines
-        // behind the semi-translucent areas will be drawn.
+        // Draw the bounding box before the fractal, when the Z-buffer is still clear,
+        // so the lines behind the semi-translucent areas will be drawn.
         if bounding_box_enabled {
             let uniforms = uniform! {
                 model: model,
diff --git a/sound.rs b/sound.rs
new file mode 100644 (file)
index 0000000..80afd5c
--- /dev/null
+++ b/sound.rs
@@ -0,0 +1,57 @@
+#[macro_use]
+
+use libxm::XMContext;
+use sdl2;
+use sdl2::audio::{AudioCallback, AudioDevice, AudioSpecDesired};
+use std::fs::File;
+use std::io::Read;
+
+
+struct XmCallback {
+    xm: XMContext,
+}
+
+impl AudioCallback for XmCallback {
+    type Channel = f32;
+
+    fn callback(&mut self, out: &mut [f32]) {
+        self.xm.generate_samples(out);
+    }
+}
+
+pub struct SoundPlayer {
+    _device: AudioDevice<XmCallback>,
+}
+
+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,
+    }
+}
+
+pub fn start() -> SoundPlayer {
+    let mut xm = Vec::new();
+    let filename = "flora.xm";
+    File::open(filename).unwrap()
+        .read_to_end(&mut xm).unwrap();
+    return play_xm(&xm);
+}