コード例 #1
0
ファイル: Fir_Resampler.cpp プロジェクト: 9a3eedi/Droidsound
blargg_err_t Fir_Resampler_::set_rate_( double new_factor )
{
	double const rolloff = 0.999;
	double const gain = 1.0;
	
	// determine number of sub-phases that yield lowest error
	double ratio_ = 0.0;
	int res = -1;
	{
		double least_error = 2;
		double pos = 0;
		for ( int r = 1; r <= max_res; r++ )
		{
			pos += new_factor;
			double nearest = floor( pos + 0.5 );
			double error = fabs( pos - nearest );
			if ( error < least_error )
			{
				res = r;
				ratio_ = nearest / res;
				least_error = error;
			}
		}
	}
	RETURN_ERR( Resampler::set_rate_( ratio_ ) );
	
	// how much of input is used for each output sample
	int const step = stereo * (int) floor( ratio_ );
	double fraction = fmod( ratio_, 1.0 );
	
	double const filter = (ratio_ < 1.0) ? 1.0 : 1.0 / ratio_;
	double pos = 0.0;
	//int input_per_cycle = 0;
	sample_t* out = impulses;
	for ( int n = res; --n >= 0; )
	{
		gen_sinc( rolloff, int (width_ * filter + 1) & ~1, pos, filter,
				double (0x7FFF * gain * filter), (int) width_, out );
		out += width_;
		
		int cur_step = step;
		pos += fraction;
		if ( pos >= 0.9999999 )
		{
			pos -= 1.0;
			cur_step += stereo;
		}
		
		*out++ = (cur_step - width_ * 2 + 4) * sizeof (sample_t);
		*out++ = 4 * sizeof (sample_t);
		//input_per_cycle += cur_step;
	}
	// last offset moves back to beginning of impulses
	out [-1] -= (char*) out - (char*) impulses;
	
	imp = impulses;
	
	return blargg_ok;
}
コード例 #2
0
ファイル: Fir_Resampler.cpp プロジェクト: Blzut3/gzdoom
double Fir_Resampler_::time_ratio( double new_factor, double rolloff, double gain )
{
	ratio_ = new_factor;
	
	double fstep = 0.0;
	{
		double least_error = 2;
		double pos = 0;
		res = -1;
		for ( int r = 1; r <= max_res; r++ )
		{
			pos += ratio_;
			double nearest = floor( pos + 0.5 );
			double error = fabs( pos - nearest );
			if ( error < least_error )
			{
				res = r;
				fstep = nearest / res;
				least_error = error;
			}
		}
	}
	
	skip_bits = 0;
	
	step = stereo * (int) floor( fstep );
	
	ratio_ = fstep;
	fstep = fmod( fstep, 1.0 );
	
	double filter = (ratio_ < 1.0) ? 1.0 : 1.0 / ratio_;
	double pos = 0.0;
	input_per_cycle = 0;
	for ( int i = 0; i < res; i++ )
	{
		gen_sinc( rolloff, int (width_ * filter + 1) & ~1, pos, filter,
				double (0x7FFF * gain * filter),
				(int) width_, impulses + i * width_ );
		
		pos += fstep;
		input_per_cycle += step;
		if ( pos >= 0.9999999 )
		{
			pos -= 1.0;
			skip_bits |= 1 << i;
			input_per_cycle++;
		}
	}
	
	clear();
	
	return ratio_;
}