Consider changing output sound format to signed
NY00123 opened this issue · 0 comments
NY00123 commented
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: libsdl-org/SDL@aef1ed4
Another commit to mention (albeit not for the bug found by me) is the following one from 2.0.22: libsdl-org/SDL@bf66720
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;