Mercurial > vba-clojure
changeset 4:5f6f2134e8ce
apu appears to not be used
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Sat, 03 Mar 2012 10:35:58 -0600 |
parents | b05d00f19d80 |
children | 8fe0c57e53d2 |
files | src/apu/Blip_Buffer.cpp src/apu/Blip_Buffer.h |
diffstat | 2 files changed, 0 insertions(+), 1021 deletions(-) [+] |
line wrap: on
line diff
1.1 --- a/src/apu/Blip_Buffer.cpp Sat Mar 03 10:33:11 2012 -0600 1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 1.3 @@ -1,465 +0,0 @@ 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 -}
2.1 --- a/src/apu/Blip_Buffer.h Sat Mar 03 10:33:11 2012 -0600 2.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 2.3 @@ -1,556 +0,0 @@ 2.4 -// Band-limited sound synthesis buffer 2.5 - 2.6 -// Blip_Buffer 0.4.1 2.7 -#ifndef BLIP_BUFFER_H 2.8 -#define BLIP_BUFFER_H 2.9 - 2.10 - // internal 2.11 - #include <limits.h> 2.12 - #if INT_MAX < 0x7FFFFFFF || LONG_MAX == 0x7FFFFFFF 2.13 - typedef long blip_long; 2.14 - typedef unsigned long blip_ulong; 2.15 - #else 2.16 - typedef int blip_long; 2.17 - typedef unsigned blip_ulong; 2.18 - #endif 2.19 - 2.20 -// Time unit at source clock rate 2.21 -typedef blip_long blip_time_t; 2.22 - 2.23 -// Output samples are 16-bit signed, with a range of -32768 to 32767 2.24 -typedef short blip_sample_t; 2.25 -enum { blip_sample_max = 32767 }; 2.26 - 2.27 -struct blip_buffer_state_t; 2.28 - 2.29 -class Blip_Buffer { 2.30 -public: 2.31 - typedef const char* blargg_err_t; 2.32 - 2.33 - // Sets output sample rate and buffer length in milliseconds (1/1000 sec, defaults 2.34 - // to 1/4 second) and clears buffer. If there isn't enough memory, leaves buffer 2.35 - // untouched and returns "Out of memory", otherwise returns NULL. 2.36 - blargg_err_t set_sample_rate( long samples_per_sec, int msec_length = 1000 / 4 ); 2.37 - 2.38 - // Sets number of source time units per second 2.39 - void clock_rate( long clocks_per_sec ); 2.40 - 2.41 - // Ends current time frame of specified duration and makes its samples available 2.42 - // (along with any still-unread samples) for reading with read_samples(). Begins 2.43 - // a new time frame at the end of the current frame. 2.44 - void end_frame( blip_time_t time ); 2.45 - 2.46 - // Reads at most 'max_samples' out of buffer into 'dest', removing them from from 2.47 - // the buffer. Returns number of samples actually read and removed. If stereo is 2.48 - // true, increments 'dest' one extra time after writing each sample, to allow 2.49 - // easy interleving of two channels into a stereo output buffer. 2.50 - long read_samples( blip_sample_t* dest, long max_samples, int stereo = 0 ); 2.51 - 2.52 -// Additional features 2.53 - 2.54 - // Removes all available samples and clear buffer to silence. If 'entire_buffer' is 2.55 - // false, just clears out any samples waiting rather than the entire buffer. 2.56 - void clear( int entire_buffer = 1 ); 2.57 - 2.58 - // Number of samples available for reading with read_samples() 2.59 - long samples_avail() const; 2.60 - 2.61 - // Removes 'count' samples from those waiting to be read 2.62 - void remove_samples( long count ); 2.63 - 2.64 - // Sets frequency high-pass filter frequency, where higher values reduce bass more 2.65 - void bass_freq( int frequency ); 2.66 - 2.67 - // Current output sample rate 2.68 - long sample_rate() const; 2.69 - 2.70 - // Length of buffer in milliseconds 2.71 - int length() const; 2.72 - 2.73 - // Number of source time units per second 2.74 - long clock_rate() const; 2.75 - 2.76 -// Experimental features 2.77 - 2.78 - // Saves state, including high-pass filter and tails of last deltas. 2.79 - // All samples must have been read from buffer before calling this. 2.80 - void save_state( blip_buffer_state_t* out ); 2.81 - 2.82 - // Loads state. State must have been saved from Blip_Buffer with same 2.83 - // settings during same run of program. States can NOT be stored on disk. 2.84 - // Clears buffer before loading state. 2.85 - void load_state( blip_buffer_state_t const& in ); 2.86 - 2.87 - // Number of samples delay from synthesis to samples read out 2.88 - int output_latency() const; 2.89 - 2.90 - // Counts number of clocks needed until 'count' samples will be available. 2.91 - // If buffer can't even hold 'count' samples, returns number of clocks until 2.92 - // buffer becomes full. 2.93 - blip_time_t count_clocks( long count ) const; 2.94 - 2.95 - // Number of raw samples that can be mixed within frame of specified duration. 2.96 - long count_samples( blip_time_t duration ) const; 2.97 - 2.98 - // Mixes in 'count' samples from 'buf_in' 2.99 - void mix_samples( blip_sample_t const* buf_in, long count ); 2.100 - 2.101 - 2.102 - // Signals that sound has been added to buffer. Could be done automatically in 2.103 - // Blip_Synth, but that would affect performance more, as you can arrange that 2.104 - // this is called only once per time frame rather than for every delta. 2.105 - void set_modified() { modified_ = this; } 2.106 - 2.107 - // not documented yet 2.108 - blip_ulong unsettled() const; 2.109 - Blip_Buffer* clear_modified() { Blip_Buffer* b = modified_; modified_ = 0; return b; } 2.110 - void remove_silence( long count ); 2.111 - typedef blip_ulong blip_resampled_time_t; 2.112 - blip_resampled_time_t resampled_duration( int t ) const { return t * factor_; } 2.113 - blip_resampled_time_t resampled_time( blip_time_t t ) const { return t * factor_ + offset_; } 2.114 - blip_resampled_time_t clock_rate_factor( long clock_rate ) const; 2.115 -public: 2.116 - Blip_Buffer(); 2.117 - ~Blip_Buffer(); 2.118 - 2.119 - // Deprecated 2.120 - typedef blip_resampled_time_t resampled_time_t; 2.121 - blargg_err_t sample_rate( long r ) { return set_sample_rate( r ); } 2.122 - blargg_err_t sample_rate( long r, int msec ) { return set_sample_rate( r, msec ); } 2.123 -private: 2.124 - // noncopyable 2.125 - Blip_Buffer( const Blip_Buffer& ); 2.126 - Blip_Buffer& operator = ( const Blip_Buffer& ); 2.127 -public: 2.128 - typedef blip_long buf_t_; 2.129 - blip_ulong factor_; 2.130 - blip_resampled_time_t offset_; 2.131 - buf_t_* buffer_; 2.132 - blip_long buffer_size_; 2.133 - blip_long reader_accum_; 2.134 - int bass_shift_; 2.135 -private: 2.136 - long sample_rate_; 2.137 - long clock_rate_; 2.138 - int bass_freq_; 2.139 - int length_; 2.140 - Blip_Buffer* modified_; // non-zero = true (more optimal than using bool, heh) 2.141 - friend class Blip_Reader; 2.142 -}; 2.143 - 2.144 -#ifdef HAVE_CONFIG_H 2.145 - #include "config.h" 2.146 -#endif 2.147 - 2.148 -// Number of bits in resample ratio fraction. Higher values give a more accurate ratio 2.149 -// but reduce maximum buffer size. 2.150 -#ifndef BLIP_BUFFER_ACCURACY 2.151 - #define BLIP_BUFFER_ACCURACY 16 2.152 -#endif 2.153 - 2.154 -// Number bits in phase offset. Fewer than 6 bits (64 phase offsets) results in 2.155 -// noticeable broadband noise when synthesizing high frequency square waves. 2.156 -// Affects size of Blip_Synth objects since they store the waveform directly. 2.157 -#ifndef BLIP_PHASE_BITS 2.158 - #if BLIP_BUFFER_FAST 2.159 - #define BLIP_PHASE_BITS 8 2.160 - #else 2.161 - #define BLIP_PHASE_BITS 6 2.162 - #endif 2.163 -#endif 2.164 - 2.165 - // Internal 2.166 - typedef blip_ulong blip_resampled_time_t; 2.167 - int const blip_widest_impulse_ = 16; 2.168 - int const blip_buffer_extra_ = blip_widest_impulse_ + 2; 2.169 - int const blip_res = 1 << BLIP_PHASE_BITS; 2.170 - class blip_eq_t; 2.171 - 2.172 - class Blip_Synth_Fast_ { 2.173 - public: 2.174 - Blip_Buffer* buf; 2.175 - int last_amp; 2.176 - int delta_factor; 2.177 - 2.178 - void volume_unit( double ); 2.179 - Blip_Synth_Fast_(); 2.180 - void treble_eq( blip_eq_t const& ) { } 2.181 - }; 2.182 - 2.183 - class Blip_Synth_ { 2.184 - public: 2.185 - Blip_Buffer* buf; 2.186 - int last_amp; 2.187 - int delta_factor; 2.188 - 2.189 - void volume_unit( double ); 2.190 - Blip_Synth_( short* impulses, int width ); 2.191 - void treble_eq( blip_eq_t const& ); 2.192 - private: 2.193 - double volume_unit_; 2.194 - short* const impulses; 2.195 - int const width; 2.196 - blip_long kernel_unit; 2.197 - int impulses_size() const { return blip_res / 2 * width + 1; } 2.198 - void adjust_impulse(); 2.199 - }; 2.200 - 2.201 -// Quality level, better = slower. In general, use blip_good_quality. 2.202 -const int blip_med_quality = 8; 2.203 -const int blip_good_quality = 12; 2.204 -const int blip_high_quality = 16; 2.205 - 2.206 -// Range specifies the greatest expected change in amplitude. Calculate it 2.207 -// by finding the difference between the maximum and minimum expected 2.208 -// amplitudes (max - min). 2.209 -template<int quality,int range> 2.210 -class Blip_Synth { 2.211 -public: 2.212 - // Sets overall volume of waveform 2.213 - void volume( double v ) { impl.volume_unit( v * (1.0 / (range < 0 ? -range : range)) ); } 2.214 - 2.215 - // Configures low-pass filter (see blip_buffer.txt) 2.216 - void treble_eq( blip_eq_t const& eq ) { impl.treble_eq( eq ); } 2.217 - 2.218 - // Gets/sets Blip_Buffer used for output 2.219 - Blip_Buffer* output() const { return impl.buf; } 2.220 - void output( Blip_Buffer* b ) { impl.buf = b; impl.last_amp = 0; } 2.221 - 2.222 - // Updates amplitude of waveform at given time. Using this requires a separate 2.223 - // Blip_Synth for each waveform. 2.224 - void update( blip_time_t time, int amplitude ); 2.225 - 2.226 -// Low-level interface 2.227 - 2.228 - // Adds an amplitude transition of specified delta, optionally into specified buffer 2.229 - // rather than the one set with output(). Delta can be positive or negative. 2.230 - // The actual change in amplitude is delta * (volume / range) 2.231 - void offset( blip_time_t, int delta, Blip_Buffer* ) const; 2.232 - void offset( blip_time_t t, int delta ) const { offset( t, delta, impl.buf ); } 2.233 - 2.234 - // Works directly in terms of fractional output samples. Contact author for more info. 2.235 - void offset_resampled( blip_resampled_time_t, int delta, Blip_Buffer* ) const; 2.236 - 2.237 - // Same as offset(), except code is inlined for higher performance 2.238 - void offset_inline( blip_time_t t, int delta, Blip_Buffer* buf ) const { 2.239 - offset_resampled( t * buf->factor_ + buf->offset_, delta, buf ); 2.240 - } 2.241 - void offset_inline( blip_time_t t, int delta ) const { 2.242 - offset_resampled( t * impl.buf->factor_ + impl.buf->offset_, delta, impl.buf ); 2.243 - } 2.244 - 2.245 -private: 2.246 -#if BLIP_BUFFER_FAST 2.247 - Blip_Synth_Fast_ impl; 2.248 -#else 2.249 - Blip_Synth_ impl; 2.250 - typedef short imp_t; 2.251 - imp_t impulses [blip_res * (quality / 2) + 1]; 2.252 -public: 2.253 - Blip_Synth() : impl( impulses, quality ) { } 2.254 -#endif 2.255 -}; 2.256 - 2.257 -// Low-pass equalization parameters 2.258 -class blip_eq_t { 2.259 -public: 2.260 - // Logarithmic rolloff to treble dB at half sampling rate. Negative values reduce 2.261 - // treble, small positive values (0 to 5.0) increase treble. 2.262 - blip_eq_t( double treble_db = 0 ); 2.263 - 2.264 - // See blip_buffer.txt 2.265 - blip_eq_t( double treble, long rolloff_freq, long sample_rate, long cutoff_freq = 0 ); 2.266 - 2.267 -private: 2.268 - double treble; 2.269 - long rolloff_freq; 2.270 - long sample_rate; 2.271 - long cutoff_freq; 2.272 - void generate( float* out, int count ) const; 2.273 - friend class Blip_Synth_; 2.274 -}; 2.275 - 2.276 -int const blip_sample_bits = 30; 2.277 - 2.278 -// Dummy Blip_Buffer to direct sound output to, for easy muting without 2.279 -// having to stop sound code. 2.280 -class Silent_Blip_Buffer : public Blip_Buffer { 2.281 - buf_t_ buf [blip_buffer_extra_ + 1]; 2.282 -public: 2.283 - // The following cannot be used (an assertion will fail if attempted): 2.284 - blargg_err_t set_sample_rate( long samples_per_sec, int msec_length ); 2.285 - blip_time_t count_clocks( long count ) const; 2.286 - void mix_samples( blip_sample_t const* buf, long count ); 2.287 - 2.288 - Silent_Blip_Buffer(); 2.289 -}; 2.290 - 2.291 - #if __GNUC__ >= 3 || _MSC_VER >= 1400 2.292 - #define BLIP_RESTRICT __restrict 2.293 - #else 2.294 - #define BLIP_RESTRICT 2.295 - #endif 2.296 - 2.297 -// Optimized reading from Blip_Buffer, for use in custom sample output 2.298 - 2.299 -// Begins reading from buffer. Name should be unique to the current block. 2.300 -#define BLIP_READER_BEGIN( name, blip_buffer ) \ 2.301 - const Blip_Buffer::buf_t_* BLIP_RESTRICT name##_reader_buf = (blip_buffer).buffer_;\ 2.302 - blip_long name##_reader_accum = (blip_buffer).reader_accum_ 2.303 - 2.304 -// Gets value to pass to BLIP_READER_NEXT() 2.305 -#define BLIP_READER_BASS( blip_buffer ) ((blip_buffer).bass_shift_) 2.306 - 2.307 -// Constant value to use instead of BLIP_READER_BASS(), for slightly more optimal 2.308 -// code at the cost of having no bass control 2.309 -int const blip_reader_default_bass = 9; 2.310 - 2.311 -// Current sample 2.312 -#define BLIP_READER_READ( name ) (name##_reader_accum >> (blip_sample_bits - 16)) 2.313 - 2.314 -// Current raw sample in full internal resolution 2.315 -#define BLIP_READER_READ_RAW( name ) (name##_reader_accum) 2.316 - 2.317 -// Advances to next sample 2.318 -#define BLIP_READER_NEXT( name, bass ) \ 2.319 - (void) (name##_reader_accum += *name##_reader_buf++ - (name##_reader_accum >> (bass))) 2.320 - 2.321 -// Ends reading samples from buffer. The number of samples read must now be removed 2.322 -// using Blip_Buffer::remove_samples(). 2.323 -#define BLIP_READER_END( name, blip_buffer ) \ 2.324 - (void) ((blip_buffer).reader_accum_ = name##_reader_accum) 2.325 - 2.326 - 2.327 -// experimental 2.328 -#define BLIP_READER_ADJ_( name, offset ) (name##_reader_buf += offset) 2.329 - 2.330 -blip_long const blip_reader_idx_factor = sizeof (Blip_Buffer::buf_t_); 2.331 - 2.332 -#define BLIP_READER_NEXT_IDX_( name, bass, idx ) {\ 2.333 - name##_reader_accum -= name##_reader_accum >> (bass);\ 2.334 - name##_reader_accum += name##_reader_buf [(idx)];\ 2.335 -} 2.336 - 2.337 -#define BLIP_READER_NEXT_RAW_IDX_( name, bass, idx ) {\ 2.338 - name##_reader_accum -= name##_reader_accum >> (bass);\ 2.339 - name##_reader_accum +=\ 2.340 - *(Blip_Buffer::buf_t_ const*) ((char const*) name##_reader_buf + (idx));\ 2.341 -} 2.342 - 2.343 -// Compatibility with older version 2.344 -const long blip_unscaled = 65535; 2.345 -const int blip_low_quality = blip_med_quality; 2.346 -const int blip_best_quality = blip_high_quality; 2.347 - 2.348 -// Deprecated; use BLIP_READER macros as follows: 2.349 -// Blip_Reader r; r.begin( buf ); -> BLIP_READER_BEGIN( r, buf ); 2.350 -// int bass = r.begin( buf ) -> BLIP_READER_BEGIN( r, buf ); int bass = BLIP_READER_BASS( buf ); 2.351 -// r.read() -> BLIP_READER_READ( r ) 2.352 -// r.read_raw() -> BLIP_READER_READ_RAW( r ) 2.353 -// r.next( bass ) -> BLIP_READER_NEXT( r, bass ) 2.354 -// r.next() -> BLIP_READER_NEXT( r, blip_reader_default_bass ) 2.355 -// r.end( buf ) -> BLIP_READER_END( r, buf ) 2.356 -class Blip_Reader { 2.357 -public: 2.358 - int begin( Blip_Buffer& ); 2.359 - blip_long read() const { return accum >> (blip_sample_bits - 16); } 2.360 - blip_long read_raw() const { return accum; } 2.361 - void next( int bass_shift = 9 ) { accum += *buf++ - (accum >> bass_shift); } 2.362 - void end( Blip_Buffer& b ) { b.reader_accum_ = accum; } 2.363 -private: 2.364 - const Blip_Buffer::buf_t_* buf; 2.365 - blip_long accum; 2.366 -}; 2.367 - 2.368 -#if defined (_M_IX86) || defined (_M_IA64) || defined (__i486__) || \ 2.369 - defined (__x86_64__) || defined (__ia64__) || defined (__i386__) 2.370 - #define BLIP_CLAMP_( in ) in < -0x8000 || 0x7FFF < in 2.371 -#else 2.372 - #define BLIP_CLAMP_( in ) (blip_sample_t) in != in 2.373 -#endif 2.374 - 2.375 -// Clamp sample to blip_sample_t range 2.376 -#define BLIP_CLAMP( sample, out )\ 2.377 - { if ( BLIP_CLAMP_( (sample) ) ) (out) = ((sample) >> 24) ^ 0x7FFF; } 2.378 - 2.379 -struct blip_buffer_state_t 2.380 -{ 2.381 - blip_resampled_time_t offset_; 2.382 - blip_long reader_accum_; 2.383 - blip_long buf [blip_buffer_extra_]; 2.384 -}; 2.385 - 2.386 -// End of public interface 2.387 - 2.388 -#ifndef assert 2.389 - #include <assert.h> 2.390 -#endif 2.391 - 2.392 -template<int quality,int range> 2.393 -inline void Blip_Synth<quality,range>::offset_resampled( blip_resampled_time_t time, 2.394 - int delta, Blip_Buffer* blip_buf ) const 2.395 -{ 2.396 - // If this assertion fails, it means that an attempt was made to add a delta 2.397 - // at a negative time or past the end of the buffer. 2.398 - assert( (blip_long) (time >> BLIP_BUFFER_ACCURACY) < blip_buf->buffer_size_ ); 2.399 - 2.400 - delta *= impl.delta_factor; 2.401 - blip_long* BLIP_RESTRICT buf = blip_buf->buffer_ + (time >> BLIP_BUFFER_ACCURACY); 2.402 - int phase = (int) (time >> (BLIP_BUFFER_ACCURACY - BLIP_PHASE_BITS) & (blip_res - 1)); 2.403 - 2.404 -#if BLIP_BUFFER_FAST 2.405 - blip_long left = buf [0] + delta; 2.406 - 2.407 - // Kind of crappy, but doing shift after multiply results in overflow. 2.408 - // Alternate way of delaying multiply by delta_factor results in worse 2.409 - // sub-sample resolution. 2.410 - blip_long right = (delta >> BLIP_PHASE_BITS) * phase; 2.411 - left -= right; 2.412 - right += buf [1]; 2.413 - 2.414 - buf [0] = left; 2.415 - buf [1] = right; 2.416 -#else 2.417 - 2.418 - int const fwd = (blip_widest_impulse_ - quality) / 2; 2.419 - int const rev = fwd + quality - 2; 2.420 - int const mid = quality / 2 - 1; 2.421 - 2.422 - imp_t const* BLIP_RESTRICT imp = impulses + blip_res - phase; 2.423 - 2.424 - #if defined (_M_IX86) || defined (_M_IA64) || defined (__i486__) || \ 2.425 - defined (__x86_64__) || defined (__ia64__) || defined (__i386__) 2.426 - 2.427 - // this straight forward version gave in better code on GCC for x86 2.428 - 2.429 - #define ADD_IMP( out, in ) \ 2.430 - buf [out] += (blip_long) imp [blip_res * (in)] * delta 2.431 - 2.432 - #define BLIP_FWD( i ) {\ 2.433 - ADD_IMP( fwd + i, i );\ 2.434 - ADD_IMP( fwd + 1 + i, i + 1 );\ 2.435 - } 2.436 - #define BLIP_REV( r ) {\ 2.437 - ADD_IMP( rev - r, r + 1 );\ 2.438 - ADD_IMP( rev + 1 - r, r );\ 2.439 - } 2.440 - 2.441 - BLIP_FWD( 0 ) 2.442 - if ( quality > 8 ) BLIP_FWD( 2 ) 2.443 - if ( quality > 12 ) BLIP_FWD( 4 ) 2.444 - { 2.445 - ADD_IMP( fwd + mid - 1, mid - 1 ); 2.446 - ADD_IMP( fwd + mid , mid ); 2.447 - imp = impulses + phase; 2.448 - } 2.449 - if ( quality > 12 ) BLIP_REV( 6 ) 2.450 - if ( quality > 8 ) BLIP_REV( 4 ) 2.451 - BLIP_REV( 2 ) 2.452 - 2.453 - ADD_IMP( rev , 1 ); 2.454 - ADD_IMP( rev + 1, 0 ); 2.455 - 2.456 - #undef ADD_IMP 2.457 - 2.458 - #else 2.459 - 2.460 - // for RISC processors, help compiler by reading ahead of writes 2.461 - 2.462 - #define BLIP_FWD( i ) {\ 2.463 - blip_long t0 = i0 * delta + buf [fwd + i];\ 2.464 - blip_long t1 = imp [blip_res * (i + 1)] * delta + buf [fwd + 1 + i];\ 2.465 - i0 = imp [blip_res * (i + 2)];\ 2.466 - buf [fwd + i] = t0;\ 2.467 - buf [fwd + 1 + i] = t1;\ 2.468 - } 2.469 - #define BLIP_REV( r ) {\ 2.470 - blip_long t0 = i0 * delta + buf [rev - r];\ 2.471 - blip_long t1 = imp [blip_res * r] * delta + buf [rev + 1 - r];\ 2.472 - i0 = imp [blip_res * (r - 1)];\ 2.473 - buf [rev - r] = t0;\ 2.474 - buf [rev + 1 - r] = t1;\ 2.475 - } 2.476 - 2.477 - blip_long i0 = *imp; 2.478 - BLIP_FWD( 0 ) 2.479 - if ( quality > 8 ) BLIP_FWD( 2 ) 2.480 - if ( quality > 12 ) BLIP_FWD( 4 ) 2.481 - { 2.482 - blip_long t0 = i0 * delta + buf [fwd + mid - 1]; 2.483 - blip_long t1 = imp [blip_res * mid] * delta + buf [fwd + mid ]; 2.484 - imp = impulses + phase; 2.485 - i0 = imp [blip_res * mid]; 2.486 - buf [fwd + mid - 1] = t0; 2.487 - buf [fwd + mid ] = t1; 2.488 - } 2.489 - if ( quality > 12 ) BLIP_REV( 6 ) 2.490 - if ( quality > 8 ) BLIP_REV( 4 ) 2.491 - BLIP_REV( 2 ) 2.492 - 2.493 - blip_long t0 = i0 * delta + buf [rev ]; 2.494 - blip_long t1 = *imp * delta + buf [rev + 1]; 2.495 - buf [rev ] = t0; 2.496 - buf [rev + 1] = t1; 2.497 - #endif 2.498 - 2.499 -#endif 2.500 -} 2.501 - 2.502 -#undef BLIP_FWD 2.503 -#undef BLIP_REV 2.504 - 2.505 -template<int quality,int range> 2.506 -#if BLIP_BUFFER_FAST 2.507 - inline 2.508 -#endif 2.509 -void Blip_Synth<quality,range>::offset( blip_time_t t, int delta, Blip_Buffer* buf ) const 2.510 -{ 2.511 - offset_resampled( t * buf->factor_ + buf->offset_, delta, buf ); 2.512 -} 2.513 - 2.514 -template<int quality,int range> 2.515 -#if BLIP_BUFFER_FAST 2.516 - inline 2.517 -#endif 2.518 -void Blip_Synth<quality,range>::update( blip_time_t t, int amp ) 2.519 -{ 2.520 - int delta = amp - impl.last_amp; 2.521 - impl.last_amp = amp; 2.522 - offset_resampled( t * impl.buf->factor_ + impl.buf->offset_, delta, impl.buf ); 2.523 -} 2.524 - 2.525 -inline blip_eq_t::blip_eq_t( double t ) : 2.526 - treble( t ), rolloff_freq( 0 ), sample_rate( 44100 ), cutoff_freq( 0 ) { } 2.527 -inline blip_eq_t::blip_eq_t( double t, long rf, long sr, long cf ) : 2.528 - treble( t ), rolloff_freq( rf ), sample_rate( sr ), cutoff_freq( cf ) { } 2.529 - 2.530 -inline int Blip_Buffer::length() const { return length_; } 2.531 -inline long Blip_Buffer::samples_avail() const { return (long) (offset_ >> BLIP_BUFFER_ACCURACY); } 2.532 -inline long Blip_Buffer::sample_rate() const { return sample_rate_; } 2.533 -inline int Blip_Buffer::output_latency() const { return blip_widest_impulse_ / 2; } 2.534 -inline long Blip_Buffer::clock_rate() const { return clock_rate_; } 2.535 -inline void Blip_Buffer::clock_rate( long cps ) { factor_ = clock_rate_factor( clock_rate_ = cps ); } 2.536 - 2.537 -inline int Blip_Reader::begin( Blip_Buffer& blip_buf ) 2.538 -{ 2.539 - buf = blip_buf.buffer_; 2.540 - accum = blip_buf.reader_accum_; 2.541 - return blip_buf.bass_shift_; 2.542 -} 2.543 - 2.544 -inline void Blip_Buffer::remove_silence( long count ) 2.545 -{ 2.546 - // fails if you try to remove more samples than available 2.547 - assert( count <= samples_avail() ); 2.548 - offset_ -= (blip_resampled_time_t) count << BLIP_BUFFER_ACCURACY; 2.549 -} 2.550 - 2.551 -inline blip_ulong Blip_Buffer::unsettled() const 2.552 -{ 2.553 - return reader_accum_ >> (blip_sample_bits - 16); 2.554 -} 2.555 - 2.556 -int const blip_max_length = 0; 2.557 -int const blip_default_length = 250; // 1/4 second 2.558 - 2.559 -#endif