Added checks for nonexistent navigation sounds in the theme configuration with fallback to the default sounds.

Also did some minor code cleanup.
This commit is contained in:
Leon Styhre 2022-12-22 17:15:15 +01:00
parent f8eb4023ec
commit 8b33fceb8a
2 changed files with 16 additions and 12 deletions

View file

@ -4,7 +4,7 @@
// Sound.cpp // Sound.cpp
// //
// Higher-level audio functions. // Higher-level audio functions.
// Reading theme sound setings, playing audio samples etc. // Navigation sounds, audio sample playback etc.
// //
#include "Sound.h" #include "Sound.h"
@ -15,8 +15,6 @@
#include "ThemeData.h" #include "ThemeData.h"
#include "resources/ResourceManager.h" #include "resources/ResourceManager.h"
std::map<std::string, std::shared_ptr<Sound>> Sound::sMap;
std::shared_ptr<Sound> Sound::get(const std::string& path) std::shared_ptr<Sound> Sound::get(const std::string& path)
{ {
auto it = sMap.find(path); auto it = sMap.find(path);
@ -43,12 +41,18 @@ std::shared_ptr<Sound> Sound::getFromTheme(ThemeData* const theme,
LOG(LogDebug) << "Sound::getFromTheme(): Looking for tag <sound name=\"" << elemName << "\">"; LOG(LogDebug) << "Sound::getFromTheme(): Looking for tag <sound name=\"" << elemName << "\">";
const ThemeData::ThemeElement* elem = theme->getElement(view, element, "sound"); const ThemeData::ThemeElement* elem {theme->getElement(view, element, "sound")};
if (!elem || !elem->has("path")) { if (!elem || !elem->has("path")) {
LOG(LogDebug) << "Sound::getFromTheme(): Tag not found, using fallback sound file"; LOG(LogDebug) << "Sound::getFromTheme(): Tag not found, using fallback sound file";
return get(ResourceManager::getInstance().getResourcePath(":/sounds/" + elemName + ".wav")); return get(ResourceManager::getInstance().getResourcePath(":/sounds/" + elemName + ".wav"));
} }
if (!Utils::FileSystem::exists(elem->get<std::string>("path"))) {
LOG(LogError) << "Sound::getFromTheme(): Navigation sound tag found but sound file does "
"not exist, falling back to default sound";
return get(ResourceManager::getInstance().getResourcePath(":/sounds/" + elemName + ".wav"));
}
LOG(LogDebug) << "Sound::getFromTheme(): Tag found, ready to load theme sound file"; LOG(LogDebug) << "Sound::getFromTheme(): Tag found, ready to load theme sound file";
return get(elem->get<std::string>("path")); return get(elem->get<std::string>("path"));
} }
@ -78,17 +82,17 @@ void Sound::init()
// Load WAV file via SDL. // Load WAV file via SDL.
SDL_AudioSpec wave; SDL_AudioSpec wave;
Uint8* data = nullptr; Uint8* data {nullptr};
Uint32 dlen = 0; Uint32 dlen {0};
if (SDL_LoadWAV(mPath.c_str(), &wave, &data, &dlen) == nullptr) { if (SDL_LoadWAV(mPath.c_str(), &wave, &data, &dlen) == nullptr) {
LOG(LogError) << "Failed to load theme navigation sound file: " << SDL_GetError(); LOG(LogError) << "Failed to load theme navigation sound file: " << SDL_GetError();
return; return;
} }
// Convert sound file to the format required by ES-DE. // Convert sound file to the format required by ES-DE.
SDL_AudioStream* conversionStream = SDL_AudioStream* conversionStream {
SDL_NewAudioStream(wave.format, wave.channels, wave.freq, AudioManager::sAudioFormat.format, SDL_NewAudioStream(wave.format, wave.channels, wave.freq, AudioManager::sAudioFormat.format,
AudioManager::sAudioFormat.channels, AudioManager::sAudioFormat.freq); AudioManager::sAudioFormat.channels, AudioManager::sAudioFormat.freq)};
if (conversionStream == nullptr) { if (conversionStream == nullptr) {
LOG(LogError) << "Failed to create sample conversion stream: " << SDL_GetError(); LOG(LogError) << "Failed to create sample conversion stream: " << SDL_GetError();
@ -101,9 +105,9 @@ void Sound::init()
return; return;
} }
int sampleLength = SDL_AudioStreamAvailable(conversionStream); int sampleLength {SDL_AudioStreamAvailable(conversionStream)};
Uint8* converted = new Uint8[sampleLength]; Uint8* converted {new Uint8[sampleLength]};
if (SDL_AudioStreamGet(conversionStream, converted, sampleLength) == -1) { if (SDL_AudioStreamGet(conversionStream, converted, sampleLength) == -1) {
LOG(LogError) << "Failed to convert sound file '" << mPath << "': " << SDL_GetError(); LOG(LogError) << "Failed to convert sound file '" << mPath << "': " << SDL_GetError();
SDL_FreeAudioStream(conversionStream); SDL_FreeAudioStream(conversionStream);

View file

@ -4,7 +4,7 @@
// Sound.h // Sound.h
// //
// Higher-level audio functions. // Higher-level audio functions.
// Reading theme sound setings, playing audio samples etc. // Navigation sounds, audio sample playback etc.
// //
#ifndef ES_CORE_SOUND_H #ifndef ES_CORE_SOUND_H
@ -47,7 +47,7 @@ public:
private: private:
Sound(const std::string& path = ""); Sound(const std::string& path = "");
static std::map<std::string, std::shared_ptr<Sound>> sMap; static inline std::map<std::string, std::shared_ptr<Sound>> sMap;
std::string mPath; std::string mPath;
SDL_AudioSpec mSampleFormat; SDL_AudioSpec mSampleFormat;
Uint8* mSampleData; Uint8* mSampleData;