| 1 | /* Band-limited sound synthesis buffer |
| 2 | * Various changes and hacks for use in Mednafen. |
| 3 | |
| 4 | * Blip_Buffer 0.4.1 |
| 5 | */ |
| 6 | #ifndef BLIP_BUFFER_H |
| 7 | #define BLIP_BUFFER_H |
| 8 | |
| 9 | #include <limits.h> |
| 10 | #include <stdint.h> |
| 11 | |
| 12 | #include <retro_inline.h> |
| 13 | |
| 14 | #ifdef __cplusplus |
| 15 | extern "C" { |
| 16 | #endif |
| 17 | |
| 18 | /* Internal */ |
| 19 | typedef int32_t blip_long; |
| 20 | typedef uint32_t blip_ulong; |
| 21 | typedef int64_t blip_s64; |
| 22 | typedef uint64_t blip_u64; |
| 23 | |
| 24 | /* Time unit at source clock rate */ |
| 25 | typedef blip_long blip_time_t; |
| 26 | |
| 27 | /* Output samples are 16-bit signed, with a range of -32768 to 32767 */ |
| 28 | typedef short blip_sample_t; |
| 29 | enum { blip_sample_max = 32767 }; |
| 30 | |
| 31 | typedef const char* blargg_err_t; |
| 32 | typedef blip_u64 blip_resampled_time_t; |
| 33 | typedef blip_resampled_time_t resampled_time_t; |
| 34 | typedef blip_time_t blip_buf_t_; |
| 35 | |
| 36 | typedef struct |
| 37 | { |
| 38 | blip_u64 factor; |
| 39 | blip_resampled_time_t offset; |
| 40 | blip_buf_t_* buffer; |
| 41 | blip_long buffer_size; |
| 42 | blip_long reader_accum; |
| 43 | int bass_shift; |
| 44 | long sample_rate; |
| 45 | long clock_rate; |
| 46 | int bass_freq; |
| 47 | int length; |
| 48 | int modified; |
| 49 | } Blip_Buffer; |
| 50 | |
| 51 | void Blip_Buffer_init(Blip_Buffer* bbuf); |
| 52 | void Blip_Buffer_deinit(Blip_Buffer* bbuf); |
| 53 | |
| 54 | /* Set output sample rate and buffer length in milliseconds (1/1000 sec, defaults |
| 55 | * to 1/4 second), then clear buffer. Returns NULL on success, otherwise if there |
| 56 | * isn't enough memory, returns error without affecting current buffer setup. |
| 57 | */ |
| 58 | blargg_err_t Blip_Buffer_set_sample_rate(Blip_Buffer* bbuf, |
| 59 | long samples_per_sec, int msec_length /*= 1000 / 4*/); |
| 60 | |
| 61 | /* End current time frame of specified duration and make its samples available |
| 62 | * (along with any still-unread samples) for reading with read_samples(). Begins |
| 63 | * a new time frame at the end of the current frame. |
| 64 | */ |
| 65 | void Blip_Buffer_end_frame(Blip_Buffer* bbuf, blip_time_t time); |
| 66 | |
| 67 | /* Read at most 'max_samples' out of buffer into 'dest', removing them from from |
| 68 | * the buffer. Returns number of samples actually read and removed. If stereo is |
| 69 | * true, increments 'dest' one extra time after writing each sample, to allow |
| 70 | * easy interleving of two channels into a stereo output buffer. |
| 71 | */ |
| 72 | long Blip_Buffer_read_samples(Blip_Buffer* bbuf, blip_sample_t* dest, |
| 73 | long max_samples); |
| 74 | |
| 75 | /* Additional optional features */ |
| 76 | |
| 77 | /* Set frequency high-pass filter frequency, where higher values reduce bass more */ |
| 78 | void Blip_Buffer_bass_freq(Blip_Buffer* bbuf, int frequency); |
| 79 | |
| 80 | /* Remove all available samples and clear buffer to silence. If 'entire_buffer' is |
| 81 | * false, just clears out any samples waiting rather than the entire buffer. |
| 82 | */ |
| 83 | void Blip_Buffer_clear(Blip_Buffer* bbuf, int entire_buffer /*= 1*/); |
| 84 | |
| 85 | /* Number of samples available for reading with read_samples() */ |
| 86 | static INLINE long Blip_Buffer_samples_avail(Blip_Buffer* bbuf); |
| 87 | |
| 88 | /* Remove 'count' samples from those waiting to be read */ |
| 89 | void Blip_Buffer_remove_samples(Blip_Buffer* bbuf, long count); |
| 90 | |
| 91 | /* Experimental features */ |
| 92 | |
| 93 | /* Number of raw samples that can be mixed within frame of specified duration. */ |
| 94 | long Blip_Buffer_count_samples(Blip_Buffer* bbuf, blip_time_t duration); |
| 95 | |
| 96 | /* Mix 'count' samples from 'buf' into buffer. */ |
| 97 | void Blip_Buffer_mix_samples(Blip_Buffer* bbuf, blip_sample_t const* buf, |
| 98 | long count); |
| 99 | |
| 100 | void Blip_Buffer_remove_silence(Blip_Buffer* bbuf, long count); |
| 101 | |
| 102 | static INLINE blip_resampled_time_t Blip_Buffer_resampled_time( |
| 103 | Blip_Buffer* bbuf, blip_time_t t) |
| 104 | { |
| 105 | return t * bbuf->factor + bbuf->offset; |
| 106 | } |
| 107 | blip_resampled_time_t Blip_Buffer_clock_rate_factor(Blip_Buffer* bbuf, |
| 108 | long clock_rate); |
| 109 | |
| 110 | |
| 111 | #define BLIP_BUFFER_ACCURACY 32 |
| 112 | #define BLIP_PHASE_BITS 8 |
| 113 | |
| 114 | /* Number of bits in resample ratio fraction. Higher values give a more accurate ratio |
| 115 | * but reduce maximum buffer size. */ |
| 116 | |
| 117 | /* Number bits in phase offset. Fewer than 6 bits (64 phase offsets) results in |
| 118 | * noticeable broadband noise when synthesizing high frequency square waves. |
| 119 | * Affects size of Blip_Synth objects since they store the waveform directly. |
| 120 | */ |
| 121 | |
| 122 | |
| 123 | /* Internal */ |
| 124 | #define blip_widest_impulse_ 16 |
| 125 | #define blip_buffer_extra_ (blip_widest_impulse_ + 2) |
| 126 | #define blip_res (1 << BLIP_PHASE_BITS) |
| 127 | #define blip_sample_bits 30 |
| 128 | |
| 129 | /* Range specifies the greatest expected change in amplitude. Calculate it |
| 130 | * by finding the difference between the maximum and minimum expected |
| 131 | * amplitudes (max - min). |
| 132 | */ |
| 133 | |
| 134 | |
| 135 | typedef struct |
| 136 | { |
| 137 | int delta_factor; |
| 138 | } Blip_Synth; |
| 139 | |
| 140 | static INLINE void Blip_Synth_set_volume(Blip_Synth* synth, double v, int range) |
| 141 | { |
| 142 | synth->delta_factor = ((v * (1.0 / (range < 0 ? -range : range))) * (1L << blip_sample_bits) + 0.5); |
| 143 | } |
| 144 | /* Works directly in terms of fractional output samples. Contact author for more info. */ |
| 145 | static INLINE void Blip_Synth_offset_resampled(Blip_Synth* synth, blip_resampled_time_t, |
| 146 | int delta, Blip_Buffer*); |
| 147 | |
| 148 | /* Add an amplitude transition of specified delta, optionally into specified buffer |
| 149 | * rather than the one set with output(). Delta can be positive or negative. |
| 150 | * The actual change in amplitude is delta * (volume / range) |
| 151 | */ |
| 152 | |
| 153 | static INLINE void Blip_Synth_offset(Blip_Synth* synth, blip_time_t t, int delta, |
| 154 | Blip_Buffer* buf) |
| 155 | { |
| 156 | Blip_Synth_offset_resampled(synth, t * buf->factor + buf->offset, delta, buf); |
| 157 | } |
| 158 | |
| 159 | /* Optimized reading from Blip_Buffer, for use in custom sample output */ |
| 160 | |
| 161 | /* Begin reading from buffer. Name should be unique to the current block. */ |
| 162 | #define BLIP_READER_BEGIN( name, blip_buffer ) \ |
| 163 | const blip_buf_t_* name##_reader_buf = (blip_buffer).buffer;\ |
| 164 | blip_long name##_reader_accum = (blip_buffer).reader_accum |
| 165 | |
| 166 | /* Get value to pass to BLIP_READER_NEXT() */ |
| 167 | #define BLIP_READER_BASS( blip_buffer ) ((blip_buffer).bass_shift) |
| 168 | |
| 169 | /* Constant value to use instead of BLIP_READER_BASS(), for slightly more optimal |
| 170 | * code at the cost of having no bass control |
| 171 | */ |
| 172 | #define blip_reader_default_bass 9 |
| 173 | |
| 174 | /* Current sample */ |
| 175 | #define BLIP_READER_READ( name ) (name##_reader_accum >> (blip_sample_bits - 16)) |
| 176 | |
| 177 | /* Current raw sample in full internal resolution */ |
| 178 | #define BLIP_READER_READ_RAW( name ) (name##_reader_accum) |
| 179 | |
| 180 | /* Advance to next sample */ |
| 181 | #define BLIP_READER_NEXT( name, bass ) \ |
| 182 | (void) (name##_reader_accum += *name##_reader_buf++ - (name##_reader_accum >> (bass))) |
| 183 | |
| 184 | /* End reading samples from buffer. The number of samples read must now be removed |
| 185 | * using Blip_Buffer_remove_samples(). */ |
| 186 | #define BLIP_READER_END( name, blip_buffer ) \ |
| 187 | (void) ((blip_buffer).reader_accum = name##_reader_accum) |
| 188 | |
| 189 | /* End of public interface */ |
| 190 | |
| 191 | static INLINE void Blip_Synth_offset_resampled( |
| 192 | Blip_Synth* synth, |
| 193 | blip_resampled_time_t time, |
| 194 | int delta, Blip_Buffer* blip_buf) |
| 195 | { |
| 196 | int phase; |
| 197 | blip_long* buf, left, right; |
| 198 | |
| 199 | delta *= synth->delta_factor; |
| 200 | buf = blip_buf->buffer + (time >> BLIP_BUFFER_ACCURACY); |
| 201 | phase = (int)(time >> (BLIP_BUFFER_ACCURACY - BLIP_PHASE_BITS) & |
| 202 | (blip_res - 1)); |
| 203 | |
| 204 | left = buf [0] + delta; |
| 205 | |
| 206 | /* Kind of crappy, but doing shift after multiply results in overflow. |
| 207 | * Alternate way of delaying multiply by delta_factor results in worse |
| 208 | * sub-sample resolution. |
| 209 | */ |
| 210 | right = (delta >> BLIP_PHASE_BITS) * phase; |
| 211 | left -= right; |
| 212 | right += buf [1]; |
| 213 | |
| 214 | buf [0] = left; |
| 215 | buf [1] = right; |
| 216 | } |
| 217 | |
| 218 | static INLINE long Blip_Buffer_samples_avail(Blip_Buffer* bbuf) |
| 219 | { |
| 220 | return (long)(bbuf->offset >> BLIP_BUFFER_ACCURACY); |
| 221 | } |
| 222 | |
| 223 | static INLINE void Blip_Buffer_set_clock_rate(Blip_Buffer* bbuf, long cps) |
| 224 | { |
| 225 | bbuf->factor = Blip_Buffer_clock_rate_factor(bbuf, bbuf->clock_rate = cps); |
| 226 | } |
| 227 | |
| 228 | #ifdef __cplusplus |
| 229 | } |
| 230 | #endif |
| 231 | |
| 232 | #endif |