Detect instrument hits in music.
authorBernie Innocenti <bernie@codewiz.org>
Sun, 11 Jun 2017 15:07:54 +0000 (11:07 -0400)
committerBernie Innocenti <bernie@codewiz.org>
Sun, 11 Jun 2017 15:07:54 +0000 (11:07 -0400)
main.rs
sound.rs

diff --git a/main.rs b/main.rs
index 49b28b12a4c705159ffafcc57fcf246e092a12a8..9500a78962bd3c411db067a8d491f758f9be3648 100644 (file)
--- a/main.rs
+++ b/main.rs
@@ -70,10 +70,11 @@ pub fn set_main_loop_callback<F>(callback : F) where F : FnMut() -> support::Act
 }
 
 fn main() {
-    let _soundplayer = sound::start();
+    let mut soundplayer = sound::start();
 
     let display = glutin::WindowBuilder::new()
         .with_dimensions(1280, 720)
+        .with_gl_profile(glutin::GlProfile::Core)
         //.with_fullscreen(glutin::get_primary_monitor())
         .with_depth_buffer(24)
         .with_vsync()
@@ -133,6 +134,7 @@ fn main() {
     let mut accum_idle_time = Duration::new(0, 0);
 
     set_main_loop_callback(|| {
+        let _ = sound::hit_event(&mut soundplayer);
         camera.update();
         let perspview = camera.get_perspview();
 
index d88850b1631d03640fc1d8abd7dc5f73f0ddad06..d7dae929fa75cf9df80097d8db954338662f5464 100644 (file)
--- a/sound.rs
+++ b/sound.rs
@@ -5,6 +5,8 @@ use std::fs::File;
 use std::io::Read;
 
 
+const SAMPLE_RATE: i32 = 48000;
+
 struct XmCallback {
     xm: XMContext,
 }
@@ -18,16 +20,15 @@ impl AudioCallback for XmCallback {
 }
 
 pub struct SoundPlayer {
-    _device: Option<AudioDevice<XmCallback>>,
+    device: Option<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),
+        freq: Some(SAMPLE_RATE),
         channels: Some(2u8),
         samples: None,
     };
@@ -42,7 +43,7 @@ fn play_xm(raw_xm: &[u8]) -> SoundPlayer {
     device.resume();
 
     SoundPlayer {
-        _device: Some(device),
+        device: Some(device),
     }
 }
 
@@ -58,5 +59,16 @@ pub fn start() -> SoundPlayer {
             println!("Couldn't open module {}: {:?}", filename, err);
         },
     }
-    return SoundPlayer { _device: None };
+    return SoundPlayer { device: None };
+}
+
+pub fn hit_event(player: &mut SoundPlayer) -> f32 {
+    use std::ops::Deref;
+    let maybe_audio_device = &mut player.device;
+    let audio_device = &mut maybe_audio_device.as_mut().unwrap();
+    let audio_device_lock = audio_device.lock();
+    let xm_callback = audio_device_lock.deref();
+    let xm = &xm_callback.xm;
+    let n_samples = xm.latest_trigger_of_instrument(0x1D);
+    return n_samples as f32 / SAMPLE_RATE as f32;
 }