diff src/apu/Blip_Buffer.cpp @ 1:f9f4f1b99eed

importing src directory
author Robert McIntyre <rlm@mit.edu>
date Sat, 03 Mar 2012 10:31:27 -0600
parents
children b05d00f19d80
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/apu/Blip_Buffer.cpp	Sat Mar 03 10:31:27 2012 -0600
     1.3 @@ -0,0 +1,465 @@
     1.4 +// Blip_Buffer 0.4.1. http://www.slack.net/~ant/
     1.5 +
     1.6 +#include "Blip_Buffer.h"
     1.7 +
     1.8 +#include <assert.h>
     1.9 +#include <limits.h>
    1.10 +#include <string.h>
    1.11 +#include <stdlib.h>
    1.12 +#include <math.h>
    1.13 +
    1.14 +/* Copyright (C) 2003-2007 Shay Green. This module is free software; you
    1.15 +can redistribute it and/or modify it under the terms of the GNU Lesser
    1.16 +General Public License as published by the Free Software Foundation; either
    1.17 +version 2.1 of the License, or (at your option) any later version. This
    1.18 +module is distributed in the hope that it will be useful, but WITHOUT ANY
    1.19 +WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    1.20 +FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
    1.21 +details. You should have received a copy of the GNU Lesser General Public
    1.22 +License along with this module; if not, write to the Free Software Foundation,
    1.23 +Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */
    1.24 +
    1.25 +// TODO: use scoped for variables in treble_eq()
    1.26 +
    1.27 +#ifdef BLARGG_ENABLE_OPTIMIZER
    1.28 +	#include BLARGG_ENABLE_OPTIMIZER
    1.29 +#endif
    1.30 +
    1.31 +int const silent_buf_size = 1; // size used for Silent_Blip_Buffer
    1.32 +
    1.33 +Blip_Buffer::Blip_Buffer()
    1.34 +{
    1.35 +	factor_       = LONG_MAX;
    1.36 +	buffer_       = 0;
    1.37 +	buffer_size_  = 0;
    1.38 +	sample_rate_  = 0;
    1.39 +	bass_shift_   = 0;
    1.40 +	clock_rate_   = 0;
    1.41 +	bass_freq_    = 16;
    1.42 +	length_       = 0;
    1.43 +
    1.44 +	// assumptions code makes about implementation-defined features
    1.45 +	#ifndef NDEBUG
    1.46 +		// right shift of negative value preserves sign
    1.47 +		buf_t_ i = -0x7FFFFFFE;
    1.48 +		assert( (i >> 1) == -0x3FFFFFFF );
    1.49 +
    1.50 +		// casting to short truncates to 16 bits and sign-extends
    1.51 +		i = 0x18000;
    1.52 +		assert( (short) i == -0x8000 );
    1.53 +	#endif
    1.54 +
    1.55 +	clear();
    1.56 +}
    1.57 +
    1.58 +Blip_Buffer::~Blip_Buffer()
    1.59 +{
    1.60 +	if ( buffer_size_ != silent_buf_size )
    1.61 +		free( buffer_ );
    1.62 +}
    1.63 +
    1.64 +Silent_Blip_Buffer::Silent_Blip_Buffer()
    1.65 +{
    1.66 +	factor_      = 0;
    1.67 +	buffer_      = buf;
    1.68 +	buffer_size_ = silent_buf_size;
    1.69 +	clear();
    1.70 +}
    1.71 +
    1.72 +void Blip_Buffer::clear( int entire_buffer )
    1.73 +{
    1.74 +	offset_       = 0;
    1.75 +	reader_accum_ = 0;
    1.76 +	modified_     = 0;
    1.77 +	if ( buffer_ )
    1.78 +	{
    1.79 +		long count = (entire_buffer ? buffer_size_ : samples_avail());
    1.80 +		memset( buffer_, 0, (count + blip_buffer_extra_) * sizeof (buf_t_) );
    1.81 +	}
    1.82 +}
    1.83 +
    1.84 +Blip_Buffer::blargg_err_t Blip_Buffer::set_sample_rate( long new_rate, int msec )
    1.85 +{
    1.86 +	if ( buffer_size_ == silent_buf_size )
    1.87 +	{
    1.88 +		assert( 0 );
    1.89 +		return "Internal (tried to resize Silent_Blip_Buffer)";
    1.90 +	}
    1.91 +
    1.92 +	// start with maximum length that resampled time can represent
    1.93 +	long new_size = (ULONG_MAX >> BLIP_BUFFER_ACCURACY) - blip_buffer_extra_ - 64;
    1.94 +	if ( msec != blip_max_length )
    1.95 +	{
    1.96 +		long s = (new_rate * (msec + 1) + 999) / 1000;
    1.97 +		if ( s < new_size )
    1.98 +			new_size = s;
    1.99 +		else
   1.100 +			assert( 0 ); // fails if requested buffer length exceeds limit
   1.101 +	}
   1.102 +
   1.103 +	if ( buffer_size_ != new_size )
   1.104 +	{
   1.105 +		void* p = realloc( buffer_, (new_size + blip_buffer_extra_) * sizeof *buffer_ );
   1.106 +		if ( !p )
   1.107 +			return "Out of memory";
   1.108 +		buffer_ = (buf_t_*) p;
   1.109 +	}
   1.110 +
   1.111 +	buffer_size_ = new_size;
   1.112 +	assert( buffer_size_ != silent_buf_size ); // size should never happen to match this
   1.113 +
   1.114 +	// update things based on the sample rate
   1.115 +	sample_rate_ = new_rate;
   1.116 +	length_ = new_size * 1000 / new_rate - 1;
   1.117 +	if ( msec )
   1.118 +		assert( length_ == msec ); // ensure length is same as that passed in
   1.119 +
   1.120 +	// update these since they depend on sample rate
   1.121 +	if ( clock_rate_ )
   1.122 +		clock_rate( clock_rate_ );
   1.123 +	bass_freq( bass_freq_ );
   1.124 +
   1.125 +	clear();
   1.126 +
   1.127 +	return 0; // success
   1.128 +}
   1.129 +
   1.130 +blip_resampled_time_t Blip_Buffer::clock_rate_factor( long rate ) const
   1.131 +{
   1.132 +	double ratio = (double) sample_rate_ / rate;
   1.133 +	blip_long factor = (blip_long) floor( ratio * (1L << BLIP_BUFFER_ACCURACY) + 0.5 );
   1.134 +	assert( factor > 0 || !sample_rate_ ); // fails if clock/output ratio is too large
   1.135 +	return (blip_resampled_time_t) factor;
   1.136 +}
   1.137 +
   1.138 +void Blip_Buffer::bass_freq( int freq )
   1.139 +{
   1.140 +	bass_freq_ = freq;
   1.141 +	int shift = 31;
   1.142 +	if ( freq > 0 )
   1.143 +	{
   1.144 +		shift = 13;
   1.145 +		long f = (freq << 16) / sample_rate_;
   1.146 +		while ( (f >>= 1) && --shift ) { }
   1.147 +	}
   1.148 +	bass_shift_ = shift;
   1.149 +}
   1.150 +
   1.151 +void Blip_Buffer::end_frame( blip_time_t t )
   1.152 +{
   1.153 +	offset_ += t * factor_;
   1.154 +	assert( samples_avail() <= (long) buffer_size_ ); // fails if time is past end of buffer
   1.155 +}
   1.156 +
   1.157 +long Blip_Buffer::count_samples( blip_time_t t ) const
   1.158 +{
   1.159 +	blip_resampled_time_t last_sample  = resampled_time( t ) >> BLIP_BUFFER_ACCURACY;
   1.160 +	blip_resampled_time_t first_sample = offset_ >> BLIP_BUFFER_ACCURACY;
   1.161 +	return long (last_sample - first_sample);
   1.162 +}
   1.163 +
   1.164 +blip_time_t Blip_Buffer::count_clocks( long count ) const
   1.165 +{
   1.166 +	if ( !factor_ )
   1.167 +	{
   1.168 +		assert( 0 ); // sample rate and clock rates must be set first
   1.169 +		return 0;
   1.170 +	}
   1.171 +
   1.172 +	if ( count > buffer_size_ )
   1.173 +		count = buffer_size_;
   1.174 +	blip_resampled_time_t time = (blip_resampled_time_t) count << BLIP_BUFFER_ACCURACY;
   1.175 +	return (blip_time_t) ((time - offset_ + factor_ - 1) / factor_);
   1.176 +}
   1.177 +
   1.178 +void Blip_Buffer::remove_samples( long count )
   1.179 +{
   1.180 +	if ( count )
   1.181 +	{
   1.182 +		remove_silence( count );
   1.183 +
   1.184 +		// copy remaining samples to beginning and clear old samples
   1.185 +		long remain = samples_avail() + blip_buffer_extra_;
   1.186 +		memmove( buffer_, buffer_ + count, remain * sizeof *buffer_ );
   1.187 +		memset( buffer_ + remain, 0, count * sizeof *buffer_ );
   1.188 +	}
   1.189 +}
   1.190 +
   1.191 +// Blip_Synth_
   1.192 +
   1.193 +Blip_Synth_Fast_::Blip_Synth_Fast_()
   1.194 +{
   1.195 +	buf          = 0;
   1.196 +	last_amp     = 0;
   1.197 +	delta_factor = 0;
   1.198 +}
   1.199 +
   1.200 +void Blip_Synth_Fast_::volume_unit( double new_unit )
   1.201 +{
   1.202 +	delta_factor = int (new_unit * (1L << blip_sample_bits) + 0.5);
   1.203 +}
   1.204 +
   1.205 +#if !BLIP_BUFFER_FAST
   1.206 +
   1.207 +Blip_Synth_::Blip_Synth_( short* p, int w ) :
   1.208 +	impulses( p ),
   1.209 +	width( w )
   1.210 +{
   1.211 +	volume_unit_ = 0.0;
   1.212 +	kernel_unit  = 0;
   1.213 +	buf          = 0;
   1.214 +	last_amp     = 0;
   1.215 +	delta_factor = 0;
   1.216 +}
   1.217 +
   1.218 +#undef PI
   1.219 +#define PI 3.1415926535897932384626433832795029
   1.220 +
   1.221 +static void gen_sinc( float* out, int count, double oversample, double treble, double cutoff )
   1.222 +{
   1.223 +	if ( cutoff >= 0.999 )
   1.224 +		cutoff = 0.999;
   1.225 +
   1.226 +	if ( treble < -300.0 )
   1.227 +		treble = -300.0;
   1.228 +	if ( treble > 5.0 )
   1.229 +		treble = 5.0;
   1.230 +
   1.231 +	double const maxh = 4096.0;
   1.232 +	double const rolloff = pow( 10.0, 1.0 / (maxh * 20.0) * treble / (1.0 - cutoff) );
   1.233 +	double const pow_a_n = pow( rolloff, maxh - maxh * cutoff );
   1.234 +	double const to_angle = PI / 2 / maxh / oversample;
   1.235 +	for ( int i = 0; i < count; i++ )
   1.236 +	{
   1.237 +		double angle = ((i - count) * 2 + 1) * to_angle;
   1.238 +		double c = rolloff * cos( (maxh - 1.0) * angle ) - cos( maxh * angle );
   1.239 +		double cos_nc_angle = cos( maxh * cutoff * angle );
   1.240 +		double cos_nc1_angle = cos( (maxh * cutoff - 1.0) * angle );
   1.241 +		double cos_angle = cos( angle );
   1.242 +
   1.243 +		c = c * pow_a_n - rolloff * cos_nc1_angle + cos_nc_angle;
   1.244 +		double d = 1.0 + rolloff * (rolloff - cos_angle - cos_angle);
   1.245 +		double b = 2.0 - cos_angle - cos_angle;
   1.246 +		double a = 1.0 - cos_angle - cos_nc_angle + cos_nc1_angle;
   1.247 +
   1.248 +		out [i] = (float) ((a * d + c * b) / (b * d)); // a / b + c / d
   1.249 +	}
   1.250 +}
   1.251 +
   1.252 +void blip_eq_t::generate( float* out, int count ) const
   1.253 +{
   1.254 +	// lower cutoff freq for narrow kernels with their wider transition band
   1.255 +	// (8 points->1.49, 16 points->1.15)
   1.256 +	double oversample = blip_res * 2.25 / count + 0.85;
   1.257 +	double half_rate = sample_rate * 0.5;
   1.258 +	if ( cutoff_freq )
   1.259 +		oversample = half_rate / cutoff_freq;
   1.260 +	double cutoff = rolloff_freq * oversample / half_rate;
   1.261 +
   1.262 +	gen_sinc( out, count, blip_res * oversample, treble, cutoff );
   1.263 +
   1.264 +	// apply (half of) hamming window
   1.265 +	double to_fraction = PI / (count - 1);
   1.266 +	for ( int i = count; i--; )
   1.267 +		out [i] *= 0.54f - 0.46f * (float) cos( i * to_fraction );
   1.268 +}
   1.269 +
   1.270 +void Blip_Synth_::adjust_impulse()
   1.271 +{
   1.272 +	// sum pairs for each phase and add error correction to end of first half
   1.273 +	int const size = impulses_size();
   1.274 +	for ( int p = blip_res; p-- >= blip_res / 2; )
   1.275 +	{
   1.276 +		int p2 = blip_res - 2 - p;
   1.277 +		long error = kernel_unit;
   1.278 +		for ( int i = 1; i < size; i += blip_res )
   1.279 +		{
   1.280 +			error -= impulses [i + p ];
   1.281 +			error -= impulses [i + p2];
   1.282 +		}
   1.283 +		if ( p == p2 )
   1.284 +			error /= 2; // phase = 0.5 impulse uses same half for both sides
   1.285 +		impulses [size - blip_res + p] += (short) error;
   1.286 +		//printf( "error: %ld\n", error );
   1.287 +	}
   1.288 +
   1.289 +	//for ( int i = blip_res; i--; printf( "\n" ) )
   1.290 +	//  for ( int j = 0; j < width / 2; j++ )
   1.291 +	//      printf( "%5ld,", impulses [j * blip_res + i + 1] );
   1.292 +}
   1.293 +
   1.294 +void Blip_Synth_::treble_eq( blip_eq_t const& eq )
   1.295 +{
   1.296 +	float fimpulse [blip_res / 2 * (blip_widest_impulse_ - 1) + blip_res * 2];
   1.297 +
   1.298 +	int const half_size = blip_res / 2 * (width - 1);
   1.299 +	eq.generate( &fimpulse [blip_res], half_size );
   1.300 +
   1.301 +	int i;
   1.302 +
   1.303 +	// need mirror slightly past center for calculation
   1.304 +	for ( i = blip_res; i--; )
   1.305 +		fimpulse [blip_res + half_size + i] = fimpulse [blip_res + half_size - 1 - i];
   1.306 +
   1.307 +	// starts at 0
   1.308 +	for ( i = 0; i < blip_res; i++ )
   1.309 +		fimpulse [i] = 0.0f;
   1.310 +
   1.311 +	// find rescale factor
   1.312 +	double total = 0.0;
   1.313 +	for ( i = 0; i < half_size; i++ )
   1.314 +		total += fimpulse [blip_res + i];
   1.315 +
   1.316 +	//double const base_unit = 44800.0 - 128 * 18; // allows treble up to +0 dB
   1.317 +	//double const base_unit = 37888.0; // allows treble to +5 dB
   1.318 +	double const base_unit = 32768.0; // necessary for blip_unscaled to work
   1.319 +	double rescale = base_unit / 2 / total;
   1.320 +	kernel_unit = (long) base_unit;
   1.321 +
   1.322 +	// integrate, first difference, rescale, convert to int
   1.323 +	double sum = 0.0;
   1.324 +	double next = 0.0;
   1.325 +	int const size = this->impulses_size();
   1.326 +	for ( i = 0; i < size; i++ )
   1.327 +	{
   1.328 +		impulses [i] = (short) (int) floor( (next - sum) * rescale + 0.5 );
   1.329 +		sum += fimpulse [i];
   1.330 +		next += fimpulse [i + blip_res];
   1.331 +	}
   1.332 +	adjust_impulse();
   1.333 +
   1.334 +	// volume might require rescaling
   1.335 +	double vol = volume_unit_;
   1.336 +	if ( vol )
   1.337 +	{
   1.338 +		volume_unit_ = 0.0;
   1.339 +		volume_unit( vol );
   1.340 +	}
   1.341 +}
   1.342 +
   1.343 +void Blip_Synth_::volume_unit( double new_unit )
   1.344 +{
   1.345 +	if ( new_unit != volume_unit_ )
   1.346 +	{
   1.347 +		// use default eq if it hasn't been set yet
   1.348 +		if ( !kernel_unit )
   1.349 +			treble_eq( -8.0 );
   1.350 +
   1.351 +		volume_unit_ = new_unit;
   1.352 +		double factor = new_unit * (1L << blip_sample_bits) / kernel_unit;
   1.353 +
   1.354 +		if ( factor > 0.0 )
   1.355 +		{
   1.356 +			int shift = 0;
   1.357 +
   1.358 +			// if unit is really small, might need to attenuate kernel
   1.359 +			while ( factor < 2.0 )
   1.360 +			{
   1.361 +				shift++;
   1.362 +				factor *= 2.0;
   1.363 +			}
   1.364 +
   1.365 +			if ( shift )
   1.366 +			{
   1.367 +				kernel_unit >>= shift;
   1.368 +				assert( kernel_unit > 0 ); // fails if volume unit is too low
   1.369 +
   1.370 +				// keep values positive to avoid round-towards-zero of sign-preserving
   1.371 +				// right shift for negative values
   1.372 +				long offset = 0x8000 + (1 << (shift - 1));
   1.373 +				long offset2 = 0x8000 >> shift;
   1.374 +				for ( int i = impulses_size(); i--; )
   1.375 +					impulses [i] = (short) (int) (((impulses [i] + offset) >> shift) - offset2);
   1.376 +				adjust_impulse();
   1.377 +			}
   1.378 +		}
   1.379 +		delta_factor = (int) floor( factor + 0.5 );
   1.380 +		//printf( "delta_factor: %d, kernel_unit: %d\n", delta_factor, kernel_unit );
   1.381 +	}
   1.382 +}
   1.383 +#endif
   1.384 +
   1.385 +long Blip_Buffer::read_samples( blip_sample_t* out_, long max_samples, int stereo )
   1.386 +{
   1.387 +	long count = samples_avail();
   1.388 +	if ( count > max_samples )
   1.389 +		count = max_samples;
   1.390 +
   1.391 +	if ( count )
   1.392 +	{
   1.393 +		int const bass = BLIP_READER_BASS( *this );
   1.394 +		BLIP_READER_BEGIN( reader, *this );
   1.395 +		BLIP_READER_ADJ_( reader, count );
   1.396 +		blip_sample_t* BLIP_RESTRICT out = out_ + count;
   1.397 +		blip_long offset = (blip_long) -count;
   1.398 +
   1.399 +		if ( !stereo )
   1.400 +		{
   1.401 +			do
   1.402 +			{
   1.403 +				blip_long s = BLIP_READER_READ( reader );
   1.404 +				BLIP_READER_NEXT_IDX_( reader, bass, offset );
   1.405 +				BLIP_CLAMP( s, s );
   1.406 +				out [offset] = (blip_sample_t) s;
   1.407 +			}
   1.408 +			while ( ++offset );
   1.409 +		}
   1.410 +		else
   1.411 +		{
   1.412 +			do
   1.413 +			{
   1.414 +				blip_long s = BLIP_READER_READ( reader );
   1.415 +				BLIP_READER_NEXT_IDX_( reader, bass, offset );
   1.416 +				BLIP_CLAMP( s, s );
   1.417 +				out [offset * 2] = (blip_sample_t) s;
   1.418 +			}
   1.419 +			while ( ++offset );
   1.420 +		}
   1.421 +
   1.422 +		BLIP_READER_END( reader, *this );
   1.423 +
   1.424 +		remove_samples( count );
   1.425 +	}
   1.426 +	return count;
   1.427 +}
   1.428 +
   1.429 +void Blip_Buffer::mix_samples( blip_sample_t const* in, long count )
   1.430 +{
   1.431 +	if ( buffer_size_ == silent_buf_size )
   1.432 +	{
   1.433 +		assert( 0 );
   1.434 +		return;
   1.435 +	}
   1.436 +
   1.437 +	buf_t_* out = buffer_ + (offset_ >> BLIP_BUFFER_ACCURACY) + blip_widest_impulse_ / 2;
   1.438 +
   1.439 +	int const sample_shift = blip_sample_bits - 16;
   1.440 +	int prev = 0;
   1.441 +	while ( count-- )
   1.442 +	{
   1.443 +		blip_long s = (blip_long) *in++ << sample_shift;
   1.444 +		*out += s - prev;
   1.445 +		prev = s;
   1.446 +		++out;
   1.447 +	}
   1.448 +	*out -= prev;
   1.449 +}
   1.450 +
   1.451 +blip_ulong const subsample_mask = (1L << BLIP_BUFFER_ACCURACY) - 1;
   1.452 +
   1.453 +void Blip_Buffer::save_state( blip_buffer_state_t* out )
   1.454 +{
   1.455 +	assert( samples_avail() == 0 );
   1.456 +	out->offset_       = offset_;
   1.457 +	out->reader_accum_ = reader_accum_;
   1.458 +	memcpy( out->buf, &buffer_ [offset_ >> BLIP_BUFFER_ACCURACY], sizeof out->buf );
   1.459 +}
   1.460 +
   1.461 +void Blip_Buffer::load_state( blip_buffer_state_t const& in )
   1.462 +{
   1.463 +	clear( false );
   1.464 +
   1.465 +	offset_       = in.offset_;
   1.466 +	reader_accum_ = in.reader_accum_;
   1.467 +	memcpy( buffer_, in.buf, sizeof in.buf );
   1.468 +}