annotate 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
rev   line source
rlm@1 1 // Blip_Buffer 0.4.1. http://www.slack.net/~ant/
rlm@1 2
rlm@1 3 #include "Blip_Buffer.h"
rlm@1 4
rlm@1 5 #include <assert.h>
rlm@1 6 #include <limits.h>
rlm@1 7 #include <string.h>
rlm@1 8 #include <stdlib.h>
rlm@1 9 #include <math.h>
rlm@1 10
rlm@1 11 /* Copyright (C) 2003-2007 Shay Green. This module is free software; you
rlm@3 12 can redistribute it and/or modify it under the terms of the GNU Lesser
rlm@3 13 General Public License as published by the Free Software Foundation; either
rlm@3 14 version 2.1 of the License, or (at your option) any later version. This
rlm@3 15 module is distributed in the hope that it will be useful, but WITHOUT ANY
rlm@3 16 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
rlm@3 17 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
rlm@3 18 details. You should have received a copy of the GNU Lesser General Public
rlm@3 19 License along with this module; if not, write to the Free Software Foundation,
rlm@3 20 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */
rlm@1 21
rlm@1 22 // TODO: use scoped for variables in treble_eq()
rlm@1 23
rlm@1 24 #ifdef BLARGG_ENABLE_OPTIMIZER
rlm@3 25 #include BLARGG_ENABLE_OPTIMIZER
rlm@1 26 #endif
rlm@1 27
rlm@1 28 int const silent_buf_size = 1; // size used for Silent_Blip_Buffer
rlm@1 29
rlm@1 30 Blip_Buffer::Blip_Buffer()
rlm@1 31 {
rlm@3 32 factor_ = LONG_MAX;
rlm@3 33 buffer_ = 0;
rlm@3 34 buffer_size_ = 0;
rlm@3 35 sample_rate_ = 0;
rlm@3 36 bass_shift_ = 0;
rlm@3 37 clock_rate_ = 0;
rlm@3 38 bass_freq_ = 16;
rlm@3 39 length_ = 0;
rlm@1 40
rlm@3 41 // assumptions code makes about implementation-defined features
rlm@3 42 #ifndef NDEBUG
rlm@3 43 // right shift of negative value preserves sign
rlm@3 44 buf_t_ i = -0x7FFFFFFE;
rlm@3 45 assert( (i >> 1) == -0x3FFFFFFF );
rlm@1 46
rlm@3 47 // casting to short truncates to 16 bits and sign-extends
rlm@3 48 i = 0x18000;
rlm@3 49 assert( (short) i == -0x8000 );
rlm@3 50 #endif
rlm@1 51
rlm@3 52 Clear();
rlm@1 53 }
rlm@1 54
rlm@1 55 Blip_Buffer::~Blip_Buffer()
rlm@1 56 {
rlm@3 57 if ( buffer_size_ != silent_buf_size )
rlm@3 58 free( buffer_ );
rlm@1 59 }
rlm@1 60
rlm@1 61 Silent_Blip_Buffer::Silent_Blip_Buffer()
rlm@1 62 {
rlm@3 63 factor_ = 0;
rlm@3 64 buffer_ = buf;
rlm@3 65 buffer_size_ = silent_buf_size;
rlm@3 66 clear();
rlm@1 67 }
rlm@1 68
rlm@1 69 void Blip_Buffer::clear( int entire_buffer )
rlm@1 70 {
rlm@3 71 offset_ = 0;
rlm@3 72 reader_accum_ = 0;
rlm@3 73 modified_ = 0;
rlm@3 74 if ( buffer_ )
rlm@3 75 {
rlm@3 76 long count = (entire_buffer ? buffer_size_ : samples_avail());
rlm@3 77 memset( buffer_, 0, (count + blip_buffer_extra_) * sizeof (buf_t_) );
rlm@3 78 }
rlm@1 79 }
rlm@1 80
rlm@1 81 Blip_Buffer::blargg_err_t Blip_Buffer::set_sample_rate( long new_rate, int msec )
rlm@1 82 {
rlm@3 83 if ( buffer_size_ == silent_buf_size )
rlm@3 84 {
rlm@3 85 assert( 0 );
rlm@3 86 return "Internal (tried to resize Silent_Blip_Buffer)";
rlm@3 87 }
rlm@1 88
rlm@3 89 // start with maximum length that resampled time can represent
rlm@3 90 long new_size = (ULONG_MAX >> BLIP_BUFFER_ACCURACY) - blip_buffer_extra_ - 64;
rlm@3 91 if ( msec != blip_max_length )
rlm@3 92 {
rlm@3 93 long s = (new_rate * (msec + 1) + 999) / 1000;
rlm@3 94 if ( s < new_size )
rlm@3 95 new_size = s;
rlm@3 96 else
rlm@3 97 assert( 0 ); // fails if requested buffer length exceeds limit
rlm@3 98 }
rlm@1 99
rlm@3 100 if ( buffer_size_ != new_size )
rlm@3 101 {
rlm@3 102 void* p = realloc( buffer_, (new_size + blip_buffer_extra_) * sizeof *buffer_ );
rlm@3 103 if ( !p )
rlm@3 104 return "Out of memory";
rlm@3 105 buffer_ = (buf_t_*) p;
rlm@3 106 }
rlm@1 107
rlm@3 108 buffer_size_ = new_size;
rlm@3 109 assert( buffer_size_ != silent_buf_size ); // size should never happen to match this
rlm@1 110
rlm@3 111 // update things based on the sample rate
rlm@3 112 sample_rate_ = new_rate;
rlm@3 113 length_ = new_size * 1000 / new_rate - 1;
rlm@3 114 if ( msec )
rlm@3 115 assert( length_ == msec ); // ensure length is same as that passed in
rlm@1 116
rlm@3 117 // update these since they depend on sample rate
rlm@3 118 if ( clock_rate_ )
rlm@3 119 clock_rate( clock_rate_ );
rlm@3 120 bass_freq( bass_freq_ );
rlm@1 121
rlm@3 122 clear();
rlm@1 123
rlm@3 124 return 0; // success
rlm@1 125 }
rlm@1 126
rlm@1 127 blip_resampled_time_t Blip_Buffer::clock_rate_factor( long rate ) const
rlm@1 128 {
rlm@3 129 double ratio = (double) sample_rate_ / rate;
rlm@3 130 blip_long factor = (blip_long) floor( ratio * (1L << BLIP_BUFFER_ACCURACY) + 0.5 );
rlm@3 131 assert( factor > 0 || !sample_rate_ ); // fails if clock/output ratio is too large
rlm@3 132 return (blip_resampled_time_t) factor;
rlm@1 133 }
rlm@1 134
rlm@1 135 void Blip_Buffer::bass_freq( int freq )
rlm@1 136 {
rlm@3 137 bass_freq_ = freq;
rlm@3 138 int shift = 31;
rlm@3 139 if ( freq > 0 )
rlm@3 140 {
rlm@3 141 shift = 13;
rlm@3 142 long f = (freq << 16) / sample_rate_;
rlm@3 143 while ( (f >>= 1) && --shift ) { }
rlm@3 144 }
rlm@3 145 bass_shift_ = shift;
rlm@1 146 }
rlm@1 147
rlm@1 148 void Blip_Buffer::end_frame( blip_time_t t )
rlm@1 149 {
rlm@3 150 offset_ += t * factor_;
rlm@3 151 assert( samples_avail() <= (long) buffer_size_ ); // fails if time is past end of buffer
rlm@1 152 }
rlm@1 153
rlm@1 154 long Blip_Buffer::count_samples( blip_time_t t ) const
rlm@1 155 {
rlm@3 156 blip_resampled_time_t last_sample = resampled_time( t ) >> BLIP_BUFFER_ACCURACY;
rlm@3 157 blip_resampled_time_t first_sample = offset_ >> BLIP_BUFFER_ACCURACY;
rlm@3 158 return long (last_sample - first_sample);
rlm@1 159 }
rlm@1 160
rlm@1 161 blip_time_t Blip_Buffer::count_clocks( long count ) const
rlm@1 162 {
rlm@3 163 if ( !factor_ )
rlm@3 164 {
rlm@3 165 assert( 0 ); // sample rate and clock rates must be set first
rlm@3 166 return 0;
rlm@3 167 }
rlm@1 168
rlm@3 169 if ( count > buffer_size_ )
rlm@3 170 count = buffer_size_;
rlm@3 171 blip_resampled_time_t time = (blip_resampled_time_t) count << BLIP_BUFFER_ACCURACY;
rlm@3 172 return (blip_time_t) ((time - offset_ + factor_ - 1) / factor_);
rlm@1 173 }
rlm@1 174
rlm@1 175 void Blip_Buffer::remove_samples( long count )
rlm@1 176 {
rlm@3 177 if ( count )
rlm@3 178 {
rlm@3 179 remove_silence( count );
rlm@1 180
rlm@3 181 // copy remaining samples to beginning and clear old samples
rlm@3 182 long remain = samples_avail() + blip_buffer_extra_;
rlm@3 183 memmove( buffer_, buffer_ + count, remain * sizeof *buffer_ );
rlm@3 184 memset( buffer_ + remain, 0, count * sizeof *buffer_ );
rlm@3 185 }
rlm@1 186 }
rlm@1 187
rlm@1 188 // Blip_Synth_
rlm@1 189
rlm@1 190 Blip_Synth_Fast_::Blip_Synth_Fast_()
rlm@1 191 {
rlm@3 192 buf = 0;
rlm@3 193 last_amp = 0;
rlm@3 194 delta_factor = 0;
rlm@1 195 }
rlm@1 196
rlm@1 197 void Blip_Synth_Fast_::volume_unit( double new_unit )
rlm@1 198 {
rlm@3 199 delta_factor = int (new_unit * (1L << blip_sample_bits) + 0.5);
rlm@1 200 }
rlm@1 201
rlm@1 202 #if !BLIP_BUFFER_FAST
rlm@1 203
rlm@1 204 Blip_Synth_::Blip_Synth_( short* p, int w ) :
rlm@3 205 impulses( p ),
rlm@3 206 width( w )
rlm@1 207 {
rlm@3 208 volume_unit_ = 0.0;
rlm@3 209 kernel_unit = 0;
rlm@3 210 buf = 0;
rlm@3 211 last_amp = 0;
rlm@3 212 delta_factor = 0;
rlm@1 213 }
rlm@1 214
rlm@1 215 #undef PI
rlm@1 216 #define PI 3.1415926535897932384626433832795029
rlm@1 217
rlm@1 218 static void gen_sinc( float* out, int count, double oversample, double treble, double cutoff )
rlm@1 219 {
rlm@3 220 if ( cutoff >= 0.999 )
rlm@3 221 cutoff = 0.999;
rlm@1 222
rlm@3 223 if ( treble < -300.0 )
rlm@3 224 treble = -300.0;
rlm@3 225 if ( treble > 5.0 )
rlm@3 226 treble = 5.0;
rlm@1 227
rlm@3 228 double const maxh = 4096.0;
rlm@3 229 double const rolloff = pow( 10.0, 1.0 / (maxh * 20.0) * treble / (1.0 - cutoff) );
rlm@3 230 double const pow_a_n = pow( rolloff, maxh - maxh * cutoff );
rlm@3 231 double const to_angle = PI / 2 / maxh / oversample;
rlm@3 232 for ( int i = 0; i < count; i++ )
rlm@3 233 {
rlm@3 234 double angle = ((i - count) * 2 + 1) * to_angle;
rlm@3 235 double c = rolloff * cos( (maxh - 1.0) * angle ) - cos( maxh * angle );
rlm@3 236 double cos_nc_angle = cos( maxh * cutoff * angle );
rlm@3 237 double cos_nc1_angle = cos( (maxh * cutoff - 1.0) * angle );
rlm@3 238 double cos_angle = cos( angle );
rlm@1 239
rlm@3 240 c = c * pow_a_n - rolloff * cos_nc1_angle + cos_nc_angle;
rlm@3 241 double d = 1.0 + rolloff * (rolloff - cos_angle - cos_angle);
rlm@3 242 double b = 2.0 - cos_angle - cos_angle;
rlm@3 243 double a = 1.0 - cos_angle - cos_nc_angle + cos_nc1_angle;
rlm@1 244
rlm@3 245 out [i] = (float) ((a * d + c * b) / (b * d)); // a / b + c / d
rlm@3 246 }
rlm@1 247 }
rlm@1 248
rlm@1 249 void blip_eq_t::generate( float* out, int count ) const
rlm@1 250 {
rlm@3 251 // lower cutoff freq for narrow kernels with their wider transition band
rlm@3 252 // (8 points->1.49, 16 points->1.15)
rlm@3 253 double oversample = blip_res * 2.25 / count + 0.85;
rlm@3 254 double half_rate = sample_rate * 0.5;
rlm@3 255 if ( cutoff_freq )
rlm@3 256 oversample = half_rate / cutoff_freq;
rlm@3 257 double cutoff = rolloff_freq * oversample / half_rate;
rlm@1 258
rlm@3 259 gen_sinc( out, count, blip_res * oversample, treble, cutoff );
rlm@1 260
rlm@3 261 // apply (half of) hamming window
rlm@3 262 double to_fraction = PI / (count - 1);
rlm@3 263 for ( int i = count; i--; )
rlm@3 264 out [i] *= 0.54f - 0.46f * (float) cos( i * to_fraction );
rlm@1 265 }
rlm@1 266
rlm@1 267 void Blip_Synth_::adjust_impulse()
rlm@1 268 {
rlm@3 269 // sum pairs for each phase and add error correction to end of first half
rlm@3 270 int const size = impulses_size();
rlm@3 271 for ( int p = blip_res; p-- >= blip_res / 2; )
rlm@3 272 {
rlm@3 273 int p2 = blip_res - 2 - p;
rlm@3 274 long error = kernel_unit;
rlm@3 275 for ( int i = 1; i < size; i += blip_res )
rlm@1 276 {
rlm@3 277 error -= impulses [i + p ];
rlm@3 278 error -= impulses [i + p2];
rlm@1 279 }
rlm@3 280 if ( p == p2 )
rlm@3 281 error /= 2; // phase = 0.5 impulse uses same half for both sides
rlm@3 282 impulses [size - blip_res + p] += (short) error;
rlm@3 283 //printf( "error: %ld\n", error );
rlm@3 284 }
rlm@1 285
rlm@3 286 //for ( int i = blip_res; i--; printf( "\n" ) )
rlm@3 287 // for ( int j = 0; j < width / 2; j++ )
rlm@3 288 // printf( "%5ld,", impulses [j * blip_res + i + 1] );
rlm@1 289 }
rlm@1 290
rlm@1 291 void Blip_Synth_::treble_eq( blip_eq_t const& eq )
rlm@1 292 {
rlm@3 293 float fimpulse [blip_res / 2 * (blip_widest_impulse_ - 1) + blip_res * 2];
rlm@1 294
rlm@3 295 int const half_size = blip_res / 2 * (width - 1);
rlm@3 296 eq.generate( &fimpulse [blip_res], half_size );
rlm@1 297
rlm@3 298 int i;
rlm@1 299
rlm@3 300 // need mirror slightly past center for calculation
rlm@3 301 for ( i = blip_res; i--; )
rlm@3 302 fimpulse [blip_res + half_size + i] = fimpulse [blip_res + half_size - 1 - i];
rlm@1 303
rlm@3 304 // starts at 0
rlm@3 305 for ( i = 0; i < blip_res; i++ )
rlm@3 306 fimpulse [i] = 0.0f;
rlm@1 307
rlm@3 308 // find rescale factor
rlm@3 309 double total = 0.0;
rlm@3 310 for ( i = 0; i < half_size; i++ )
rlm@3 311 total += fimpulse [blip_res + i];
rlm@1 312
rlm@3 313 //double const base_unit = 44800.0 - 128 * 18; // allows treble up to +0 dB
rlm@3 314 //double const base_unit = 37888.0; // allows treble to +5 dB
rlm@3 315 double const base_unit = 32768.0; // necessary for blip_unscaled to work
rlm@3 316 double rescale = base_unit / 2 / total;
rlm@3 317 kernel_unit = (long) base_unit;
rlm@1 318
rlm@3 319 // integrate, first difference, rescale, convert to int
rlm@3 320 double sum = 0.0;
rlm@3 321 double next = 0.0;
rlm@3 322 int const size = this->impulses_size();
rlm@3 323 for ( i = 0; i < size; i++ )
rlm@3 324 {
rlm@3 325 impulses [i] = (short) (int) floor( (next - sum) * rescale + 0.5 );
rlm@3 326 sum += fimpulse [i];
rlm@3 327 next += fimpulse [i + blip_res];
rlm@3 328 }
rlm@3 329 adjust_impulse();
rlm@1 330
rlm@3 331 // volume might require rescaling
rlm@3 332 double vol = volume_unit_;
rlm@3 333 if ( vol )
rlm@3 334 {
rlm@3 335 volume_unit_ = 0.0;
rlm@3 336 volume_unit( vol );
rlm@3 337 }
rlm@1 338 }
rlm@1 339
rlm@1 340 void Blip_Synth_::volume_unit( double new_unit )
rlm@1 341 {
rlm@3 342 if ( new_unit != volume_unit_ )
rlm@3 343 {
rlm@3 344 // use default eq if it hasn't been set yet
rlm@3 345 if ( !kernel_unit )
rlm@3 346 treble_eq( -8.0 );
rlm@3 347
rlm@3 348 volume_unit_ = new_unit;
rlm@3 349 double factor = new_unit * (1L << blip_sample_bits) / kernel_unit;
rlm@3 350
rlm@3 351 if ( factor > 0.0 )
rlm@1 352 {
rlm@3 353 int shift = 0;
rlm@1 354
rlm@3 355 // if unit is really small, might need to attenuate kernel
rlm@3 356 while ( factor < 2.0 )
rlm@3 357 {
rlm@3 358 shift++;
rlm@3 359 factor *= 2.0;
rlm@3 360 }
rlm@1 361
rlm@3 362 if ( shift )
rlm@3 363 {
rlm@3 364 kernel_unit >>= shift;
rlm@3 365 assert( kernel_unit > 0 ); // fails if volume unit is too low
rlm@1 366
rlm@3 367 // keep values positive to avoid round-towards-zero of sign-preserving
rlm@3 368 // right shift for negative values
rlm@3 369 long offset = 0x8000 + (1 << (shift - 1));
rlm@3 370 long offset2 = 0x8000 >> shift;
rlm@3 371 for ( int i = impulses_size(); i--; )
rlm@3 372 impulses [i] = (short) (int) (((impulses [i] + offset) >> shift) - offset2);
rlm@3 373 adjust_impulse();
rlm@3 374 }
rlm@1 375 }
rlm@3 376 delta_factor = (int) floor( factor + 0.5 );
rlm@3 377 //printf( "delta_factor: %d, kernel_unit: %d\n", delta_factor, kernel_unit );
rlm@3 378 }
rlm@1 379 }
rlm@1 380 #endif
rlm@1 381
rlm@1 382 long Blip_Buffer::read_samples( blip_sample_t* out_, long max_samples, int stereo )
rlm@1 383 {
rlm@3 384 long count = samples_avail();
rlm@3 385 if ( count > max_samples )
rlm@3 386 count = max_samples;
rlm@1 387
rlm@3 388 if ( count )
rlm@3 389 {
rlm@3 390 int const bass = BLIP_READER_BASS( *this );
rlm@3 391 BLIP_READER_BEGIN( reader, *this );
rlm@3 392 BLIP_READER_ADJ_( reader, count );
rlm@3 393 blip_sample_t* BLIP_RESTRICT out = out_ + count;
rlm@3 394 blip_long offset = (blip_long) -count;
rlm@3 395
rlm@3 396 if ( !stereo )
rlm@1 397 {
rlm@3 398 do
rlm@3 399 {
rlm@3 400 blip_long s = BLIP_READER_READ( reader );
rlm@3 401 BLIP_READER_NEXT_IDX_( reader, bass, offset );
rlm@3 402 BLIP_CLAMP( s, s );
rlm@3 403 out [offset] = (blip_sample_t) s;
rlm@3 404 }
rlm@3 405 while ( ++offset );
rlm@3 406 }
rlm@3 407 else
rlm@3 408 {
rlm@3 409 do
rlm@3 410 {
rlm@3 411 blip_long s = BLIP_READER_READ( reader );
rlm@3 412 BLIP_READER_NEXT_IDX_( reader, bass, offset );
rlm@3 413 BLIP_CLAMP( s, s );
rlm@3 414 out [offset * 2] = (blip_sample_t) s;
rlm@3 415 }
rlm@3 416 while ( ++offset );
rlm@3 417 }
rlm@1 418
rlm@3 419 BLIP_READER_END( reader, *this );
rlm@1 420
rlm@3 421 remove_samples( count );
rlm@3 422 }
rlm@3 423 return count;
rlm@1 424 }
rlm@1 425
rlm@1 426 void Blip_Buffer::mix_samples( blip_sample_t const* in, long count )
rlm@1 427 {
rlm@3 428 if ( buffer_size_ == silent_buf_size )
rlm@3 429 {
rlm@3 430 assert( 0 );
rlm@3 431 return;
rlm@3 432 }
rlm@1 433
rlm@3 434 buf_t_* out = buffer_ + (offset_ >> BLIP_BUFFER_ACCURACY) + blip_widest_impulse_ / 2;
rlm@1 435
rlm@3 436 int const sample_shift = blip_sample_bits - 16;
rlm@3 437 int prev = 0;
rlm@3 438 while ( count-- )
rlm@3 439 {
rlm@3 440 blip_long s = (blip_long) *in++ << sample_shift;
rlm@3 441 *out += s - prev;
rlm@3 442 prev = s;
rlm@3 443 ++out;
rlm@3 444 }
rlm@3 445 *out -= prev;
rlm@1 446 }
rlm@1 447
rlm@1 448 blip_ulong const subsample_mask = (1L << BLIP_BUFFER_ACCURACY) - 1;
rlm@1 449
rlm@1 450 void Blip_Buffer::save_state( blip_buffer_state_t* out )
rlm@1 451 {
rlm@3 452 assert( samples_avail() == 0 );
rlm@3 453 out->offset_ = offset_;
rlm@3 454 out->reader_accum_ = reader_accum_;
rlm@3 455 memcpy( out->buf, &buffer_ [offset_ >> BLIP_BUFFER_ACCURACY], sizeof out->buf );
rlm@1 456 }
rlm@1 457
rlm@1 458 void Blip_Buffer::load_state( blip_buffer_state_t const& in )
rlm@1 459 {
rlm@3 460 clear( false );
rlm@1 461
rlm@3 462 offset_ = in.offset_;
rlm@3 463 reader_accum_ = in.reader_accum_;
rlm@3 464 memcpy( buffer_, in.buf, sizeof in.buf );
rlm@1 465 }