From 671c90f7dc0565067a47ff8ae5c55bda3bd8b168 Mon Sep 17 00:00:00 2001 From: Misa Date: Sat, 18 Mar 2023 15:33:30 -0700 Subject: [PATCH] Fix music playing wrong if played after track of different format As described in #886, if a track was played when an existing track was already playing, and the sample rates of the two tracks differ, then the second track would play wrong and distorted. This is because the second track would play with the sample rate of the first. To fix this, halt the track if the sample rate is mismatched, which destroys the voice. This results in the voice being recreated later in the Play() function. The track is also halted if the number of channels or bit depth is mismatched. Fixes #886. --- desktop_version/src/Music.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/desktop_version/src/Music.cpp b/desktop_version/src/Music.cpp index f48a4d28..e963db22 100644 --- a/desktop_version/src/Music.cpp +++ b/desktop_version/src/Music.cpp @@ -314,6 +314,17 @@ end: sample_pos = 0; stb_vorbis_seek_start(vorbis); + if (!IsHalted()) + { + FAudioVoiceDetails details; + FAudioVoice_GetVoiceDetails(musicVoice, &details); + if (details.InputSampleRate != format.nSamplesPerSec || + details.InputChannels != format.nChannels) + { + Halt(); + } + } + if (IsHalted()) { SDL_zero(callbacks);