c@119
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
c@119
|
2 /*
|
c@119
|
3 Constant-Q library
|
c@119
|
4 Copyright (c) 2013-2014 Queen Mary, University of London
|
c@119
|
5
|
c@119
|
6 Permission is hereby granted, free of charge, to any person
|
c@119
|
7 obtaining a copy of this software and associated documentation
|
c@119
|
8 files (the "Software"), to deal in the Software without
|
c@119
|
9 restriction, including without limitation the rights to use, copy,
|
c@119
|
10 modify, merge, publish, distribute, sublicense, and/or sell copies
|
c@119
|
11 of the Software, and to permit persons to whom the Software is
|
c@119
|
12 furnished to do so, subject to the following conditions:
|
c@119
|
13
|
c@119
|
14 The above copyright notice and this permission notice shall be
|
c@119
|
15 included in all copies or substantial portions of the Software.
|
c@119
|
16
|
c@119
|
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
c@119
|
18 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
c@119
|
19 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
c@119
|
20 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
c@119
|
21 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
|
c@119
|
22 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
c@119
|
23 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
c@119
|
24
|
c@119
|
25 Except as contained in this notice, the names of the Centre for
|
c@119
|
26 Digital Music; Queen Mary, University of London; and Chris Cannam
|
c@119
|
27 shall not be used in advertising or otherwise to promote the sale,
|
c@119
|
28 use or other dealings in this Software without prior written
|
c@119
|
29 authorization.
|
c@119
|
30 */
|
c@119
|
31
|
c@119
|
32 #ifndef RESAMPLER_H
|
c@119
|
33 #define RESAMPLER_H
|
c@119
|
34
|
c@119
|
35 #include <vector>
|
c@119
|
36
|
c@119
|
37 /**
|
c@119
|
38 * Resampler resamples a stream from one integer sample rate to
|
c@119
|
39 * another (arbitrary) rate, using a kaiser-windowed sinc filter. The
|
c@119
|
40 * results and performance are pretty similar to libraries such as
|
c@119
|
41 * libsamplerate, though this implementation does not support
|
c@119
|
42 * time-varying ratios (the ratio is fixed on construction).
|
c@119
|
43 *
|
c@119
|
44 * See also Decimator, which is faster and rougher but supports only
|
c@119
|
45 * power-of-two downsampling factors.
|
c@119
|
46 */
|
c@119
|
47 class Resampler
|
c@119
|
48 {
|
c@119
|
49 public:
|
c@119
|
50 /**
|
c@119
|
51 * Construct a Resampler to resample from sourceRate to
|
c@119
|
52 * targetRate.
|
c@119
|
53 */
|
c@119
|
54 Resampler(int sourceRate, int targetRate);
|
c@119
|
55
|
c@119
|
56 /**
|
c@119
|
57 * Construct a Resampler to resample from sourceRate to
|
c@119
|
58 * targetRate, using the given filter parameters.
|
c@119
|
59 */
|
c@119
|
60 Resampler(int sourceRate, int targetRate,
|
c@119
|
61 double snr, double bandwidth);
|
c@119
|
62
|
c@119
|
63 virtual ~Resampler();
|
c@119
|
64
|
c@119
|
65 /**
|
c@119
|
66 * Read n input samples from src and write resampled data to
|
c@119
|
67 * dst. The return value is the number of samples written, which
|
c@119
|
68 * will be no more than ceil((n * targetRate) / sourceRate). The
|
c@119
|
69 * caller must ensure the dst buffer has enough space for the
|
c@119
|
70 * samples returned.
|
c@119
|
71 */
|
c@119
|
72 int process(const double *src, double *dst, int n);
|
c@119
|
73
|
c@119
|
74 /**
|
c@119
|
75 * Read n input samples from src and return resampled data by
|
c@119
|
76 * value.
|
c@119
|
77 */
|
c@119
|
78 std::vector<double> process(const double *src, int n);
|
c@119
|
79
|
c@119
|
80 /**
|
c@119
|
81 * Return the number of samples of latency at the output due by
|
c@119
|
82 * the filter. (That is, the output will be delayed by this number
|
c@119
|
83 * of samples relative to the input.)
|
c@119
|
84 */
|
c@119
|
85 int getLatency() const { return m_latency; }
|
c@119
|
86
|
c@119
|
87 /**
|
c@119
|
88 * Carry out a one-off resample of a single block of n
|
c@119
|
89 * samples. The output is latency-compensated.
|
c@119
|
90 */
|
c@119
|
91 static std::vector<double> resample
|
c@119
|
92 (int sourceRate, int targetRate, const double *data, int n);
|
c@119
|
93
|
c@119
|
94 private:
|
c@119
|
95 int m_sourceRate;
|
c@119
|
96 int m_targetRate;
|
c@119
|
97 int m_gcd;
|
c@119
|
98 int m_filterLength;
|
c@119
|
99 int m_latency;
|
c@119
|
100 double m_peakToPole;
|
c@119
|
101
|
c@119
|
102 struct Phase {
|
c@119
|
103 int nextPhase;
|
c@119
|
104 std::vector<double> filter;
|
c@119
|
105 int drop;
|
c@119
|
106 };
|
c@119
|
107
|
c@119
|
108 Phase *m_phaseData;
|
c@119
|
109 int m_phase;
|
c@119
|
110 std::vector<double> m_buffer;
|
c@119
|
111 int m_bufferOrigin;
|
c@119
|
112
|
c@119
|
113 void initialise(double, double);
|
c@119
|
114 double reconstructOne();
|
c@119
|
115 };
|
c@119
|
116
|
c@119
|
117 #endif
|
c@119
|
118
|