annotate dsp/rateconversion/Resampler.h @ 385:0d79970811c7

Add std::vector-returning process call
author Chris Cannam <c.cannam@qmul.ac.uk>
date Tue, 05 Nov 2013 16:36:53 +0000
parents 88971211795c
children cc51baf8e37e
rev   line source
c@362 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
c@375 2 /*
c@375 3 QM DSP Library
c@375 4
c@375 5 Centre for Digital Music, Queen Mary, University of London.
c@375 6 This file by Chris Cannam.
c@375 7
c@375 8 This program is free software; you can redistribute it and/or
c@375 9 modify it under the terms of the GNU General Public License as
c@375 10 published by the Free Software Foundation; either version 2 of the
c@375 11 License, or (at your option) any later version. See the file
c@375 12 COPYING included with this distribution for more information.
c@375 13 */
c@362 14
c@362 15 #ifndef RESAMPLER_H
c@362 16 #define RESAMPLER_H
c@362 17
c@362 18 #include <vector>
c@362 19
c@375 20 /**
c@375 21 * Resampler resamples a stream from one integer sample rate to
c@375 22 * another (arbitrary) rate, using a kaiser-windowed sinc filter. The
c@375 23 * results and performance are pretty similar to libraries such as
c@375 24 * libsamplerate, though this implementation does not support
c@375 25 * time-varying ratios (the ratio is fixed on construction).
c@375 26 *
c@375 27 * See also Decimator, which is faster and rougher but supports only
c@375 28 * power-of-two downsampling factors.
c@375 29 */
c@362 30 class Resampler
c@362 31 {
c@362 32 public:
c@362 33 /**
c@362 34 * Construct a Resampler to resample from sourceRate to
c@362 35 * targetRate.
c@362 36 */
c@362 37 Resampler(int sourceRate, int targetRate);
c@374 38
c@374 39 /**
c@374 40 * Construct a Resampler to resample from sourceRate to
c@374 41 * targetRate, using the given filter parameters.
c@374 42 */
c@374 43 Resampler(int sourceRate, int targetRate,
c@374 44 double snr, double bandwidth);
c@374 45
c@362 46 virtual ~Resampler();
c@362 47
c@362 48 /**
c@362 49 * Read n input samples from src and write resampled data to
c@362 50 * dst. The return value is the number of samples written, which
c@362 51 * will be no more than ceil((n * targetRate) / sourceRate). The
c@362 52 * caller must ensure the dst buffer has enough space for the
c@362 53 * samples returned.
c@362 54 */
c@362 55 int process(const double *src, double *dst, int n);
c@362 56
c@362 57 /**
c@385 58 * Read n input samples from src and return resampled data by
c@385 59 * value.
c@385 60 */
c@385 61 std::vector<double> process(const double *src, int n);
c@385 62
c@385 63 /**
c@362 64 * Return the number of samples of latency at the output due by
c@362 65 * the filter. (That is, the output will be delayed by this number
c@362 66 * of samples relative to the input.)
c@362 67 */
c@362 68 int getLatency() const { return m_latency; }
c@362 69
c@363 70 /**
c@363 71 * Carry out a one-off resample of a single block of n
c@363 72 * samples. The output is latency-compensated.
c@363 73 */
c@363 74 static std::vector<double> resample
c@363 75 (int sourceRate, int targetRate, const double *data, int n);
c@363 76
c@362 77 private:
c@362 78 int m_sourceRate;
c@362 79 int m_targetRate;
c@362 80 int m_gcd;
c@362 81 int m_filterLength;
c@362 82 int m_bufferLength;
c@362 83 int m_latency;
c@381 84 double m_peakToPole;
c@362 85
c@362 86 struct Phase {
c@362 87 int nextPhase;
c@362 88 std::vector<double> filter;
c@362 89 int drop;
c@362 90 };
c@362 91
c@362 92 Phase *m_phaseData;
c@362 93 int m_phase;
c@364 94 std::vector<double> m_buffer;
c@370 95 int m_bufferOrigin;
c@362 96
c@374 97 void initialise(double, double);
c@366 98 double reconstructOne();
c@362 99 };
c@362 100
c@362 101 #endif
c@362 102