2015-09-26 01:09:35 -04:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2015 Jared Boone, ShareBrained Technology, Inc.
|
|
|
|
*
|
|
|
|
* This file is part of PortaPack.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; see the file COPYING. If not, write to
|
|
|
|
* the Free Software Foundation, Inc., 51 Franklin Street,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __LINEAR_RESAMPLER_H__
|
|
|
|
#define __LINEAR_RESAMPLER_H__
|
|
|
|
|
|
|
|
namespace dsp {
|
|
|
|
namespace interpolation {
|
|
|
|
|
|
|
|
class LinearResampler {
|
2023-05-18 16:16:05 -04:00
|
|
|
public:
|
|
|
|
void configure(
|
|
|
|
const float input_rate,
|
|
|
|
const float output_rate) {
|
|
|
|
phase_increment = calculate_increment(input_rate, output_rate);
|
|
|
|
}
|
2015-09-26 01:09:35 -04:00
|
|
|
|
2023-05-18 16:16:05 -04:00
|
|
|
template <typename InterpolatedSampleHandler>
|
|
|
|
void operator()(
|
|
|
|
const float sample,
|
|
|
|
InterpolatedSampleHandler interpolated_sample_handler) {
|
|
|
|
const float sample_delta = sample - last_sample;
|
|
|
|
while (phase < 1.0f) {
|
|
|
|
const float interpolated_value = last_sample + phase * sample_delta;
|
|
|
|
interpolated_sample_handler(interpolated_value);
|
|
|
|
phase += phase_increment;
|
|
|
|
}
|
|
|
|
last_sample = sample;
|
|
|
|
phase -= 1.0f;
|
|
|
|
}
|
2015-09-26 01:09:35 -04:00
|
|
|
|
2023-05-18 16:16:05 -04:00
|
|
|
void advance(const float fraction) {
|
|
|
|
phase += (fraction * phase_increment);
|
|
|
|
}
|
2015-09-26 01:09:35 -04:00
|
|
|
|
2023-05-18 16:16:05 -04:00
|
|
|
private:
|
|
|
|
float last_sample{0.0f};
|
|
|
|
float phase{0.0f};
|
|
|
|
float phase_increment{0.0f};
|
2015-09-26 01:09:35 -04:00
|
|
|
|
2023-05-18 16:16:05 -04:00
|
|
|
static constexpr float calculate_increment(const float input_rate, const float output_rate) {
|
|
|
|
return input_rate / output_rate;
|
|
|
|
}
|
2015-09-26 01:09:35 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
} /* namespace interpolation */
|
|
|
|
} /* namespace dsp */
|
|
|
|
|
2023-05-18 16:16:05 -04:00
|
|
|
#endif /*__LINEAR_RESAMPLER_H__*/
|