From d4407cf8499d761ebf4575a43388f60db70dddb1 Mon Sep 17 00:00:00 2001 From: Rekai Musuka Date: Tue, 3 Aug 2021 19:33:27 -0500 Subject: [PATCH] fix(apu): implement NR50 volume controls --- src/apu.rs | 10 +++++++--- src/apu/types.rs | 18 ++++++++++++++---- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/src/apu.rs b/src/apu.rs index 3416b91..734a48f 100644 --- a/src/apu.rs +++ b/src/apu.rs @@ -154,8 +154,11 @@ impl Apu { let ch4_left = self.ctrl.output.ch4_left() as u8 as f32 * ch4_amplitude; let ch4_right = self.ctrl.output.ch4_right() as u8 as f32 * ch4_amplitude; - let left = (ch1_left + ch2_left + ch3_left + ch4_left) / 4.0; - let right = (ch1_right + ch2_right + ch3_right + ch4_right) / 4.0; + let left_mixed = (ch1_left + ch2_left + ch3_left + ch4_left) / 4.0; + let right_mixed = (ch1_right + ch2_right + ch3_right + ch4_right) / 4.0; + + let left = (self.ctrl.channel.left_volume() + 1.0) * left_mixed; + let right = (self.ctrl.channel.right_volume() + 1.0) * right_mixed; prod.push(left) .and(prod.push(right)) @@ -742,7 +745,8 @@ impl Channel3 { } fn amplitude(&self) -> f32 { - let dac_input = self.read_sample(self.offset) >> self.volume.shift_count(); + let dac_input = + (self.read_sample(self.offset) >> self.volume.shift_count()) * self.enabled as u8; (dac_input as f32 / 7.5) - 1.0 } diff --git a/src/apu/types.rs b/src/apu/types.rs index 4b304a5..f8a69b9 100644 --- a/src/apu/types.rs +++ b/src/apu/types.rs @@ -503,10 +503,20 @@ impl From for u8 { bitfield! { pub struct ChannelControl(u8); impl Debug; - vin_so2, _: 7; - so2_level, _: 6, 4; - vin_so1, _: 3; - so1_level, _: 2, 0; + vin_left, _: 7; + _left_volume, _: 6, 4; + vin_right, _: 3; + _right_volume, _: 2, 0; +} + +impl ChannelControl { + pub(crate) fn left_volume(&self) -> f32 { + self._left_volume() as f32 + } + + pub(crate) fn right_volume(&self) -> f32 { + self._right_volume() as f32 + } } impl Copy for ChannelControl {}