-
Notifications
You must be signed in to change notification settings - Fork 30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
sdl2-mixer
sound effect playback with Mix_PlayChannel
is truncated
#304
Comments
For reference, I also have a similar issue when using Moonlight. The output audio waveform looks like the following image. UPDATE: It seems that Moonlight does not use sdl-mixer. There may be a buffer underrun in moonlight. |
We fixed an audio problem that could cause this over the weekend. What version of sdl2-compat are you using? |
I am using the latest commit from the main branch. |
bool SdlAudioRenderer::prepareForPlayback(const OPUS_MULTISTREAM_CONFIGURATION* opusConfig)
{
SDL_AudioSpec want, have;
SDL_zero(want);
want.freq = opusConfig->sampleRate;
want.format = AUDIO_F32SYS;
want.channels = opusConfig->channelCount;
// On PulseAudio systems, setting a value too small can cause underruns for other
// applications sharing this output device. We impose a floor of 480 samples (10 ms)
// to mitigate this issue. Otherwise, we will buffer up to 3 frames of audio which
// is 15 ms at regular 5 ms frames and 30 ms at 10 ms frames for slow connections.
// The buffering helps avoid audio underruns due to network jitter.
#ifndef Q_OS_DARWIN
want.samples = SDL_max(480, opusConfig->samplesPerFrame * 3);
#else
// HACK: Changing the buffer size can lead to Bluetooth HFP
// audio issues on macOS, so we're leaving this alone.
// https://github.com/moonlight-stream/moonlight-qt/issues/1071
want.samples = SDL_max(480, opusConfig->samplesPerFrame);
#endif
m_FrameSize = opusConfig->samplesPerFrame *
opusConfig->channelCount *
getAudioBufferSampleSize();
m_AudioDevice = SDL_OpenAudioDevice(NULL, 0, &want, &have, 0);
} Moonlight's |
My sdl2-compact is v2.30.52. I'll try with latest master later. |
I can reproduce the moonlight problems and running both sdl2-compat and sdl3 straight from git doesn't seem to resolve the issues. |
Somebody reported the same issue on the moonlight repo and I directed them into this issue. |
I think I have this issue with our game, SRB2 We have a few set of DOOM SFX lumps that just plain PCM8 WAV data that we feed into so, our DOOM lump |
Could tell with code from master the issue still exists. My test case is |
The issue on Moonlight mentions that version 2.30.51 works fine. # bad: [97acb6a6622cbb39b3d5b93747bc63bf17aaf1d7] change a few passthrough SDL_xxx() calls to SDL3_xxx().
# good: [0c526f15c65f7cde54f246ceca060e9219c3b906] Updated to version 2.30.51 for release
git bisect start 'HEAD' 'release-2.30.51'
# bad: [188cb119325aa9a99899f1ff9e2e0d99ae5abf87] minor coding style clean-up after PR/281.
git bisect bad 188cb119325aa9a99899f1ff9e2e0d99ae5abf87
# bad: [0ff6b3cd77defebfa7878179f063ba37d514f574] event: Drop PIXEL_SIZE_CHANGED events if the window is hidden.
git bisect bad 0ff6b3cd77defebfa7878179f063ba37d514f574
# bad: [f56d4f9559dd4fb84fefd5c84a1a8cfea4ba0e1e] fix build using c++ compilers.
git bisect bad f56d4f9559dd4fb84fefd5c84a1a8cfea4ba0e1e
# bad: [9110cc1f391aee8c5d6b6ca323a3337b26355bc8] audio: Call audio callbacks with consistent buffer size.
git bisect bad 9110cc1f391aee8c5d6b6ca323a3337b26355bc8
# good: [b2d2190946b4c15ba0ef425f8a54ba6f9056fd2e] Give SDL2 applications the audio spec they expect
git bisect good b2d2190946b4c15ba0ef425f8a54ba6f9056fd2e
# first bad commit: [9110cc1f391aee8c5d6b6ca323a3337b26355bc8] audio: Call audio callbacks with consistent buffer size. Use the following reproduction case for testing. #include <SDL2/SDL.h>
int main() {
SDL_AudioSpec want, wavSpec;
SDL_Event e;
Uint32 wavLength;
Uint8 *wavBuffer;
SDL_zero(want);
if (SDL_Init(SDL_INIT_AUDIO|SDL_INIT_EVENTS) < 0) {
SDL_Log("Couldn't initialize SDL: %s\n", SDL_GetError());
return 255;
}
SDL_LoadWAV("test.wav", &wavSpec, &wavBuffer, &wavLength);
SDL_Log("Loaded WAV file\n");
SDL_Log("Frequency: %d\n", wavSpec.freq);
SDL_Log("Samples: %d\n", wavSpec.samples);
SDL_Log("Channels: %d\n", wavSpec.channels);
SDL_Log("Size: %d\n", wavLength);
want.freq = wavSpec.freq;
want.format = wavSpec.format;
want.channels = wavSpec.channels;
want.samples = 480; // moonlight value
SDL_AudioDeviceID devid =
SDL_OpenAudioDevice(NULL, 0, &want, NULL, 0);
if (devid == 0) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,"Failed to open audio device: %s", SDL_GetError());
return 0;
}
SDL_PauseAudioDevice(devid, 0);
SDL_QueueAudio(devid, wavBuffer, wavLength);
while (1) {
SDL_PollEvent(&e);
if (e.type == SDL_QUIT || SDL_GetQueuedAudioSize(devid) == 0) {
SDL_Log("exit\n");
break;
}
SDL_Delay(100);
}
SDL_Quit();
return 0;
} |
My issue still reproduces when loading with v2.30.50 and v2.30.51.
I'm still confused. Rolling back can't solve my issue. Nor building from scratch with master does. |
May different components be affected. Moonlight does not use sdl2-mixer, so I might need to open another issue about Moonlight. |
@bbaa-bbaa tested again with your example code. With sdl2 the wav plays to the end, then exits. With sdl2-compact the binary simply exits and I could hear nothing.
Yep. I think yours is not mixer issue, but it could be another issue with sdl2-compact, or even sdl3, as @svenstaro mentioned it could be reproduced with sdl3. @icculus maybe there is an missing queue check for sound playback in sdl2-compact or sdl3? |
I think the problem is that (In SpaceCadetPinball's case the source format is This patch fixes the truncated audio in SpaceCadetPinball for me (code originates from SDL2's diff --git a/src/sdl2_compat.c b/src/sdl2_compat.c
index 91df91d..662f78d 100644
--- a/src/sdl2_compat.c
+++ b/src/sdl2_compat.c
@@ -9579,6 +9579,20 @@ SDL_BuildAudioCVT(SDL_AudioCVT *cvt,
cvt->needed = 0;
}
+ if (src_format != dst_format) {
+ const Uint16 src_bitsize = SDL_AUDIO_BITSIZE(src_format);
+ const Uint16 dst_bitsize = SDL_AUDIO_BITSIZE(dst_format);
+
+ if (src_bitsize < dst_bitsize) {
+ const int mult = (dst_bitsize / src_bitsize);
+ cvt->len_mult *= mult;
+ cvt->len_ratio *= mult;
+ } else if (src_bitsize > dst_bitsize) {
+ const int div = (src_bitsize / dst_bitsize);
+ cvt->len_ratio /= div;
+ }
+ }
+
if (src_channels < dst_channels) {
cvt->len_mult = ((cvt->len_mult * dst_channels) + (src_channels - 1)) / src_channels;
} |
OK, I can confirm that this patch to |
The patch also fixed my issue. As there is already conversion in |
Just pushed a separate fix for Moonlight; checking this diff now. |
Okay, this looks reasonable, so I've applied it. You should be good to go now! |
sdl2-mixer
sound effect playback withMix_PlayChannel
performs differently withsdl2-compact
compared to an actualsdl2
library. In my case, sound become truncated.I noticed this while playing SpaceCadetPinball, after Arch's upgrade to
sdl3
andsdl2-compact
for compatibility.It seemed the game implements sound playback in
SpaceCadetPinball/Sound.cpp:Sound::PlaySound: L60: auto channel = Mix_PlayChannel(-1, wavePtr, 0);
, where thewavePtr
is previously loaded fromMix_LoadWAV
. Withsdl2-compact
the sound effect is truncated to less than a half of duration (the actual playback duration is not fixed for every chunk), whilesdl2
performs well.I tried to figure this issue out with a snippet from libsdl-org/SDL_mixer#646:
But the issue is gone with this situation, also another version with sdl3-mixer built from source just worked fine. No sound truncate felt except for last one (when main exited). As the loading process of chunk seems fine, I guess the issue comes from playback.
As migration into
sdl3
is still in progress, I suppose we keep a similar behavior insdl2-compact
😄The text was updated successfully, but these errors were encountered: