From f4c0f54a9db5fb7f6bb0e6c415eea9437b39f079 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jo=C3=A3o=20Magalh=C3=A3es?= <joamag@gmail.com>
Date: Thu, 18 May 2023 20:39:27 +0100
Subject: [PATCH] fix: issue where audio was not restored in UI

---
 .../react/components/audio-gb/audio-gb.tsx    | 24 ++++++++++++-------
 src/gb.rs                                     | 24 +++++++++++++++----
 2 files changed, 36 insertions(+), 12 deletions(-)

diff --git a/frontends/web/react/components/audio-gb/audio-gb.tsx b/frontends/web/react/components/audio-gb/audio-gb.tsx
index 79c52aa0..ae6eb83c 100644
--- a/frontends/web/react/components/audio-gb/audio-gb.tsx
+++ b/frontends/web/react/components/audio-gb/audio-gb.tsx
@@ -34,10 +34,18 @@ export const AudioGB: FC<AudioGBProps> = ({
     const [audioOutput, setAudioOutput] = useState<Record<string, number[]>>(
         {}
     );
-    const [ch1Enabled, setCh1Enabled] = useState(true);
-    const [ch2Enabled, setCh2Enabled] = useState(true);
-    const [ch3Enabled, setCh3Enabled] = useState(true);
-    const [ch4Enabled, setCh4Enabled] = useState(true);
+    const [ch1Enabled, setCh1Enabled] = useState(
+        emulator.instance?.audio_ch1_enabled() ?? true
+    );
+    const [ch2Enabled, setCh2Enabled] = useState(
+        emulator.instance?.audio_ch2_enabled() ?? true
+    );
+    const [ch3Enabled, setCh3Enabled] = useState(
+        emulator.instance?.audio_ch3_enabled() ?? true
+    );
+    const [ch4Enabled, setCh4Enabled] = useState(
+        emulator.instance?.audio_ch4_enabled() ?? true
+    );
     const intervalsRef = useRef<number>();
     const intervalsExtraRef = useRef<number>();
 
@@ -184,7 +192,7 @@ export const AudioGB: FC<AudioGBProps> = ({
                     "ch1",
                     ["selector", ch1Enabled ? "" : "disabled"],
                     () => {
-                        emulator.instance?.audio_ch1_enabled(!ch1Enabled);
+                        emulator.instance?.set_audio_ch1_enabled(!ch1Enabled);
                         setCh1Enabled(!ch1Enabled);
                     }
                 )}
@@ -193,7 +201,7 @@ export const AudioGB: FC<AudioGBProps> = ({
                     "ch2",
                     ["selector", ch2Enabled ? "" : "disabled"],
                     () => {
-                        emulator.instance?.audio_ch2_enabled(!ch2Enabled);
+                        emulator.instance?.set_audio_ch2_enabled(!ch2Enabled);
                         setCh2Enabled(!ch2Enabled);
                     }
                 )}
@@ -202,7 +210,7 @@ export const AudioGB: FC<AudioGBProps> = ({
                     "ch3",
                     ["selector", ch3Enabled ? "" : "disabled"],
                     () => {
-                        emulator.instance?.audio_ch3_enabled(!ch3Enabled);
+                        emulator.instance?.set_audio_ch3_enabled(!ch3Enabled);
                         setCh3Enabled(!ch3Enabled);
                     }
                 )}
@@ -211,7 +219,7 @@ export const AudioGB: FC<AudioGBProps> = ({
                     "ch4",
                     ["selector", ch4Enabled ? "" : "disabled"],
                     () => {
-                        emulator.instance?.audio_ch4_enabled(!ch4Enabled);
+                        emulator.instance?.set_audio_ch4_enabled(!ch4Enabled);
                         setCh4Enabled(!ch4Enabled);
                     }
                 )}
diff --git a/src/gb.rs b/src/gb.rs
index da6f9520..67244e46 100644
--- a/src/gb.rs
+++ b/src/gb.rs
@@ -557,19 +557,35 @@ impl GameBoy {
         self.apu_i().ch4_output()
     }
 
-    pub fn audio_ch1_enabled(&mut self, enabled: bool) {
+    pub fn audio_ch1_enabled(&mut self) -> bool {
+        self.apu().ch2_enabled()
+    }
+
+    pub fn set_audio_ch1_enabled(&mut self, enabled: bool) {
         self.apu().set_ch1_enabled(enabled)
     }
 
-    pub fn audio_ch2_enabled(&mut self, enabled: bool) {
+    pub fn audio_ch2_enabled(&mut self) -> bool {
+        self.apu().ch2_enabled()
+    }
+
+    pub fn set_audio_ch2_enabled(&mut self, enabled: bool) {
         self.apu().set_ch2_enabled(enabled)
     }
 
-    pub fn audio_ch3_enabled(&mut self, enabled: bool) {
+    pub fn audio_ch3_enabled(&mut self) -> bool {
+        self.apu().ch3_enabled()
+    }
+
+    pub fn set_audio_ch3_enabled(&mut self, enabled: bool) {
         self.apu().set_ch3_enabled(enabled)
     }
 
-    pub fn audio_ch4_enabled(&mut self, enabled: bool) {
+    pub fn audio_ch4_enabled(&mut self) -> bool {
+        self.apu().ch4_enabled()
+    }
+
+    pub fn set_audio_ch4_enabled(&mut self, enabled: bool) {
         self.apu().set_ch4_enabled(enabled)
     }
 
-- 
GitLab