refactor
This commit is contained in:
+24
-5
@@ -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
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user