summaryrefslogtreecommitdiff
path: root/libs/qm-dsp/dsp/rateconversion/Resampler.h
blob: 92c0169ba0ab5dedecd1e5e16bdd7261598234f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
/*
    QM DSP Library

    Centre for Digital Music, Queen Mary, University of London.
    This file by Chris Cannam.

    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 of the
    License, or (at your option) any later version.  See the file
    COPYING included with this distribution for more information.
*/

#ifndef RESAMPLER_H
#define RESAMPLER_H

#include <vector>

/**
 * Resampler resamples a stream from one integer sample rate to
 * another (arbitrary) rate, using a kaiser-windowed sinc filter.  The
 * results and performance are pretty similar to libraries such as
 * libsamplerate, though this implementation does not support
 * time-varying ratios (the ratio is fixed on construction).
 *
 * See also Decimator, which is faster and rougher but supports only
 * power-of-two downsampling factors.
 */
class Resampler
{
public:
    /**
     * Construct a Resampler to resample from sourceRate to
     * targetRate.
     */
    Resampler(int sourceRate, int targetRate);

    /**
     * Construct a Resampler to resample from sourceRate to
     * targetRate, using the given filter parameters.
     */
    Resampler(int sourceRate, int targetRate,
              double snr, double bandwidth);

    virtual ~Resampler();

    /**
     * Read n input samples from src and write resampled data to
     * dst. The return value is the number of samples written, which
     * will be no more than ceil((n * targetRate) / sourceRate). The
     * caller must ensure the dst buffer has enough space for the
     * samples returned.
     */
    int process(const double *src, double *dst, int n);

    /**
     * Read n input samples from src and return resampled data by
     * value.
     */
    std::vector<double> process(const double *src, int n);

    /**
     * Return the number of samples of latency at the output due by
     * the filter. (That is, the output will be delayed by this number
     * of samples relative to the input.)
     */
    int getLatency() const { return m_latency; }

    /**
     * Carry out a one-off resample of a single block of n
     * samples. The output is latency-compensated.
     */
    static std::vector<double> resample
    (int sourceRate, int targetRate, const double *data, int n);

private:
    int m_sourceRate;
    int m_targetRate;
    int m_gcd;
    int m_filterLength;
    int m_bufferLength;
    int m_latency;
    double m_peakToPole;
    
    struct Phase {
        int nextPhase;
        std::vector<double> filter;
        int drop;
    };

    Phase *m_phaseData;
    int m_phase;
    std::vector<double> m_buffer;
    int m_bufferOrigin;

    void initialise(double, double);
    double reconstructOne();
};

#endif