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