2016-01-12 21:54:09 +00:00
|
|
|
//
|
|
|
|
// Speaker.hpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 12/01/2016.
|
|
|
|
// Copyright © 2016 Thomas Harte. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#ifndef Speaker_hpp
|
|
|
|
#define Speaker_hpp
|
|
|
|
|
2017-07-26 00:20:55 +00:00
|
|
|
#include <cstdint>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <ctime>
|
2016-11-10 02:17:50 +00:00
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
#include <list>
|
2017-07-16 17:54:07 +00:00
|
|
|
#include <vector>
|
2016-11-10 02:17:50 +00:00
|
|
|
|
2016-01-13 03:19:56 +00:00
|
|
|
#include "../SignalProcessing/Stepper.hpp"
|
2016-03-16 01:34:00 +00:00
|
|
|
#include "../SignalProcessing/FIRFilter.hpp"
|
2016-10-07 20:56:34 +00:00
|
|
|
#include "../Concurrency/AsyncTaskQueue.hpp"
|
2017-07-26 00:20:55 +00:00
|
|
|
#include "../ClockReceiver/ClockReceiver.hpp"
|
2016-01-12 21:54:09 +00:00
|
|
|
|
2016-01-14 02:03:43 +00:00
|
|
|
namespace Outputs {
|
2016-01-12 21:54:09 +00:00
|
|
|
|
2016-06-30 12:46:29 +00:00
|
|
|
/*!
|
|
|
|
Provides the base class for an audio output source, with an input rate (the speed at which the source will
|
|
|
|
provide data), an output rate (the speed at which the destination will receive data), a delegate to receive
|
|
|
|
the output and some help for the output in picking an appropriate rate once the input rate is known.
|
|
|
|
|
|
|
|
Intended to be a parent class, allowing descendants to pick the strategy by which input samples are mapped to
|
|
|
|
output samples.
|
|
|
|
*/
|
2016-10-20 01:15:04 +00:00
|
|
|
class Speaker {
|
2016-01-12 21:54:09 +00:00
|
|
|
public:
|
2016-01-14 02:03:43 +00:00
|
|
|
class Delegate {
|
|
|
|
public:
|
2017-07-16 19:01:39 +00:00
|
|
|
virtual void speaker_did_complete_samples(Speaker *speaker, const std::vector<int16_t> &buffer) = 0;
|
2016-01-14 02:03:43 +00:00
|
|
|
};
|
2016-01-12 21:54:09 +00:00
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
float get_ideal_clock_rate_in_range(float minimum, float maximum) {
|
2016-08-23 02:18:05 +00:00
|
|
|
// return twice the cut off, if applicable
|
2016-12-03 16:02:34 +00:00
|
|
|
if(high_frequency_cut_off_ > 0.0f && input_cycles_per_second_ >= high_frequency_cut_off_ * 3.0f && input_cycles_per_second_ <= high_frequency_cut_off_ * 3.0f) return high_frequency_cut_off_ * 3.0f;
|
2016-08-23 02:18:05 +00:00
|
|
|
|
2016-06-01 01:23:44 +00:00
|
|
|
// return exactly the input rate if possible
|
2016-12-03 16:02:34 +00:00
|
|
|
if(input_cycles_per_second_ >= minimum && input_cycles_per_second_ <= maximum) return input_cycles_per_second_;
|
2016-06-01 01:23:44 +00:00
|
|
|
|
|
|
|
// if the input rate is lower, return the minimum
|
2016-12-03 16:02:34 +00:00
|
|
|
if(input_cycles_per_second_ < minimum) return minimum;
|
2016-06-01 01:23:44 +00:00
|
|
|
|
|
|
|
// otherwise, return the maximum
|
|
|
|
return maximum;
|
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
void set_output_rate(float cycles_per_second, int buffer_size) {
|
2016-12-03 16:02:34 +00:00
|
|
|
output_cycles_per_second_ = cycles_per_second;
|
2017-07-16 19:01:39 +00:00
|
|
|
buffer_in_progress_.resize((size_t)buffer_size);
|
2016-01-13 03:19:56 +00:00
|
|
|
set_needs_updated_filter_coefficients();
|
|
|
|
}
|
2016-01-12 21:54:09 +00:00
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
void set_output_quality(int number_of_taps) {
|
2017-07-22 01:54:05 +00:00
|
|
|
requested_number_of_taps_ = (size_t)number_of_taps;
|
2016-04-18 00:45:57 +00:00
|
|
|
set_needs_updated_filter_coefficients();
|
|
|
|
}
|
2016-01-13 03:19:56 +00:00
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
void set_delegate(Delegate *delegate) {
|
2016-12-03 16:02:34 +00:00
|
|
|
delegate_ = delegate;
|
2016-01-13 03:19:56 +00:00
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
void set_input_rate(float cycles_per_second) {
|
2016-12-03 16:02:34 +00:00
|
|
|
input_cycles_per_second_ = cycles_per_second;
|
2016-01-13 03:19:56 +00:00
|
|
|
set_needs_updated_filter_coefficients();
|
|
|
|
}
|
|
|
|
|
2016-08-21 22:13:31 +00:00
|
|
|
/*!
|
|
|
|
Sets the cut-off frequency for a low-pass filter attached to the output of this speaker; optional.
|
|
|
|
*/
|
2017-03-26 18:34:47 +00:00
|
|
|
void set_high_frequency_cut_off(float high_frequency) {
|
2016-12-03 16:02:34 +00:00
|
|
|
high_frequency_cut_off_ = high_frequency;
|
2016-08-21 22:13:31 +00:00
|
|
|
set_needs_updated_filter_coefficients();
|
|
|
|
}
|
|
|
|
|
2016-12-03 16:02:34 +00:00
|
|
|
Speaker() : buffer_in_progress_pointer_(0), requested_number_of_taps_(0), high_frequency_cut_off_(-1.0), _queue(new Concurrency::AsyncTaskQueue) {}
|
2016-01-15 02:27:02 +00:00
|
|
|
|
2016-11-10 02:17:50 +00:00
|
|
|
/*!
|
|
|
|
Ensures any deferred processing occurs now.
|
|
|
|
*/
|
2017-03-26 18:34:47 +00:00
|
|
|
void flush() {
|
2017-08-02 11:20:59 +00:00
|
|
|
if(!queued_functions_) return;
|
2016-12-03 16:02:34 +00:00
|
|
|
std::shared_ptr<std::list<std::function<void(void)>>> queued_functions = queued_functions_;
|
|
|
|
queued_functions_.reset();
|
2016-11-10 02:17:50 +00:00
|
|
|
_queue->enqueue([queued_functions] {
|
2017-03-26 18:34:47 +00:00
|
|
|
for(auto function : *queued_functions) {
|
2016-11-10 02:17:50 +00:00
|
|
|
function();
|
|
|
|
}
|
|
|
|
});
|
2016-10-20 01:15:04 +00:00
|
|
|
}
|
2016-11-10 02:17:50 +00:00
|
|
|
|
|
|
|
protected:
|
2017-03-26 18:34:47 +00:00
|
|
|
void enqueue(std::function<void(void)> function) {
|
2016-12-03 16:02:34 +00:00
|
|
|
if(!queued_functions_) queued_functions_.reset(new std::list<std::function<void(void)>>);
|
|
|
|
queued_functions_->push_back(function);
|
2016-10-20 01:15:04 +00:00
|
|
|
}
|
2016-12-03 16:02:34 +00:00
|
|
|
std::shared_ptr<std::list<std::function<void(void)>>> queued_functions_;
|
2016-10-20 01:15:04 +00:00
|
|
|
|
2017-07-16 17:54:07 +00:00
|
|
|
std::vector<int16_t> buffer_in_progress_;
|
2016-12-03 16:02:34 +00:00
|
|
|
float high_frequency_cut_off_;
|
2017-07-22 01:28:04 +00:00
|
|
|
size_t buffer_in_progress_pointer_;
|
|
|
|
size_t number_of_taps_, requested_number_of_taps_;
|
2016-12-03 16:02:34 +00:00
|
|
|
bool coefficients_are_dirty_;
|
|
|
|
Delegate *delegate_;
|
2016-01-13 03:19:56 +00:00
|
|
|
|
2016-12-03 16:02:34 +00:00
|
|
|
float input_cycles_per_second_, output_cycles_per_second_;
|
2016-01-13 03:19:56 +00:00
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
void set_needs_updated_filter_coefficients() {
|
2016-12-03 16:02:34 +00:00
|
|
|
coefficients_are_dirty_ = true;
|
2016-01-13 03:19:56 +00:00
|
|
|
}
|
2016-07-05 00:48:27 +00:00
|
|
|
|
|
|
|
void get_samples(unsigned int quantity, int16_t *target) {}
|
2017-03-26 18:34:47 +00:00
|
|
|
void skip_samples(unsigned int quantity) {
|
2016-07-05 00:48:27 +00:00
|
|
|
int16_t throwaway_samples[quantity];
|
|
|
|
get_samples(quantity, throwaway_samples);
|
|
|
|
}
|
2016-10-20 01:15:04 +00:00
|
|
|
|
|
|
|
std::unique_ptr<Concurrency::AsyncTaskQueue> _queue;
|
2016-01-14 02:03:43 +00:00
|
|
|
};
|
|
|
|
|
2016-06-30 12:46:29 +00:00
|
|
|
/*!
|
|
|
|
A concrete descendant of Speaker that uses a FIR filter to map from input data to output data when scaling
|
|
|
|
and a copy-through buffer when input and output rates are the same.
|
|
|
|
|
|
|
|
Audio sources should use @c Filter as both a template and a parent, implementing at least
|
|
|
|
`get_samples(unsigned int quantity, int16_t *target)` and ideally also `skip_samples(unsigned int quantity)`
|
|
|
|
to provide source data.
|
|
|
|
|
2017-07-25 01:29:13 +00:00
|
|
|
Call `run_for` to request that the next period of input data is collected.
|
2016-06-30 12:46:29 +00:00
|
|
|
*/
|
2017-07-27 11:40:02 +00:00
|
|
|
template <class T> class Filter: public Speaker {
|
2016-01-14 02:03:43 +00:00
|
|
|
public:
|
2017-03-26 18:34:47 +00:00
|
|
|
~Filter() {
|
2016-11-10 02:17:50 +00:00
|
|
|
_queue->flush();
|
2016-10-20 01:15:04 +00:00
|
|
|
}
|
|
|
|
|
2017-07-28 02:05:29 +00:00
|
|
|
void run_for(const Cycles cycles) {
|
2016-10-07 20:56:34 +00:00
|
|
|
enqueue([=]() {
|
2017-07-25 01:29:13 +00:00
|
|
|
unsigned int cycles_remaining = (unsigned int)cycles.as_int();
|
2016-12-03 16:02:34 +00:00
|
|
|
if(coefficients_are_dirty_) update_filter_coefficients();
|
2016-01-14 02:03:43 +00:00
|
|
|
|
2016-10-07 20:56:34 +00:00
|
|
|
// if input and output rates exactly match, just accumulate results and pass on
|
2017-03-26 18:34:47 +00:00
|
|
|
if(input_cycles_per_second_ == output_cycles_per_second_ && high_frequency_cut_off_ < 0.0) {
|
|
|
|
while(cycles_remaining) {
|
2017-07-16 19:01:39 +00:00
|
|
|
unsigned int cycles_to_read = (unsigned int)(buffer_in_progress_.size() - (size_t)buffer_in_progress_pointer_);
|
2016-10-07 20:56:34 +00:00
|
|
|
if(cycles_to_read > cycles_remaining) cycles_to_read = cycles_remaining;
|
2016-06-01 23:53:16 +00:00
|
|
|
|
2017-07-16 17:54:07 +00:00
|
|
|
static_cast<T *>(this)->get_samples(cycles_to_read, &buffer_in_progress_[(size_t)buffer_in_progress_pointer_]);
|
2016-12-03 16:02:34 +00:00
|
|
|
buffer_in_progress_pointer_ += cycles_to_read;
|
2016-03-16 03:37:35 +00:00
|
|
|
|
2016-10-07 20:56:34 +00:00
|
|
|
// announce to delegate if full
|
2017-07-16 19:01:39 +00:00
|
|
|
if(buffer_in_progress_pointer_ == buffer_in_progress_.size()) {
|
2016-12-03 16:02:34 +00:00
|
|
|
buffer_in_progress_pointer_ = 0;
|
2017-03-26 18:34:47 +00:00
|
|
|
if(delegate_) {
|
2017-07-16 19:01:39 +00:00
|
|
|
delegate_->speaker_did_complete_samples(this, buffer_in_progress_);
|
2016-10-07 20:56:34 +00:00
|
|
|
}
|
2016-03-16 03:37:35 +00:00
|
|
|
}
|
2016-10-07 20:56:34 +00:00
|
|
|
|
|
|
|
cycles_remaining -= cycles_to_read;
|
2016-01-14 03:02:39 +00:00
|
|
|
}
|
|
|
|
|
2016-10-07 20:56:34 +00:00
|
|
|
return;
|
2016-06-01 23:53:16 +00:00
|
|
|
}
|
|
|
|
|
2016-10-07 20:56:34 +00:00
|
|
|
// if the output rate is less than the input rate, use the filter
|
2017-03-26 18:34:47 +00:00
|
|
|
if(input_cycles_per_second_ > output_cycles_per_second_ || (input_cycles_per_second_ == output_cycles_per_second_ && high_frequency_cut_off_ >= 0.0)) {
|
|
|
|
while(cycles_remaining) {
|
2017-07-22 01:54:05 +00:00
|
|
|
unsigned int cycles_to_read = (unsigned int)std::min((size_t)cycles_remaining, number_of_taps_ - input_buffer_depth_);
|
2017-07-16 17:54:07 +00:00
|
|
|
static_cast<T *>(this)->get_samples(cycles_to_read, &input_buffer_[(size_t)input_buffer_depth_]);
|
2016-10-07 20:56:34 +00:00
|
|
|
cycles_remaining -= cycles_to_read;
|
2016-12-03 16:02:34 +00:00
|
|
|
input_buffer_depth_ += cycles_to_read;
|
2016-06-01 23:53:16 +00:00
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
if(input_buffer_depth_ == number_of_taps_) {
|
2017-07-16 17:54:07 +00:00
|
|
|
buffer_in_progress_[(size_t)buffer_in_progress_pointer_] = filter_->apply(input_buffer_.data());
|
2016-12-03 16:02:34 +00:00
|
|
|
buffer_in_progress_pointer_++;
|
2016-10-07 20:56:34 +00:00
|
|
|
|
|
|
|
// announce to delegate if full
|
2017-07-16 19:01:39 +00:00
|
|
|
if(buffer_in_progress_pointer_ == buffer_in_progress_.size()) {
|
2016-12-03 16:02:34 +00:00
|
|
|
buffer_in_progress_pointer_ = 0;
|
2017-03-26 18:34:47 +00:00
|
|
|
if(delegate_) {
|
2017-07-16 19:01:39 +00:00
|
|
|
delegate_->speaker_did_complete_samples(this, buffer_in_progress_);
|
2016-10-07 20:56:34 +00:00
|
|
|
}
|
2016-06-01 23:53:16 +00:00
|
|
|
}
|
|
|
|
|
2016-10-07 20:56:34 +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.
|
2016-12-03 16:02:34 +00:00
|
|
|
uint64_t steps = stepper_->step();
|
2017-03-26 18:34:47 +00:00
|
|
|
if(steps < number_of_taps_) {
|
2017-07-16 17:54:07 +00:00
|
|
|
int16_t *input_buffer = input_buffer_.data();
|
2016-12-03 16:02:34 +00:00
|
|
|
memmove(input_buffer, &input_buffer[steps], sizeof(int16_t) * ((size_t)number_of_taps_ - (size_t)steps));
|
|
|
|
input_buffer_depth_ -= steps;
|
2017-03-26 18:34:47 +00:00
|
|
|
} else {
|
2016-12-03 16:02:34 +00:00
|
|
|
if(steps > number_of_taps_)
|
|
|
|
static_cast<T *>(this)->skip_samples((unsigned int)steps - (unsigned int)number_of_taps_);
|
|
|
|
input_buffer_depth_ = 0;
|
2016-10-07 20:56:34 +00:00
|
|
|
}
|
2016-06-01 23:53:16 +00:00
|
|
|
}
|
2016-03-17 00:38:17 +00:00
|
|
|
}
|
2016-06-01 23:53:16 +00:00
|
|
|
|
2016-10-07 20:56:34 +00:00
|
|
|
return;
|
|
|
|
}
|
2016-06-01 23:53:16 +00:00
|
|
|
|
|
|
|
// TODO: input rate is less than output rate
|
2016-10-07 20:56:34 +00:00
|
|
|
});
|
2016-01-14 02:03:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2016-12-03 16:02:34 +00:00
|
|
|
std::unique_ptr<SignalProcessing::Stepper> stepper_;
|
|
|
|
std::unique_ptr<SignalProcessing::FIRFilter> filter_;
|
2016-03-16 03:37:35 +00:00
|
|
|
|
2017-07-16 17:54:07 +00:00
|
|
|
std::vector<int16_t> input_buffer_;
|
2017-07-22 01:54:05 +00:00
|
|
|
size_t input_buffer_depth_;
|
2016-01-13 03:19:56 +00:00
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
void update_filter_coefficients() {
|
2016-04-18 00:45:57 +00:00
|
|
|
// make a guess at a good number of taps if this hasn't been provided explicitly
|
2017-03-26 18:34:47 +00:00
|
|
|
if(requested_number_of_taps_) {
|
2016-12-03 16:02:34 +00:00
|
|
|
number_of_taps_ = requested_number_of_taps_;
|
2017-03-26 18:34:47 +00:00
|
|
|
} else {
|
2017-07-22 01:54:05 +00:00
|
|
|
number_of_taps_ = (size_t)ceilf((input_cycles_per_second_ + output_cycles_per_second_) / output_cycles_per_second_);
|
2016-12-03 16:02:34 +00:00
|
|
|
number_of_taps_ *= 2;
|
|
|
|
number_of_taps_ |= 1;
|
2016-04-18 00:45:57 +00:00
|
|
|
}
|
2016-04-18 00:43:20 +00:00
|
|
|
|
2016-12-03 16:02:34 +00:00
|
|
|
coefficients_are_dirty_ = false;
|
|
|
|
buffer_in_progress_pointer_ = 0;
|
2016-01-13 03:19:56 +00:00
|
|
|
|
2016-12-03 16:02:34 +00:00
|
|
|
stepper_.reset(new SignalProcessing::Stepper((uint64_t)input_cycles_per_second_, (uint64_t)output_cycles_per_second_));
|
2016-08-21 16:13:41 +00:00
|
|
|
|
|
|
|
float high_pass_frequency;
|
2017-03-26 18:34:47 +00:00
|
|
|
if(high_frequency_cut_off_ > 0.0) {
|
2017-07-22 01:54:05 +00:00
|
|
|
high_pass_frequency = std::min(output_cycles_per_second_ / 2.0f, high_frequency_cut_off_);
|
2017-03-26 18:34:47 +00:00
|
|
|
} else {
|
2017-07-22 01:54:05 +00:00
|
|
|
high_pass_frequency = output_cycles_per_second_ / 2.0f;
|
2016-08-21 16:13:41 +00:00
|
|
|
}
|
2016-12-03 16:02:34 +00:00
|
|
|
filter_.reset(new SignalProcessing::FIRFilter((unsigned int)number_of_taps_, (float)input_cycles_per_second_, 0.0, high_pass_frequency, SignalProcessing::FIRFilter::DefaultAttenuation));
|
2016-03-16 03:37:35 +00:00
|
|
|
|
2017-07-16 17:54:07 +00:00
|
|
|
input_buffer_.resize((size_t)number_of_taps_);
|
2016-12-03 16:02:34 +00:00
|
|
|
input_buffer_depth_ = 0;
|
2016-01-13 03:19:56 +00:00
|
|
|
}
|
2016-01-12 21:54:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* Speaker_hpp */
|