Fixed an issue where defining the same sound file for multiple navigation sounds would log an error to es_log.txt on theme change.

This commit is contained in:
Leon Styhre 2023-01-05 11:17:34 +01:00
parent ad973e1ffa
commit 60a6776b53

View file

@ -59,7 +59,7 @@ void AudioManager::init()
sRequestedAudioFormat.callback = mixAudio;
sRequestedAudioFormat.userdata = nullptr;
for (int i = 0; i < SDL_GetNumAudioDevices(0); ++i) {
for (int i {0}; i < SDL_GetNumAudioDevices(0); ++i) {
LOG(LogInfo) << "Detected playback device: " << SDL_GetAudioDeviceName(i, 0);
}
@ -126,7 +126,7 @@ void AudioManager::deinit()
void AudioManager::mixAudio(void* /*unused*/, Uint8* stream, int len)
{
// Process navigation sounds.
bool stillPlaying = false;
bool stillPlaying {false};
// Initialize the buffer to "silence".
SDL_memset(stream, 0, len);
@ -134,10 +134,10 @@ void AudioManager::mixAudio(void* /*unused*/, Uint8* stream, int len)
// Iterate through all our samples.
std::vector<std::shared_ptr<Sound>>::const_iterator soundIt = sSoundVector.cbegin();
while (soundIt != sSoundVector.cend()) {
std::shared_ptr<Sound> sound = *soundIt;
std::shared_ptr<Sound> sound {*soundIt};
if (sound->isPlaying()) {
// 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 stream length is smaller than sample length, clip it.
restLength = len;
@ -159,7 +159,7 @@ void AudioManager::mixAudio(void* /*unused*/, Uint8* stream, int len)
}
// Process video stream audio generated by VideoFFmpegComponent.
int streamLength = SDL_AudioStreamAvailable(sConversionStream);
int streamLength {SDL_AudioStreamAvailable(sConversionStream)};
if (streamLength <= 0) {
// If nothing is playing, pause the device until there is more audio to output.
@ -168,7 +168,7 @@ void AudioManager::mixAudio(void* /*unused*/, Uint8* stream, int len)
return;
}
int chunkLength = 0;
int chunkLength {0};
// Cap the chunk length to the buffer size.
if (streamLength > len)
@ -178,8 +178,8 @@ void AudioManager::mixAudio(void* /*unused*/, Uint8* stream, int len)
std::vector<Uint8> converted(chunkLength);
int processedLength =
SDL_AudioStreamGet(sConversionStream, static_cast<void*>(&converted.at(0)), chunkLength);
int processedLength {
SDL_AudioStreamGet(sConversionStream, static_cast<void*>(&converted.at(0)), chunkLength)};
if (processedLength < 0) {
LOG(LogError) << "AudioManager::mixAudio(): Couldn't convert sound chunk:";
@ -196,7 +196,7 @@ void AudioManager::mixAudio(void* /*unused*/, Uint8* stream, int len)
// stream is not played when the video player has been stopped. Otherwise there would
// be a short time period when the audio would keep playing after the video was stopped
// and before the stream was cleared in clearStream().
bool muteStream = sMuteStream;
bool muteStream {sMuteStream};
if (muteStream) {
SDL_MixAudioFormat(stream, &converted.at(0), sAudioFormat.format, processedLength, 0);
}
@ -219,14 +219,13 @@ void AudioManager::registerSound(std::shared_ptr<Sound> sound)
void AudioManager::unregisterSound(std::shared_ptr<Sound> sound)
{
for (unsigned int i = 0; i < sSoundVector.size(); ++i) {
for (unsigned int i {0}; i < sSoundVector.size(); ++i) {
if (sSoundVector.at(i) == sound) {
sSoundVector[i]->stop();
sSoundVector.erase(sSoundVector.cbegin() + i);
return;
}
}
LOG(LogError) << "AudioManager - tried to unregister a sound that wasn't registered";
}
void AudioManager::play()
@ -238,7 +237,7 @@ void AudioManager::play()
void AudioManager::stop()
{
// Stop playing all Sounds.
for (unsigned int i = 0; i < sSoundVector.size(); ++i) {
for (unsigned int i {0}; i < sSoundVector.size(); ++i) {
if (sSoundVector.at(i)->isPlaying())
sSoundVector[i]->stop();
}
@ -248,7 +247,7 @@ void AudioManager::stop()
void AudioManager::setupAudioStream(int sampleRate)
{
SDL_AudioStatus audioStatus = SDL_GetAudioDeviceStatus(sAudioDevice);
SDL_AudioStatus audioStatus {SDL_GetAudioDeviceStatus(sAudioDevice)};
// It's very important to pause the audio device before setting up the stream,
// or we may get random crashes if attempting to play samples at the same time.