diff options
-rw-r--r-- | src/crepe/api/AudioSource.cpp | 1 | ||||
-rw-r--r-- | src/crepe/api/AudioSource.h | 2 | ||||
-rw-r--r-- | src/crepe/api/Config.h | 25 | ||||
-rw-r--r-- | src/crepe/facade/Sound.h | 7 | ||||
-rw-r--r-- | src/crepe/facade/SoundContext.cpp | 31 | ||||
-rw-r--r-- | src/crepe/facade/SoundContext.h | 10 | ||||
-rw-r--r-- | src/crepe/system/AudioSystem.cpp | 30 | ||||
-rw-r--r-- | src/crepe/system/AudioSystem.h | 2 | ||||
-rw-r--r-- | src/crepe/util/Log.cpp | 2 | ||||
-rw-r--r-- | src/crepe/util/Log.h | 16 |
10 files changed, 89 insertions, 37 deletions
diff --git a/src/crepe/api/AudioSource.cpp b/src/crepe/api/AudioSource.cpp index 4baac9a..c646aeb 100644 --- a/src/crepe/api/AudioSource.cpp +++ b/src/crepe/api/AudioSource.cpp @@ -15,6 +15,5 @@ void AudioSource::play(bool looping) { void AudioSource::stop() { this->playing = false; - this->rewind = true; } diff --git a/src/crepe/api/AudioSource.h b/src/crepe/api/AudioSource.h index 9d76f0b..8dc1645 100644 --- a/src/crepe/api/AudioSource.h +++ b/src/crepe/api/AudioSource.h @@ -44,8 +44,6 @@ private: //! If this source is playing audio bool playing = false; - //! Rewind the sample location - bool rewind = false; private: //! AudioSystem::ComponentPrivate diff --git a/src/crepe/api/Config.h b/src/crepe/api/Config.h index 693400a..7be506e 100644 --- a/src/crepe/api/Config.h +++ b/src/crepe/api/Config.h @@ -1,8 +1,10 @@ #pragma once +#include <string> + #include "../util/Log.h" + #include "types.h" -#include <string> namespace crepe { @@ -18,7 +20,20 @@ struct Config final { static Config & get_instance(); //! Logging-related settings - Log::Config log; + struct { + /** + * \brief Log level + * + * Only messages with equal or higher priority than this value will be logged. + */ + Log::Level level = Log::Level::INFO; + /** + * \brief Colored log output + * + * Enables log coloring using ANSI escape codes. + */ + bool color = true; + } log; //! Save manager struct { @@ -62,6 +77,12 @@ struct Config final { */ std::string root_pattern = ".crepe-root"; } asset; + + //! Audio system settings + struct { + //! Max amount of simultanious voices + unsigned int voices = 32; + } audio; }; } // namespace crepe diff --git a/src/crepe/facade/Sound.h b/src/crepe/facade/Sound.h index 10d7c3c..35bccdb 100644 --- a/src/crepe/facade/Sound.h +++ b/src/crepe/facade/Sound.h @@ -20,13 +20,8 @@ public: Sound(const Asset & src); ~Sound(); // dbg_trace - class Handle { - private: + struct Handle { SoLoud::handle handle; - float volume = 1.0f; - bool looping = false; - - friend class SoundContext; }; private: diff --git a/src/crepe/facade/SoundContext.cpp b/src/crepe/facade/SoundContext.cpp index b65dfb2..3e9a3d1 100644 --- a/src/crepe/facade/SoundContext.cpp +++ b/src/crepe/facade/SoundContext.cpp @@ -6,10 +6,37 @@ using namespace crepe; SoundContext::SoundContext() { dbg_trace(); - engine.init(); + this->engine.init(); + this->engine.setMaxActiveVoiceCount(this->config.audio.voices); } SoundContext::~SoundContext() { dbg_trace(); - engine.deinit(); + this->engine.deinit(); } + +Sound::Handle SoundContext::play(Sound & resource) { + return { + .handle = this->engine.play(resource.sample, this->default_volume), + }; +} + +void SoundContext::stop(Sound::Handle & handle) { + this->engine.stop(handle.handle); +} + +void SoundContext::set_volume(Sound & resource, Sound::Handle & handle, float volume) { + this->engine.setVolume(handle.handle, volume); + this->default_volume = volume; +} + +void SoundContext::set_loop(Sound & resource, Sound::Handle & handle, bool loop) { + this->engine.setLooping(handle.handle, loop); +} + +bool SoundContext::get_playing(Sound::Handle & handle) { + // See Soloud::stopVoice_internal in soloud/src/core/soloud_core_voiceops.cpp for why this is + // the correct method to use here (samples are currently never paused) + return this->engine.isValidVoiceHandle(handle.handle); +} + diff --git a/src/crepe/facade/SoundContext.h b/src/crepe/facade/SoundContext.h index 286ced8..e02977e 100644 --- a/src/crepe/facade/SoundContext.h +++ b/src/crepe/facade/SoundContext.h @@ -2,6 +2,8 @@ #include <soloud/soloud.h> +#include "../api/Config.h" + #include "Sound.h" namespace crepe { @@ -22,9 +24,17 @@ public: SoundContext & operator=(const SoundContext &) = delete; SoundContext & operator=(SoundContext &&) = delete; + Sound::Handle play(Sound & resource); + void stop(Sound::Handle &); + void set_volume(Sound &, Sound::Handle &, float); + void set_loop(Sound &, Sound::Handle &, bool); + bool get_playing(Sound::Handle &); private: SoLoud::Soloud engine; + float default_volume = 1.0f; + + Config & config = Config::get_instance(); }; } // namespace crepe diff --git a/src/crepe/system/AudioSystem.cpp b/src/crepe/system/AudioSystem.cpp index 191dbbb..98aff58 100644 --- a/src/crepe/system/AudioSystem.cpp +++ b/src/crepe/system/AudioSystem.cpp @@ -20,6 +20,7 @@ void AudioSystem::update() { if (component.private_data.empty()) { auto & data = component.private_data.set<ComponentPrivate>(); this->update_last(component, data); + data.last_playing = false; // always start } auto & data = component.private_data.get<ComponentPrivate>(); @@ -29,18 +30,35 @@ void AudioSystem::update() { } } -void AudioSystem::diff_update(AudioSource & component, const ComponentPrivate & data, Sound & resource) { +void AudioSystem::diff_update(AudioSource & component, ComponentPrivate & data, Sound & resource) { bool update_playing = component.playing != data.last_playing; bool update_volume = component.volume != data.last_volume; bool update_loop = component.loop != data.last_loop; bool update_active = component.active != data.last_active; - if (update_active) - if (component.rewind) { - component.playing = false; - // this->context.rewind(resource, data.handle); + if (update_active) { + if (component.active) { + update_playing = true; + if (component.play_on_awake) + component.playing = true; + } else { + this->context.stop(data.handle); + return; + } + } + if (!component.active) return; + if (update_playing) { + if (component.playing) data.handle = this->context.play(resource); + else this->context.stop(data.handle); + } else { + component.playing = this->context.get_playing(data.handle); + } + if (update_volume) { + this->context.set_volume(resource, data.handle, component.volume); + } + if (update_loop) { + this->context.set_loop(resource, data.handle, component.loop); } - } void AudioSystem::update_last(const AudioSource & component, ComponentPrivate & data) { diff --git a/src/crepe/system/AudioSystem.h b/src/crepe/system/AudioSystem.h index 4650178..3404878 100644 --- a/src/crepe/system/AudioSystem.h +++ b/src/crepe/system/AudioSystem.h @@ -34,7 +34,7 @@ private: void update_last(const AudioSource & component, ComponentPrivate & data); - void diff_update(AudioSource & component, const ComponentPrivate & data, Sound & resource); + void diff_update(AudioSource & component, ComponentPrivate & data, Sound & resource); private: SoundContext context {}; diff --git a/src/crepe/util/Log.cpp b/src/crepe/util/Log.cpp index bc86c7e..84d80a8 100644 --- a/src/crepe/util/Log.cpp +++ b/src/crepe/util/Log.cpp @@ -25,7 +25,7 @@ string Log::prefix(const Level & level) { } void Log::log(const Level & level, const string & msg) { - auto & cfg = crepe::Config::get_instance(); + auto & cfg = Config::get_instance(); if (level < cfg.log.level) return; string out = Log::prefix(level) + msg; diff --git a/src/crepe/util/Log.h b/src/crepe/util/Log.h index 914145a..fc0bb3a 100644 --- a/src/crepe/util/Log.h +++ b/src/crepe/util/Log.h @@ -77,22 +77,6 @@ private: * \return Colored message severity prefix string */ static std::string prefix(const Level & level); - -public: - struct Config { - /** - * \brief Log level - * - * Only messages with equal or higher priority than this value will be logged. - */ - Level level = INFO; - /** - * \brief Colored log output - * - * Enables log coloring using ANSI escape codes. - */ - bool color = true; - }; }; } // namespace crepe |