reorganise

This commit is contained in:
Alex Janka 2023-02-17 09:06:17 +11:00
parent 08da9f7857
commit e60426fe0f
3 changed files with 109 additions and 107 deletions

View file

@ -1,5 +1,7 @@
use std::sync::mpsc::{channel, Sender};
use self::{
channels::EnvelopeMode,
types::{Channels, DacSample, Mixer, VinEnable, Volume},
};
use crate::{
processor::{
memory::{masked_update, Address},
@ -13,10 +15,10 @@ use cpal::{
Device, Stream, StreamConfig,
};
use samplerate::{ConverterType, Samplerate};
use self::channels::{EnvelopeMode, NoiseChannel, PwmChannel, WaveChannel};
use std::sync::mpsc::{channel, Sender};
mod channels;
mod types;
const MEM_START: usize = 0xFF10;
const MEM_SIZE: usize = 0xFF40 - MEM_START;
@ -25,107 +27,6 @@ const fn reg(a: Address) -> usize {
(a as usize) - MEM_START
}
struct Channels {
one: PwmChannel,
two: PwmChannel,
three: WaveChannel,
four: NoiseChannel,
}
impl Default for Channels {
fn default() -> Self {
Self {
one: PwmChannel::new(true),
two: PwmChannel::new(false),
three: WaveChannel::new(false),
four: NoiseChannel::new(false),
}
}
}
#[derive(Default)]
struct VinEnable {
left: bool,
right: bool,
}
enum Volume {
Muted,
Enabled,
}
impl Volume {
fn scale(&self) -> f32 {
match self {
Volume::Muted => 0.,
Volume::Enabled => 1.,
}
}
fn bool(&self) -> bool {
match self {
Volume::Muted => false,
Volume::Enabled => true,
}
}
fn from_bool(val: bool) -> Self {
if val {
Self::Enabled
} else {
Self::Muted
}
}
}
struct ChannelVol {
left: Volume,
right: Volume,
}
impl Default for ChannelVol {
fn default() -> Self {
Self {
left: Volume::Muted,
right: Volume::Muted,
}
}
}
struct Mixer {
vol_left: u8,
vol_right: u8,
ch1: ChannelVol,
ch2: ChannelVol,
ch3: ChannelVol,
ch4: ChannelVol,
}
impl Default for Mixer {
fn default() -> Self {
Self {
vol_left: 7,
vol_right: 7,
ch1: Default::default(),
ch2: Default::default(),
ch3: Default::default(),
ch4: Default::default(),
}
}
}
#[derive(Clone, Copy)]
struct DacSample {
one: f32,
two: f32,
}
impl Default for DacSample {
fn default() -> Self {
Self { one: 0., two: 0. }
}
}
impl DacSample {
fn mixed(&self, mixer: &Mixer) -> [f32; 2] {
let left = (self.one * mixer.ch1.left.scale()) + (self.two * mixer.ch2.left.scale());

View file

@ -0,0 +1,102 @@
use super::channels::{NoiseChannel, PwmChannel, WaveChannel};
pub(super) struct Channels {
pub(super) one: PwmChannel,
pub(super) two: PwmChannel,
pub(super) three: WaveChannel,
pub(super) four: NoiseChannel,
}
impl Default for Channels {
fn default() -> Self {
Self {
one: PwmChannel::new(true),
two: PwmChannel::new(false),
three: WaveChannel::new(false),
four: NoiseChannel::new(false),
}
}
}
#[derive(Default)]
pub(super) struct VinEnable {
pub(super) left: bool,
pub(super) right: bool,
}
pub(super) enum Volume {
Muted,
Enabled,
}
impl Volume {
pub(super) fn scale(&self) -> f32 {
match self {
Volume::Muted => 0.,
Volume::Enabled => 1.,
}
}
pub(super) fn bool(&self) -> bool {
match self {
Volume::Muted => false,
Volume::Enabled => true,
}
}
pub(super) fn from_bool(val: bool) -> Self {
if val {
Self::Enabled
} else {
Self::Muted
}
}
}
pub(super) struct ChannelVol {
pub(super) left: Volume,
pub(super) right: Volume,
}
impl Default for ChannelVol {
fn default() -> Self {
Self {
left: Volume::Muted,
right: Volume::Muted,
}
}
}
pub(super) struct Mixer {
pub(super) vol_left: u8,
pub(super) vol_right: u8,
pub(super) ch1: ChannelVol,
pub(super) ch2: ChannelVol,
pub(super) ch3: ChannelVol,
pub(super) ch4: ChannelVol,
}
impl Default for Mixer {
fn default() -> Self {
Self {
vol_left: 7,
vol_right: 7,
ch1: Default::default(),
ch2: Default::default(),
ch3: Default::default(),
ch4: Default::default(),
}
}
}
#[derive(Clone, Copy)]
pub(super) struct DacSample {
pub(super) one: f32,
pub(super) two: f32,
}
impl Default for DacSample {
fn default() -> Self {
Self { one: 0., two: 0. }
}
}

View file

@ -1,9 +1,8 @@
use std::time::Duration;
use crate::{
processor::Cpu,
util::{get_bit, set_bit},
};
use std::time::Duration;
pub(super) struct Timers {
div_counter: usize,