Skip to content

Commit

Permalink
Feed cpal data
Browse files Browse the repository at this point in the history
  • Loading branch information
IsaacMarovitz committed Feb 9, 2024
1 parent eead095 commit 50c17b4
Showing 1 changed file with 32 additions and 2 deletions.
34 changes: 32 additions & 2 deletions src/sound/apu.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![allow(unused)]
// TODO: Remove this allow

use std::sync::{Arc, Mutex};
use crate::components::memory::Memory;
use crate::sound::prelude::*;
use crate::blip::buffer::BlipBuf;
Expand Down Expand Up @@ -47,7 +48,9 @@ pub struct APU {
ch4: CH4,
div_one: bool,
freq: f64,
sample_rate: u32,
blip: Blip,
buffer: Arc<Mutex<Vec<(f32, f32)>>>,
stream: Stream
}

Expand Down Expand Up @@ -79,6 +82,8 @@ impl APU {
blip_buf.set_rates(CLOCK_FREQUENCY, sample_rate);
let blip = Blip::new(blip_buf);

let buffer = Arc::new(Mutex::new(Vec::new()));

Self {
audio_enabled: true,
is_ch_4_on: false,
Expand All @@ -94,22 +99,36 @@ impl APU {
ch4: CH4::new(),
div_one: false,
freq: 256.0,
sample_rate,
blip,
buffer: buffer.clone(),
stream: match sample_format {
SampleFormat::F32 => {
let buffer_data = buffer.clone();

device.build_output_stream(
&config.config(),
move |data: &mut[f32], _| {

let len = std::cmp::min(data.len() / 2, buffer_data.lock().unwrap().len());
for (i, (data_l, data_r)) in buffer_data.lock().unwrap().drain(..len).enumerate() {
data[i * 2 + 0] = data_l;
data[i * 2 + 1] = data_r;
}
},
move |err| println!("{}", err),
None).unwrap()
}
SampleFormat::F64 => {
let buffer_data = buffer.clone();

device.build_output_stream(
&config.config(),
move |data: &mut[f64], _| {

let len = std::cmp::min(data.len() / 2, buffer_data.lock().unwrap().len());
for (i, (data_l, data_r)) in buffer_data.lock().unwrap().drain(..len).enumerate() {
data[i * 2 + 0] = data_l as f64;
data[i * 2 + 1] = data_r as f64;
}
},
move |err| println!("{}", err),
None).unwrap()
Expand All @@ -119,6 +138,17 @@ impl APU {
}
}

pub fn play(&mut self, l: &[f32], r: &[f32]) {
assert_eq!(l.len(), r.len());
let mut buffer = self.buffer.lock().unwrap();
for (l, r) in l.iter().zip(r) {
if buffer.len() > self.sample_rate as usize {
return;
}
buffer.push((*l, *r));
}
}

pub fn cycle(&mut self, div: u8) {
let mut div_tick = false;

Expand Down

0 comments on commit 50c17b4

Please sign in to comment.