From afdf5c6236c5fa2af209f79ef6cf13456580a06e Mon Sep 17 00:00:00 2001 From: Borin Ouch Date: Sun, 28 Apr 2024 20:32:43 -0700 Subject: [PATCH] Allow individual sound source to be enabled/disabled --- src/sound.cpp | 18 ++++++++++++------ src/sound.h | 10 +++++++++- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/src/sound.cpp b/src/sound.cpp index 328da31..1cb72e2 100644 --- a/src/sound.cpp +++ b/src/sound.cpp @@ -293,11 +293,14 @@ void SoundManager::render() { int sid_to_remove = -1; for (int sid = 0; sid < sources.size(); sid += 1) { + auto &pair = sources[sid]; ImGui::PushID(sid); ImGui::BeginChild("##Sound", ImVec2(0, 0), ImGuiChildFlags_Border | ImGuiChildFlags_AutoResizeY); - ImGui::Text("Sound #%d", (sid + 1)); + ImGui::Text("Sound #%d (%s)", (sid + 1), pair.source->name()); - sources[sid]->render(); + ImGui::Checkbox("Enable", &pair.enable); + + pair.source->render(); if (ImGui::Button("Remove")) { sid_to_remove = sid; @@ -319,9 +322,9 @@ void SoundManager::update(bool play_sound) { memset(samples.data(), 0, samples.size() * sizeof(float)); - for (auto &source : sources) { - source->update(play_sound, time); - const float* source_samples = source->get_samples(); + for (auto &pair : sources) { + pair.source->update(play_sound && pair.enable, time); + const float* source_samples = pair.source->get_samples(); for (int i = 0; i < buffer.size(); i += 1) { samples[i] += source_samples[i]; } @@ -337,7 +340,10 @@ void SoundManager::update(bool play_sound) { } void SoundManager::add_source(std::unique_ptr source) { - sources.push_back(std::move(source)); + sources.push_back(sound_source_pair { + std::move(source), + true, + }); } void SoundManager::remove_source_at(size_t index) { diff --git a/src/sound.h b/src/sound.h index 22e7944..6908b24 100644 --- a/src/sound.h +++ b/src/sound.h @@ -15,6 +15,7 @@ class SoundSource { public: virtual ~SoundSource() = default; + virtual const char* name() const = 0; virtual void render() = 0; virtual void update(bool play_sound, double time) = 0; @@ -30,6 +31,7 @@ class WaveGeneratorSource final : public SoundSource { public: virtual ~WaveGeneratorSource() = default; + virtual const char* name() const override { return "Wave Generator"; } virtual void render() override; virtual void update(bool play_sound, double time) override; @@ -45,6 +47,7 @@ class WaveFileSource final : public SoundSource { public: virtual ~WaveFileSource(); + virtual const char* name() const override { return "Wave File"; } virtual void render() override; virtual void update(bool play_sound, double time) override; @@ -75,9 +78,14 @@ class SoundManager final { void remove_source_at(size_t index); private: + struct sound_source_pair { + std::unique_ptr source; + bool enable; + }; + double time = 0; AudioStream stream; std::array samples; std::array buffer; - std::vector> sources; + std::vector sources; };