| 1 | // Band-limited sound synthesis buffer |
| 2 | // Various changes and hacks for use in Mednafen. |
| 3 | |
| 4 | #ifdef __GNUC__ |
| 5 | #define blip_inline inline __attribute__((always_inline)) |
| 6 | #else |
| 7 | #define blip_inline inline |
| 8 | #endif |
| 9 | |
| 10 | #include <limits.h> |
| 11 | #include <stdint.h> |
| 12 | |
| 13 | // Blip_Buffer 0.4.1 |
| 14 | #ifndef BLIP_BUFFER_H |
| 15 | #define BLIP_BUFFER_H |
| 16 | |
| 17 | // Internal |
| 18 | typedef int32_t blip_long; |
| 19 | typedef uint32_t blip_ulong; |
| 20 | typedef int64_t blip_s64; |
| 21 | typedef uint64_t blip_u64; |
| 22 | |
| 23 | // Time unit at source clock rate |
| 24 | typedef blip_long blip_time_t; |
| 25 | |
| 26 | // Output samples are 16-bit signed, with a range of -32768 to 32767 |
| 27 | typedef short blip_sample_t; |
| 28 | enum { blip_sample_max = 32767 }; |
| 29 | |
| 30 | class Blip_Buffer { |
| 31 | public: |
| 32 | typedef const char* blargg_err_t; |
| 33 | |
| 34 | // Set output sample rate and buffer length in milliseconds (1/1000 sec, defaults |
| 35 | // to 1/4 second), then clear buffer. Returns NULL on success, otherwise if there |
| 36 | // isn't enough memory, returns error without affecting current buffer setup. |
| 37 | blargg_err_t set_sample_rate( long samples_per_sec, int msec_length = 1000 / 4 ); |
| 38 | |
| 39 | // Set number of source time units per second |
| 40 | void clock_rate( long ); |
| 41 | |
| 42 | // End current time frame of specified duration and make its samples available |
| 43 | // (along with any still-unread samples) for reading with read_samples(). Begins |
| 44 | // a new time frame at the end of the current frame. |
| 45 | void end_frame( blip_time_t time ); |
| 46 | |
| 47 | // Read at most 'max_samples' out of buffer into 'dest', removing them from from |
| 48 | // the buffer. Returns number of samples actually read and removed. If stereo is |
| 49 | // true, increments 'dest' one extra time after writing each sample, to allow |
| 50 | // easy interleving of two channels into a stereo output buffer. |
| 51 | long read_samples( blip_sample_t* dest, long max_samples, int stereo = 0 ); |
| 52 | |
| 53 | // Additional optional features |
| 54 | |
| 55 | // Current output sample rate |
| 56 | long sample_rate() const; |
| 57 | |
| 58 | // Length of buffer, in milliseconds |
| 59 | int length() const; |
| 60 | |
| 61 | // Number of source time units per second |
| 62 | long clock_rate() const; |
| 63 | |
| 64 | // Set frequency high-pass filter frequency, where higher values reduce bass more |
| 65 | void bass_freq( int frequency ); |
| 66 | |
| 67 | // Number of samples delay from synthesis to samples read out |
| 68 | int output_latency() const; |
| 69 | |
| 70 | // Remove all available samples and clear buffer to silence. If 'entire_buffer' is |
| 71 | // false, just clears out any samples waiting rather than the entire buffer. |
| 72 | void clear( int entire_buffer = 1 ); |
| 73 | |
| 74 | // Number of samples available for reading with read_samples() |
| 75 | long samples_avail() const; |
| 76 | |
| 77 | // Remove 'count' samples from those waiting to be read |
| 78 | void remove_samples( long count ); |
| 79 | |
| 80 | // Experimental features |
| 81 | |
| 82 | // Count number of clocks needed until 'count' samples will be available. |
| 83 | // If buffer can't even hold 'count' samples, returns number of clocks until |
| 84 | // buffer becomes full. |
| 85 | blip_time_t count_clocks( long count ) const; |
| 86 | |
| 87 | // Number of raw samples that can be mixed within frame of specified duration. |
| 88 | long count_samples( blip_time_t duration ) const; |
| 89 | |
| 90 | // Mix 'count' samples from 'buf' into buffer. |
| 91 | void mix_samples( blip_sample_t const* buf, long count ); |
| 92 | |
| 93 | // not documented yet |
| 94 | void set_modified() { modified_ = 1; } |
| 95 | int clear_modified() { int b = modified_; modified_ = 0; return b; } |
| 96 | typedef blip_u64 blip_resampled_time_t; |
| 97 | void remove_silence( long count ); |
| 98 | blip_resampled_time_t resampled_duration( int t ) const { return t * factor_; } |
| 99 | blip_resampled_time_t resampled_time( blip_time_t t ) const { return t * factor_ + offset_; } |
| 100 | blip_resampled_time_t clock_rate_factor( long clock_rate ) const; |
| 101 | public: |
| 102 | Blip_Buffer(); |
| 103 | ~Blip_Buffer(); |
| 104 | |
| 105 | // Deprecated |
| 106 | typedef blip_resampled_time_t resampled_time_t; |
| 107 | blargg_err_t sample_rate( long r ) { return set_sample_rate( r ); } |
| 108 | blargg_err_t sample_rate( long r, int msec ) { return set_sample_rate( r, msec ); } |
| 109 | private: |
| 110 | // noncopyable |
| 111 | Blip_Buffer( const Blip_Buffer& ); |
| 112 | Blip_Buffer& operator = ( const Blip_Buffer& ); |
| 113 | public: |
| 114 | typedef blip_time_t buf_t_; |
| 115 | blip_u64 factor_; |
| 116 | blip_resampled_time_t offset_; |
| 117 | buf_t_* buffer_; |
| 118 | blip_long buffer_size_; |
| 119 | blip_long reader_accum_; |
| 120 | int bass_shift_; |
| 121 | private: |
| 122 | long sample_rate_; |
| 123 | long clock_rate_; |
| 124 | int bass_freq_; |
| 125 | int length_; |
| 126 | int modified_; |
| 127 | friend class Blip_Reader; |
| 128 | }; |
| 129 | |
| 130 | #ifdef HAVE_CONFIG_H |
| 131 | #include "config.h" |
| 132 | #endif |
| 133 | |
| 134 | #define BLIP_BUFFER_ACCURACY 32 |
| 135 | #define BLIP_PHASE_BITS 8 |
| 136 | |
| 137 | // Number of bits in resample ratio fraction. Higher values give a more accurate ratio |
| 138 | // but reduce maximum buffer size. |
| 139 | //#ifndef BLIP_BUFFER_ACCURACY |
| 140 | // #define BLIP_BUFFER_ACCURACY 16 |
| 141 | //#endif |
| 142 | |
| 143 | // Number bits in phase offset. Fewer than 6 bits (64 phase offsets) results in |
| 144 | // noticeable broadband noise when synthesizing high frequency square waves. |
| 145 | // Affects size of Blip_Synth objects since they store the waveform directly. |
| 146 | //#ifndef BLIP_PHASE_BITS |
| 147 | // #if BLIP_BUFFER_FAST |
| 148 | // #define BLIP_PHASE_BITS 8 |
| 149 | // #else |
| 150 | // #define BLIP_PHASE_BITS 6 |
| 151 | // #endif |
| 152 | //#endif |
| 153 | |
| 154 | // Internal |
| 155 | typedef blip_u64 blip_resampled_time_t; |
| 156 | int const blip_widest_impulse_ = 16; |
| 157 | int const blip_buffer_extra_ = blip_widest_impulse_ + 2; |
| 158 | int const blip_res = 1 << BLIP_PHASE_BITS; |
| 159 | class blip_eq_t; |
| 160 | |
| 161 | class Blip_Synth_Fast_ { |
| 162 | public: |
| 163 | Blip_Buffer* buf; |
| 164 | int last_amp; |
| 165 | int delta_factor; |
| 166 | |
| 167 | void volume_unit( double ); |
| 168 | Blip_Synth_Fast_(); |
| 169 | void treble_eq( blip_eq_t const& ) { } |
| 170 | }; |
| 171 | |
| 172 | class Blip_Synth_ { |
| 173 | public: |
| 174 | Blip_Buffer* buf; |
| 175 | int last_amp; |
| 176 | int delta_factor; |
| 177 | |
| 178 | void volume_unit( double ); |
| 179 | Blip_Synth_( short* impulses, int width ); |
| 180 | void treble_eq( blip_eq_t const& ); |
| 181 | private: |
| 182 | double volume_unit_; |
| 183 | short* const impulses; |
| 184 | int const width; |
| 185 | blip_long kernel_unit; |
| 186 | int impulses_size() const { return blip_res / 2 * width + 1; } |
| 187 | void adjust_impulse(); |
| 188 | }; |
| 189 | |
| 190 | // Quality level. Start with blip_good_quality. |
| 191 | const int blip_med_quality = 8; |
| 192 | const int blip_good_quality = 12; |
| 193 | const int blip_high_quality = 16; |
| 194 | |
| 195 | // Range specifies the greatest expected change in amplitude. Calculate it |
| 196 | // by finding the difference between the maximum and minimum expected |
| 197 | // amplitudes (max - min). |
| 198 | template<int quality,int range> |
| 199 | class Blip_Synth { |
| 200 | public: |
| 201 | // Set overall volume of waveform |
| 202 | void volume( double v ) { impl.volume_unit( v * (1.0 / (range < 0 ? -range : range)) ); } |
| 203 | |
| 204 | // Configure low-pass filter (see blip_buffer.txt) |
| 205 | void treble_eq( blip_eq_t const& eq ) { impl.treble_eq( eq ); } |
| 206 | |
| 207 | // Get/set Blip_Buffer used for output |
| 208 | Blip_Buffer* output() const { return impl.buf; } |
| 209 | void output( Blip_Buffer* b ) { impl.buf = b; impl.last_amp = 0; } |
| 210 | |
| 211 | // Update amplitude of waveform at given time. Using this requires a separate |
| 212 | // Blip_Synth for each waveform. |
| 213 | void update( blip_time_t time, int amplitude ); |
| 214 | |
| 215 | // Low-level interface |
| 216 | |
| 217 | // Add an amplitude transition of specified delta, optionally into specified buffer |
| 218 | // rather than the one set with output(). Delta can be positive or negative. |
| 219 | // The actual change in amplitude is delta * (volume / range) |
| 220 | void offset( blip_time_t, int delta, Blip_Buffer* ) const; |
| 221 | void offset( blip_time_t t, int delta ) const { offset( t, delta, impl.buf ); } |
| 222 | |
| 223 | // Works directly in terms of fractional output samples. Contact author for more info. |
| 224 | void offset_resampled( blip_resampled_time_t, int delta, Blip_Buffer* ) const; |
| 225 | |
| 226 | // Same as offset(), except code is inlined for higher performance |
| 227 | void offset_inline( blip_time_t t, int delta, Blip_Buffer* buf ) const { |
| 228 | offset_resampled( t * buf->factor_ + buf->offset_, delta, buf ); |
| 229 | } |
| 230 | void offset_inline( blip_time_t t, int delta ) const { |
| 231 | offset_resampled( t * impl.buf->factor_ + impl.buf->offset_, delta, impl.buf ); |
| 232 | } |
| 233 | |
| 234 | private: |
| 235 | Blip_Synth_Fast_ impl; |
| 236 | }; |
| 237 | |
| 238 | // Low-pass equalization parameters |
| 239 | class blip_eq_t { |
| 240 | public: |
| 241 | // Logarithmic rolloff to treble dB at half sampling rate. Negative values reduce |
| 242 | // treble, small positive values (0 to 5.0) increase treble. |
| 243 | blip_eq_t( double treble_db = 0 ); |
| 244 | |
| 245 | // See blip_buffer.txt |
| 246 | blip_eq_t( double treble, long rolloff_freq, long sample_rate, long cutoff_freq = 0 ); |
| 247 | |
| 248 | private: |
| 249 | double treble; |
| 250 | long rolloff_freq; |
| 251 | long sample_rate; |
| 252 | long cutoff_freq; |
| 253 | void generate( float* out, int count ) const; |
| 254 | friend class Blip_Synth_; |
| 255 | }; |
| 256 | |
| 257 | int const blip_sample_bits = 30; |
| 258 | |
| 259 | #define BLIP_RESTRICT |
| 260 | |
| 261 | // Optimized reading from Blip_Buffer, for use in custom sample output |
| 262 | |
| 263 | // Begin reading from buffer. Name should be unique to the current block. |
| 264 | #define BLIP_READER_BEGIN( name, blip_buffer ) \ |
| 265 | const Blip_Buffer::buf_t_* BLIP_RESTRICT name##_reader_buf = (blip_buffer).buffer_;\ |
| 266 | blip_long name##_reader_accum = (blip_buffer).reader_accum_ |
| 267 | |
| 268 | // Get value to pass to BLIP_READER_NEXT() |
| 269 | #define BLIP_READER_BASS( blip_buffer ) ((blip_buffer).bass_shift_) |
| 270 | |
| 271 | // Constant value to use instead of BLIP_READER_BASS(), for slightly more optimal |
| 272 | // code at the cost of having no bass control |
| 273 | int const blip_reader_default_bass = 9; |
| 274 | |
| 275 | // Current sample |
| 276 | #define BLIP_READER_READ( name ) (name##_reader_accum >> (blip_sample_bits - 16)) |
| 277 | |
| 278 | // Current raw sample in full internal resolution |
| 279 | #define BLIP_READER_READ_RAW( name ) (name##_reader_accum) |
| 280 | |
| 281 | // Advance to next sample |
| 282 | #define BLIP_READER_NEXT( name, bass ) \ |
| 283 | (void) (name##_reader_accum += *name##_reader_buf++ - (name##_reader_accum >> (bass))) |
| 284 | |
| 285 | // End reading samples from buffer. The number of samples read must now be removed |
| 286 | // using Blip_Buffer::remove_samples(). |
| 287 | #define BLIP_READER_END( name, blip_buffer ) \ |
| 288 | (void) ((blip_buffer).reader_accum_ = name##_reader_accum) |
| 289 | |
| 290 | |
| 291 | // Compatibility with older version |
| 292 | const long blip_unscaled = 65535; |
| 293 | const int blip_low_quality = blip_med_quality; |
| 294 | const int blip_best_quality = blip_high_quality; |
| 295 | |
| 296 | // Deprecated; use BLIP_READER macros as follows: |
| 297 | // Blip_Reader r; r.begin( buf ); -> BLIP_READER_BEGIN( r, buf ); |
| 298 | // int bass = r.begin( buf ) -> BLIP_READER_BEGIN( r, buf ); int bass = BLIP_READER_BASS( buf ); |
| 299 | // r.read() -> BLIP_READER_READ( r ) |
| 300 | // r.read_raw() -> BLIP_READER_READ_RAW( r ) |
| 301 | // r.next( bass ) -> BLIP_READER_NEXT( r, bass ) |
| 302 | // r.next() -> BLIP_READER_NEXT( r, blip_reader_default_bass ) |
| 303 | // r.end( buf ) -> BLIP_READER_END( r, buf ) |
| 304 | class Blip_Reader { |
| 305 | public: |
| 306 | int begin( Blip_Buffer& ); |
| 307 | blip_long read() const { return accum >> (blip_sample_bits - 16); } |
| 308 | blip_long read_raw() const { return accum; } |
| 309 | void next( int bass_shift = 9 ) { accum += *buf++ - (accum >> bass_shift); } |
| 310 | void end( Blip_Buffer& b ) { b.reader_accum_ = accum; } |
| 311 | |
| 312 | private: |
| 313 | const Blip_Buffer::buf_t_* buf; |
| 314 | blip_long accum; |
| 315 | }; |
| 316 | |
| 317 | // End of public interface |
| 318 | |
| 319 | #include <assert.h> |
| 320 | |
| 321 | template<int quality,int range> |
| 322 | blip_inline void Blip_Synth<quality,range>::offset_resampled( blip_resampled_time_t time, |
| 323 | int delta, Blip_Buffer* blip_buf ) const |
| 324 | { |
| 325 | // Fails if time is beyond end of Blip_Buffer, due to a bug in caller code or the |
| 326 | // need for a longer buffer as set by set_sample_rate(). |
| 327 | assert( (blip_long) (time >> BLIP_BUFFER_ACCURACY) < blip_buf->buffer_size_ ); |
| 328 | delta *= impl.delta_factor; |
| 329 | blip_long* BLIP_RESTRICT buf = blip_buf->buffer_ + (time >> BLIP_BUFFER_ACCURACY); |
| 330 | int phase = (int) (time >> (BLIP_BUFFER_ACCURACY - BLIP_PHASE_BITS) & (blip_res - 1)); |
| 331 | |
| 332 | blip_long left = buf [0] + delta; |
| 333 | |
| 334 | // Kind of crappy, but doing shift after multiply results in overflow. |
| 335 | // Alternate way of delaying multiply by delta_factor results in worse |
| 336 | // sub-sample resolution. |
| 337 | blip_long right = (delta >> BLIP_PHASE_BITS) * phase; |
| 338 | left -= right; |
| 339 | right += buf [1]; |
| 340 | |
| 341 | buf [0] = left; |
| 342 | buf [1] = right; |
| 343 | } |
| 344 | |
| 345 | #undef BLIP_FWD |
| 346 | #undef BLIP_REV |
| 347 | |
| 348 | template<int quality,int range> |
| 349 | blip_inline void Blip_Synth<quality,range>::offset( blip_time_t t, int delta, Blip_Buffer* buf ) const |
| 350 | { |
| 351 | offset_resampled( t * buf->factor_ + buf->offset_, delta, buf ); |
| 352 | } |
| 353 | |
| 354 | template<int quality,int range> |
| 355 | blip_inline void Blip_Synth<quality,range>::update( blip_time_t t, int amp ) |
| 356 | { |
| 357 | int delta = amp - impl.last_amp; |
| 358 | impl.last_amp = amp; |
| 359 | offset_resampled( t * impl.buf->factor_ + impl.buf->offset_, delta, impl.buf ); |
| 360 | } |
| 361 | |
| 362 | blip_inline blip_eq_t::blip_eq_t( double t ) : |
| 363 | treble( t ), rolloff_freq( 0 ), sample_rate( 44100 ), cutoff_freq( 0 ) { } |
| 364 | blip_inline blip_eq_t::blip_eq_t( double t, long rf, long sr, long cf ) : |
| 365 | treble( t ), rolloff_freq( rf ), sample_rate( sr ), cutoff_freq( cf ) { } |
| 366 | |
| 367 | blip_inline int Blip_Buffer::length() const { return length_; } |
| 368 | blip_inline long Blip_Buffer::samples_avail() const { return (long) (offset_ >> BLIP_BUFFER_ACCURACY); } |
| 369 | blip_inline long Blip_Buffer::sample_rate() const { return sample_rate_; } |
| 370 | blip_inline int Blip_Buffer::output_latency() const { return blip_widest_impulse_ / 2; } |
| 371 | blip_inline long Blip_Buffer::clock_rate() const { return clock_rate_; } |
| 372 | blip_inline void Blip_Buffer::clock_rate( long cps ) { factor_ = clock_rate_factor( clock_rate_ = cps ); } |
| 373 | |
| 374 | blip_inline int Blip_Reader::begin( Blip_Buffer& blip_buf ) |
| 375 | { |
| 376 | buf = blip_buf.buffer_; |
| 377 | accum = blip_buf.reader_accum_; |
| 378 | return blip_buf.bass_shift_; |
| 379 | } |
| 380 | |
| 381 | int const blip_max_length = 0; |
| 382 | int const blip_default_length = 250; |
| 383 | |
| 384 | #endif |