2017-12-18 02:26:06 +00:00
|
|
|
//
|
2021-11-21 20:37:29 +00:00
|
|
|
// LowpassSpeaker.hpp
|
2017-12-18 02:26:06 +00:00
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 15/12/2017.
|
2018-05-13 19:19:52 +00:00
|
|
|
// Copyright 2017 Thomas Harte. All rights reserved.
|
2017-12-18 02:26:06 +00:00
|
|
|
//
|
|
|
|
|
2024-01-17 04:34:46 +00:00
|
|
|
#pragma once
|
2017-12-18 02:26:06 +00:00
|
|
|
|
2024-02-12 15:55:52 +00:00
|
|
|
#include "BufferSource.hpp"
|
2017-12-18 02:26:06 +00:00
|
|
|
#include "../Speaker.hpp"
|
|
|
|
#include "../../../SignalProcessing/FIRFilter.hpp"
|
|
|
|
#include "../../../ClockReceiver/ClockReceiver.hpp"
|
|
|
|
#include "../../../Concurrency/AsyncTaskQueue.hpp"
|
|
|
|
|
2021-11-21 20:37:29 +00:00
|
|
|
#include <algorithm>
|
2021-12-02 18:00:25 +00:00
|
|
|
#include <cassert>
|
2019-07-08 22:14:03 +00:00
|
|
|
#include <cmath>
|
2021-12-02 18:00:25 +00:00
|
|
|
#include <cstring>
|
|
|
|
#include <mutex>
|
2017-12-19 02:39:23 +00:00
|
|
|
|
2023-05-10 21:02:18 +00:00
|
|
|
namespace Outputs::Speaker {
|
2017-12-18 02:26:06 +00:00
|
|
|
|
2021-11-21 20:37:29 +00:00
|
|
|
template <typename ConcreteT, bool is_stereo> class LowpassBase: public Speaker {
|
2017-12-18 02:26:06 +00:00
|
|
|
public:
|
|
|
|
/*!
|
|
|
|
Sets the clock rate of the input audio.
|
|
|
|
*/
|
|
|
|
void set_input_rate(float cycles_per_second) {
|
2020-06-15 04:24:10 +00:00
|
|
|
std::lock_guard lock_guard(filter_parameters_mutex_);
|
2020-02-09 21:32:32 +00:00
|
|
|
if(filter_parameters_.input_cycles_per_second == cycles_per_second) {
|
|
|
|
return;
|
|
|
|
}
|
2017-12-18 02:26:06 +00:00
|
|
|
filter_parameters_.input_cycles_per_second = cycles_per_second;
|
|
|
|
filter_parameters_.parameters_are_dirty = true;
|
2018-03-22 13:23:01 +00:00
|
|
|
filter_parameters_.input_rate_changed = true;
|
2017-12-18 02:26:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Allows a cut-off frequency to be specified for audio. Ordinarily this low-pass speaker
|
|
|
|
will determine a cut-off based on the output audio rate. A caller can manually select
|
|
|
|
an alternative cut-off. This allows machines with a low-pass filter on their audio output
|
|
|
|
path to be explicit about its effect, and get that simulation for free.
|
|
|
|
*/
|
|
|
|
void set_high_frequency_cutoff(float high_frequency) {
|
2020-06-15 04:24:10 +00:00
|
|
|
std::lock_guard lock_guard(filter_parameters_mutex_);
|
2020-02-09 21:32:32 +00:00
|
|
|
if(filter_parameters_.high_frequency_cutoff == high_frequency) {
|
|
|
|
return;
|
|
|
|
}
|
2017-12-18 02:26:06 +00:00
|
|
|
filter_parameters_.high_frequency_cutoff = high_frequency;
|
|
|
|
filter_parameters_.parameters_are_dirty = true;
|
|
|
|
}
|
|
|
|
|
2018-04-21 22:01:18 +00:00
|
|
|
private:
|
2021-11-21 20:37:29 +00:00
|
|
|
float get_ideal_clock_rate_in_range(float minimum, float maximum) final {
|
|
|
|
std::lock_guard lock_guard(filter_parameters_mutex_);
|
2020-02-10 00:14:25 +00:00
|
|
|
|
2021-11-21 20:37:29 +00:00
|
|
|
// Return twice the cut off, if applicable.
|
|
|
|
if( filter_parameters_.high_frequency_cutoff > 0.0f &&
|
|
|
|
filter_parameters_.input_cycles_per_second >= filter_parameters_.high_frequency_cutoff * 3.0f &&
|
|
|
|
filter_parameters_.input_cycles_per_second <= filter_parameters_.high_frequency_cutoff * 3.0f)
|
|
|
|
return filter_parameters_.high_frequency_cutoff * 3.0f;
|
2020-02-15 23:55:19 +00:00
|
|
|
|
2021-11-21 20:37:29 +00:00
|
|
|
// Return exactly the input rate if possible.
|
|
|
|
if( filter_parameters_.input_cycles_per_second >= minimum &&
|
|
|
|
filter_parameters_.input_cycles_per_second <= maximum)
|
|
|
|
return filter_parameters_.input_cycles_per_second;
|
2020-02-10 00:14:25 +00:00
|
|
|
|
2021-11-21 20:37:29 +00:00
|
|
|
// If the input rate is lower, return the minimum...
|
|
|
|
if(filter_parameters_.input_cycles_per_second < minimum)
|
|
|
|
return minimum;
|
2020-02-15 23:55:19 +00:00
|
|
|
|
2021-11-21 20:37:29 +00:00
|
|
|
// ... otherwise, return the maximum.
|
|
|
|
return maximum;
|
|
|
|
}
|
2017-12-18 02:26:06 +00:00
|
|
|
|
2021-11-21 20:37:29 +00:00
|
|
|
// Implemented as per Speaker.
|
|
|
|
void set_computed_output_rate(float cycles_per_second, int buffer_size, bool) final {
|
|
|
|
std::lock_guard lock_guard(filter_parameters_mutex_);
|
|
|
|
if(filter_parameters_.output_cycles_per_second == cycles_per_second && size_t(buffer_size) == output_buffer_.size()) {
|
|
|
|
return;
|
2017-12-18 02:26:06 +00:00
|
|
|
}
|
2021-11-21 20:37:29 +00:00
|
|
|
|
|
|
|
filter_parameters_.output_cycles_per_second = cycles_per_second;
|
|
|
|
filter_parameters_.parameters_are_dirty = true;
|
|
|
|
output_buffer_.resize(std::size_t(buffer_size) * (is_stereo + 1));
|
2017-12-18 02:26:06 +00:00
|
|
|
}
|
|
|
|
|
2021-11-21 20:37:29 +00:00
|
|
|
// MARK: - Filtering.
|
2017-12-18 02:26:06 +00:00
|
|
|
|
|
|
|
std::size_t output_buffer_pointer_ = 0;
|
|
|
|
std::size_t input_buffer_depth_ = 0;
|
|
|
|
std::vector<int16_t> input_buffer_;
|
|
|
|
std::vector<int16_t> output_buffer_;
|
|
|
|
|
2020-09-16 22:53:44 +00:00
|
|
|
float step_rate_ = 0.0f;
|
|
|
|
float position_error_ = 0.0f;
|
2017-12-18 02:26:06 +00:00
|
|
|
std::unique_ptr<SignalProcessing::FIRFilter> filter_;
|
|
|
|
|
2018-03-22 23:01:20 +00:00
|
|
|
std::mutex filter_parameters_mutex_;
|
2017-12-18 02:26:06 +00:00
|
|
|
struct FilterParameters {
|
|
|
|
float input_cycles_per_second = 0.0f;
|
|
|
|
float output_cycles_per_second = 0.0f;
|
|
|
|
float high_frequency_cutoff = -1.0;
|
|
|
|
|
|
|
|
bool parameters_are_dirty = true;
|
2018-03-22 13:23:01 +00:00
|
|
|
bool input_rate_changed = false;
|
2017-12-18 02:26:06 +00:00
|
|
|
} filter_parameters_;
|
|
|
|
|
2018-03-22 23:01:20 +00:00
|
|
|
void update_filter_coefficients(const FilterParameters &filter_parameters) {
|
2018-04-05 22:40:07 +00:00
|
|
|
float high_pass_frequency = filter_parameters.output_cycles_per_second / 2.0f;
|
|
|
|
if(filter_parameters.high_frequency_cutoff > 0.0) {
|
|
|
|
high_pass_frequency = std::min(filter_parameters.high_frequency_cutoff, high_pass_frequency);
|
|
|
|
}
|
|
|
|
|
2017-12-18 02:26:06 +00:00
|
|
|
// Make a guess at a good number of taps.
|
2019-07-17 02:13:47 +00:00
|
|
|
std::size_t number_of_taps = std::size_t(
|
2018-04-05 22:40:07 +00:00
|
|
|
ceilf((filter_parameters.input_cycles_per_second + high_pass_frequency) / high_pass_frequency)
|
2017-12-18 02:26:06 +00:00
|
|
|
);
|
|
|
|
number_of_taps = (number_of_taps * 2) | 1;
|
|
|
|
|
2020-09-16 22:53:44 +00:00
|
|
|
step_rate_ = filter_parameters.input_cycles_per_second / filter_parameters.output_cycles_per_second;
|
|
|
|
position_error_ = 0.0f;
|
2017-12-18 02:26:06 +00:00
|
|
|
|
2019-12-24 02:31:46 +00:00
|
|
|
filter_ = std::make_unique<SignalProcessing::FIRFilter>(
|
2020-05-10 03:00:39 +00:00
|
|
|
unsigned(number_of_taps),
|
2018-03-22 23:01:20 +00:00
|
|
|
filter_parameters.input_cycles_per_second,
|
2017-12-18 02:26:06 +00:00
|
|
|
0.0,
|
|
|
|
high_pass_frequency,
|
2019-12-24 02:31:46 +00:00
|
|
|
SignalProcessing::FIRFilter::DefaultAttenuation);
|
2017-12-18 02:26:06 +00:00
|
|
|
|
2020-02-10 00:14:25 +00:00
|
|
|
// Pick the new conversion function.
|
|
|
|
if( filter_parameters.input_cycles_per_second == filter_parameters.output_cycles_per_second &&
|
|
|
|
filter_parameters.high_frequency_cutoff < 0.0) {
|
|
|
|
// If input and output rates exactly match, and no additional cut-off has been specified,
|
|
|
|
// just accumulate results and pass on.
|
|
|
|
conversion_ = Conversion::Copy;
|
|
|
|
} else if( filter_parameters.input_cycles_per_second > filter_parameters.output_cycles_per_second ||
|
|
|
|
(filter_parameters.input_cycles_per_second == filter_parameters.output_cycles_per_second && filter_parameters.high_frequency_cutoff >= 0.0)) {
|
|
|
|
// If the output rate is less than the input rate, or an additional cut-off has been specified, use the filter.
|
|
|
|
conversion_ = Conversion::ResampleSmaller;
|
|
|
|
} else {
|
|
|
|
conversion_ = Conversion::ResampleLarger;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do something sensible with any dangling input, if necessary.
|
2021-11-21 20:37:29 +00:00
|
|
|
const int scale = static_cast<ConcreteT *>(this)->get_scale();
|
2020-02-10 00:14:25 +00:00
|
|
|
switch(conversion_) {
|
|
|
|
// Neither direct copying nor resampling larger currently use any temporary input.
|
|
|
|
// Although in the latter case that's just because it's unimplemented. But, regardless,
|
|
|
|
// that means nothing to do.
|
|
|
|
default: break;
|
|
|
|
|
2020-02-15 23:55:19 +00:00
|
|
|
case Conversion::ResampleSmaller: {
|
2020-02-10 00:14:25 +00:00
|
|
|
// Reize the input buffer only if absolutely necessary; if sizing downward
|
|
|
|
// such that a sample would otherwise be lost then output it now. Keep anything
|
|
|
|
// currently in the input buffer that hasn't yet been processed.
|
2021-11-21 20:37:29 +00:00
|
|
|
const size_t required_buffer_size = size_t(number_of_taps) * (is_stereo + 1);
|
2020-02-15 23:55:19 +00:00
|
|
|
if(input_buffer_.size() != required_buffer_size) {
|
|
|
|
if(input_buffer_depth_ >= required_buffer_size) {
|
2020-05-09 21:57:21 +00:00
|
|
|
resample_input_buffer(scale);
|
2020-02-15 23:55:19 +00:00
|
|
|
input_buffer_depth_ %= required_buffer_size;
|
2020-02-10 00:14:25 +00:00
|
|
|
}
|
2020-02-15 23:55:19 +00:00
|
|
|
input_buffer_.resize(required_buffer_size);
|
2020-02-10 00:14:25 +00:00
|
|
|
}
|
2020-02-15 23:55:19 +00:00
|
|
|
} break;
|
2020-02-10 00:14:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-09 21:57:21 +00:00
|
|
|
inline void resample_input_buffer(int scale) {
|
2022-07-12 20:03:09 +00:00
|
|
|
if(output_buffer_.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-11-21 20:37:29 +00:00
|
|
|
if constexpr (is_stereo) {
|
2020-02-15 23:55:19 +00:00
|
|
|
output_buffer_[output_buffer_pointer_ + 0] = filter_->apply(input_buffer_.data(), 2);
|
|
|
|
output_buffer_[output_buffer_pointer_ + 1] = filter_->apply(input_buffer_.data() + 1, 2);
|
|
|
|
output_buffer_pointer_+= 2;
|
|
|
|
} else {
|
|
|
|
output_buffer_[output_buffer_pointer_] = filter_->apply(input_buffer_.data());
|
|
|
|
output_buffer_pointer_++;
|
|
|
|
}
|
2020-02-10 00:14:25 +00:00
|
|
|
|
2020-05-09 21:57:21 +00:00
|
|
|
// Apply scale, if supplied, clamping appropriately.
|
|
|
|
if(scale != 65536) {
|
2021-11-21 20:37:29 +00:00
|
|
|
#define SCALE(x) x = int16_t(std::clamp((int(x) * scale) >> 16, -32768, 32767))
|
|
|
|
if constexpr (is_stereo) {
|
2020-05-09 21:57:21 +00:00
|
|
|
SCALE(output_buffer_[output_buffer_pointer_ - 2]);
|
|
|
|
SCALE(output_buffer_[output_buffer_pointer_ - 1]);
|
|
|
|
} else {
|
|
|
|
SCALE(output_buffer_[output_buffer_pointer_ - 1]);
|
|
|
|
}
|
|
|
|
#undef SCALE
|
|
|
|
}
|
|
|
|
|
2020-02-10 00:14:25 +00:00
|
|
|
// Announce to delegate if full.
|
|
|
|
if(output_buffer_pointer_ == output_buffer_.size()) {
|
|
|
|
output_buffer_pointer_ = 0;
|
2021-11-21 20:37:29 +00:00
|
|
|
did_complete_samples(this, output_buffer_, is_stereo);
|
2020-02-10 00:14:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If the next loop around is going to reuse some of the samples just collected, use a memmove to
|
|
|
|
// preserve them in the correct locations (TODO: use a longer buffer to fix that?) and don't skip
|
|
|
|
// anything. Otherwise skip as required to get to the next sample batch and don't expect to reuse.
|
2021-11-21 20:37:29 +00:00
|
|
|
const size_t steps = size_t(step_rate_ + position_error_) * (is_stereo + 1);
|
2020-09-16 22:53:44 +00:00
|
|
|
position_error_ = fmodf(step_rate_ + position_error_, 1.0f);
|
2020-02-10 00:14:25 +00:00
|
|
|
if(steps < input_buffer_.size()) {
|
|
|
|
auto *const input_buffer = input_buffer_.data();
|
|
|
|
std::memmove( input_buffer,
|
|
|
|
&input_buffer[steps],
|
|
|
|
sizeof(int16_t) * (input_buffer_.size() - steps));
|
|
|
|
input_buffer_depth_ -= steps;
|
|
|
|
} else {
|
2020-02-15 23:55:19 +00:00
|
|
|
if(steps > input_buffer_.size()) {
|
2021-11-21 20:37:29 +00:00
|
|
|
static_cast<ConcreteT *>(this)->skip_samples((steps - input_buffer_.size()) / (1 + is_stereo));
|
2020-02-15 23:55:19 +00:00
|
|
|
}
|
2020-02-10 00:14:25 +00:00
|
|
|
input_buffer_depth_ = 0;
|
|
|
|
}
|
2017-12-18 02:26:06 +00:00
|
|
|
}
|
2020-05-09 21:57:21 +00:00
|
|
|
|
2021-11-21 20:37:29 +00:00
|
|
|
enum class Conversion {
|
|
|
|
ResampleSmaller,
|
|
|
|
Copy,
|
|
|
|
ResampleLarger
|
|
|
|
} conversion_ = Conversion::Copy;
|
|
|
|
|
|
|
|
bool recalculate_filter_if_dirty() {
|
|
|
|
FilterParameters filter_parameters;
|
|
|
|
{
|
|
|
|
std::lock_guard lock_guard(filter_parameters_mutex_);
|
|
|
|
filter_parameters = filter_parameters_;
|
|
|
|
filter_parameters_.parameters_are_dirty = false;
|
|
|
|
filter_parameters_.input_rate_changed = false;
|
|
|
|
}
|
|
|
|
if(filter_parameters.parameters_are_dirty) update_filter_coefficients(filter_parameters);
|
|
|
|
return filter_parameters.input_rate_changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2022-07-15 01:59:40 +00:00
|
|
|
bool process(size_t length) {
|
2021-11-21 20:37:29 +00:00
|
|
|
const auto delegate = delegate_.load(std::memory_order::memory_order_relaxed);
|
2022-07-15 01:59:40 +00:00
|
|
|
if(!delegate) return false;
|
2021-11-21 20:37:29 +00:00
|
|
|
|
|
|
|
const int scale = static_cast<ConcreteT *>(this)->get_scale();
|
|
|
|
|
|
|
|
if(recalculate_filter_if_dirty()) {
|
|
|
|
delegate->speaker_did_change_input_clock(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(conversion_) {
|
|
|
|
case Conversion::Copy:
|
|
|
|
while(length) {
|
|
|
|
const auto samples_to_read = std::min((output_buffer_.size() - output_buffer_pointer_) / (1 + is_stereo), length);
|
|
|
|
static_cast<ConcreteT *>(this)->get_samples(samples_to_read, &output_buffer_[output_buffer_pointer_ ]);
|
|
|
|
output_buffer_pointer_ += samples_to_read * (1 + is_stereo);
|
|
|
|
|
|
|
|
// TODO: apply scale.
|
|
|
|
|
|
|
|
// Announce to delegate if full.
|
|
|
|
if(output_buffer_pointer_ == output_buffer_.size()) {
|
|
|
|
output_buffer_pointer_ = 0;
|
|
|
|
did_complete_samples(this, output_buffer_, is_stereo);
|
|
|
|
}
|
|
|
|
|
|
|
|
length -= samples_to_read;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Conversion::ResampleSmaller:
|
|
|
|
while(length) {
|
|
|
|
const auto cycles_to_read = std::min((input_buffer_.size() - input_buffer_depth_) / (1 + is_stereo), length);
|
|
|
|
static_cast<ConcreteT *>(this)->get_samples(cycles_to_read, &input_buffer_[input_buffer_depth_]);
|
|
|
|
input_buffer_depth_ += cycles_to_read * (1 + is_stereo);
|
|
|
|
|
|
|
|
if(input_buffer_depth_ == input_buffer_.size()) {
|
|
|
|
resample_input_buffer(scale);
|
|
|
|
}
|
|
|
|
|
|
|
|
length -= cycles_to_read;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Conversion::ResampleLarger:
|
|
|
|
// TODO: input rate is less than output rate.
|
|
|
|
break;
|
|
|
|
}
|
2022-07-15 01:59:40 +00:00
|
|
|
|
|
|
|
return true;
|
2021-11-21 20:37:29 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Provides a low-pass speaker to which blocks of samples are pushed.
|
|
|
|
*/
|
|
|
|
template <bool is_stereo> class PushLowpass: public LowpassBase<PushLowpass<is_stereo>, is_stereo> {
|
|
|
|
private:
|
|
|
|
using BaseT = LowpassBase<PushLowpass<is_stereo>, is_stereo>;
|
|
|
|
friend BaseT;
|
|
|
|
using BaseT::process;
|
|
|
|
|
2021-12-02 17:55:11 +00:00
|
|
|
std::atomic<int> scale_ = 65536;
|
2021-11-21 20:37:29 +00:00
|
|
|
int get_scale() {
|
|
|
|
return scale_;
|
|
|
|
}
|
|
|
|
|
2021-11-22 16:18:17 +00:00
|
|
|
const int16_t *buffer_ = nullptr;
|
2021-11-21 20:37:29 +00:00
|
|
|
|
|
|
|
void skip_samples(size_t count) {
|
|
|
|
buffer_ += count;
|
|
|
|
}
|
|
|
|
|
|
|
|
void get_samples(size_t length, int16_t *target) {
|
2021-12-02 17:52:43 +00:00
|
|
|
const auto word_length = length * (1 + is_stereo);
|
|
|
|
memcpy(target, buffer_, word_length * sizeof(int16_t));
|
|
|
|
buffer_ += word_length;
|
2021-11-21 20:37:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
void set_output_volume(float volume) final {
|
2021-12-02 17:55:11 +00:00
|
|
|
scale_.store(int(std::clamp(volume * 65536.0f, 0.0f, 65536.0f)));
|
2021-11-21 20:37:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool get_is_stereo() final {
|
|
|
|
return is_stereo;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Filters and posts onward the provided buffer, on the calling thread.
|
2021-12-02 17:52:43 +00:00
|
|
|
|
|
|
|
@param buffer The source for samples.
|
|
|
|
@param length The number of samples provided; in mono this will be the number of int16_ts
|
|
|
|
it is safe to read from @c buffer, and in stereo it will be half the number — it is a count
|
|
|
|
of the number of time points at which audio was sampled.
|
2021-11-21 20:37:29 +00:00
|
|
|
*/
|
2021-11-22 16:18:17 +00:00
|
|
|
void push(const int16_t *buffer, size_t length) {
|
2021-11-21 20:37:29 +00:00
|
|
|
buffer_ = buffer;
|
2022-07-15 01:59:40 +00:00
|
|
|
#ifndef NDEBUG
|
|
|
|
const bool did_process =
|
|
|
|
#endif
|
|
|
|
process(length);
|
|
|
|
assert(!did_process || buffer_ == buffer + (length * (1 + is_stereo)));
|
2021-11-21 20:37:29 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/*!
|
|
|
|
The low-pass speaker expects an Outputs::Speaker::SampleSource-derived
|
|
|
|
template class, and uses the instance supplied to its constructor as the
|
|
|
|
source of a high-frequency stream of audio which it filters down to a
|
|
|
|
lower-frequency output.
|
|
|
|
*/
|
2024-02-02 02:29:00 +00:00
|
|
|
template <typename SampleSource> class PullLowpass: public LowpassBase<PullLowpass<SampleSource>, SampleSource::is_stereo> {
|
2021-11-21 20:37:29 +00:00
|
|
|
public:
|
|
|
|
PullLowpass(SampleSource &sample_source) : sample_source_(sample_source) {
|
|
|
|
// Propagate an initial volume level.
|
|
|
|
sample_source.set_sample_volume_range(32767);
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_output_volume(float volume) final {
|
|
|
|
// Clamp to the acceptable range, and set.
|
|
|
|
volume = std::clamp(volume, 0.0f, 1.0f);
|
|
|
|
sample_source_.set_sample_volume_range(int16_t(32767.0f * volume));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool get_is_stereo() final {
|
2024-02-02 02:29:00 +00:00
|
|
|
return SampleSource::is_stereo;
|
2021-11-21 20:37:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Schedules an advancement by the number of cycles specified on the provided queue.
|
|
|
|
The speaker will advance by obtaining data from the sample source supplied
|
|
|
|
at construction, filtering it and passing it on to the speaker's delegate if there is one.
|
|
|
|
*/
|
2022-07-16 18:41:04 +00:00
|
|
|
void run_for(Concurrency::AsyncTaskQueue<false> &queue, const Cycles cycles) {
|
2022-07-13 19:05:34 +00:00
|
|
|
if(cycles == Cycles(0)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-07-14 20:39:26 +00:00
|
|
|
queue.enqueue([this, cycles] {
|
2021-11-21 20:37:29 +00:00
|
|
|
run_for(cycles);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2024-02-02 02:29:00 +00:00
|
|
|
using BaseT = LowpassBase<PullLowpass<SampleSource>, SampleSource::is_stereo>;
|
2021-11-21 20:37:29 +00:00
|
|
|
friend BaseT;
|
|
|
|
using BaseT::process;
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Advances by the number of cycles specified, obtaining data from the sample source supplied
|
|
|
|
at construction, filtering it and passing it on to the speaker's delegate if there is one.
|
|
|
|
*/
|
|
|
|
void run_for(const Cycles cycles) {
|
2022-07-13 19:05:34 +00:00
|
|
|
process(size_t(cycles.as_integral()));
|
2021-11-21 20:37:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SampleSource &sample_source_;
|
|
|
|
|
|
|
|
void skip_samples(size_t count) {
|
2024-02-12 15:55:52 +00:00
|
|
|
sample_source_.template apply_samples<Action::Ignore>(count, nullptr);
|
2021-11-21 20:37:29 +00:00
|
|
|
}
|
|
|
|
|
2020-05-09 21:57:21 +00:00
|
|
|
int get_scale() {
|
2024-02-02 02:29:00 +00:00
|
|
|
return int(65536.0 / sample_source_.average_output_peak());
|
2021-11-21 20:37:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void get_samples(size_t length, int16_t *target) {
|
2024-02-09 14:15:48 +00:00
|
|
|
if constexpr (SampleSource::is_stereo) {
|
|
|
|
StereoSample *const stereo_target = reinterpret_cast<StereoSample *>(target);
|
2024-02-12 15:55:52 +00:00
|
|
|
sample_source_.template apply_samples<Action::Store>(length, stereo_target);
|
2024-02-09 14:15:48 +00:00
|
|
|
} else {
|
2024-02-12 15:55:52 +00:00
|
|
|
sample_source_.template apply_samples<Action::Store>(length, target);
|
2024-02-09 14:15:48 +00:00
|
|
|
}
|
2021-11-21 20:37:29 +00:00
|
|
|
}
|
2017-12-18 02:26:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|