Subsistema d'àudio: port de ../aee/core/audio refactoritzat amb namespace Ja, desacoblat d'Options via Audio::Config i extensió de Resource::Cache per preloading de música i sons; pyramid_1.ogg sona a GameScene (v0.15.0)

This commit is contained in:
JailDesigner committed 2026-04-19 23:19:20 +02:00
1 parent 6f3fafbfff
commit e2b8d0e459
16 files changed
+6796 -5

No files matched your search

+60
View File
@@ -4,10 +4,21 @@
#include <iostream>
#include <stdexcept>
#include "core/audio/jail_audio.hpp"
#include "core/resources/palette_loader.hpp"
#include "core/resources/resource_helper.hpp"
#include "utils/utils.hpp"
// Deleters dels recursos d'àudio: necessiten veure el tipus complet de
// Ja::Music / Ja::Sound (definits a jail_audio.hpp), per això van ací.
void MusicDeleter::operator()(Ja::Music* music) const noexcept {
if (music != nullptr) { Ja::deleteMusic(music); }
}
void SoundDeleter::operator()(Ja::Sound* sound) const noexcept {
if (sound != nullptr) { Ja::deleteSound(sound); }
}
namespace Resource {
auto Cache::get() -> Cache& {
@@ -62,4 +73,53 @@ namespace Resource {
return it->palette;
}
void Cache::loadMusic(const std::string& name, const std::string& file_path) {
auto bytes = Helper::loadFile(file_path);
if (bytes.empty()) {
throw std::runtime_error("Cache: cannot load music " + file_path);
}
Ja::Music* raw = Ja::loadMusic(bytes.data(), static_cast<Uint32>(bytes.size()), file_path.c_str());
if (raw == nullptr) {
throw std::runtime_error("Cache: Ja::loadMusic failed for " + file_path);
}
musics_.emplace_back(MusicResource{.name = name, .music = std::unique_ptr<Ja::Music, MusicDeleter>(raw)});
printWithDots("Music : ", getFileName(file_path), "[ LOADED ]");
}
void Cache::loadSound(const std::string& name, const std::string& file_path) {
auto bytes = Helper::loadFile(file_path);
if (bytes.empty()) {
throw std::runtime_error("Cache: cannot load sound " + file_path);
}
Ja::Sound* raw = Ja::loadSound(bytes.data(), static_cast<std::uint32_t>(bytes.size()));
if (raw == nullptr) {
throw std::runtime_error("Cache: Ja::loadSound failed for " + file_path);
}
sounds_.emplace_back(SoundResource{.name = name, .sound = std::unique_ptr<Ja::Sound, SoundDeleter>(raw)});
printWithDots("Sound : ", getFileName(file_path), "[ LOADED ]");
}
auto Cache::getMusic(const std::string& name) -> Ja::Music* {
auto it = std::ranges::find_if(musics_, [&name](const auto& m) { return m.name == name; });
if (it == musics_.end()) {
std::cerr << "Cache: music not found: " << name << '\n';
throw std::runtime_error("Music not found: " + name);
}
return it->music.get();
}
auto Cache::getSound(const std::string& name) -> Ja::Sound* {
auto it = std::ranges::find_if(sounds_, [&name](const auto& s) { return s.name == name; });
if (it == sounds_.end()) {
std::cerr << "Cache: sound not found: " << name << '\n';
throw std::runtime_error("Sound not found: " + name);
}
return it->sound.get();
}
void Cache::clearAudio() {
musics_.clear();
sounds_.clear();
}
} // namespace Resource