Expand toggle into a full-on DAC.

This commit is contained in:
Thomas Harte committed 2026-03-28 23:10:55 -04:00
1 parent 5309d49ae8
commit 8c357bd6dd
2 files changed
+41 -20

No files matched your search

+18 -13
View File
@@ -12,23 +12,28 @@
using namespace Audio;
Audio::Toggle::Toggle(Concurrency::AsyncTaskQueue<false> &audio_queue) :
audio_queue_(audio_queue) {}
Audio::DAC::DAC(Concurrency::AsyncTaskQueue<false> &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_;
}
+23 -7
View File
@@ -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<Toggle, false> {
class DAC: public Outputs::Speaker::BufferSource<DAC, false> {
public:
Toggle(Concurrency::AsyncTaskQueue<false> &audio_queue);
DAC(Concurrency::AsyncTaskQueue<false> &audio_queue, int16_t max);
template <Outputs::Speaker::Action action>
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<false> &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<false> &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());
}
};
}