mirror of
https://github.com/aicodix/dsp.git
synced 2026-04-27 22:35:45 +00:00
added streaming resampler
This commit is contained in:
parent
b857753ddf
commit
db805aff47
1 changed files with 49 additions and 0 deletions
49
resampler.hh
49
resampler.hh
|
|
@ -49,5 +49,54 @@ public:
|
|||
}
|
||||
};
|
||||
|
||||
template <typename TYPE, typename IO, int TAPS, int OVER>
|
||||
class Resampler2
|
||||
{
|
||||
typedef DSP::UniformNaturalCubicSpline<TAPS * OVER, TYPE, TYPE> spline_type;
|
||||
spline_type lpf;
|
||||
TYPE rate;
|
||||
TYPE dpos;
|
||||
IO buf[TAPS];
|
||||
public:
|
||||
Resampler2(TYPE rate, TYPE cutoff, TYPE alpha) : rate(rate), dpos(0)
|
||||
{
|
||||
TYPE tmp[TAPS * OVER];
|
||||
DSP::Kaiser<TYPE> window(alpha);
|
||||
for (int n = 0; n < TAPS * OVER; ++n) {
|
||||
TYPE x = TYPE(n) / TYPE(OVER) - TYPE(TAPS - 1) / TYPE(2);
|
||||
TYPE y = TYPE(2) * cutoff / rate * sinc(TYPE(2) * cutoff / rate * x);
|
||||
tmp[n] = y * window(n, TAPS * OVER);
|
||||
}
|
||||
lpf = spline_type(tmp, 0, 1.0 / OVER);
|
||||
for (int i = 0; i < TAPS; ++i)
|
||||
buf[i] = 0;
|
||||
}
|
||||
int operator ()(IO *output, IO input, TYPE diff)
|
||||
{
|
||||
for (int i = 0; i < TAPS-1; ++i)
|
||||
buf[i] = buf[i+1];
|
||||
buf[TAPS-1] = input;
|
||||
if (dpos < TYPE(-0.5)) {
|
||||
dpos += TYPE(1);
|
||||
return 0;
|
||||
}
|
||||
TYPE delta = diff / rate;
|
||||
int samples = 0;
|
||||
again:
|
||||
IO sum = 0;
|
||||
for (int s = 0; s < TAPS; ++s) {
|
||||
TYPE k = s + dpos;
|
||||
sum += lpf(k) * buf[s];
|
||||
}
|
||||
output[samples++] = sum;
|
||||
dpos += delta;
|
||||
if (dpos > TYPE(0.5)) {
|
||||
dpos -= TYPE(1);
|
||||
goto again;
|
||||
}
|
||||
return samples;
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue