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
|
|
|
|
|
|
|
|
#include <stdint.h>
|
2016-01-14 02:03:43 +00:00
|
|
|
#include <stdio.h>
|
2016-01-18 19:50:19 +00:00
|
|
|
#include <time.h>
|
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-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-01-14 02:03:43 +00:00
|
|
|
class Speaker {
|
2016-01-12 21:54:09 +00:00
|
|
|
public:
|
2016-01-14 02:03:43 +00:00
|
|
|
class Delegate {
|
|
|
|
public:
|
2016-01-15 01:33:22 +00:00
|
|
|
virtual void speaker_did_complete_samples(Speaker *speaker, const int16_t *buffer, int buffer_size) = 0;
|
2016-01-14 02:03:43 +00:00
|
|
|
};
|
2016-01-12 21:54:09 +00:00
|
|
|
|
2016-06-13 23:30:41 +00:00
|
|
|
float get_ideal_clock_rate_in_range(float minimum, float maximum)
|
2016-06-01 01:23:44 +00:00
|
|
|
{
|
2016-08-23 02:18:05 +00:00
|
|
|
// return twice the cut off, if applicable
|
|
|
|
if(_high_frequency_cut_off > 0.0f && _input_cycles_per_second >= _high_frequency_cut_off * 2.0f && _input_cycles_per_second <= _high_frequency_cut_off * 2.0f) return _high_frequency_cut_off * 2.0f;
|
|
|
|
|
2016-06-01 01:23:44 +00:00
|
|
|
// return exactly the input rate if possible
|
|
|
|
if(_input_cycles_per_second >= minimum && _input_cycles_per_second <= maximum) return _input_cycles_per_second;
|
|
|
|
|
|
|
|
// if the input rate is lower, return the minimum
|
|
|
|
if(_input_cycles_per_second < minimum) return minimum;
|
|
|
|
|
|
|
|
// otherwise, return the maximum
|
|
|
|
return maximum;
|
|
|
|
}
|
|
|
|
|
2016-06-13 23:30:41 +00:00
|
|
|
void set_output_rate(float cycles_per_second, int buffer_size)
|
2016-01-13 03:19:56 +00:00
|
|
|
{
|
|
|
|
_output_cycles_per_second = cycles_per_second;
|
|
|
|
if(_buffer_size != buffer_size)
|
|
|
|
{
|
2016-06-24 00:52:44 +00:00
|
|
|
_buffer_in_progress.reset(new int16_t[buffer_size]);
|
2016-01-13 03:19:56 +00:00
|
|
|
_buffer_size = buffer_size;
|
|
|
|
}
|
|
|
|
set_needs_updated_filter_coefficients();
|
|
|
|
}
|
2016-01-12 21:54:09 +00:00
|
|
|
|
2016-04-18 00:45:57 +00:00
|
|
|
void set_output_quality(int number_of_taps)
|
|
|
|
{
|
|
|
|
_requested_number_of_taps = number_of_taps;
|
|
|
|
set_needs_updated_filter_coefficients();
|
|
|
|
}
|
2016-01-13 03:19:56 +00:00
|
|
|
|
|
|
|
void set_delegate(Delegate *delegate)
|
|
|
|
{
|
|
|
|
_delegate = delegate;
|
|
|
|
}
|
|
|
|
|
2016-06-13 23:30:41 +00:00
|
|
|
void set_input_rate(float cycles_per_second)
|
2016-01-13 03:19:56 +00:00
|
|
|
{
|
|
|
|
_input_cycles_per_second = cycles_per_second;
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
void set_high_frequency_cut_off(float high_frequency)
|
|
|
|
{
|
|
|
|
_high_frequency_cut_off = high_frequency;
|
|
|
|
set_needs_updated_filter_coefficients();
|
|
|
|
}
|
|
|
|
|
|
|
|
Speaker() : _buffer_in_progress_pointer(0), _requested_number_of_taps(0), _high_frequency_cut_off(-1.0) {}
|
2016-01-15 02:27:02 +00:00
|
|
|
|
2016-01-14 02:03:43 +00:00
|
|
|
protected:
|
2016-03-16 01:34:00 +00:00
|
|
|
std::unique_ptr<int16_t> _buffer_in_progress;
|
2016-08-21 22:13:31 +00:00
|
|
|
float _high_frequency_cut_off;
|
2016-01-13 03:19:56 +00:00
|
|
|
int _buffer_size;
|
|
|
|
int _buffer_in_progress_pointer;
|
2016-04-18 00:45:57 +00:00
|
|
|
int _number_of_taps, _requested_number_of_taps;
|
2016-01-13 03:19:56 +00:00
|
|
|
bool _coefficients_are_dirty;
|
|
|
|
Delegate *_delegate;
|
|
|
|
|
2016-06-13 23:30:41 +00:00
|
|
|
float _input_cycles_per_second, _output_cycles_per_second;
|
2016-01-13 03:19:56 +00:00
|
|
|
|
|
|
|
void set_needs_updated_filter_coefficients()
|
|
|
|
{
|
|
|
|
_coefficients_are_dirty = true;
|
|
|
|
}
|
2016-07-05 00:48:27 +00:00
|
|
|
|
|
|
|
void get_samples(unsigned int quantity, int16_t *target) {}
|
2016-07-05 01:14:28 +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-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.
|
|
|
|
|
|
|
|
Call `run_for_cycles(n)` to request that the next n cycles of input data are collected.
|
|
|
|
*/
|
2016-01-14 02:03:43 +00:00
|
|
|
template <class T> class Filter: public Speaker {
|
|
|
|
public:
|
2016-04-14 02:31:59 +00:00
|
|
|
void run_for_cycles(unsigned int input_cycles)
|
2016-01-14 02:03:43 +00:00
|
|
|
{
|
|
|
|
if(_coefficients_are_dirty) update_filter_coefficients();
|
|
|
|
|
2016-06-01 23:53:16 +00:00
|
|
|
// if input and output rates exactly match, just accumulate results and pass on
|
2016-08-21 16:13:41 +00:00
|
|
|
if(_input_cycles_per_second == _output_cycles_per_second && _high_frequency_cut_off < 0.0)
|
2016-01-14 02:03:43 +00:00
|
|
|
{
|
2016-06-01 23:53:16 +00:00
|
|
|
while(input_cycles)
|
2016-01-14 03:02:39 +00:00
|
|
|
{
|
2016-06-01 23:53:16 +00:00
|
|
|
unsigned int cycles_to_read = (unsigned int)(_buffer_size - _buffer_in_progress_pointer);
|
|
|
|
if(cycles_to_read > input_cycles) cycles_to_read = input_cycles;
|
|
|
|
|
|
|
|
static_cast<T *>(this)->get_samples(cycles_to_read, &_buffer_in_progress.get()[_buffer_in_progress_pointer]);
|
|
|
|
_buffer_in_progress_pointer += cycles_to_read;
|
2016-03-16 03:37:35 +00:00
|
|
|
|
|
|
|
// announce to delegate if full
|
|
|
|
if(_buffer_in_progress_pointer == _buffer_size)
|
2016-01-14 03:02:39 +00:00
|
|
|
{
|
2016-03-16 03:37:35 +00:00
|
|
|
_buffer_in_progress_pointer = 0;
|
|
|
|
if(_delegate)
|
|
|
|
{
|
|
|
|
_delegate->speaker_did_complete_samples(this, _buffer_in_progress.get(), _buffer_size);
|
|
|
|
}
|
2016-01-14 03:02:39 +00:00
|
|
|
}
|
|
|
|
|
2016-06-01 23:53:16 +00:00
|
|
|
input_cycles -= cycles_to_read;
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if the output rate is less than the input rate, use the filter
|
|
|
|
if(_input_cycles_per_second > _output_cycles_per_second)
|
|
|
|
{
|
|
|
|
while(input_cycles)
|
|
|
|
{
|
|
|
|
unsigned int cycles_to_read = (unsigned int)std::min((int)input_cycles, _number_of_taps - _input_buffer_depth);
|
|
|
|
static_cast<T *>(this)->get_samples(cycles_to_read, &_input_buffer.get()[_input_buffer_depth]);
|
|
|
|
input_cycles -= cycles_to_read;
|
|
|
|
_input_buffer_depth += cycles_to_read;
|
|
|
|
|
|
|
|
if(_input_buffer_depth == _number_of_taps)
|
2016-03-17 00:38:17 +00:00
|
|
|
{
|
2016-06-01 23:53:16 +00:00
|
|
|
_buffer_in_progress.get()[_buffer_in_progress_pointer] = _filter->apply(_input_buffer.get());
|
|
|
|
_buffer_in_progress_pointer++;
|
|
|
|
|
|
|
|
// announce to delegate if full
|
|
|
|
if(_buffer_in_progress_pointer == _buffer_size)
|
|
|
|
{
|
|
|
|
_buffer_in_progress_pointer = 0;
|
|
|
|
if(_delegate)
|
|
|
|
{
|
|
|
|
_delegate->speaker_did_complete_samples(this, _buffer_in_progress.get(), _buffer_size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
uint64_t steps = _stepper->step();
|
|
|
|
if(steps < _number_of_taps)
|
|
|
|
{
|
|
|
|
int16_t *input_buffer = _input_buffer.get();
|
2016-06-30 12:46:29 +00:00
|
|
|
memmove(input_buffer, &input_buffer[steps], sizeof(int16_t) * ((size_t)_number_of_taps - (size_t)steps));
|
2016-06-01 23:53:16 +00:00
|
|
|
_input_buffer_depth -= steps;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(steps > _number_of_taps)
|
|
|
|
static_cast<T *>(this)->skip_samples((unsigned int)steps - (unsigned int)_number_of_taps);
|
|
|
|
_input_buffer_depth = 0;
|
|
|
|
}
|
2016-03-17 00:38:17 +00:00
|
|
|
}
|
2016-03-16 03:37:35 +00:00
|
|
|
}
|
2016-06-01 23:53:16 +00:00
|
|
|
|
|
|
|
return;
|
2016-01-14 02:03:43 +00:00
|
|
|
}
|
2016-06-01 23:53:16 +00:00
|
|
|
|
|
|
|
// TODO: input rate is less than output rate
|
2016-01-14 02:03:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2016-03-16 01:34:00 +00:00
|
|
|
std::unique_ptr<SignalProcessing::Stepper> _stepper;
|
|
|
|
std::unique_ptr<SignalProcessing::FIRFilter> _filter;
|
2016-03-16 03:37:35 +00:00
|
|
|
|
|
|
|
std::unique_ptr<int16_t> _input_buffer;
|
|
|
|
int _input_buffer_depth;
|
2016-01-13 03:19:56 +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
|
|
|
|
if(_requested_number_of_taps)
|
|
|
|
{
|
|
|
|
_number_of_taps = _requested_number_of_taps;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-06-13 23:30:41 +00:00
|
|
|
_number_of_taps = (int)ceilf((_input_cycles_per_second + _output_cycles_per_second) / _output_cycles_per_second);
|
2016-04-18 00:45:57 +00:00
|
|
|
_number_of_taps *= 2;
|
|
|
|
_number_of_taps |= 1;
|
|
|
|
}
|
2016-04-18 00:43:20 +00:00
|
|
|
|
2016-01-13 03:19:56 +00:00
|
|
|
_coefficients_are_dirty = false;
|
|
|
|
_buffer_in_progress_pointer = 0;
|
|
|
|
|
2016-06-24 00:52:44 +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;
|
|
|
|
if(_high_frequency_cut_off > 0.0)
|
|
|
|
{
|
|
|
|
high_pass_frequency = std::min((float)_output_cycles_per_second / 2.0f, _high_frequency_cut_off);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
high_pass_frequency = (float)_output_cycles_per_second / 2.0f;
|
|
|
|
}
|
|
|
|
_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
|
|
|
|
2016-06-24 00:52:44 +00:00
|
|
|
_input_buffer.reset(new int16_t[_number_of_taps]);
|
2016-03-16 03:37:35 +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 */
|