Fixed an audio mixing issue in AudioManager.

This commit is contained in:
Leon Styhre 2021-05-12 22:45:01 +02:00
parent f92b314a0d
commit 398e47e2cc

View file

@ -132,7 +132,6 @@ void AudioManager::mixAudio(void* /*unused*/, Uint8* stream, int len)
{
// Process navigation sounds.
bool stillPlaying = false;
bool playedSample = false;
// Initialize the buffer to "silence".
SDL_memset(stream, 0, len);
@ -142,7 +141,6 @@ void AudioManager::mixAudio(void* /*unused*/, Uint8* stream, int len)
while (soundIt != sSoundVector.cend()) {
std::shared_ptr<Sound> sound = *soundIt;
if (sound->isPlaying()) {
playedSample = true;
// Calculate rest length of current sample.
Uint32 restLength = (sound->getLength() - sound->getPosition());
if (restLength > static_cast<Uint32>(len)) {
@ -165,26 +163,23 @@ void AudioManager::mixAudio(void* /*unused*/, Uint8* stream, int len)
soundIt++;
}
if (playedSample) {
// We have processed all samples. check if some will continue playing.
if (!stillPlaying) {
// Nothing is playing, pause the audio until Sound::play() wakes us up.
// Process video stream audio generated by VideoFFmpegComponent.
int streamLength = SDL_AudioStreamAvailable(sConversionStream);
if (streamLength == 0) {
// If nothing is playing, pause the device until there is more audio to output.
if (!stillPlaying)
SDL_PauseAudioDevice(sAudioDevice, 1);
}
return;
}
// Process video stream audio.
// Currently only used by VideoFFmpegComponent as the audio streaming seems to
// be broken for libVLC.
int chunkLength = SDL_AudioStreamAvailable(sConversionStream);
if (chunkLength == 0)
return;
int chunkLength = 0;
// Cap the chunk length to the buffer size.
if (chunkLength > len)
if (streamLength > len)
chunkLength = len;
else
chunkLength = streamLength;
std::vector<Uint8> converted(chunkLength);
@ -192,21 +187,22 @@ void AudioManager::mixAudio(void* /*unused*/, Uint8* stream, int len)
static_cast<void*>(&converted.at(0)), chunkLength);
if (processedLength == -1) {
LOG(LogError) << "AudioManager::mixAudio(): Failed to convert sound chunk:";
LOG(LogError) << "AudioManager::mixAudio(): Couldn't convert sound chunk:";
LOG(LogError) << SDL_GetError();
return;
}
// Enable only when needed, as this generates a lot of debug output.
// LOG(LogDebug) << "AudioManager::mixAudio(): chunkLength"
// "/ processedLength: " << chunkLength << " / " <<
// " / " << processedLength;
// LOG(LogDebug) << "AudioManager::mixAudio(): chunkLength "
// "/ processedLength / streamLength: " << chunkLength << " / " <<
// " / " << processedLength << " / " << streamLength;
SDL_MixAudioFormat(stream, &converted.at(0), sAudioFormat.format, processedLength,
static_cast<int>(Settings::getInstance()->
getInt("SoundVolumeVideos") * 1.28f));
if (SDL_AudioStreamAvailable(sConversionStream) == 0)
// If nothing is playing, pause the device until there is more audio to output.
if (!stillPlaying && SDL_AudioStreamAvailable(sConversionStream) == 0)
SDL_PauseAudioDevice(sAudioDevice, 1);
}