From fa92d52fdb8deab57a1f6dab9a35582d25daa920 Mon Sep 17 00:00:00 2001 From: Julian Xhokaxhiu Date: Sun, 5 May 2024 18:42:38 +0200 Subject: [PATCH] SFX: Allow to configure vanilla SFX IDs using their direct ID Fixes #689 --- Changelog.md | 5 ++++ src/audio.cpp | 66 ++++++++++++++++++++++----------------------------- src/audio.h | 6 ++--- 3 files changed, 36 insertions(+), 41 deletions(-) diff --git a/Changelog.md b/Changelog.md index 2d4fe079..4d824462 100644 --- a/Changelog.md +++ b/Changelog.md @@ -2,6 +2,11 @@ - Full commit list since last stable release: https://github.com/julianxhokaxhiu/FFNx/compare/1.19.1...master +## Common + +- Audio: Fix bug that won't allow to configure vanilla SFX IDs using the `sfx/config.toml` file + + ## FF8 - Audio: Add ambient layer support for Fields and Battles diff --git a/src/audio.cpp b/src/audio.cpp index f0f41a96..32649ba5 100644 --- a/src/audio.cpp +++ b/src/audio.cpp @@ -77,8 +77,7 @@ void NxAudioEngine::loadConfig() } } -template -bool NxAudioEngine::getFilenameFullPath(char *_out, T _key, NxAudioEngineLayer _type) +bool NxAudioEngine::getFilenameFullPath(char *_out, const char* _key, NxAudioEngineLayer _type) { std::vector extensions; @@ -105,19 +104,19 @@ bool NxAudioEngine::getFilenameFullPath(char *_out, T _key, NxAudioEngineLayer _ switch (_type) { case NxAudioEngineLayer::NXAUDIOENGINE_SFX: - sprintf(_out, "%s/%s/%d.%s", basedir, external_sfx_path.c_str(), (int)_key, extension.c_str()); + sprintf(_out, "%s/%s/%s.%s", basedir, external_sfx_path.c_str(), _key, extension.c_str()); break; case NxAudioEngineLayer::NXAUDIOENGINE_MUSIC: - sprintf(_out, "%s/%s/%s.%s", basedir, external_music_path.c_str(), (const char*)_key, extension.c_str()); + sprintf(_out, "%s/%s/%s.%s", basedir, external_music_path.c_str(), _key, extension.c_str()); break; case NxAudioEngineLayer::NXAUDIOENGINE_VOICE: - sprintf(_out, "%s/%s/%s.%s", basedir, external_voice_path.c_str(), (const char*)_key, extension.c_str()); + sprintf(_out, "%s/%s/%s.%s", basedir, external_voice_path.c_str(), _key, extension.c_str()); break; case NxAudioEngineLayer::NXAUDIOENGINE_AMBIENT: - sprintf(_out, "%s/%s/%s.%s", basedir, external_ambient_path.c_str(), (const char*)_key, extension.c_str()); + sprintf(_out, "%s/%s/%s.%s", basedir, external_ambient_path.c_str(), _key, extension.c_str()); break; case NxAudioEngineLayer::NXAUDIOENGINE_MOVIE_AUDIO: - sprintf(_out, "%s.%s", (const char*)_key, extension.c_str()); + sprintf(_out, "%s.%s", _key, extension.c_str()); break; } @@ -211,27 +210,17 @@ void NxAudioEngine::cleanup() } // SFX -bool NxAudioEngine::canPlaySFX(int id) +SoLoud::VGMStream* NxAudioEngine::loadSFX(std::string id, bool loop) { - char filename[MAX_PATH]; - - return getFilenameFullPath(filename, id, NxAudioEngineLayer::NXAUDIOENGINE_SFX); -} - -SoLoud::VGMStream* NxAudioEngine::loadSFX(int id, bool loop) -{ - int _curId = id - 1; - if (_engineInitialized) { char filename[MAX_PATH]; - bool exists = getFilenameFullPath(filename, id, NxAudioEngineLayer::NXAUDIOENGINE_SFX); + bool exists = getFilenameFullPath(filename, id.c_str(), NxAudioEngineLayer::NXAUDIOENGINE_SFX); if (exists) { - std::string _id = std::to_string(id); - auto node = nxAudioEngineConfig[NxAudioEngineLayer::NXAUDIOENGINE_SFX][_id]; + auto node = nxAudioEngineConfig[NxAudioEngineLayer::NXAUDIOENGINE_SFX][id]; if (node) { @@ -296,8 +285,9 @@ void NxAudioEngine::unloadSFXChannel(int channel) bool NxAudioEngine::playSFX(const char* name, int id, int channel, float panning, bool loop) { NxAudioEngineSFX *options = &_sfxChannels[channel - 1]; - int _curId = id - 1; + int _curId = id; bool skipPlay = false; + std::string _id(name); // If channel is known to be reusable if (channel <= _sfxReusableChannels) @@ -338,7 +328,8 @@ bool NxAudioEngine::playSFX(const char* name, int id, int channel, float panning { auto _newId = shuffleIds->get(getRandomInt(0, shuffleIds->size() - 1)); - _curId = _newId->value_or(id) - 1; + _curId = _newId->value_or(id); + _id = std::to_string(_curId); } // Sequentially playback new SFX ids, if any entry found for the current id @@ -352,7 +343,8 @@ bool NxAudioEngine::playSFX(const char* name, int id, int channel, float panning _sfxSequentialIndexes[name]++; - _curId = _newId->value_or(id) - 1; + _curId = _newId->value_or(id); + _id = std::to_string(_curId); } // Should we skip playing the track? @@ -366,9 +358,9 @@ bool NxAudioEngine::playSFX(const char* name, int id, int channel, float panning if (options->stream == nullptr) { options->game_id = id; - options->id = _curId + 1; + options->id = _curId; // Avoid loading a stream if it is meant to be skipped - options->stream = skipPlay ? nullptr : loadSFX(options->id, loop); + options->stream = skipPlay ? nullptr : loadSFX(_id, loop); } if (skipPlay) @@ -526,7 +518,7 @@ bool NxAudioEngine::canPlayMusic(const char* name) { char filename[MAX_PATH]; - return getFilenameFullPath(filename, name, NxAudioEngineLayer::NXAUDIOENGINE_MUSIC); + return getFilenameFullPath(filename, name, NxAudioEngineLayer::NXAUDIOENGINE_MUSIC); } bool NxAudioEngine::isMusicDisabled(const char* name) @@ -576,7 +568,7 @@ SoLoud::AudioSource* NxAudioEngine::loadMusic(const char* name, bool isFullPath, if (!exists) { - exists = getFilenameFullPath(filename, name, NxAudioEngineLayer::NXAUDIOENGINE_MUSIC); + exists = getFilenameFullPath(filename, name, NxAudioEngineLayer::NXAUDIOENGINE_MUSIC); } if (exists) @@ -1044,7 +1036,7 @@ bool NxAudioEngine::canPlayVoice(const char* name) { char filename[MAX_PATH]; - return getFilenameFullPath(filename, name, NxAudioEngineLayer::NXAUDIOENGINE_VOICE); + return getFilenameFullPath(filename, name, NxAudioEngineLayer::NXAUDIOENGINE_VOICE); } bool NxAudioEngine::playVoice(const char* name, int slot, float volume) @@ -1075,7 +1067,7 @@ bool NxAudioEngine::playVoice(const char* name, int slot, float volume) { auto _newName = shuffleNames->get(getRandomInt(0, shuffleNames->size() - 1)); - exists = getFilenameFullPath(filename, _newName->value_or(name), NxAudioEngineLayer::NXAUDIOENGINE_VOICE); + exists = getFilenameFullPath(filename, _newName->value_or(name), NxAudioEngineLayer::NXAUDIOENGINE_VOICE); } // Sequentially playback new voice items, if any entry found for the current id @@ -1089,13 +1081,13 @@ bool NxAudioEngine::playVoice(const char* name, int slot, float volume) _voiceSequentialIndexes[name]++; - exists = getFilenameFullPath(filename, _newName->value_or(name), NxAudioEngineLayer::NXAUDIOENGINE_VOICE); + exists = getFilenameFullPath(filename, _newName->value_or(name), NxAudioEngineLayer::NXAUDIOENGINE_VOICE); } } // If none of the previous configurations worked, load the default one as last tentative if (!exists) { - exists = getFilenameFullPath(filename, name, NxAudioEngineLayer::NXAUDIOENGINE_VOICE); + exists = getFilenameFullPath(filename, name, NxAudioEngineLayer::NXAUDIOENGINE_VOICE); } if (trace_all || trace_voice) ffnx_trace("NxAudioEngine::%s: slot[%d] %s\n", __func__, slot, filename); @@ -1194,7 +1186,7 @@ bool NxAudioEngine::canPlayAmbient(const char* name) { char filename[MAX_PATH]; - return getFilenameFullPath(filename, name, NxAudioEngineLayer::NXAUDIOENGINE_AMBIENT); + return getFilenameFullPath(filename, name, NxAudioEngineLayer::NXAUDIOENGINE_AMBIENT); } bool NxAudioEngine::playAmbient(const char* name, float volume, double time) @@ -1216,7 +1208,7 @@ bool NxAudioEngine::playAmbient(const char* name, float volume, double time) { auto _newName = shuffleIds->get(getRandomInt(0, shuffleIds->size() - 1)); - exists = getFilenameFullPath(filename, _newName->value_or(""), NxAudioEngineLayer::NXAUDIOENGINE_AMBIENT); + exists = getFilenameFullPath(filename, _newName->value_or(""), NxAudioEngineLayer::NXAUDIOENGINE_AMBIENT); } // Sequentially playback new Ambient ids, if any entry found for the current id @@ -1233,7 +1225,7 @@ bool NxAudioEngine::playAmbient(const char* name, float volume, double time) _ambientSequentialIndexes[name]++; - exists = getFilenameFullPath(filename, _newName->value_or(""), NxAudioEngineLayer::NXAUDIOENGINE_AMBIENT); + exists = getFilenameFullPath(filename, _newName->value_or(""), NxAudioEngineLayer::NXAUDIOENGINE_AMBIENT); } // Fade In time for this track, if configured @@ -1253,7 +1245,7 @@ bool NxAudioEngine::playAmbient(const char* name, float volume, double time) } } else - exists = getFilenameFullPath(filename, name, NxAudioEngineLayer::NXAUDIOENGINE_AMBIENT); + exists = getFilenameFullPath(filename, name, NxAudioEngineLayer::NXAUDIOENGINE_AMBIENT); if (trace_all || trace_ambient) ffnx_trace("NxAudioEngine::%s: %s\n", __func__, filename); @@ -1374,13 +1366,13 @@ bool NxAudioEngine::canPlayMovieAudio(const char* nameWithPath) { char filename[MAX_PATH]; - return getFilenameFullPath(filename, nameWithPath, NxAudioEngineLayer::NXAUDIOENGINE_MOVIE_AUDIO); + return getFilenameFullPath(filename, nameWithPath, NxAudioEngineLayer::NXAUDIOENGINE_MOVIE_AUDIO); } bool NxAudioEngine::playMovieAudio(const char* nameWithPath, int slot, float volume) { char filename[MAX_PATH]; - bool exists = getFilenameFullPath(filename, nameWithPath, NxAudioEngineLayer::NXAUDIOENGINE_MOVIE_AUDIO); + bool exists = getFilenameFullPath(filename, nameWithPath, NxAudioEngineLayer::NXAUDIOENGINE_MOVIE_AUDIO); if (trace_all || trace_movies) ffnx_trace("NxAudioEngine::%s: %s\n", __func__, filename); diff --git a/src/audio.h b/src/audio.h index af16b381..0f98c9b9 100644 --- a/src/audio.h +++ b/src/audio.h @@ -176,7 +176,7 @@ class NxAudioEngine std::map _sfxEffectsHandler; std::vector _sfxLazyUnloadChannels; - SoLoud::VGMStream* loadSFX(int id, bool loop = false); + SoLoud::VGMStream* loadSFX(std::string id, bool loop = false); void unloadSFXChannel(int channel); // MUSIC @@ -218,8 +218,7 @@ class NxAudioEngine // MISC // Returns false if the file does not exist - template - bool getFilenameFullPath(char *_out, T _key, NxAudioEngineLayer _type); + bool getFilenameFullPath(char *_out, const char* _key, NxAudioEngineLayer _type); bool fileExists(const char* filename); @@ -237,7 +236,6 @@ class NxAudioEngine // SFX int getSFXIdFromChannel(int channel); void unloadSFX(int id); - bool canPlaySFX(int id); bool playSFX(const char* name, int id, int channel, float panning, bool loop = false); void stopSFX(int channel, double time = 0); void pauseSFX(int channel);