From fd73c24fc34d1306a538070798d7492de3479013 Mon Sep 17 00:00:00 2001 From: Thomas Harte Date: Thu, 1 Feb 2024 21:32:16 -0500 Subject: [PATCH] Use `std::fill`; update volume with slider. --- Components/AudioToggle/AudioToggle.cpp | 8 +++++--- Components/AudioToggle/AudioToggle.hpp | 1 + 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/Components/AudioToggle/AudioToggle.cpp b/Components/AudioToggle/AudioToggle.cpp index 7188badd9..ee8e2b417 100644 --- a/Components/AudioToggle/AudioToggle.cpp +++ b/Components/AudioToggle/AudioToggle.cpp @@ -8,19 +8,20 @@ #include "AudioToggle.hpp" +#include + using namespace Audio; Audio::Toggle::Toggle(Concurrency::AsyncTaskQueue &audio_queue) : audio_queue_(audio_queue) {} void Toggle::get_samples(std::size_t number_of_samples, std::int16_t *target) { - for(std::size_t sample = 0; sample < number_of_samples; ++sample) { - target[sample] = level_; - } + std::fill(target, target + number_of_samples, level_); } void Toggle::set_sample_volume_range(std::int16_t range) { volume_ = range; + level_ = level_active_ ? volume_ : 0; } void Toggle::skip_samples(std::size_t) {} @@ -29,6 +30,7 @@ void Toggle::set_output(bool enabled) { if(is_enabled_ == enabled) return; is_enabled_ = enabled; audio_queue_.enqueue([this, enabled] { + level_active_ = enabled; level_ = enabled ? volume_ : 0; }); } diff --git a/Components/AudioToggle/AudioToggle.hpp b/Components/AudioToggle/AudioToggle.hpp index 8209b4653..defd7a976 100644 --- a/Components/AudioToggle/AudioToggle.hpp +++ b/Components/AudioToggle/AudioToggle.hpp @@ -34,6 +34,7 @@ class Toggle: public Outputs::Speaker::SampleSource { // Accessed on the audio thread. int16_t level_ = 0, volume_ = 0; + bool level_active_ = false; }; }