From 12e7ce5075b5abf1426a226de2a1cfc6060ce39c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jo=C3=A3o=20Magalh=C3=A3es?= <joamag@gmail.com>
Date: Tue, 11 Apr 2023 13:37:57 +0100
Subject: [PATCH] feat: support for variable audio speed

---
 frontends/web/ts/gb.ts | 1 +
 src/apu.rs             | 7 ++++---
 2 files changed, 5 insertions(+), 3 deletions(-)

diff --git a/frontends/web/ts/gb.ts b/frontends/web/ts/gb.ts
index 41554b48..9a5729e9 100644
--- a/frontends/web/ts/gb.ts
+++ b/frontends/web/ts/gb.ts
@@ -548,6 +548,7 @@ export class GameboyEmulator extends EmulatorBase implements Emulator {
     set frequency(value: number) {
         value = Math.max(value, 0);
         this.logicFrequency = value;
+        this.gameBoy?.set_clock_freq(value);
         this.trigger("frequency", value);
     }
 
diff --git a/src/apu.rs b/src/apu.rs
index 0473b30f..1d7c5f9f 100644
--- a/src/apu.rs
+++ b/src/apu.rs
@@ -312,9 +312,10 @@ impl Apu {
                 self.audio_buffer.push_back(self.output());
             }
 
-            // @TODO the CPU clock is hardcoded here, we must handle situations
-            // where there's some kind of overclock, and for that to happen the
-            // current CPU clock must be propagated here
+            // calculates the rate at which a new audio sample should be
+            // created based on the (base/CPU) clock frequency and the
+            // sampling rate, this is basically the amount of APU clock
+            // calls that should be performed until an audio sample is created
             self.output_timer += (self.clock_freq as f32 / self.sampling_rate as f32) as i16;
         }
     }
-- 
GitLab