audio-related minor refactor

This commit is contained in:
MihailRis 2024-04-10 20:11:43 +03:00
parent c8ca56fb8e
commit 1549a02731
5 changed files with 20 additions and 28 deletions

View File

@ -207,10 +207,10 @@ ALSpeaker::~ALSpeaker() {
} }
} }
void ALSpeaker::update(const Channel* channel, float masterVolume) { void ALSpeaker::update(const Channel* channel) {
if (source == 0) if (source == 0)
return; return;
float gain = this->volume * channel->getVolume()*masterVolume; float gain = this->volume * channel->getVolume();
AL_CHECK(alSourcef(source, AL_GAIN, gain)); AL_CHECK(alSourcef(source, AL_GAIN, gain));
if (!paused) { if (!paused) {
@ -471,6 +471,7 @@ void ALAudio::setListener(glm::vec3 position, glm::vec3 velocity, glm::vec3 at,
AL_CHECK(alListener3f(AL_POSITION, position.x, position.y, position.z)); AL_CHECK(alListener3f(AL_POSITION, position.x, position.y, position.z));
AL_CHECK(alListener3f(AL_VELOCITY, velocity.x, velocity.y, velocity.z)); AL_CHECK(alListener3f(AL_VELOCITY, velocity.x, velocity.y, velocity.z));
AL_CHECK(alListenerfv(AL_ORIENTATION, listenerOri)); AL_CHECK(alListenerfv(AL_ORIENTATION, listenerOri));
AL_CHECK(alListenerf(AL_GAIN, get_channel(0)->getVolume()));
} }
void ALAudio::update(double delta) { void ALAudio::update(double delta) {

View File

@ -91,7 +91,7 @@ namespace audio {
ALSpeaker(ALAudio* al, uint source, int priority, int channel); ALSpeaker(ALAudio* al, uint source, int priority, int channel);
~ALSpeaker(); ~ALSpeaker();
void update(const Channel* channel, float masterVolume) override; void update(const Channel* channel) override;
int getChannel() const override; int getChannel() const override;
State getState() const override; State getState() const override;

View File

@ -383,13 +383,12 @@ void audio::update(double delta) {
entry.second->update(delta); entry.second->update(delta);
} }
float masterVolume = channels.at(0)->getVolume();
for (auto it = speakers.begin(); it != speakers.end();) { for (auto it = speakers.begin(); it != speakers.end();) {
auto speaker = it->second.get(); auto speaker = it->second.get();
int speakerChannel = speaker->getChannel(); int speakerChannel = speaker->getChannel();
auto channel = get_channel(speakerChannel); auto channel = get_channel(speakerChannel);
if (channel != nullptr) { if (channel != nullptr) {
speaker->update(channel, speakerChannel == 0 ? 1.0f : masterVolume); speaker->update(channel);
} }
if (speaker->isStopped()) { if (speaker->isStopped()) {
streams.erase(it->first); streams.erase(it->first);

View File

@ -235,8 +235,7 @@ namespace audio {
/// @brief Synchronize the speaker with channel settings /// @brief Synchronize the speaker with channel settings
/// @param channel speaker channel /// @param channel speaker channel
/// @param masterVolume volume of the master channel virtual void update(const Channel* channel) = 0;
virtual void update(const Channel* channel, float masterVolume) = 0;
/// @brief Check speaker channel index /// @brief Check speaker channel index
virtual int getChannel() const = 0; virtual int getChannel() const = 0;

View File

@ -46,6 +46,15 @@ void addWorldGenerators() {
WorldGenerators::addGenerator<FlatWorldGenerator>("core:flat"); WorldGenerators::addGenerator<FlatWorldGenerator>("core:flat");
} }
inline void create_channel(std::string name, NumberSetting& setting) {
if (name != "master") {
audio::create_channel(name);
}
setting.observe([=](auto value) {
audio::get_channel(name)->setVolume(value*value);
});
}
Engine::Engine(EngineSettings& settings, EnginePaths* paths) Engine::Engine(EngineSettings& settings, EnginePaths* paths)
: settings(settings), settingsHandler(settings), paths(paths) : settings(settings), settingsHandler(settings), paths(paths)
{ {
@ -53,32 +62,16 @@ Engine::Engine(EngineSettings& settings, EnginePaths* paths)
throw initialize_error("could not initialize window"); throw initialize_error("could not initialize window");
} }
audio::initialize(settings.audio.enabled); audio::initialize(settings.audio.enabled);
audio::create_channel("regular"); create_channel("master", settings.audio.volumeMaster);
audio::create_channel("music"); create_channel("regular", settings.audio.volumeRegular);
audio::create_channel("ambient"); create_channel("music", settings.audio.volumeMusic);
audio::create_channel("ui"); create_channel("ambient", settings.audio.volumeAmbient);
create_channel("ui", settings.audio.volumeUI);
settings.audio.volumeMaster.observe([=](auto value) {
audio::get_channel("master")->setVolume(value*value);
});
settings.audio.volumeRegular.observe([=](auto value) {
audio::get_channel("regular")->setVolume(value*value);
});
settings.audio.volumeUI.observe([=](auto value) {
audio::get_channel("ui")->setVolume(value*value);
});
settings.audio.volumeAmbient.observe([=](auto value) {
audio::get_channel("ambient")->setVolume(value*value);
});
settings.audio.volumeMusic.observe([=](auto value) {
audio::get_channel("music")->setVolume(value*value);
});
auto resdir = paths->getResources(); auto resdir = paths->getResources();
logger.info() << "loading assets"; logger.info() << "loading assets";
std::vector<fs::path> roots {resdir}; std::vector<fs::path> roots {resdir};
resPaths = std::make_unique<ResPaths>(resdir, roots); resPaths = std::make_unique<ResPaths>(resdir, roots);
try { try {
loadAssets(); loadAssets();