aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/main.rs32
-rw-r--r--src/music.rs37
2 files changed, 45 insertions, 24 deletions
diff --git a/src/main.rs b/src/main.rs
index 7d42d2f..82ec725 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -4,40 +4,21 @@ mod font;
pub mod generated;
mod interpolation;
+mod music;
mod render;
pub mod vendored;
-use std::io::{BufWriter, Write};
use std::time::{Duration, SystemTime};
+use music::{mmc2r_to_pcm, Music};
use sdl2::{event::Event, keyboard::Keycode};
#[cfg(all(not(feature = "notimeout"), not(feature = "32k")))]
const TIMEOUT_DEATH: f64 = render::JUST_DVD + 20.0;
-use sdl2::audio::{AudioCallback, AudioSpecDesired};
+use sdl2::audio::AudioSpecDesired;
use vendored::micromod::MmC2r;
-struct Music<'a> {
- mm: MmC2r<'a>,
-}
-
-impl<'a> AudioCallback for Music<'a> {
- type Channel = f32;
-
- fn callback(&mut self, out_f: &mut [f32]) {
- let len = out_f.len();
- println!("{len}");
- let mut out = [0; 32768];
- if !self.mm.get_audio(&mut out, len as isize) {
- return;
- }
- for i in 0..len {
- out_f[i] = f32::from(out[i]) / f32::from(0x7FFF as i16);
- }
- }
-}
-
pub fn main() {
let sdl_context = sdl2::init().unwrap();
@@ -45,7 +26,7 @@ pub fn main() {
let desired_spec = AudioSpecDesired {
freq: Some(48_000),
- channels: Some(1),
+ channels: Some(2),
samples: Some(8192 as u16),
};
@@ -53,7 +34,10 @@ pub fn main() {
.open_playback(None, &desired_spec, |spec| {
// initialize the audio callback
Music {
- mm: MmC2r::new(include_bytes!("../uwudhd.mod"), spec.freq as isize).unwrap(),
+ offset: 0,
+ pcm: mmc2r_to_pcm(
+ &mut MmC2r::new(include_bytes!("../uwudhd.mod"), spec.freq as isize).unwrap(),
+ ),
}
})
.unwrap();
diff --git a/src/music.rs b/src/music.rs
new file mode 100644
index 0000000..d44eabf
--- /dev/null
+++ b/src/music.rs
@@ -0,0 +1,37 @@
+use sdl2::audio::AudioCallback;
+
+use crate::vendored::micromod::MmC2r;
+
+pub struct Music {
+ pub pcm: Vec<i16>,
+ pub offset: usize,
+}
+
+impl AudioCallback for Music {
+ type Channel = f32;
+
+ fn callback(&mut self, out_f: &mut [f32]) {
+ let len = out_f.len();
+ pcm_to_f32pcm(&self.pcm[self.offset..(self.offset + len)], out_f, len);
+ self.offset += len;
+ }
+}
+
+pub fn mmc2r_to_pcm(state: &mut MmC2r) -> Vec<i16> {
+ let mut destination = Vec::<i16>::new();
+ loop {
+ let mut out = [0; 4096];
+ if !state.get_audio(&mut out, 2048) {
+ break;
+ }
+ for sample in out {
+ destination.push(sample);
+ }
+ }
+ destination
+}
+pub fn pcm_to_f32pcm(input: &[i16], output: &mut [f32], len: usize) {
+ for i in 0..len {
+ output[i] = f32::from(input[i]) / f32::from(0x7FFF as i16);
+ }
+}