Allow adjusting the audio sample rate

This commit is contained in:
Ry 2022-07-27 18:29:32 -07:00
parent 8e4f2bc114
commit 2d6730bb0b
3 changed files with 8 additions and 3 deletions

View File

@ -3,6 +3,7 @@
pub struct Audio { pub struct Audio {
pub current_buffer_is_0: bool, pub current_buffer_is_0: bool,
pub playing: bool, pub playing: bool,
pub sample_rate: u32,
} }
impl Audio { impl Audio {
@ -10,6 +11,7 @@ impl Audio {
Audio { Audio {
current_buffer_is_0: true, current_buffer_is_0: true,
playing: false, playing: false,
sample_rate: 0,
} }
} }
} }

View File

@ -174,6 +174,7 @@ impl Bus {
0x80000600 => { // audio port 0x80000600 => { // audio port
let mut audio_lock = self.audio.lock().unwrap(); let mut audio_lock = self.audio.lock().unwrap();
audio_lock.playing = word != 0; 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 audio_lock.current_buffer_is_0 = false; // the first buffer refill interrupt will invert this to true
} }
0x80001000..=0x80005003 => { // disk controller port 0x80001000..=0x80005003 => { // disk controller port

View File

@ -191,7 +191,7 @@ fn main() {
let (_stream, stream_handle) = OutputStream::try_default().unwrap(); let (_stream, stream_handle) = OutputStream::try_default().unwrap();
let sink = Sink::try_new(&stream_handle).unwrap(); let sink = Sink::try_new(&stream_handle).unwrap();
loop { 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(); let mut audio_lock = audio.lock().unwrap();
if audio_lock.playing { if audio_lock.playing {
let current_buffer: Vec<i16> = if audio_lock.current_buffer_is_0 { let current_buffer: Vec<i16> = if audio_lock.current_buffer_is_0 {
@ -201,11 +201,13 @@ fn main() {
audio_lock.current_buffer_is_0 = true; 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() 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); sink.append(buffer);
drop(audio_lock); drop(audio_lock);
interrupt_sender_audio.send(Interrupt::Request(0xFE)).unwrap(); // audio interrupt, swap audio buffers 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));
} }
} }
} }