Skip to content

Commit

Permalink
Allow individual sound source to be enabled/disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
aceiii committed Apr 29, 2024
1 parent 4b5cd6a commit afdf5c6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
18 changes: 12 additions & 6 deletions src/sound.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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];
}
Expand All @@ -337,7 +340,10 @@ void SoundManager::update(bool play_sound) {
}

void SoundManager::add_source(std::unique_ptr<SoundSource> 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) {
Expand Down
10 changes: 9 additions & 1 deletion src/sound.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;

Expand All @@ -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;

Expand Down Expand Up @@ -75,9 +78,14 @@ class SoundManager final {
void remove_source_at(size_t index);

private:
struct sound_source_pair {
std::unique_ptr<SoundSource> source;
bool enable;
};

double time = 0;
AudioStream stream;
std::array<float, kMaxSamplesPerUpdate> samples;
std::array<short, kMaxSamplesPerUpdate> buffer;
std::vector<std::unique_ptr<SoundSource>> sources;
std::vector<sound_source_pair> sources;
};

0 comments on commit afdf5c6

Please sign in to comment.