cog/Frameworks/GME/gme/Fir_Resampler.cpp

124 lines
3.3 KiB
C++
Raw Normal View History

2013-09-28 03:24:23 +00:00
// $package. http://www.slack.net/~ant/
2007-10-11 23:11:58 +00:00
#include "Fir_Resampler.h"
#include <math.h>
2013-09-28 03:24:23 +00:00
/* Copyright (C) 2004-2008 Shay Green. This module is free software; you
2007-10-11 23:11:58 +00:00
can redistribute it and/or modify it under the terms of the GNU Lesser
General Public License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version. This
module is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
details. You should have received a copy of the GNU Lesser General Public
License along with this module; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */
#include "blargg_source.h"
#undef PI
#define PI 3.1415926535897932384626433832795029
static void gen_sinc( double rolloff, int width, double offset, double spacing, double scale,
int count, short* out )
{
double const maxh = 256;
double const step = PI / maxh * spacing;
double const to_w = maxh * 2 / width;
double const pow_a_n = pow( rolloff, maxh );
scale /= maxh * 2;
double angle = (count / 2 - 1 + offset) * -step;
while ( count-- )
{
*out++ = 0;
double w = angle * to_w;
if ( fabs( w ) < PI )
{
double rolloff_cos_a = rolloff * cos( angle );
double num = 1 - rolloff_cos_a -
pow_a_n * cos( maxh * angle ) +
pow_a_n * rolloff * cos( (maxh - 1) * angle );
double den = 1 - rolloff_cos_a - rolloff_cos_a + rolloff * rolloff;
double sinc = scale * num / den - scale;
out [-1] = (short) (cos( w ) * sinc + sinc);
}
angle += step;
}
}
2013-09-28 03:24:23 +00:00
Fir_Resampler_::Fir_Resampler_( int width, sample_t impulses_ [] ) :
2007-10-11 23:11:58 +00:00
width_( width ),
impulses( impulses_ )
{
2013-09-28 03:24:23 +00:00
imp = NULL;
2007-10-11 23:11:58 +00:00
}
2013-09-28 03:24:23 +00:00
void Fir_Resampler_::clear_()
2007-10-11 23:11:58 +00:00
{
2013-09-28 03:24:23 +00:00
imp = impulses;
Resampler::clear_();
2007-10-11 23:11:58 +00:00
}
2013-09-28 03:24:23 +00:00
blargg_err_t Fir_Resampler_::set_rate_( double new_factor )
2007-10-11 23:11:58 +00:00
{
2013-09-28 03:24:23 +00:00
double const rolloff = 0.999;
double const gain = 1.0;
2007-10-11 23:11:58 +00:00
2013-09-28 03:24:23 +00:00
// determine number of sub-phases that yield lowest error
double ratio_ = 0.0;
int res = -1;
2007-10-11 23:11:58 +00:00
{
double least_error = 2;
double pos = 0;
for ( int r = 1; r <= max_res; r++ )
{
2013-09-28 03:24:23 +00:00
pos += new_factor;
2007-10-11 23:11:58 +00:00
double nearest = floor( pos + 0.5 );
double error = fabs( pos - nearest );
if ( error < least_error )
{
res = r;
2013-09-28 03:24:23 +00:00
ratio_ = nearest / res;
2007-10-11 23:11:58 +00:00
least_error = error;
}
}
}
2013-09-28 03:24:23 +00:00
RETURN_ERR( Resampler::set_rate_( ratio_ ) );
2007-10-11 23:11:58 +00:00
2013-09-28 03:24:23 +00:00
// how much of input is used for each output sample
int const step = stereo * (int) floor( ratio_ );
double fraction = fmod( ratio_, 1.0 );
2007-10-11 23:11:58 +00:00
2013-09-28 03:24:23 +00:00
double const filter = (ratio_ < 1.0) ? 1.0 : 1.0 / ratio_;
2007-10-11 23:11:58 +00:00
double pos = 0.0;
2013-09-28 03:24:23 +00:00
//int input_per_cycle = 0;
sample_t* out = impulses;
for ( int n = res; --n >= 0; )
2007-10-11 23:11:58 +00:00
{
gen_sinc( rolloff, int (width_ * filter + 1) & ~1, pos, filter,
2013-09-28 03:24:23 +00:00
double (0x7FFF * gain * filter), (int) width_, out );
out += width_;
2007-10-11 23:11:58 +00:00
2013-09-28 03:24:23 +00:00
int cur_step = step;
pos += fraction;
2007-10-11 23:11:58 +00:00
if ( pos >= 0.9999999 )
{
pos -= 1.0;
2013-09-28 03:24:23 +00:00
cur_step += stereo;
2007-10-11 23:11:58 +00:00
}
2013-09-28 03:24:23 +00:00
*out++ = (cur_step - width_ * 2 + 4) * sizeof (sample_t);
*out++ = 4 * sizeof (sample_t);
//input_per_cycle += cur_step;
2007-10-11 23:11:58 +00:00
}
2013-09-28 03:24:23 +00:00
// last offset moves back to beginning of impulses
out [-1] -= (char*) out - (char*) impulses;
2007-10-11 23:11:58 +00:00
2013-09-28 03:24:23 +00:00
imp = impulses;
2007-10-11 23:11:58 +00:00
2013-09-28 03:24:23 +00:00
return blargg_ok;
2007-10-11 23:11:58 +00:00
}