Compare commits

..

No commits in common. "3fb182b9cb1b5946e88045c51b94b985dc99251e" and "b4b84d0cc53c139d15c8b64e8521b13da40ed990" have entirely different histories.

5 changed files with 24 additions and 18 deletions

View File

@ -11,13 +11,13 @@ mod types;
const WAVE_PATTERN_RAM_LEN: usize = 0x10;
const SAMPLE_RATE: u32 = 44100; // Hz
const SAMPLE_RATE: u32 = 48000; // Hz
const AUDIO_BUFFER_LEN: usize = 512;
const CHANNEL_COUNT: usize = 2;
const SAMPLE_INCREMENT: u64 = SAMPLE_RATE as u64;
#[derive(Default, Debug, Clone)]
pub struct Apu {
pub(crate) struct Apu {
pub(crate) ctrl: SoundControl,
/// Tone & Sweep
pub(crate) ch1: Channel1,
@ -102,7 +102,7 @@ impl Apu {
}
}
pub fn set_audio_sender(&mut self, sender: AudioSender<f32>) {
pub(crate) fn set_audio_src(&mut self, sender: AudioSender<f32>) {
self.sender = Some(sender);
}

View File

@ -1,5 +1,5 @@
use super::{AUDIO_BUFFER_LEN, CHANNEL_COUNT, SAMPLE_RATE};
use crossbeam_channel::{Receiver, SendError, Sender};
use crossbeam_channel::{Receiver, Sender, TrySendError};
use rodio::Source;
use std::collections::VecDeque;
@ -22,8 +22,8 @@ pub struct AudioSender<T> {
}
impl<T> AudioSender<T> {
pub(crate) fn send_samples(&self, left: T, right: T) -> Result<(), SendError<T>> {
self.inner.send(left).and(self.inner.send(right))?;
pub(crate) fn send_samples(&self, left: T, right: T) -> Result<(), TrySendError<T>> {
self.inner.try_send(left).and(self.inner.try_send(right))?;
Ok(())
}
}

View File

@ -1,3 +1,4 @@
use crate::apu::gen::AudioSender;
use crate::apu::Apu;
use crate::cartridge::Cartridge;
use crate::high_ram::HighRam;
@ -15,12 +16,12 @@ const BOOT_ROM_SIZE: usize = 0x100;
pub struct Bus {
boot: Option<[u8; BOOT_ROM_SIZE]>, // Boot ROM is 256b long
cartridge: Option<Cartridge>,
pub(crate) ppu: Ppu,
pub ppu: Ppu,
work_ram: WorkRam,
var_ram: VariableWorkRam,
pub(crate) timer: Timer,
int: Interrupt,
pub(crate) apu: Apu,
apu: Apu,
high_ram: HighRam,
serial: Serial,
pub(crate) joypad: Joypad,
@ -74,6 +75,10 @@ impl Bus {
&mut self.apu
}
pub(crate) fn pass_audio_src(&mut self, sender: AudioSender<f32>) {
self.apu.set_audio_src(sender)
}
pub(crate) fn clock(&mut self) {
self.ppu.clock();
self.timer.clock();

View File

@ -1,4 +1,4 @@
use crate::apu::Apu;
use crate::apu::gen::AudioSender;
use crate::bus::{Bus, BusIo};
use crate::instruction::{Cycle, Instruction};
use crate::interrupt::{InterruptEnable, InterruptFlag};
@ -45,6 +45,10 @@ impl Cpu {
})
}
pub fn set_audio_src(&mut self, sender: AudioSender<f32>) {
self.bus.pass_audio_src(sender)
}
pub(crate) fn ime(&self) -> ImeState {
self.ime
}
@ -170,10 +174,6 @@ impl Cpu {
&self.bus.ppu
}
pub fn apu_mut(&mut self) -> &mut Apu {
&mut self.bus.apu
}
pub(crate) fn joypad_mut(&mut self) -> &mut Joypad {
&mut self.bus.joypad
}

View File

@ -3,7 +3,7 @@ use clap::{crate_authors, crate_description, crate_name, crate_version, App, Arg
use gb::{AudioMPSC, Cycle, Egui, GB_HEIGHT, GB_WIDTH};
use gilrs::Gilrs;
use pixels::{Pixels, SurfaceTexture};
use rodio::OutputStream;
use rodio::{OutputStream, Sink};
use std::time::Instant;
use winit::dpi::LogicalSize;
use winit::event::{Event, VirtualKeyCode};
@ -67,15 +67,16 @@ fn main() -> Result<()> {
};
let (send, recv) = AudioMPSC::init();
game_boy.apu_mut().set_audio_sender(send);
game_boy.set_audio_src(send);
// Initialize Audio
let (_stream, stream_handle) = OutputStream::try_default().expect("Initialized Audio");
let sink = Sink::try_new(&stream_handle).expect("Initialize Audio Sink");
std::thread::spawn(move || {
stream_handle
.play_raw(recv)
.expect("Failed to play Audio Source");
sink.append(recv);
sink.sleep_until_end();
});
let mut now = Instant::now();