fragglet / sdl-sopwith

Classic bi-plane shoot-'em up
https://fragglet.github.io/sdl-sopwith
GNU General Public License v2.0
66 stars 13 forks source link

Consider changing output sound format to signed #19

Closed NY00123 closed 1 year ago

NY00123 commented 1 year ago

Apart from simplifying one line of code, this can also resolve a bug (missing sounds + unexpected clicks) when used with SDL 2.0.10. I originally tried running sdl-sopwith with this version, given that it's what you get from Ubuntu 20.04 LTS' repository.

This is probably the impacting SDL commit, which is in 2.0.12: https://github.com/libsdl-org/SDL/commit/aef1ed4ac66fa11a551389153089c414947033d5

Another commit to mention (albeit not for the bug found by me) is the following one from 2.0.22: https://github.com/libsdl-org/SDL/commit/bf66720a4d5feeb1d06dcb1d1a0156ac015bba4b

These are the changes applied by me:

diff --git a/src/sdl/pcsound.c b/src/sdl/pcsound.c
index 12edde5..0dfef94 100644
--- a/src/sdl/pcsound.c
+++ b/src/sdl/pcsound.c
@@ -188,7 +188,7 @@ static void snd_callback(void *userdata, Uint8 *stream8, int len)
 {
        static int lasttime;
        static float lastfreq;
-       uint16_t *stream = (uint16_t *) stream8;
+       int16_t *stream = (int16_t *) stream8;
        float sample;
        int i;

@@ -213,7 +213,7 @@ static void snd_callback(void *userdata, Uint8 *stream8, int len)
                                           / output_freq);
                }
                sample = FilterNext(&tinny_filter, sample);
-               stream[i] = (1 << 15) + (signed int) (sample * VOLUME);
+               stream[i] = (int16_t) (sample * VOLUME);
        }

        lasttime += len;
@@ -307,7 +307,7 @@ void Speaker_Init(void)

        audiospec.samples = 1024;
        audiospec.freq = 48000;
-       audiospec.format = AUDIO_U16SYS;
+       audiospec.format = AUDIO_S16SYS;
        audiospec.channels = 1;
        audiospec.callback = &snd_callback;