This commit is contained in:
MihailRis
2025-01-22 08:18:17 +03:00
parent da10151d5a
commit 02554e469a
6 changed files with 67 additions and 35 deletions
+24 -5
View File
@@ -9,19 +9,21 @@
#include "AL/ALAudio.hpp"
#include "NoAudio.hpp"
#include "debug/Logger.hpp"
#include "util/ObjectsKeeper.hpp"
static debug::Logger logger("audio");
namespace audio {
using namespace audio;
namespace {
static speakerid_t nextId = 1;
static Backend* backend;
static std::unordered_map<speakerid_t, std::unique_ptr<Speaker>> speakers;
static std::unordered_map<speakerid_t, std::shared_ptr<Stream>> streams;
static std::vector<std::unique_ptr<Channel>> channels;
static util::ObjectsKeeper objects_keeper {};
}
using namespace audio;
Channel::Channel(std::string name) : name(std::move(name)) {
}
@@ -148,7 +150,8 @@ public:
}
};
void audio::initialize(bool enabled) {
void audio::initialize(bool enabled, AudioSettings& settings) {
enabled = enabled && settings.enabled.get();
if (enabled) {
logger.info() << "initializing ALAudio backend";
backend = ALAudio::create().release();
@@ -160,7 +163,22 @@ void audio::initialize(bool enabled) {
logger.info() << "initializing NoAudio backend";
backend = NoAudio::create().release();
}
create_channel("master");
struct {
std::string name;
NumberSetting* setting;
} builtin_channels[] {
{"master", &settings.volumeMaster},
{"regular", &settings.volumeRegular},
{"music", &settings.volumeMusic},
{"ambient", &settings.volumeAmbient},
{"ui", &settings.volumeUI}
};
for (auto& channel : builtin_channels) {
create_channel(channel.name);
objects_keeper.keepAlive(channel.setting->observe([=](auto value) {
audio::get_channel(channel.name)->setVolume(value * value);
}, true));
}
}
std::unique_ptr<PCM> audio::load_PCM(const fs::path& file, bool headerOnly) {
@@ -442,4 +460,5 @@ void audio::close() {
speakers.clear();
delete backend;
backend = nullptr;
objects_keeper.clearKeepedObjects();
}
+2 -1
View File
@@ -6,6 +6,7 @@
#include <vector>
#include "typedefs.hpp"
#include "settings.hpp"
namespace fs = std::filesystem;
@@ -357,7 +358,7 @@ namespace audio {
/// @brief Initialize audio system or use no audio mode
/// @param enabled try to initialize actual audio
void initialize(bool enabled);
void initialize(bool enabled, AudioSettings& settings);
/// @brief Load audio file info and PCM data
/// @param file audio file