diff --git a/src/apu/envelope.rs b/src/apu/channels/common/envelope.rs similarity index 100% rename from src/apu/envelope.rs rename to src/apu/channels/common/envelope.rs diff --git a/src/apu/length_counter.rs b/src/apu/channels/common/length_counter.rs similarity index 100% rename from src/apu/length_counter.rs rename to src/apu/channels/common/length_counter.rs diff --git a/src/apu/channels/common/mod.rs b/src/apu/channels/common/mod.rs new file mode 100644 index 0000000..96557ba --- /dev/null +++ b/src/apu/channels/common/mod.rs @@ -0,0 +1,26 @@ +mod envelope; +mod length_counter; +mod sequencer; +mod sweep; +mod timer; + +pub use envelope::Envelope; +pub use length_counter::LengthCounter; +pub use sequencer::Sequencer; +pub use sweep::Sweep; +pub use timer::Timer; + +pub trait Channel { + fn write_register(&mut self, address: u16, value: u8); + fn raw_sample(&self) -> u8; + fn is_active(&self) -> bool; + fn is_mute(&self) -> bool; + fn set_enabled(&mut self, value: bool); + + fn get_sample(&self) -> f32 { + match self.is_mute() { + true => 0.0, + false => self.raw_sample() as f32, + } + } +} diff --git a/src/apu/sequencer.rs b/src/apu/channels/common/sequencer.rs similarity index 100% rename from src/apu/sequencer.rs rename to src/apu/channels/common/sequencer.rs diff --git a/src/apu/sweep.rs b/src/apu/channels/common/sweep.rs similarity index 100% rename from src/apu/sweep.rs rename to src/apu/channels/common/sweep.rs diff --git a/src/apu/timer.rs b/src/apu/channels/common/timer.rs similarity index 100% rename from src/apu/timer.rs rename to src/apu/channels/common/timer.rs diff --git a/src/apu/channels/mod.rs b/src/apu/channels/mod.rs index 98d9cc1..95f325a 100644 --- a/src/apu/channels/mod.rs +++ b/src/apu/channels/mod.rs @@ -1,22 +1,9 @@ +mod common; mod noise; mod pulse; mod triangle; +pub use common::Channel; pub use noise::Noise; pub use pulse::Pulse; pub use triangle::Triangle; - -pub trait Channel { - fn write_register(&mut self, address: u16, value: u8); - fn raw_sample(&self) -> u8; - fn is_active(&self) -> bool; - fn is_mute(&self) -> bool; - fn set_enabled(&mut self, value: bool); - - fn get_sample(&self) -> f32 { - match self.is_mute() { - true => 0.0, - false => self.raw_sample() as f32, - } - } -} diff --git a/src/apu/channels/noise.rs b/src/apu/channels/noise.rs index 4c6709c..ad9d609 100644 --- a/src/apu/channels/noise.rs +++ b/src/apu/channels/noise.rs @@ -1,16 +1,11 @@ // https://www.nesdev.org/wiki/APU_Noise use crate::{ - apu::{ - envelope::Envelope, - frame_counter::{ClockFrame, Frame}, - length_counter::LengthCounter, - timer::Timer, - }, + apu::frame_counter::{ClockFrame, Frame}, utils::{BitFlag, Clock}, }; -use super::Channel; +use super::common::{Channel, Envelope, LengthCounter, Timer}; const PERIODS: [u16; 16] = [ 4, 8, 16, 32, 64, 96, 128, 160, 202, 254, 380, 508, 762, 1016, 2034, 4068, diff --git a/src/apu/channels/pulse.rs b/src/apu/channels/pulse.rs index 92c5d52..5844658 100644 --- a/src/apu/channels/pulse.rs +++ b/src/apu/channels/pulse.rs @@ -1,18 +1,11 @@ // https://www.nesdev.org/wiki/APU_Pulse use crate::{ - apu::{ - envelope::Envelope, - frame_counter::{ClockFrame, Frame}, - length_counter::LengthCounter, - sequencer::Sequencer, - sweep::Sweep, - timer::Timer, - }, + apu::frame_counter::{ClockFrame, Frame}, utils::{BitFlag, Clock}, }; -use super::Channel; +use super::common::{Channel, Envelope, LengthCounter, Sequencer, Sweep, Timer}; const WAVEFORMS: [[u8; 8]; 4] = [ [0, 1, 0, 0, 0, 0, 0, 0], diff --git a/src/apu/channels/triangle.rs b/src/apu/channels/triangle.rs index df351a4..c1f68fd 100644 --- a/src/apu/channels/triangle.rs +++ b/src/apu/channels/triangle.rs @@ -1,16 +1,11 @@ // https://www.nesdev.org/wiki/APU_Triangle use crate::{ - apu::{ - frame_counter::{ClockFrame, Frame}, - length_counter::LengthCounter, - sequencer::Sequencer, - timer::Timer, - }, + apu::frame_counter::{ClockFrame, Frame}, utils::{BitFlag, Clock}, }; -use super::Channel; +use super::common::{Channel, LengthCounter, Sequencer, Timer}; #[rustfmt::skip] const WAVEFORMS: [u8; 32] = [ diff --git a/src/apu/mod.rs b/src/apu/mod.rs index 816e538..cfcd5c3 100644 --- a/src/apu/mod.rs +++ b/src/apu/mod.rs @@ -1,12 +1,7 @@ // https://www.nesdev.org/wiki/APU mod channels; -mod envelope; mod frame_counter; -mod length_counter; -mod sequencer; -mod sweep; -mod timer; use channels::{Channel, Noise, Pulse, Triangle}; use frame_counter::{ClockFrame, FrameCounter};