1 // $package. http://www.slack.net/~ant/
2
3 #include "Downsampler.h"
4
5 /* Copyright (C) 2004-2008 Shay Green. This module is free software; you
6 can redistribute it and/or modify it under the terms of the GNU Lesser
7 General Public License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version. This
9 module is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12 details. You should have received a copy of the GNU Lesser General Public
13 License along with this module; if not, write to the Free Software Foundation,
14 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */
15
16 #include "blargg_source.h"
17
18 int const shift = 14;
19 int const unit = 1 << shift;
20
clear_()21 void Downsampler::clear_()
22 {
23 pos = 0;
24 Resampler::clear_();
25 }
26
Downsampler()27 Downsampler::Downsampler()
28 {
29 clear();
30 }
31
set_rate_(double new_factor)32 blargg_err_t Downsampler::set_rate_( double new_factor )
33 {
34 step = (int) (new_factor * unit + 0.5);
35 return Resampler::set_rate_( 1.0 / unit * step );
36 }
37
resample_(sample_t ** out_,sample_t const * out_end,sample_t const in[],int in_size)38 Resampler::sample_t const* Downsampler::resample_( sample_t** out_,
39 sample_t const* out_end, sample_t const in [], int in_size )
40 {
41 in_size -= write_offset;
42 if ( in_size > 0 )
43 {
44 sample_t* BLARGG_RESTRICT out = *out_;
45 sample_t const* const in_end = in + in_size;
46
47 int const step = this->step;
48 int pos = this->pos;
49
50 // TODO: IIR filter, then linear resample
51 // TODO: detect skipped sample, allowing merging of IIR and resample?
52
53 do
54 {
55 #define INTERP( i, out )\
56 out = (in [0 + i] * (unit - pos) + ((in [2 + i] + in [4 + i] + in [6 + i]) << shift) +\
57 in [8 + i] * pos) >> (shift + 2);
58
59 int out_0;
60 INTERP( 0, out_0 )
61 INTERP( 1, out [0] = out_0; out [1] )
62 out += stereo;
63
64 pos += step;
65 in += ((unsigned) pos >> shift) * stereo;
66 pos &= unit - 1;
67 }
68 while ( in < in_end && out < out_end );
69
70 this->pos = pos;
71 *out_ = out;
72 }
73 return in;
74 }
75