mirror of
https://github.com/exoticorn/microw8.git
synced 2026-01-20 11:16:42 +01:00
implement backchannel from audio thread
This commit is contained in:
@@ -91,6 +91,7 @@ impl MicroW8 {
|
||||
struct Uw8Sound {
|
||||
stream: cpal::Stream,
|
||||
tx: mpsc::SyncSender<[u8; 32]>,
|
||||
rx: mpsc::Receiver<[u8; 32]>,
|
||||
}
|
||||
|
||||
impl super::Runtime for MicroW8 {
|
||||
@@ -260,15 +261,27 @@ impl super::Runtime for MicroW8 {
|
||||
};
|
||||
|
||||
let (tx, rx) = mpsc::sync_channel::<[u8; 32]>(1);
|
||||
let (back_tx, back_rx) = mpsc::sync_channel::<[u8; 32]>(2);
|
||||
|
||||
let start_time = Instant::now();
|
||||
|
||||
let mut sample_index = 0;
|
||||
let stream = {
|
||||
device.build_output_stream(
|
||||
&config,
|
||||
move |buffer: &mut [f32], _| {
|
||||
if let Ok(regs) = rx.try_recv() {
|
||||
if let Ok(mut regs) = rx.try_recv() {
|
||||
memory.write(&mut store, 80, ®s).unwrap();
|
||||
memory.read(&mut store, 0x12c80, &mut regs).unwrap();
|
||||
back_tx.send(regs).unwrap();
|
||||
}
|
||||
|
||||
{
|
||||
let time = start_time.elapsed().as_millis() as i32;
|
||||
let mem = memory.data_mut(&mut store);
|
||||
mem[64..68].copy_from_slice(&time.to_le_bytes());
|
||||
}
|
||||
|
||||
for v in buffer {
|
||||
*v = snd.call(&mut store, (sample_index,)).unwrap_or(0.0);
|
||||
sample_index = sample_index.wrapping_add(1);
|
||||
@@ -280,7 +293,11 @@ impl super::Runtime for MicroW8 {
|
||||
)?
|
||||
};
|
||||
|
||||
Uw8Sound { stream, tx }
|
||||
Uw8Sound {
|
||||
stream,
|
||||
tx,
|
||||
rx: back_rx,
|
||||
}
|
||||
};
|
||||
|
||||
sound.stream.play()?;
|
||||
@@ -302,6 +319,13 @@ impl super::Runtime for MicroW8 {
|
||||
fn run_frame(&mut self) -> Result<()> {
|
||||
let mut result = Ok(());
|
||||
if let Some(mut instance) = self.instance.take() {
|
||||
while let Ok(regs) = instance.sound.rx.try_recv() {
|
||||
instance
|
||||
.memory
|
||||
.write(&mut instance.store, 0x12c80, ®s)
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
{
|
||||
let time = instance.start_time.elapsed().as_millis() as i32;
|
||||
let mut gamepad: u32 = 0;
|
||||
|
||||
Reference in New Issue
Block a user