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-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-01-13 03:19:56 +00:00
|
|
|
void set_output_rate(int cycles_per_second, int buffer_size)
|
|
|
|
{
|
|
|
|
_output_cycles_per_second = cycles_per_second;
|
|
|
|
if(_buffer_size != buffer_size)
|
|
|
|
{
|
2016-03-16 01:34:00 +00:00
|
|
|
_buffer_in_progress = std::unique_ptr<int16_t>(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-01-13 03:19:56 +00:00
|
|
|
void set_output_quality(int number_of_taps)
|
|
|
|
{
|
|
|
|
_number_of_taps = number_of_taps;
|
|
|
|
set_needs_updated_filter_coefficients();
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_delegate(Delegate *delegate)
|
|
|
|
{
|
|
|
|
_delegate = delegate;
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_input_rate(int cycles_per_second)
|
|
|
|
{
|
|
|
|
_input_cycles_per_second = cycles_per_second;
|
|
|
|
set_needs_updated_filter_coefficients();
|
|
|
|
}
|
|
|
|
|
2016-01-15 02:27:02 +00:00
|
|
|
Speaker() : _buffer_in_progress_pointer(0) {}
|
|
|
|
|
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-01-13 03:19:56 +00:00
|
|
|
int _buffer_size;
|
|
|
|
int _buffer_in_progress_pointer;
|
|
|
|
int _number_of_taps;
|
|
|
|
bool _coefficients_are_dirty;
|
|
|
|
Delegate *_delegate;
|
|
|
|
|
|
|
|
int _input_cycles_per_second, _output_cycles_per_second;
|
|
|
|
|
|
|
|
void set_needs_updated_filter_coefficients()
|
|
|
|
{
|
|
|
|
_coefficients_are_dirty = true;
|
|
|
|
}
|
2016-01-14 02:03:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template <class T> class Filter: public Speaker {
|
|
|
|
public:
|
|
|
|
void run_for_cycles(int input_cycles)
|
|
|
|
{
|
|
|
|
if(_coefficients_are_dirty) update_filter_coefficients();
|
|
|
|
|
2016-03-16 03:37:35 +00:00
|
|
|
// TODO: what if output rate is greater than input rate?
|
|
|
|
|
|
|
|
// fill up as much of the input buffer as possible
|
|
|
|
while(input_cycles)
|
2016-01-14 02:03:43 +00:00
|
|
|
{
|
2016-03-16 03:37:35 +00:00
|
|
|
unsigned int cycles_to_read = (unsigned int)std::min(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;
|
2016-01-14 03:02:39 +00:00
|
|
|
|
2016-03-16 03:37:35 +00:00
|
|
|
if(_input_buffer_depth == _number_of_taps)
|
2016-01-14 03:02:39 +00:00
|
|
|
{
|
2016-03-16 03:37:35 +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)
|
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-03-16 03:37:35 +00:00
|
|
|
uint64_t steps = _stepper->step();
|
|
|
|
int16_t *input_buffer = _input_buffer.get();
|
|
|
|
memmove(input_buffer, &input_buffer[steps], sizeof(int16_t) * ((size_t)_number_of_taps - (size_t)steps));
|
|
|
|
_input_buffer_depth -= steps;
|
|
|
|
}
|
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()
|
|
|
|
{
|
|
|
|
_coefficients_are_dirty = false;
|
|
|
|
_buffer_in_progress_pointer = 0;
|
|
|
|
|
2016-03-16 01:34:00 +00:00
|
|
|
_stepper = std::unique_ptr<SignalProcessing::Stepper>(new SignalProcessing::Stepper((uint64_t)_input_cycles_per_second, (uint64_t)_output_cycles_per_second));
|
|
|
|
_filter = std::unique_ptr<SignalProcessing::FIRFilter>(new SignalProcessing::FIRFilter((unsigned int)_number_of_taps, (unsigned int)_input_cycles_per_second, 0.0, (float)_output_cycles_per_second / 2.0f, SignalProcessing::FIRFilter::DefaultAttenuation));
|
2016-03-16 03:37:35 +00:00
|
|
|
|
|
|
|
_input_buffer = std::unique_ptr<int16_t>(new int16_t[_number_of_taps]);
|
|
|
|
_input_buffer_depth = 0;
|
2016-01-13 03:19:56 +00:00
|
|
|
}
|
2016-01-12 21:54:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* Speaker_hpp */
|