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