diff --git a/Components/AudioToggle/AudioToggle.cpp b/Components/AudioToggle/AudioToggle.cpp index f3d74c540..d3d1d2a00 100644 --- a/Components/AudioToggle/AudioToggle.cpp +++ b/Components/AudioToggle/AudioToggle.cpp @@ -12,23 +12,28 @@ using namespace Audio; -Audio::Toggle::Toggle(Concurrency::AsyncTaskQueue &audio_queue) : - audio_queue_(audio_queue) {} +Audio::DAC::DAC(Concurrency::AsyncTaskQueue &audio_queue, const int16_t max) : + audio_queue_(audio_queue), max_output_(max) {} -void Toggle::set_sample_volume_range(const std::int16_t range) { - volume_ = range; - level_ = level_active_ ? volume_ : 0; +void DAC::update_level() { + level_ = (output_ * volume_) / max_output_; } -void Toggle::set_output(const bool enabled) { - if(is_enabled_ == enabled) return; - is_enabled_ = enabled; - audio_queue_.enqueue([this, enabled] { - level_active_ = enabled; - level_ = enabled ? volume_ : 0; +void DAC::set_sample_volume_range(const std::int16_t range) { + volume_ = range; + update_level(); +} + +void DAC::set_output(const int16_t output) { + if(set_output_ == output) return; + set_output_ = output; + + audio_queue_.enqueue([this, output] { + output_ = output; + update_level(); }); } -bool Toggle::get_output() const { - return is_enabled_; +int16_t DAC::get_output() const { + return set_output_; } diff --git a/Components/AudioToggle/AudioToggle.hpp b/Components/AudioToggle/AudioToggle.hpp index 06b6e168c..5847306b0 100644 --- a/Components/AudioToggle/AudioToggle.hpp +++ b/Components/AudioToggle/AudioToggle.hpp @@ -14,11 +14,11 @@ namespace Audio { /*! - Provides a sample source that can programmatically be set to one of two values. + Provides a sample source that can programmatically be set to a fixed value */ -class Toggle: public Outputs::Speaker::BufferSource { +class DAC: public Outputs::Speaker::BufferSource { public: - Toggle(Concurrency::AsyncTaskQueue &audio_queue); + DAC(Concurrency::AsyncTaskQueue &audio_queue, int16_t max); template void apply_samples(const std::size_t number_of_samples, Outputs::Speaker::MonoSample *const target) { @@ -29,17 +29,33 @@ public: return !level_; } - void set_output(bool enabled); - bool get_output() const; + void set_output(int16_t); + int16_t get_output() const; private: // Accessed on the calling thread. - bool is_enabled_ = false; + int16_t set_output_ = 0; Concurrency::AsyncTaskQueue &audio_queue_; // Accessed on the audio thread. int16_t level_ = 0, volume_ = 0; - bool level_active_ = false; + int16_t output_ = 0, max_output_ = 0; + void update_level(); +}; + +/*! + Provides a 1-bit specialisation of the DAC. +*/ +struct Toggle: public DAC { + Toggle(Concurrency::AsyncTaskQueue &audio_queue) : + DAC(audio_queue, 1) {} + + void set_output(const bool enabled) { + DAC::set_output(enabled); + } + bool get_output() const { + return bool(DAC::get_output()); + } }; }