diff src/apu/Blip_Buffer.cpp @ 3:b05d00f19d80

fix some formatting
author Robert McIntyre <rlm@mit.edu>
date Sat, 03 Mar 2012 10:33:11 -0600
parents f9f4f1b99eed
children
line wrap: on
line diff
     1.1 --- a/src/apu/Blip_Buffer.cpp	Sat Mar 03 10:31:47 2012 -0600
     1.2 +++ b/src/apu/Blip_Buffer.cpp	Sat Mar 03 10:33:11 2012 -0600
     1.3 @@ -9,207 +9,207 @@
     1.4  #include <math.h>
     1.5  
     1.6  /* Copyright (C) 2003-2007 Shay Green. This module is free software; you
     1.7 -can redistribute it and/or modify it under the terms of the GNU Lesser
     1.8 -General Public License as published by the Free Software Foundation; either
     1.9 -version 2.1 of the License, or (at your option) any later version. This
    1.10 -module is distributed in the hope that it will be useful, but WITHOUT ANY
    1.11 -WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    1.12 -FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
    1.13 -details. You should have received a copy of the GNU Lesser General Public
    1.14 -License along with this module; if not, write to the Free Software Foundation,
    1.15 -Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */
    1.16 +   can redistribute it and/or modify it under the terms of the GNU Lesser
    1.17 +   General Public License as published by the Free Software Foundation; either
    1.18 +   version 2.1 of the License, or (at your option) any later version. This
    1.19 +   module is distributed in the hope that it will be useful, but WITHOUT ANY
    1.20 +   WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    1.21 +   FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
    1.22 +   details. You should have received a copy of the GNU Lesser General Public
    1.23 +   License along with this module; if not, write to the Free Software Foundation,
    1.24 +   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */
    1.25  
    1.26  // TODO: use scoped for variables in treble_eq()
    1.27  
    1.28  #ifdef BLARGG_ENABLE_OPTIMIZER
    1.29 -	#include BLARGG_ENABLE_OPTIMIZER
    1.30 +#include BLARGG_ENABLE_OPTIMIZER
    1.31  #endif
    1.32  
    1.33  int const silent_buf_size = 1; // size used for Silent_Blip_Buffer
    1.34  
    1.35  Blip_Buffer::Blip_Buffer()
    1.36  {
    1.37 -	factor_       = LONG_MAX;
    1.38 -	buffer_       = 0;
    1.39 -	buffer_size_  = 0;
    1.40 -	sample_rate_  = 0;
    1.41 -	bass_shift_   = 0;
    1.42 -	clock_rate_   = 0;
    1.43 -	bass_freq_    = 16;
    1.44 -	length_       = 0;
    1.45 +  factor_       = LONG_MAX;
    1.46 +  buffer_       = 0;
    1.47 +  buffer_size_  = 0;
    1.48 +  sample_rate_  = 0;
    1.49 +  bass_shift_   = 0;
    1.50 +  clock_rate_   = 0;
    1.51 +  bass_freq_    = 16;
    1.52 +  length_       = 0;
    1.53  
    1.54 -	// assumptions code makes about implementation-defined features
    1.55 -	#ifndef NDEBUG
    1.56 -		// right shift of negative value preserves sign
    1.57 -		buf_t_ i = -0x7FFFFFFE;
    1.58 -		assert( (i >> 1) == -0x3FFFFFFF );
    1.59 +  // assumptions code makes about implementation-defined features
    1.60 +#ifndef NDEBUG
    1.61 +  // right shift of negative value preserves sign
    1.62 +  buf_t_ i = -0x7FFFFFFE;
    1.63 +  assert( (i >> 1) == -0x3FFFFFFF );
    1.64  
    1.65 -		// casting to short truncates to 16 bits and sign-extends
    1.66 -		i = 0x18000;
    1.67 -		assert( (short) i == -0x8000 );
    1.68 -	#endif
    1.69 +  // casting to short truncates to 16 bits and sign-extends
    1.70 +  i = 0x18000;
    1.71 +  assert( (short) i == -0x8000 );
    1.72 +#endif
    1.73  
    1.74 -	clear();
    1.75 +  Clear();
    1.76  }
    1.77  
    1.78  Blip_Buffer::~Blip_Buffer()
    1.79  {
    1.80 -	if ( buffer_size_ != silent_buf_size )
    1.81 -		free( buffer_ );
    1.82 +  if ( buffer_size_ != silent_buf_size )
    1.83 +    free( buffer_ );
    1.84  }
    1.85  
    1.86  Silent_Blip_Buffer::Silent_Blip_Buffer()
    1.87  {
    1.88 -	factor_      = 0;
    1.89 -	buffer_      = buf;
    1.90 -	buffer_size_ = silent_buf_size;
    1.91 -	clear();
    1.92 +  factor_      = 0;
    1.93 +  buffer_      = buf;
    1.94 +  buffer_size_ = silent_buf_size;
    1.95 +  clear();
    1.96  }
    1.97  
    1.98  void Blip_Buffer::clear( int entire_buffer )
    1.99  {
   1.100 -	offset_       = 0;
   1.101 -	reader_accum_ = 0;
   1.102 -	modified_     = 0;
   1.103 -	if ( buffer_ )
   1.104 -	{
   1.105 -		long count = (entire_buffer ? buffer_size_ : samples_avail());
   1.106 -		memset( buffer_, 0, (count + blip_buffer_extra_) * sizeof (buf_t_) );
   1.107 -	}
   1.108 +  offset_       = 0;
   1.109 +  reader_accum_ = 0;
   1.110 +  modified_     = 0;
   1.111 +  if ( buffer_ )
   1.112 +    {
   1.113 +      long count = (entire_buffer ? buffer_size_ : samples_avail());
   1.114 +      memset( buffer_, 0, (count + blip_buffer_extra_) * sizeof (buf_t_) );
   1.115 +    }
   1.116  }
   1.117  
   1.118  Blip_Buffer::blargg_err_t Blip_Buffer::set_sample_rate( long new_rate, int msec )
   1.119  {
   1.120 -	if ( buffer_size_ == silent_buf_size )
   1.121 -	{
   1.122 -		assert( 0 );
   1.123 -		return "Internal (tried to resize Silent_Blip_Buffer)";
   1.124 -	}
   1.125 +  if ( buffer_size_ == silent_buf_size )
   1.126 +    {
   1.127 +      assert( 0 );
   1.128 +      return "Internal (tried to resize Silent_Blip_Buffer)";
   1.129 +    }
   1.130  
   1.131 -	// start with maximum length that resampled time can represent
   1.132 -	long new_size = (ULONG_MAX >> BLIP_BUFFER_ACCURACY) - blip_buffer_extra_ - 64;
   1.133 -	if ( msec != blip_max_length )
   1.134 -	{
   1.135 -		long s = (new_rate * (msec + 1) + 999) / 1000;
   1.136 -		if ( s < new_size )
   1.137 -			new_size = s;
   1.138 -		else
   1.139 -			assert( 0 ); // fails if requested buffer length exceeds limit
   1.140 -	}
   1.141 +  // start with maximum length that resampled time can represent
   1.142 +  long new_size = (ULONG_MAX >> BLIP_BUFFER_ACCURACY) - blip_buffer_extra_ - 64;
   1.143 +  if ( msec != blip_max_length )
   1.144 +    {
   1.145 +      long s = (new_rate * (msec + 1) + 999) / 1000;
   1.146 +      if ( s < new_size )
   1.147 +	new_size = s;
   1.148 +      else
   1.149 +	assert( 0 ); // fails if requested buffer length exceeds limit
   1.150 +    }
   1.151  
   1.152 -	if ( buffer_size_ != new_size )
   1.153 -	{
   1.154 -		void* p = realloc( buffer_, (new_size + blip_buffer_extra_) * sizeof *buffer_ );
   1.155 -		if ( !p )
   1.156 -			return "Out of memory";
   1.157 -		buffer_ = (buf_t_*) p;
   1.158 -	}
   1.159 +  if ( buffer_size_ != new_size )
   1.160 +    {
   1.161 +      void* p = realloc( buffer_, (new_size + blip_buffer_extra_) * sizeof *buffer_ );
   1.162 +      if ( !p )
   1.163 +	return "Out of memory";
   1.164 +      buffer_ = (buf_t_*) p;
   1.165 +    }
   1.166  
   1.167 -	buffer_size_ = new_size;
   1.168 -	assert( buffer_size_ != silent_buf_size ); // size should never happen to match this
   1.169 +  buffer_size_ = new_size;
   1.170 +  assert( buffer_size_ != silent_buf_size ); // size should never happen to match this
   1.171  
   1.172 -	// update things based on the sample rate
   1.173 -	sample_rate_ = new_rate;
   1.174 -	length_ = new_size * 1000 / new_rate - 1;
   1.175 -	if ( msec )
   1.176 -		assert( length_ == msec ); // ensure length is same as that passed in
   1.177 +  // update things based on the sample rate
   1.178 +  sample_rate_ = new_rate;
   1.179 +  length_ = new_size * 1000 / new_rate - 1;
   1.180 +  if ( msec )
   1.181 +    assert( length_ == msec ); // ensure length is same as that passed in
   1.182  
   1.183 -	// update these since they depend on sample rate
   1.184 -	if ( clock_rate_ )
   1.185 -		clock_rate( clock_rate_ );
   1.186 -	bass_freq( bass_freq_ );
   1.187 +  // update these since they depend on sample rate
   1.188 +  if ( clock_rate_ )
   1.189 +    clock_rate( clock_rate_ );
   1.190 +  bass_freq( bass_freq_ );
   1.191  
   1.192 -	clear();
   1.193 +  clear();
   1.194  
   1.195 -	return 0; // success
   1.196 +  return 0; // success
   1.197  }
   1.198  
   1.199  blip_resampled_time_t Blip_Buffer::clock_rate_factor( long rate ) const
   1.200  {
   1.201 -	double ratio = (double) sample_rate_ / rate;
   1.202 -	blip_long factor = (blip_long) floor( ratio * (1L << BLIP_BUFFER_ACCURACY) + 0.5 );
   1.203 -	assert( factor > 0 || !sample_rate_ ); // fails if clock/output ratio is too large
   1.204 -	return (blip_resampled_time_t) factor;
   1.205 +  double ratio = (double) sample_rate_ / rate;
   1.206 +  blip_long factor = (blip_long) floor( ratio * (1L << BLIP_BUFFER_ACCURACY) + 0.5 );
   1.207 +  assert( factor > 0 || !sample_rate_ ); // fails if clock/output ratio is too large
   1.208 +  return (blip_resampled_time_t) factor;
   1.209  }
   1.210  
   1.211  void Blip_Buffer::bass_freq( int freq )
   1.212  {
   1.213 -	bass_freq_ = freq;
   1.214 -	int shift = 31;
   1.215 -	if ( freq > 0 )
   1.216 -	{
   1.217 -		shift = 13;
   1.218 -		long f = (freq << 16) / sample_rate_;
   1.219 -		while ( (f >>= 1) && --shift ) { }
   1.220 -	}
   1.221 -	bass_shift_ = shift;
   1.222 +  bass_freq_ = freq;
   1.223 +  int shift = 31;
   1.224 +  if ( freq > 0 )
   1.225 +    {
   1.226 +      shift = 13;
   1.227 +      long f = (freq << 16) / sample_rate_;
   1.228 +      while ( (f >>= 1) && --shift ) { }
   1.229 +    }
   1.230 +  bass_shift_ = shift;
   1.231  }
   1.232  
   1.233  void Blip_Buffer::end_frame( blip_time_t t )
   1.234  {
   1.235 -	offset_ += t * factor_;
   1.236 -	assert( samples_avail() <= (long) buffer_size_ ); // fails if time is past end of buffer
   1.237 +  offset_ += t * factor_;
   1.238 +  assert( samples_avail() <= (long) buffer_size_ ); // fails if time is past end of buffer
   1.239  }
   1.240  
   1.241  long Blip_Buffer::count_samples( blip_time_t t ) const
   1.242  {
   1.243 -	blip_resampled_time_t last_sample  = resampled_time( t ) >> BLIP_BUFFER_ACCURACY;
   1.244 -	blip_resampled_time_t first_sample = offset_ >> BLIP_BUFFER_ACCURACY;
   1.245 -	return long (last_sample - first_sample);
   1.246 +  blip_resampled_time_t last_sample  = resampled_time( t ) >> BLIP_BUFFER_ACCURACY;
   1.247 +  blip_resampled_time_t first_sample = offset_ >> BLIP_BUFFER_ACCURACY;
   1.248 +  return long (last_sample - first_sample);
   1.249  }
   1.250  
   1.251  blip_time_t Blip_Buffer::count_clocks( long count ) const
   1.252  {
   1.253 -	if ( !factor_ )
   1.254 -	{
   1.255 -		assert( 0 ); // sample rate and clock rates must be set first
   1.256 -		return 0;
   1.257 -	}
   1.258 +  if ( !factor_ )
   1.259 +    {
   1.260 +      assert( 0 ); // sample rate and clock rates must be set first
   1.261 +      return 0;
   1.262 +    }
   1.263  
   1.264 -	if ( count > buffer_size_ )
   1.265 -		count = buffer_size_;
   1.266 -	blip_resampled_time_t time = (blip_resampled_time_t) count << BLIP_BUFFER_ACCURACY;
   1.267 -	return (blip_time_t) ((time - offset_ + factor_ - 1) / factor_);
   1.268 +  if ( count > buffer_size_ )
   1.269 +    count = buffer_size_;
   1.270 +  blip_resampled_time_t time = (blip_resampled_time_t) count << BLIP_BUFFER_ACCURACY;
   1.271 +  return (blip_time_t) ((time - offset_ + factor_ - 1) / factor_);
   1.272  }
   1.273  
   1.274  void Blip_Buffer::remove_samples( long count )
   1.275  {
   1.276 -	if ( count )
   1.277 -	{
   1.278 -		remove_silence( count );
   1.279 +  if ( count )
   1.280 +    {
   1.281 +      remove_silence( count );
   1.282  
   1.283 -		// copy remaining samples to beginning and clear old samples
   1.284 -		long remain = samples_avail() + blip_buffer_extra_;
   1.285 -		memmove( buffer_, buffer_ + count, remain * sizeof *buffer_ );
   1.286 -		memset( buffer_ + remain, 0, count * sizeof *buffer_ );
   1.287 -	}
   1.288 +      // copy remaining samples to beginning and clear old samples
   1.289 +      long remain = samples_avail() + blip_buffer_extra_;
   1.290 +      memmove( buffer_, buffer_ + count, remain * sizeof *buffer_ );
   1.291 +      memset( buffer_ + remain, 0, count * sizeof *buffer_ );
   1.292 +    }
   1.293  }
   1.294  
   1.295  // Blip_Synth_
   1.296  
   1.297  Blip_Synth_Fast_::Blip_Synth_Fast_()
   1.298  {
   1.299 -	buf          = 0;
   1.300 -	last_amp     = 0;
   1.301 -	delta_factor = 0;
   1.302 +  buf          = 0;
   1.303 +  last_amp     = 0;
   1.304 +  delta_factor = 0;
   1.305  }
   1.306  
   1.307  void Blip_Synth_Fast_::volume_unit( double new_unit )
   1.308  {
   1.309 -	delta_factor = int (new_unit * (1L << blip_sample_bits) + 0.5);
   1.310 +  delta_factor = int (new_unit * (1L << blip_sample_bits) + 0.5);
   1.311  }
   1.312  
   1.313  #if !BLIP_BUFFER_FAST
   1.314  
   1.315  Blip_Synth_::Blip_Synth_( short* p, int w ) :
   1.316 -	impulses( p ),
   1.317 -	width( w )
   1.318 +  impulses( p ),
   1.319 +  width( w )
   1.320  {
   1.321 -	volume_unit_ = 0.0;
   1.322 -	kernel_unit  = 0;
   1.323 -	buf          = 0;
   1.324 -	last_amp     = 0;
   1.325 -	delta_factor = 0;
   1.326 +  volume_unit_ = 0.0;
   1.327 +  kernel_unit  = 0;
   1.328 +  buf          = 0;
   1.329 +  last_amp     = 0;
   1.330 +  delta_factor = 0;
   1.331  }
   1.332  
   1.333  #undef PI
   1.334 @@ -217,249 +217,249 @@
   1.335  
   1.336  static void gen_sinc( float* out, int count, double oversample, double treble, double cutoff )
   1.337  {
   1.338 -	if ( cutoff >= 0.999 )
   1.339 -		cutoff = 0.999;
   1.340 +  if ( cutoff >= 0.999 )
   1.341 +    cutoff = 0.999;
   1.342  
   1.343 -	if ( treble < -300.0 )
   1.344 -		treble = -300.0;
   1.345 -	if ( treble > 5.0 )
   1.346 -		treble = 5.0;
   1.347 +  if ( treble < -300.0 )
   1.348 +    treble = -300.0;
   1.349 +  if ( treble > 5.0 )
   1.350 +    treble = 5.0;
   1.351  
   1.352 -	double const maxh = 4096.0;
   1.353 -	double const rolloff = pow( 10.0, 1.0 / (maxh * 20.0) * treble / (1.0 - cutoff) );
   1.354 -	double const pow_a_n = pow( rolloff, maxh - maxh * cutoff );
   1.355 -	double const to_angle = PI / 2 / maxh / oversample;
   1.356 -	for ( int i = 0; i < count; i++ )
   1.357 -	{
   1.358 -		double angle = ((i - count) * 2 + 1) * to_angle;
   1.359 -		double c = rolloff * cos( (maxh - 1.0) * angle ) - cos( maxh * angle );
   1.360 -		double cos_nc_angle = cos( maxh * cutoff * angle );
   1.361 -		double cos_nc1_angle = cos( (maxh * cutoff - 1.0) * angle );
   1.362 -		double cos_angle = cos( angle );
   1.363 +  double const maxh = 4096.0;
   1.364 +  double const rolloff = pow( 10.0, 1.0 / (maxh * 20.0) * treble / (1.0 - cutoff) );
   1.365 +  double const pow_a_n = pow( rolloff, maxh - maxh * cutoff );
   1.366 +  double const to_angle = PI / 2 / maxh / oversample;
   1.367 +  for ( int i = 0; i < count; i++ )
   1.368 +    {
   1.369 +      double angle = ((i - count) * 2 + 1) * to_angle;
   1.370 +      double c = rolloff * cos( (maxh - 1.0) * angle ) - cos( maxh * angle );
   1.371 +      double cos_nc_angle = cos( maxh * cutoff * angle );
   1.372 +      double cos_nc1_angle = cos( (maxh * cutoff - 1.0) * angle );
   1.373 +      double cos_angle = cos( angle );
   1.374  
   1.375 -		c = c * pow_a_n - rolloff * cos_nc1_angle + cos_nc_angle;
   1.376 -		double d = 1.0 + rolloff * (rolloff - cos_angle - cos_angle);
   1.377 -		double b = 2.0 - cos_angle - cos_angle;
   1.378 -		double a = 1.0 - cos_angle - cos_nc_angle + cos_nc1_angle;
   1.379 +      c = c * pow_a_n - rolloff * cos_nc1_angle + cos_nc_angle;
   1.380 +      double d = 1.0 + rolloff * (rolloff - cos_angle - cos_angle);
   1.381 +      double b = 2.0 - cos_angle - cos_angle;
   1.382 +      double a = 1.0 - cos_angle - cos_nc_angle + cos_nc1_angle;
   1.383  
   1.384 -		out [i] = (float) ((a * d + c * b) / (b * d)); // a / b + c / d
   1.385 -	}
   1.386 +      out [i] = (float) ((a * d + c * b) / (b * d)); // a / b + c / d
   1.387 +    }
   1.388  }
   1.389  
   1.390  void blip_eq_t::generate( float* out, int count ) const
   1.391  {
   1.392 -	// lower cutoff freq for narrow kernels with their wider transition band
   1.393 -	// (8 points->1.49, 16 points->1.15)
   1.394 -	double oversample = blip_res * 2.25 / count + 0.85;
   1.395 -	double half_rate = sample_rate * 0.5;
   1.396 -	if ( cutoff_freq )
   1.397 -		oversample = half_rate / cutoff_freq;
   1.398 -	double cutoff = rolloff_freq * oversample / half_rate;
   1.399 +  // lower cutoff freq for narrow kernels with their wider transition band
   1.400 +  // (8 points->1.49, 16 points->1.15)
   1.401 +  double oversample = blip_res * 2.25 / count + 0.85;
   1.402 +  double half_rate = sample_rate * 0.5;
   1.403 +  if ( cutoff_freq )
   1.404 +    oversample = half_rate / cutoff_freq;
   1.405 +  double cutoff = rolloff_freq * oversample / half_rate;
   1.406  
   1.407 -	gen_sinc( out, count, blip_res * oversample, treble, cutoff );
   1.408 +  gen_sinc( out, count, blip_res * oversample, treble, cutoff );
   1.409  
   1.410 -	// apply (half of) hamming window
   1.411 -	double to_fraction = PI / (count - 1);
   1.412 -	for ( int i = count; i--; )
   1.413 -		out [i] *= 0.54f - 0.46f * (float) cos( i * to_fraction );
   1.414 +  // apply (half of) hamming window
   1.415 +  double to_fraction = PI / (count - 1);
   1.416 +  for ( int i = count; i--; )
   1.417 +    out [i] *= 0.54f - 0.46f * (float) cos( i * to_fraction );
   1.418  }
   1.419  
   1.420  void Blip_Synth_::adjust_impulse()
   1.421  {
   1.422 -	// sum pairs for each phase and add error correction to end of first half
   1.423 -	int const size = impulses_size();
   1.424 -	for ( int p = blip_res; p-- >= blip_res / 2; )
   1.425 +  // sum pairs for each phase and add error correction to end of first half
   1.426 +  int const size = impulses_size();
   1.427 +  for ( int p = blip_res; p-- >= blip_res / 2; )
   1.428 +    {
   1.429 +      int p2 = blip_res - 2 - p;
   1.430 +      long error = kernel_unit;
   1.431 +      for ( int i = 1; i < size; i += blip_res )
   1.432  	{
   1.433 -		int p2 = blip_res - 2 - p;
   1.434 -		long error = kernel_unit;
   1.435 -		for ( int i = 1; i < size; i += blip_res )
   1.436 -		{
   1.437 -			error -= impulses [i + p ];
   1.438 -			error -= impulses [i + p2];
   1.439 -		}
   1.440 -		if ( p == p2 )
   1.441 -			error /= 2; // phase = 0.5 impulse uses same half for both sides
   1.442 -		impulses [size - blip_res + p] += (short) error;
   1.443 -		//printf( "error: %ld\n", error );
   1.444 +	  error -= impulses [i + p ];
   1.445 +	  error -= impulses [i + p2];
   1.446  	}
   1.447 +      if ( p == p2 )
   1.448 +	error /= 2; // phase = 0.5 impulse uses same half for both sides
   1.449 +      impulses [size - blip_res + p] += (short) error;
   1.450 +      //printf( "error: %ld\n", error );
   1.451 +    }
   1.452  
   1.453 -	//for ( int i = blip_res; i--; printf( "\n" ) )
   1.454 -	//  for ( int j = 0; j < width / 2; j++ )
   1.455 -	//      printf( "%5ld,", impulses [j * blip_res + i + 1] );
   1.456 +  //for ( int i = blip_res; i--; printf( "\n" ) )
   1.457 +  //  for ( int j = 0; j < width / 2; j++ )
   1.458 +  //      printf( "%5ld,", impulses [j * blip_res + i + 1] );
   1.459  }
   1.460  
   1.461  void Blip_Synth_::treble_eq( blip_eq_t const& eq )
   1.462  {
   1.463 -	float fimpulse [blip_res / 2 * (blip_widest_impulse_ - 1) + blip_res * 2];
   1.464 +  float fimpulse [blip_res / 2 * (blip_widest_impulse_ - 1) + blip_res * 2];
   1.465  
   1.466 -	int const half_size = blip_res / 2 * (width - 1);
   1.467 -	eq.generate( &fimpulse [blip_res], half_size );
   1.468 +  int const half_size = blip_res / 2 * (width - 1);
   1.469 +  eq.generate( &fimpulse [blip_res], half_size );
   1.470  
   1.471 -	int i;
   1.472 +  int i;
   1.473  
   1.474 -	// need mirror slightly past center for calculation
   1.475 -	for ( i = blip_res; i--; )
   1.476 -		fimpulse [blip_res + half_size + i] = fimpulse [blip_res + half_size - 1 - i];
   1.477 +  // need mirror slightly past center for calculation
   1.478 +  for ( i = blip_res; i--; )
   1.479 +    fimpulse [blip_res + half_size + i] = fimpulse [blip_res + half_size - 1 - i];
   1.480  
   1.481 -	// starts at 0
   1.482 -	for ( i = 0; i < blip_res; i++ )
   1.483 -		fimpulse [i] = 0.0f;
   1.484 +  // starts at 0
   1.485 +  for ( i = 0; i < blip_res; i++ )
   1.486 +    fimpulse [i] = 0.0f;
   1.487  
   1.488 -	// find rescale factor
   1.489 -	double total = 0.0;
   1.490 -	for ( i = 0; i < half_size; i++ )
   1.491 -		total += fimpulse [blip_res + i];
   1.492 +  // find rescale factor
   1.493 +  double total = 0.0;
   1.494 +  for ( i = 0; i < half_size; i++ )
   1.495 +    total += fimpulse [blip_res + i];
   1.496  
   1.497 -	//double const base_unit = 44800.0 - 128 * 18; // allows treble up to +0 dB
   1.498 -	//double const base_unit = 37888.0; // allows treble to +5 dB
   1.499 -	double const base_unit = 32768.0; // necessary for blip_unscaled to work
   1.500 -	double rescale = base_unit / 2 / total;
   1.501 -	kernel_unit = (long) base_unit;
   1.502 +  //double const base_unit = 44800.0 - 128 * 18; // allows treble up to +0 dB
   1.503 +  //double const base_unit = 37888.0; // allows treble to +5 dB
   1.504 +  double const base_unit = 32768.0; // necessary for blip_unscaled to work
   1.505 +  double rescale = base_unit / 2 / total;
   1.506 +  kernel_unit = (long) base_unit;
   1.507  
   1.508 -	// integrate, first difference, rescale, convert to int
   1.509 -	double sum = 0.0;
   1.510 -	double next = 0.0;
   1.511 -	int const size = this->impulses_size();
   1.512 -	for ( i = 0; i < size; i++ )
   1.513 -	{
   1.514 -		impulses [i] = (short) (int) floor( (next - sum) * rescale + 0.5 );
   1.515 -		sum += fimpulse [i];
   1.516 -		next += fimpulse [i + blip_res];
   1.517 -	}
   1.518 -	adjust_impulse();
   1.519 +  // integrate, first difference, rescale, convert to int
   1.520 +  double sum = 0.0;
   1.521 +  double next = 0.0;
   1.522 +  int const size = this->impulses_size();
   1.523 +  for ( i = 0; i < size; i++ )
   1.524 +    {
   1.525 +      impulses [i] = (short) (int) floor( (next - sum) * rescale + 0.5 );
   1.526 +      sum += fimpulse [i];
   1.527 +      next += fimpulse [i + blip_res];
   1.528 +    }
   1.529 +  adjust_impulse();
   1.530  
   1.531 -	// volume might require rescaling
   1.532 -	double vol = volume_unit_;
   1.533 -	if ( vol )
   1.534 -	{
   1.535 -		volume_unit_ = 0.0;
   1.536 -		volume_unit( vol );
   1.537 -	}
   1.538 +  // volume might require rescaling
   1.539 +  double vol = volume_unit_;
   1.540 +  if ( vol )
   1.541 +    {
   1.542 +      volume_unit_ = 0.0;
   1.543 +      volume_unit( vol );
   1.544 +    }
   1.545  }
   1.546  
   1.547  void Blip_Synth_::volume_unit( double new_unit )
   1.548  {
   1.549 -	if ( new_unit != volume_unit_ )
   1.550 +  if ( new_unit != volume_unit_ )
   1.551 +    {
   1.552 +      // use default eq if it hasn't been set yet
   1.553 +      if ( !kernel_unit )
   1.554 +	treble_eq( -8.0 );
   1.555 +
   1.556 +      volume_unit_ = new_unit;
   1.557 +      double factor = new_unit * (1L << blip_sample_bits) / kernel_unit;
   1.558 +
   1.559 +      if ( factor > 0.0 )
   1.560  	{
   1.561 -		// use default eq if it hasn't been set yet
   1.562 -		if ( !kernel_unit )
   1.563 -			treble_eq( -8.0 );
   1.564 +	  int shift = 0;
   1.565  
   1.566 -		volume_unit_ = new_unit;
   1.567 -		double factor = new_unit * (1L << blip_sample_bits) / kernel_unit;
   1.568 +	  // if unit is really small, might need to attenuate kernel
   1.569 +	  while ( factor < 2.0 )
   1.570 +	    {
   1.571 +	      shift++;
   1.572 +	      factor *= 2.0;
   1.573 +	    }
   1.574  
   1.575 -		if ( factor > 0.0 )
   1.576 -		{
   1.577 -			int shift = 0;
   1.578 +	  if ( shift )
   1.579 +	    {
   1.580 +	      kernel_unit >>= shift;
   1.581 +	      assert( kernel_unit > 0 ); // fails if volume unit is too low
   1.582  
   1.583 -			// if unit is really small, might need to attenuate kernel
   1.584 -			while ( factor < 2.0 )
   1.585 -			{
   1.586 -				shift++;
   1.587 -				factor *= 2.0;
   1.588 -			}
   1.589 -
   1.590 -			if ( shift )
   1.591 -			{
   1.592 -				kernel_unit >>= shift;
   1.593 -				assert( kernel_unit > 0 ); // fails if volume unit is too low
   1.594 -
   1.595 -				// keep values positive to avoid round-towards-zero of sign-preserving
   1.596 -				// right shift for negative values
   1.597 -				long offset = 0x8000 + (1 << (shift - 1));
   1.598 -				long offset2 = 0x8000 >> shift;
   1.599 -				for ( int i = impulses_size(); i--; )
   1.600 -					impulses [i] = (short) (int) (((impulses [i] + offset) >> shift) - offset2);
   1.601 -				adjust_impulse();
   1.602 -			}
   1.603 -		}
   1.604 -		delta_factor = (int) floor( factor + 0.5 );
   1.605 -		//printf( "delta_factor: %d, kernel_unit: %d\n", delta_factor, kernel_unit );
   1.606 +	      // keep values positive to avoid round-towards-zero of sign-preserving
   1.607 +	      // right shift for negative values
   1.608 +	      long offset = 0x8000 + (1 << (shift - 1));
   1.609 +	      long offset2 = 0x8000 >> shift;
   1.610 +	      for ( int i = impulses_size(); i--; )
   1.611 +		impulses [i] = (short) (int) (((impulses [i] + offset) >> shift) - offset2);
   1.612 +	      adjust_impulse();
   1.613 +	    }
   1.614  	}
   1.615 +      delta_factor = (int) floor( factor + 0.5 );
   1.616 +      //printf( "delta_factor: %d, kernel_unit: %d\n", delta_factor, kernel_unit );
   1.617 +    }
   1.618  }
   1.619  #endif
   1.620  
   1.621  long Blip_Buffer::read_samples( blip_sample_t* out_, long max_samples, int stereo )
   1.622  {
   1.623 -	long count = samples_avail();
   1.624 -	if ( count > max_samples )
   1.625 -		count = max_samples;
   1.626 +  long count = samples_avail();
   1.627 +  if ( count > max_samples )
   1.628 +    count = max_samples;
   1.629  
   1.630 -	if ( count )
   1.631 +  if ( count )
   1.632 +    {
   1.633 +      int const bass = BLIP_READER_BASS( *this );
   1.634 +      BLIP_READER_BEGIN( reader, *this );
   1.635 +      BLIP_READER_ADJ_( reader, count );
   1.636 +      blip_sample_t* BLIP_RESTRICT out = out_ + count;
   1.637 +      blip_long offset = (blip_long) -count;
   1.638 +
   1.639 +      if ( !stereo )
   1.640  	{
   1.641 -		int const bass = BLIP_READER_BASS( *this );
   1.642 -		BLIP_READER_BEGIN( reader, *this );
   1.643 -		BLIP_READER_ADJ_( reader, count );
   1.644 -		blip_sample_t* BLIP_RESTRICT out = out_ + count;
   1.645 -		blip_long offset = (blip_long) -count;
   1.646 +	  do
   1.647 +	    {
   1.648 +	      blip_long s = BLIP_READER_READ( reader );
   1.649 +	      BLIP_READER_NEXT_IDX_( reader, bass, offset );
   1.650 +	      BLIP_CLAMP( s, s );
   1.651 +	      out [offset] = (blip_sample_t) s;
   1.652 +	    }
   1.653 +	  while ( ++offset );
   1.654 +	}
   1.655 +      else
   1.656 +	{
   1.657 +	  do
   1.658 +	    {
   1.659 +	      blip_long s = BLIP_READER_READ( reader );
   1.660 +	      BLIP_READER_NEXT_IDX_( reader, bass, offset );
   1.661 +	      BLIP_CLAMP( s, s );
   1.662 +	      out [offset * 2] = (blip_sample_t) s;
   1.663 +	    }
   1.664 +	  while ( ++offset );
   1.665 +	}
   1.666  
   1.667 -		if ( !stereo )
   1.668 -		{
   1.669 -			do
   1.670 -			{
   1.671 -				blip_long s = BLIP_READER_READ( reader );
   1.672 -				BLIP_READER_NEXT_IDX_( reader, bass, offset );
   1.673 -				BLIP_CLAMP( s, s );
   1.674 -				out [offset] = (blip_sample_t) s;
   1.675 -			}
   1.676 -			while ( ++offset );
   1.677 -		}
   1.678 -		else
   1.679 -		{
   1.680 -			do
   1.681 -			{
   1.682 -				blip_long s = BLIP_READER_READ( reader );
   1.683 -				BLIP_READER_NEXT_IDX_( reader, bass, offset );
   1.684 -				BLIP_CLAMP( s, s );
   1.685 -				out [offset * 2] = (blip_sample_t) s;
   1.686 -			}
   1.687 -			while ( ++offset );
   1.688 -		}
   1.689 +      BLIP_READER_END( reader, *this );
   1.690  
   1.691 -		BLIP_READER_END( reader, *this );
   1.692 -
   1.693 -		remove_samples( count );
   1.694 -	}
   1.695 -	return count;
   1.696 +      remove_samples( count );
   1.697 +    }
   1.698 +  return count;
   1.699  }
   1.700  
   1.701  void Blip_Buffer::mix_samples( blip_sample_t const* in, long count )
   1.702  {
   1.703 -	if ( buffer_size_ == silent_buf_size )
   1.704 -	{
   1.705 -		assert( 0 );
   1.706 -		return;
   1.707 -	}
   1.708 +  if ( buffer_size_ == silent_buf_size )
   1.709 +    {
   1.710 +      assert( 0 );
   1.711 +      return;
   1.712 +    }
   1.713  
   1.714 -	buf_t_* out = buffer_ + (offset_ >> BLIP_BUFFER_ACCURACY) + blip_widest_impulse_ / 2;
   1.715 +  buf_t_* out = buffer_ + (offset_ >> BLIP_BUFFER_ACCURACY) + blip_widest_impulse_ / 2;
   1.716  
   1.717 -	int const sample_shift = blip_sample_bits - 16;
   1.718 -	int prev = 0;
   1.719 -	while ( count-- )
   1.720 -	{
   1.721 -		blip_long s = (blip_long) *in++ << sample_shift;
   1.722 -		*out += s - prev;
   1.723 -		prev = s;
   1.724 -		++out;
   1.725 -	}
   1.726 -	*out -= prev;
   1.727 +  int const sample_shift = blip_sample_bits - 16;
   1.728 +  int prev = 0;
   1.729 +  while ( count-- )
   1.730 +    {
   1.731 +      blip_long s = (blip_long) *in++ << sample_shift;
   1.732 +      *out += s - prev;
   1.733 +      prev = s;
   1.734 +      ++out;
   1.735 +    }
   1.736 +  *out -= prev;
   1.737  }
   1.738  
   1.739  blip_ulong const subsample_mask = (1L << BLIP_BUFFER_ACCURACY) - 1;
   1.740  
   1.741  void Blip_Buffer::save_state( blip_buffer_state_t* out )
   1.742  {
   1.743 -	assert( samples_avail() == 0 );
   1.744 -	out->offset_       = offset_;
   1.745 -	out->reader_accum_ = reader_accum_;
   1.746 -	memcpy( out->buf, &buffer_ [offset_ >> BLIP_BUFFER_ACCURACY], sizeof out->buf );
   1.747 +  assert( samples_avail() == 0 );
   1.748 +  out->offset_       = offset_;
   1.749 +  out->reader_accum_ = reader_accum_;
   1.750 +  memcpy( out->buf, &buffer_ [offset_ >> BLIP_BUFFER_ACCURACY], sizeof out->buf );
   1.751  }
   1.752  
   1.753  void Blip_Buffer::load_state( blip_buffer_state_t const& in )
   1.754  {
   1.755 -	clear( false );
   1.756 +  clear( false );
   1.757  
   1.758 -	offset_       = in.offset_;
   1.759 -	reader_accum_ = in.reader_accum_;
   1.760 -	memcpy( buffer_, in.buf, sizeof in.buf );
   1.761 +  offset_       = in.offset_;
   1.762 +  reader_accum_ = in.reader_accum_;
   1.763 +  memcpy( buffer_, in.buf, sizeof in.buf );
   1.764  }