foxygit / doom Log in
commit 080ed4ee4324fa873b4049f5cda8749d9bd801c7
Author:     NY00123 <NY00123@users.noreply.github.com>
AuthorDate: Fri Jan 20 09:41:23 2023 +0200
Commit:     GitHub <noreply@github.com>
CommitDate: Fri Jan 20 08:41:23 2023 +0100

    pcsound: Partial fix for #795 #866 (#866)
---
 pcsound/pcsound_linux.c | 21 +++++++++++++--------
 pcsound/pcsound_sdl.c   | 15 +++++----------
 2 files changed, 18 insertions(+), 18 deletions(-)

diff --git a/pcsound/pcsound_linux.c b/pcsound/pcsound_linux.c
index b8a5bbca..7cb59c9b 100644
--- a/pcsound/pcsound_linux.c
+++ b/pcsound/pcsound_linux.c
@@ -89,6 +89,7 @@ static void AdjustedSleep(unsigned int ms)

 static int SoundThread(void *unused)
 {
+    int current_freq = 0;
     int frequency;
     int duration;
     int cycles;
@@ -97,16 +98,20 @@ static int SoundThread(void *unused)
     {
         callback(&duration, &frequency);

-        if (frequency != 0)
+        if (current_freq != frequency)
         {
-            cycles = PCSOUND_8253_FREQUENCY / frequency;
+            current_freq = frequency;
+            if (frequency != 0)
+            {
+                cycles = PCSOUND_8253_FREQUENCY / frequency;
+            }
+            else
+            {
+                cycles = 0;
+            }
+
+            ioctl(console_handle, KIOCSOUND, cycles);
         }
-        else
-        {
-            cycles = 0;
-        }
-
-        ioctl(console_handle, KIOCSOUND, cycles);

         AdjustedSleep(duration);
     }
diff --git a/pcsound/pcsound_sdl.c b/pcsound/pcsound_sdl.c
index 4f413fed..2a5b63d8 100644
--- a/pcsound/pcsound_sdl.c
+++ b/pcsound/pcsound_sdl.c
@@ -62,7 +62,7 @@ static void PCSound_Mix_Callback(int chan, void *stream, int len, void *udata)
     Sint16 *leftptr;
     Sint16 *rightptr;
     Sint16 this_value;
-    int oldfreq;
+    int frequency;
     int i;
     int nsamples;

@@ -82,19 +82,14 @@ static void PCSound_Mix_Callback(int chan, void *stream, int len, void *udata)

         while (current_remaining == 0)
         {
-            oldfreq = current_freq;
-
             // Get the next frequency to play

-            callback(&current_remaining, &current_freq);
+            callback(&current_remaining, &frequency);

-            if (current_freq != 0)
+            if (current_freq != frequency)
             {
-                // Adjust phase to match to the new frequency.
-                // This gives us a smooth transition between different tones,
-                // with no impulse changes.
-
-                phase_offset = (phase_offset * oldfreq) / current_freq;
+                current_freq = frequency;
+                phase_offset = 0;
             }

             current_remaining = (current_remaining * mixing_freq) / 1000;