| 1 | // Blip_Buffer 0.4.1. http://www.slack.net/~ant/ |
| 2 | |
| 3 | #include "../include/blip/Blip_Buffer.h" |
| 4 | |
| 5 | #include <assert.h> |
| 6 | #include <climits> |
| 7 | #include <string.h> |
| 8 | #include <stdlib.h> |
| 9 | #include <math.h> |
| 10 | |
| 11 | /* Copyright (C) 2003-2006 Shay Green. This module is free software; you |
| 12 | can redistribute it and/or modify it under the terms of the GNU Lesser |
| 13 | General Public License as published by the Free Software Foundation; either |
| 14 | version 2.1 of the License, or (at your option) any later version. This |
| 15 | module is distributed in the hope that it will be useful, but WITHOUT ANY |
| 16 | WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 17 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
| 18 | details. You should have received a copy of the GNU Lesser General Public |
| 19 | License along with this module; if not, write to the Free Software Foundation, |
| 20 | Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ |
| 21 | |
| 22 | #ifdef BLARGG_ENABLE_OPTIMIZER |
| 23 | #include BLARGG_ENABLE_OPTIMIZER |
| 24 | #endif |
| 25 | |
| 26 | Blip_Buffer::Blip_Buffer() |
| 27 | { |
| 28 | factor_ = (blip_u64)ULLONG_MAX; |
| 29 | offset_ = 0; |
| 30 | buffer_ = 0; |
| 31 | buffer_size_ = 0; |
| 32 | sample_rate_ = 0; |
| 33 | reader_accum_ = 0; |
| 34 | bass_shift_ = 0; |
| 35 | clock_rate_ = 0; |
| 36 | bass_freq_ = 16; |
| 37 | length_ = 0; |
| 38 | } |
| 39 | |
| 40 | Blip_Buffer::~Blip_Buffer() |
| 41 | { |
| 42 | if (buffer_) |
| 43 | free( buffer_ ); |
| 44 | } |
| 45 | |
| 46 | void Blip_Buffer::clear( int entire_buffer ) |
| 47 | { |
| 48 | offset_ = 0; |
| 49 | reader_accum_ = 0; |
| 50 | modified_ = 0; |
| 51 | if ( buffer_ ) |
| 52 | { |
| 53 | long count = (entire_buffer ? buffer_size_ : samples_avail()); |
| 54 | memset( buffer_, 0, (count + blip_buffer_extra_) * sizeof (buf_t_) ); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | Blip_Buffer::blargg_err_t Blip_Buffer::set_sample_rate( long new_rate, int msec ) |
| 59 | { |
| 60 | // start with maximum length that resampled time can represent |
| 61 | blip_s64 new_size = (ULLONG_MAX >> BLIP_BUFFER_ACCURACY) - blip_buffer_extra_ - 64; |
| 62 | |
| 63 | // simple safety check, since code elsewhere may not be safe for sizes approaching (2 ^ 31). |
| 64 | if(new_size > ((1LL << 30) - 1)) |
| 65 | new_size = (1LL << 30) - 1; |
| 66 | |
| 67 | if ( msec != blip_max_length ) |
| 68 | { |
| 69 | blip_s64 s = ((blip_s64)new_rate * (msec + 1) + 999) / 1000; |
| 70 | if ( s < new_size ) |
| 71 | new_size = s; |
| 72 | else |
| 73 | assert( 0 ); // fails if requested buffer length exceeds limit |
| 74 | } |
| 75 | |
| 76 | if ( buffer_size_ != new_size ) |
| 77 | { |
| 78 | void* p = realloc( buffer_, (new_size + blip_buffer_extra_) * sizeof *buffer_ ); |
| 79 | if ( !p ) |
| 80 | return "Out of memory"; |
| 81 | |
| 82 | buffer_ = (buf_t_*) p; |
| 83 | } |
| 84 | |
| 85 | buffer_size_ = new_size; |
| 86 | |
| 87 | // update things based on the sample rate |
| 88 | sample_rate_ = new_rate; |
| 89 | length_ = new_size * 1000 / new_rate - 1; |
| 90 | if ( msec ) |
| 91 | assert( length_ == msec ); // ensure length is same as that passed in |
| 92 | if ( clock_rate_ ) |
| 93 | clock_rate( clock_rate_ ); |
| 94 | bass_freq( bass_freq_ ); |
| 95 | |
| 96 | clear(); |
| 97 | |
| 98 | return 0; // success |
| 99 | } |
| 100 | |
| 101 | blip_resampled_time_t Blip_Buffer::clock_rate_factor( long rate ) const |
| 102 | { |
| 103 | double ratio = (double) sample_rate_ / rate; |
| 104 | blip_s64 factor = (blip_s64) floor( ratio * (1LL << BLIP_BUFFER_ACCURACY) + 0.5 ); |
| 105 | assert( factor > 0 || !sample_rate_ ); // fails if clock/output ratio is too large |
| 106 | return (blip_resampled_time_t) factor; |
| 107 | } |
| 108 | |
| 109 | void Blip_Buffer::bass_freq( int freq ) |
| 110 | { |
| 111 | bass_freq_ = freq; |
| 112 | int shift = 31; |
| 113 | if ( freq > 0 ) |
| 114 | { |
| 115 | shift = 13; |
| 116 | long f = (freq << 16) / sample_rate_; |
| 117 | while ( (f >>= 1) && --shift ) { } |
| 118 | } |
| 119 | bass_shift_ = shift; |
| 120 | } |
| 121 | |
| 122 | void Blip_Buffer::end_frame( blip_time_t t ) |
| 123 | { |
| 124 | offset_ += t * factor_; |
| 125 | assert( samples_avail() <= (long) buffer_size_ ); // time outside buffer length |
| 126 | } |
| 127 | |
| 128 | void Blip_Buffer::remove_silence( long count ) |
| 129 | { |
| 130 | assert( count <= samples_avail() ); // tried to remove more samples than available |
| 131 | offset_ -= (blip_resampled_time_t) count << BLIP_BUFFER_ACCURACY; |
| 132 | } |
| 133 | |
| 134 | long Blip_Buffer::count_samples( blip_time_t t ) const |
| 135 | { |
| 136 | unsigned long last_sample = resampled_time( t ) >> BLIP_BUFFER_ACCURACY; |
| 137 | unsigned long first_sample = offset_ >> BLIP_BUFFER_ACCURACY; |
| 138 | return (long) (last_sample - first_sample); |
| 139 | } |
| 140 | |
| 141 | blip_time_t Blip_Buffer::count_clocks( long count ) const |
| 142 | { |
| 143 | if ( !factor_ ) |
| 144 | { |
| 145 | assert( 0 ); // sample rate and clock rates must be set first |
| 146 | return 0; |
| 147 | } |
| 148 | |
| 149 | if ( count > buffer_size_ ) |
| 150 | count = buffer_size_; |
| 151 | blip_resampled_time_t time = (blip_resampled_time_t) count << BLIP_BUFFER_ACCURACY; |
| 152 | return (blip_time_t) ((time - offset_ + factor_ - 1) / factor_); |
| 153 | } |
| 154 | |
| 155 | void Blip_Buffer::remove_samples( long count ) |
| 156 | { |
| 157 | if ( count ) |
| 158 | { |
| 159 | remove_silence( count ); |
| 160 | |
| 161 | // copy remaining samples to beginning and clear old samples |
| 162 | long remain = samples_avail() + blip_buffer_extra_; |
| 163 | memmove( buffer_, buffer_ + count, remain * sizeof *buffer_ ); |
| 164 | memset( buffer_ + remain, 0, count * sizeof *buffer_ ); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | // Blip_Synth_ |
| 169 | |
| 170 | Blip_Synth_Fast_::Blip_Synth_Fast_() |
| 171 | { |
| 172 | buf = 0; |
| 173 | last_amp = 0; |
| 174 | delta_factor = 0; |
| 175 | } |
| 176 | |
| 177 | void Blip_Synth_Fast_::volume_unit( double new_unit ) |
| 178 | { |
| 179 | delta_factor = int (new_unit * (1L << blip_sample_bits) + 0.5); |
| 180 | } |
| 181 | |
| 182 | long Blip_Buffer::read_samples( blip_sample_t* BLIP_RESTRICT out, long max_samples, int stereo ) |
| 183 | { |
| 184 | long count = samples_avail(); |
| 185 | if ( count > max_samples ) |
| 186 | count = max_samples; |
| 187 | |
| 188 | if ( count ) |
| 189 | { |
| 190 | int const bass = BLIP_READER_BASS( *this ); |
| 191 | BLIP_READER_BEGIN( reader, *this ); |
| 192 | |
| 193 | #ifndef WANT_STEREO_SOUND |
| 194 | if ( !stereo ) |
| 195 | { |
| 196 | for ( blip_long n = count; n; --n ) |
| 197 | { |
| 198 | blip_long s = BLIP_READER_READ( reader ); |
| 199 | if ( (blip_sample_t) s != s ) |
| 200 | s = 0x7FFF - (s >> 24); |
| 201 | *out++ = (blip_sample_t) s; |
| 202 | BLIP_READER_NEXT( reader, bass ); |
| 203 | } |
| 204 | } |
| 205 | else |
| 206 | #endif |
| 207 | { |
| 208 | for ( blip_long n = count; n; --n ) |
| 209 | { |
| 210 | blip_long s = BLIP_READER_READ( reader ); |
| 211 | if ( (blip_sample_t) s != s ) |
| 212 | s = 0x7FFF - (s >> 24); |
| 213 | *out = (blip_sample_t) s; |
| 214 | out += 2; |
| 215 | BLIP_READER_NEXT( reader, bass ); |
| 216 | } |
| 217 | } |
| 218 | BLIP_READER_END( reader, *this ); |
| 219 | |
| 220 | remove_samples( count ); |
| 221 | } |
| 222 | return count; |
| 223 | } |