From eefaeb807eb5d0b08353389070bf3d27c3d0d958 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Sat, 30 Nov 2024 16:32:13 +0100 Subject: test and debug audio system --- src/crepe/facade/SoundContext.h | 8 +- src/crepe/system/AudioSystem.cpp | 28 ++++--- src/crepe/system/AudioSystem.h | 4 +- src/crepe/system/System.cpp | 4 +- src/test/AudioTest.cpp | 174 ++++++++++++++++++++++++++++----------- 5 files changed, 150 insertions(+), 68 deletions(-) (limited to 'src') diff --git a/src/crepe/facade/SoundContext.h b/src/crepe/facade/SoundContext.h index 91d3fe9..c651cd5 100644 --- a/src/crepe/facade/SoundContext.h +++ b/src/crepe/facade/SoundContext.h @@ -24,10 +24,10 @@ 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); + virtual Sound::Handle play(Sound & resource); + virtual void stop(Sound::Handle &); + virtual void set_volume(Sound &, Sound::Handle &, float); + virtual void set_loop(Sound &, Sound::Handle &, bool); private: SoLoud::Soloud engine; diff --git a/src/crepe/system/AudioSystem.cpp b/src/crepe/system/AudioSystem.cpp index b105a4d..84a101a 100644 --- a/src/crepe/system/AudioSystem.cpp +++ b/src/crepe/system/AudioSystem.cpp @@ -29,39 +29,43 @@ void AudioSystem::update() { } void AudioSystem::diff_update(AudioSource & component, ComponentPrivate & data, Sound & resource) { - bool update_volume = component.volume != data.last_volume; - bool update_loop = component.loop != data.last_loop; - bool update_active = component.active != data.last_active; + SoundContext & context = this->get_context(); - if (update_active) { + if (component.active != data.last_active) { if (component.active) { component.oneshot_play = component.play_on_awake; } else { - this->context.stop(data.handle); + context.stop(data.handle); return; } } if (!component.active) return; + if (component.oneshot_play) { - data.handle = this->context.play(resource); + data.handle = context.play(resource); component.oneshot_play = false; } if (component.oneshot_stop) { - this->context.stop(data.handle); + context.stop(data.handle); component.oneshot_stop = false; } - if (update_volume) { - this->context.set_volume(resource, data.handle, component.volume); + if (component.volume != data.last_volume) { + context.set_volume(resource, data.handle, component.volume); } - if (update_loop) { - this->context.set_loop(resource, data.handle, component.loop); + if (component.loop != data.last_loop) { + context.set_loop(resource, data.handle, component.loop); } } void AudioSystem::update_last(const AudioSource & component, ComponentPrivate & data) { data.last_active = component.active; - if (!component.active) return; data.last_loop = component.loop; data.last_volume = component.volume; } +SoundContext & AudioSystem::get_context() { + if (this->context.empty()) + this->context.set(); + return this->context.get(); +} + diff --git a/src/crepe/system/AudioSystem.h b/src/crepe/system/AudioSystem.h index dee82f6..a004c60 100644 --- a/src/crepe/system/AudioSystem.h +++ b/src/crepe/system/AudioSystem.h @@ -35,8 +35,10 @@ private: void diff_update(AudioSource & component, ComponentPrivate & data, Sound & resource); +protected: + virtual SoundContext & get_context(); private: - SoundContext context {}; + Private context; }; } // namespace crepe diff --git a/src/crepe/system/System.cpp b/src/crepe/system/System.cpp index f68549b..ecc740d 100644 --- a/src/crepe/system/System.cpp +++ b/src/crepe/system/System.cpp @@ -1,7 +1,5 @@ -#include "../util/Log.h" - #include "System.h" using namespace crepe; -System::System(const Mediator & mediator) : mediator(mediator) { dbg_trace(); } +System::System(const Mediator & mediator) : mediator(mediator) {} diff --git a/src/test/AudioTest.cpp b/src/test/AudioTest.cpp index 2a2d0e1..9c3cb9c 100644 --- a/src/test/AudioTest.cpp +++ b/src/test/AudioTest.cpp @@ -1,6 +1,5 @@ -#include "util/Log.h" #include -#include +#include #include #include @@ -14,62 +13,141 @@ using namespace crepe; using namespace testing; class AudioTest : public Test { +private: + class TestSoundContext : public SoundContext { + public: + MOCK_METHOD(Sound::Handle, play, (Sound & resource), (override)); + MOCK_METHOD(void, stop, (Sound::Handle &), (override)); + MOCK_METHOD(void, set_volume, (Sound &, Sound::Handle &, float), (override)); + MOCK_METHOD(void, set_loop, (Sound &, Sound::Handle &, bool), (override)); + }; + + class TestAudioSystem : public AudioSystem { + public: + using AudioSystem::AudioSystem; + StrictMock context; + virtual SoundContext & get_context() { + return this->context; + } + }; + +private: Mediator mediator; -public: ComponentManager component_manager{mediator}; ResourceManager resource_manager{mediator}; - AudioSystem system {mediator}; +public: + TestAudioSystem system {mediator}; + TestSoundContext & context = system.context; private: GameObject entity = component_manager.new_object("name"); public: - AudioSource & bgm = entity.add_component("mwe/audio/bgm.ogg"); - AudioSource & sfx1 = entity.add_component("mwe/audio/sfx1.wav"); - AudioSource & sfx2 = entity.add_component("mwe/audio/sfx2.wav"); - AudioSource & sfx3 = entity.add_component("mwe/audio/sfx3.wav"); - - void SetUp() override { - bgm.play_on_awake = true; - } + AudioSource & component = entity.add_component("mwe/audio/bgm.ogg"); }; TEST_F(AudioTest, Default) { - bool example_done = false; - - future example = async([&](){ - // Start the background track. This happens automatically due to the play_on_awake property - // being true. The following call is optional and doesn't start two simultanious voices if - // left in: - // bgm.play(); - - // Play each sample sequentially while pausing and resuming the background track - this_thread::sleep_for(500ms); - sfx1.play(); - this_thread::sleep_for(500ms); - sfx2.play(); - bgm.active = false; - this_thread::sleep_for(500ms); - sfx3.play(); - bgm.active = true; - this_thread::sleep_for(500ms); - - // Play all samples simultaniously - sfx1.play(); - sfx2.play(); - sfx3.play(); - this_thread::sleep_for(1000ms); - }); - - future system_loop = async([&](){ - while (!example_done) { - auto next = chrono::steady_clock::now() + 25ms; - system.update(); - this_thread::sleep_until(next); - } - }); + EXPECT_CALL(context, play(_)).Times(0); + EXPECT_CALL(context, stop(_)).Times(0); + EXPECT_CALL(context, set_volume(_, _, _)).Times(0); + EXPECT_CALL(context, set_loop(_, _, _)).Times(0); + system.update(); +} + +TEST_F(AudioTest, Play) { + system.update(); + + { + InSequence seq; - example.wait(); - example_done = true; - system_loop.wait(); + EXPECT_CALL(context, play(_)).Times(0); + component.play(); + } + + { + InSequence seq; + + EXPECT_CALL(context, play(_)).Times(1); + system.update(); + } +} + +TEST_F(AudioTest, Stop) { + system.update(); + + { + InSequence seq; + + EXPECT_CALL(context, stop(_)).Times(0); + component.stop(); + } + + { + InSequence seq; + + EXPECT_CALL(context, stop(_)).Times(1); + system.update(); + } +} + +TEST_F(AudioTest, Volume) { + system.update(); + + { + InSequence seq; + + EXPECT_CALL(context, set_volume(_, _, _)).Times(0); + component.volume += 0.2; + } + + { + InSequence seq; + + EXPECT_CALL(context, set_volume(_, _, component.volume)).Times(1); + system.update(); + } +} + +TEST_F(AudioTest, Looping) { + system.update(); + + { + InSequence seq; + + EXPECT_CALL(context, set_loop(_, _, _)).Times(0); + component.loop = !component.loop; + } + + { + InSequence seq; + + EXPECT_CALL(context, set_loop(_, _, component.loop)).Times(1); + system.update(); + } +} + +TEST_F(AudioTest, StopOnDeactivate) { + system.update(); + + { + InSequence seq; + + EXPECT_CALL(context, stop(_)).Times(1); + component.active = false; + system.update(); + } +} + +TEST_F(AudioTest, PlayOnActive) { + component.active = false; + component.play_on_awake = true; + system.update(); + + { + InSequence seq; + + EXPECT_CALL(context, play(_)).Times(1); + component.active = true; + system.update(); + } } -- cgit v1.2.3