fix compile errors
[wdl/wdl-ol.git] / WDL / resample.h
blob49478cb85bab008d8f2be38a3721edc169b58299
1 /*
2 WDL - resample.h
3 Copyright (C) 2010 and later Cockos Incorporated
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
21 You may also distribute this software under the LGPL v2 or later.
25 #ifndef _WDL_RESAMPLE_H_
26 #define _WDL_RESAMPLE_H_
28 #include <stdlib.h>
29 #include <string.h>
30 #include "wdltypes.h"
31 #include "heapbuf.h"
33 // default to floats for sinc filter ceofficients
34 #ifdef WDL_RESAMPLE_FULL_SINC_PRECISION
35 typedef double WDL_SincFilterSample;
36 #else
37 typedef float WDL_SincFilterSample;
38 #endif
40 // default to doubles for audio samples
41 #ifdef WDL_RESAMPLE_TYPE
42 typedef WDL_RESAMPLE_TYPE WDL_ResampleSample;
43 #else
44 typedef double WDL_ResampleSample;
45 #endif
48 #ifndef WDL_RESAMPLE_MAX_FILTERS
49 #define WDL_RESAMPLE_MAX_FILTERS 4
50 #endif
52 #ifndef WDL_RESAMPLE_MAX_NCH
53 #define WDL_RESAMPLE_MAX_NCH 64
54 #endif
57 class WDL_Resampler
59 public:
60 WDL_Resampler();
61 ~WDL_Resampler();
62 // if sinc set, it overrides interp or filtercnt
63 void SetMode(bool interp, int filtercnt, bool sinc, int sinc_size=64, int sinc_interpsize=32);
65 void SetFilterParms(float filterpos=0.693, float filterq=0.707) { m_filterpos=filterpos; m_filterq=filterq; } // used for filtercnt>0 but not sinc
66 void SetFeedMode(bool wantInputDriven) { m_feedmode=wantInputDriven; } // if true, that means the first parameter to ResamplePrepare will specify however much input you have, not how much you want
68 void Reset(double fracpos=0.0);
69 void SetRates(double rate_in, double rate_out);
71 double GetCurrentLatency(); // amount of input that has been received but not yet converted to output, in seconds
73 // req_samples is output samples desired if !wantInputDriven, or if wantInputDriven is input samples that we have
74 // returns number of samples desired (put these into *inbuffer)
75 // note that it is safe to call ResamplePrepare without calling ResampleOut (the next call of ResamplePrepare will function as normal)
76 int ResamplePrepare(int req_samples, int nch, WDL_ResampleSample **inbuffer);
79 // if numsamples_in < the value return by ResamplePrepare(), then it will be flushed to produce all remaining valid samples
80 // do NOT call with nsamples_in greater than the value returned from resamplerprpare()! the extra samples will be ignored.
81 // returns number of samples successfully outputted to out
82 int ResampleOut(WDL_ResampleSample *out, int nsamples_in, int nsamples_out, int nch);
86 private:
87 void BuildLowPass(double filtpos);
88 void inline SincSample(WDL_ResampleSample *outptr, const WDL_ResampleSample *inptr, double fracpos, int nch, const WDL_SincFilterSample *filter, int filtsz);
89 void inline SincSample1(WDL_ResampleSample *outptr, const WDL_ResampleSample *inptr, double fracpos, const WDL_SincFilterSample *filter, int filtsz);
90 void inline SincSample2(WDL_ResampleSample *outptr, const WDL_ResampleSample *inptr, double fracpos, const WDL_SincFilterSample *filter, int filtsz);
92 double m_sratein WDL_FIXALIGN;
93 double m_srateout;
94 double m_fracpos;
95 double m_ratio;
96 double m_filter_ratio;
97 float m_filterq, m_filterpos;
98 WDL_TypedBuf<WDL_ResampleSample> m_rsinbuf;
99 WDL_TypedBuf<WDL_SincFilterSample> m_filter_coeffs;
101 class WDL_Resampler_IIRFilter;
102 WDL_Resampler_IIRFilter *m_iirfilter;
104 int m_filter_coeffs_size;
105 int m_last_requested;
106 int m_filtlatency;
107 int m_samples_in_rsinbuf;
108 int m_lp_oversize;
110 int m_sincsize;
111 int m_filtercnt;
112 int m_sincoversize;
113 bool m_interp;
114 bool m_feedmode;
120 #endif