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