From 2d6730bb0b9086f2a33dc30baf15496f6c5e9d1e Mon Sep 17 00:00:00 2001 From: Ry Date: Wed, 27 Jul 2022 18:29:32 -0700 Subject: [PATCH] Allow adjusting the audio sample rate --- src/audio.rs | 2 ++ src/bus.rs | 1 + src/main.rs | 8 +++++--- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/audio.rs b/src/audio.rs index 2d3dc9c..cdfca02 100644 --- a/src/audio.rs +++ b/src/audio.rs @@ -3,6 +3,7 @@ pub struct Audio { pub current_buffer_is_0: bool, pub playing: bool, + pub sample_rate: u32, } impl Audio { @@ -10,6 +11,7 @@ impl Audio { Audio { current_buffer_is_0: true, playing: false, + sample_rate: 0, } } } diff --git a/src/bus.rs b/src/bus.rs index bff0891..2f934f9 100644 --- a/src/bus.rs +++ b/src/bus.rs @@ -174,6 +174,7 @@ impl Bus { 0x80000600 => { // audio port let mut audio_lock = self.audio.lock().unwrap(); audio_lock.playing = word != 0; + audio_lock.sample_rate = word; audio_lock.current_buffer_is_0 = false; // the first buffer refill interrupt will invert this to true } 0x80001000..=0x80005003 => { // disk controller port diff --git a/src/main.rs b/src/main.rs index f73955b..f8ef2c1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -191,7 +191,7 @@ fn main() { let (_stream, stream_handle) = OutputStream::try_default().unwrap(); let sink = Sink::try_new(&stream_handle).unwrap(); loop { - // every 500 ms, play what is in the audio buffer and tell fox32 to swap them + // every `sleep` number of ms, play what is in the audio buffer and tell fox32 to swap them let mut audio_lock = audio.lock().unwrap(); if audio_lock.playing { let current_buffer: Vec = if audio_lock.current_buffer_is_0 { @@ -201,11 +201,13 @@ fn main() { audio_lock.current_buffer_is_0 = true; memory_audio.ram()[AUDIO_BUFFER_1_ADDRESS..AUDIO_BUFFER_1_ADDRESS+AUDIO_BUFFER_SIZE].to_vec().chunks_exact(2).map(|x| ((x[1] as i16) << 8) | x[0] as i16).collect() }; - let buffer = SamplesBuffer::new(1, 22050, current_buffer); + let buffer = SamplesBuffer::new(1, audio_lock.sample_rate, current_buffer); + // 1 Hz = 1000 ms + let sleep: f32 = (1000 as f32 / audio_lock.sample_rate as f32) * (AUDIO_BUFFER_SIZE as f32 / 2.0); sink.append(buffer); drop(audio_lock); interrupt_sender_audio.send(Interrupt::Request(0xFE)).unwrap(); // audio interrupt, swap audio buffers - thread::sleep(time::Duration::from_millis(500)); + thread::sleep(time::Duration::from_millis(sleep as u64)); } } }