Chris@137: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@150: /* Chris@150: QM DSP Library Chris@150: Chris@150: Centre for Digital Music, Queen Mary, University of London. Chris@150: This file by Chris Cannam. Chris@150: Chris@150: This program is free software; you can redistribute it and/or Chris@150: modify it under the terms of the GNU General Public License as Chris@150: published by the Free Software Foundation; either version 2 of the Chris@150: License, or (at your option) any later version. See the file Chris@150: COPYING included with this distribution for more information. Chris@150: */ Chris@137: Chris@137: #ifndef RESAMPLER_H Chris@137: #define RESAMPLER_H Chris@137: Chris@137: #include Chris@137: Chris@150: /** Chris@150: * Resampler resamples a stream from one integer sample rate to Chris@150: * another (arbitrary) rate, using a kaiser-windowed sinc filter. The Chris@150: * results and performance are pretty similar to libraries such as Chris@150: * libsamplerate, though this implementation does not support Chris@150: * time-varying ratios (the ratio is fixed on construction). Chris@150: * Chris@150: * See also Decimator, which is faster and rougher but supports only Chris@150: * power-of-two downsampling factors. Chris@150: */ Chris@137: class Resampler Chris@137: { Chris@137: public: Chris@137: /** Chris@137: * Construct a Resampler to resample from sourceRate to Chris@137: * targetRate. Chris@137: */ Chris@137: Resampler(int sourceRate, int targetRate); Chris@149: Chris@149: /** Chris@149: * Construct a Resampler to resample from sourceRate to Chris@149: * targetRate, using the given filter parameters. Chris@149: */ Chris@149: Resampler(int sourceRate, int targetRate, Chris@149: double snr, double bandwidth); Chris@149: Chris@137: virtual ~Resampler(); Chris@137: Chris@137: /** Chris@137: * Read n input samples from src and write resampled data to Chris@137: * dst. The return value is the number of samples written, which Chris@137: * will be no more than ceil((n * targetRate) / sourceRate). The Chris@137: * caller must ensure the dst buffer has enough space for the Chris@137: * samples returned. Chris@137: */ Chris@137: int process(const double *src, double *dst, int n); Chris@137: Chris@137: /** Chris@137: * Return the number of samples of latency at the output due by Chris@137: * the filter. (That is, the output will be delayed by this number Chris@137: * of samples relative to the input.) Chris@137: */ Chris@137: int getLatency() const { return m_latency; } Chris@137: Chris@138: /** Chris@138: * Carry out a one-off resample of a single block of n Chris@138: * samples. The output is latency-compensated. Chris@138: */ Chris@138: static std::vector resample Chris@138: (int sourceRate, int targetRate, const double *data, int n); Chris@138: Chris@137: private: Chris@137: int m_sourceRate; Chris@137: int m_targetRate; Chris@137: int m_gcd; Chris@137: int m_filterLength; Chris@137: int m_bufferLength; Chris@137: int m_latency; Chris@156: double m_peakToPole; Chris@137: Chris@137: struct Phase { Chris@137: int nextPhase; Chris@137: std::vector filter; Chris@137: int drop; Chris@137: }; Chris@137: Chris@137: Phase *m_phaseData; Chris@137: int m_phase; Chris@139: std::vector m_buffer; Chris@145: int m_bufferOrigin; Chris@137: Chris@149: void initialise(double, double); Chris@141: double reconstructOne(); Chris@137: }; Chris@137: Chris@137: #endif Chris@137: