Skip to content

Commit

Permalink
Optional Synth + APU Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
IsaacMarovitz committed May 3, 2024
1 parent c0b65f5 commit cfd6aee
Showing 1 changed file with 41 additions and 40 deletions.
81 changes: 41 additions & 40 deletions src/sound/apu.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
#![allow(unused)]
// TODO: Remove this allow

use std::sync::{Arc, Mutex};
use crate::components::memory::Memory;
use crate::sound::prelude::*;
use crate::config::APUConfig;
use bitflags::bitflags;
use cpal::{SampleFormat, Stream};
use cpal::traits::{DeviceTrait, HostTrait};
use crate::CLOCK_FREQUENCY;
use crate::config::{APUConfig, Config};

pub struct APU {
config: APUConfig,
Expand All @@ -17,16 +10,15 @@ pub struct APU {
is_ch_3_on: bool,
is_ch_2_on: bool,
is_ch_1_on: bool,
div_one: bool,
left_volume: u8,
right_volume: u8,
panning: Panning,
ch1: CH1,
ch2: CH2,
ch3: CH3,
ch4: CH4,
synth: Synth,
div_one: bool,
freq: f64
synth: Option<Synth>
}

bitflags! {
Expand All @@ -45,23 +37,28 @@ bitflags! {

impl APU {
pub fn new(config: APUConfig) -> Self {
let synth = if config.master_enabled {
Some(Synth::new())
} else {
None
};

Self {
config,
audio_enabled: true,
is_ch_4_on: false,
is_ch_3_on: false,
is_ch_2_on: false,
is_ch_1_on: true,
div_one: false,
left_volume: 0,
right_volume: 0,
panning: Panning::empty(),
ch1: CH1::new(),
ch2: CH2::new(),
ch3: CH3::new(),
ch4: CH4::new(),
synth: Synth::new(),
div_one: false,
freq: 256.0,
synth
}
}

Expand Down Expand Up @@ -165,36 +162,40 @@ impl APU {
}
};

self.synth.ch1_freq.set_value(131072.0 / (2048.0 - self.ch1.period as f64));
self.synth.ch1_vol.set_value(ch1_vol);
self.synth.ch1_duty.set_value(ch1_duty);
self.synth.ch1_l.set_value(if self.panning.contains(Panning::CH1_LEFT) { 1.0 } else { 0.0 });
self.synth.ch1_r.set_value(if self.panning.contains(Panning::CH1_RIGHT) { 1.0 } else { 0.0 });
match &self.synth {
Some(synth) => {
synth.ch1_freq.set_value(131072.0 / (2048.0 - self.ch1.period as f64));
synth.ch1_vol.set_value(ch1_vol);
synth.ch1_duty.set_value(ch1_duty);
synth.ch1_l.set_value(if self.panning.contains(Panning::CH1_LEFT) { 1.0 } else { 0.0 });
synth.ch1_r.set_value(if self.panning.contains(Panning::CH1_RIGHT) { 1.0 } else { 0.0 });

synth.ch2_freq.set_value(131072.0 / (2048.0 - self.ch2.period as f64));
synth.ch2_vol.set_value(ch2_vol);
synth.ch2_duty.set_value(ch2_duty);
synth.ch2_l.set_value(if self.panning.contains(Panning::CH2_LEFT) { 1.0 } else { 0.0 });
synth.ch2_r.set_value(if self.panning.contains(Panning::CH2_RIGHT) { 1.0 } else { 0.0 });

synth.ch3_freq.set_value(65536.0 / (2048.0 - self.ch3.period as f64));
synth.ch3_vol.set_value(ch3_vol);

for i in 0..ch3_wave.len() {
synth.ch3_wave.set(i, ch3_wave[i]);
}

self.synth.ch2_freq.set_value(131072.0 / (2048.0 - self.ch2.period as f64));
self.synth.ch2_vol.set_value(ch2_vol);
self.synth.ch2_duty.set_value(ch2_duty);
self.synth.ch2_l.set_value(if self.panning.contains(Panning::CH2_LEFT) { 1.0 } else { 0.0 });
self.synth.ch2_r.set_value(if self.panning.contains(Panning::CH2_RIGHT) { 1.0 } else { 0.0 });
synth.ch3_l.set_value(if self.panning.contains(Panning::CH3_LEFT) { 1.0 } else { 0.0 });
synth.ch3_r.set_value(if self.panning.contains(Panning::CH3_RIGHT) { 1.0 } else { 0.0 });

self.synth.ch3_freq.set_value(65536.0 / (2048.0 - self.ch3.period as f64));
self.synth.ch3_vol.set_value(ch3_vol);
synth.ch4_freq.set_value(self.ch4.frequency as f64);
synth.ch4_vol.set_value(ch4_vol);
synth.ch4_l.set_value(if self.panning.contains(Panning::CH4_LEFT) { 1.0 } else { 0.0 });
synth.ch4_r.set_value(if self.panning.contains(Panning::CH4_RIGHT) { 1.0 } else { 0.0 });

for i in 0..ch3_wave.len() {
self.synth.ch3_wave.set(i, ch3_wave[i]);
synth.global_l.set_value(global_l);
synth.global_r.set_value(global_r);
}
_ => {}
}

self.synth.ch3_l.set_value(if self.panning.contains(Panning::CH3_LEFT) { 1.0 } else { 0.0 });
self.synth.ch3_r.set_value(if self.panning.contains(Panning::CH3_RIGHT) { 1.0 } else { 0.0 });

self.synth.ch4_freq.set_value(self.ch4.frequency as f64);
self.synth.ch4_vol.set_value(ch4_vol);
self.synth.ch4_l.set_value(if self.panning.contains(Panning::CH4_LEFT) { 1.0 } else { 0.0 });
self.synth.ch4_r.set_value(if self.panning.contains(Panning::CH4_RIGHT) { 1.0 } else { 0.0 });

self.synth.global_l.set_value(global_l);
self.synth.global_r.set_value(global_r);

}
}

Expand Down

0 comments on commit cfd6aee

Please sign in to comment.