diff --git a/framework/sound/CMakeLists.txt b/framework/sound/CMakeLists.txt index f90377d..0e64e64 100644 --- a/framework/sound/CMakeLists.txt +++ b/framework/sound/CMakeLists.txt @@ -19,21 +19,6 @@ target_link_libraries(sound_service ############################### -add_library(filesystem_soloud_file - ./src/mm/soloud_filesystem_file_impl.hpp - ./src/mm/soloud_filesystem_file_impl.cpp - - ./src/mm/sound_loader_wav.hpp - ./src/mm/sound_loader_wav.cpp -) - -target_link_libraries(filesystem_soloud_file - filesystem_service - soloud -) - -############################### - add_library(soloud_json ./src/mm/soloud_json.hpp ./src/mm/soloud_json.cpp @@ -46,6 +31,24 @@ target_link_libraries(soloud_json nlohmann_json::nlohmann_json ) +############################### + +add_library(filesystem_soloud_file + ./src/mm/soloud_filesystem_file_impl.hpp + ./src/mm/soloud_filesystem_file_impl.cpp + + ./src/mm/sound_loader_wav.hpp + ./src/mm/sound_loader_wav.cpp + ./src/mm/sound_loader_sfxr.hpp + ./src/mm/sound_loader_sfxr.cpp +) + +target_link_libraries(filesystem_soloud_file + filesystem_service + soloud + soloud_json +) + if (BUILD_TESTING) add_subdirectory(test) endif() diff --git a/framework/sound/src/mm/sound_loader_sfxr.cpp b/framework/sound/src/mm/sound_loader_sfxr.cpp new file mode 100644 index 0000000..31328cc --- /dev/null +++ b/framework/sound/src/mm/sound_loader_sfxr.cpp @@ -0,0 +1,38 @@ +#include "./sound_loader_sfxr.hpp" + +#include + +#include "./soloud_json.hpp" + +#include + + +namespace MM { + +std::shared_ptr<::SoLoud::Sfxr> SoundLoaderSfxrPreset::load(const ::SoLoud::Sfxr::SFXR_PRESETS preset, const int seed) const { + auto sfxr = std::make_shared<::SoLoud::Sfxr>(); + sfxr->loadPreset(preset, seed); + return sfxr; +} + +std::shared_ptr<::SoLoud::Sfxr> SoundLoaderSfxrJson::load(const nlohmann::json& j) const { + auto sfxr = std::make_shared<::SoLoud::Sfxr>(); + *sfxr = j; + return sfxr; +} + +std::shared_ptr<::SoLoud::Sfxr> SoundLoaderSfxrFile::load(const std::string& path, MM::Engine& engine) const { + auto& fs = engine.getService(); + + if (!fs.isFile(path.c_str())) { + // TODO: log error + return nullptr; + } + + auto j = fs.readJson(path.c_str()); + + return SoundLoaderSfxrJson{}.load(j); +} + +} // MM + diff --git a/framework/sound/src/mm/sound_loader_sfxr.hpp b/framework/sound/src/mm/sound_loader_sfxr.hpp new file mode 100644 index 0000000..7aa7243 --- /dev/null +++ b/framework/sound/src/mm/sound_loader_sfxr.hpp @@ -0,0 +1,28 @@ +#pragma once + +#include +#include + +#include + +#include + +namespace MM { + + // fwd + class Engine; + + struct SoundLoaderSfxrPreset { + std::shared_ptr<::SoLoud::Sfxr> load(const ::SoLoud::Sfxr::SFXR_PRESETS preset, const int seed) const; + }; + + struct SoundLoaderSfxrJson { + std::shared_ptr<::SoLoud::Sfxr> load(const nlohmann::json& j) const; + }; + + struct SoundLoaderSfxrFile { + std::shared_ptr<::SoLoud::Sfxr> load(const std::string& path, MM::Engine& engine) const; + }; + +} // MM + diff --git a/framework/sound/src/mm/sound_loader_wav.cpp b/framework/sound/src/mm/sound_loader_wav.cpp index a9522a2..9478426 100644 --- a/framework/sound/src/mm/sound_loader_wav.cpp +++ b/framework/sound/src/mm/sound_loader_wav.cpp @@ -9,8 +9,10 @@ namespace MM { std::shared_ptr<::SoLoud::Wav> SoundLoaderWavFile::load(const std::string& path, Engine& engine) const { auto& fs = engine.getService(); - if (!fs.isFile(path.c_str())) + if (!fs.isFile(path.c_str())) { + // TODO: log error return nullptr; + } auto h = fs.open(path.c_str()); @@ -19,7 +21,7 @@ std::shared_ptr<::SoLoud::Wav> SoundLoaderWavFile::load(const std::string& path, auto ptr = std::make_shared<::SoLoud::Wav>(); auto r = ptr->loadFile(&sl_f); if (r != ::SoLoud::SO_NO_ERROR) { - // log error + // TODO: log error return nullptr; } diff --git a/framework/sound/test/loader_test.cpp b/framework/sound/test/loader_test.cpp index 0014c96..0b0ac1a 100644 --- a/framework/sound/test/loader_test.cpp +++ b/framework/sound/test/loader_test.cpp @@ -5,9 +5,11 @@ #include #include #include +#include #include +#include #include #include #include @@ -21,7 +23,7 @@ using namespace entt::literals; extern char* argv0; -TEST(soloud_fs_loader, basic) { +TEST(soloud_fs_loader, wav) { MM::Engine engine; // setup @@ -59,3 +61,38 @@ TEST(soloud_fs_loader, basic) { rm.clear(); } +TEST(soloud_fs_loader, sfxr) { + MM::Engine engine; + + // setup + auto& sound = engine.addService(); + ASSERT_TRUE(engine.enableService()); + + engine.addService(argv0, "soloud_filesystem_loader_test"); + ASSERT_TRUE(engine.enableService()); + + auto& rm = MM::ResourceManager::ref(); + + sound.engine.setGlobalVolume(0.4f); + + ASSERT_FALSE(rm.contains("test_preset"_hs)); + rm.load("test_preset", ::SoLoud::Sfxr::SFXR_PRESETS::EXPLOSION, 0); + ASSERT_TRUE(rm.contains("test_preset"_hs)); + + // TODO: add load from json + // TODO: add load from json from file + + { + auto sh = rm.get("test_preset"_hs); + + sound.engine.play(*sh); + + while (sound.engine.getActiveVoiceCount()) { + using namespace std::chrono_literals; + std::this_thread::sleep_for(5ms); + } + } + + rm.clear(); +} +