| 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_Hcxx . . $ `mednafen/include/blip/Blip_Buffer.h:7:9`! |
| 8 | |
| 9 | #include <limits.h> |
| 10 | #include <stdint.h> |
| 11 | |
| 12 | #include <retro_inline.h>cxx . . $ `<file>/libretro-common/include/retro_inline.h`/ |
| 13 | |
| 14 | #ifdef __cplusplus |
| 15 | extern "C" { |
| 16 | #endif |
| 17 | |
| 18 | /* Internal */ |
| 19 | typedef int32_tcxx . . $ int32_t# blip_longcxx . . $ blip_long#; |
| 20 | typedef uint32_tcxx . . $ uint32_t# blip_ulongcxx . . $ blip_ulong#; |
| 21 | typedef int64_tcxx . . $ int64_t# blip_s64cxx . . $ blip_s64#; |
| 22 | typedef uint64_tcxx . . $ uint64_t# blip_u64cxx . . $ blip_u64#; |
| 23 | |
| 24 | /* Time unit at source clock rate */ |
| 25 | typedef blip_longcxx . . $ blip_long# blip_time_tcxx . . $ blip_time_t#; |
| 26 | |
| 27 | /* Output samples are 16-bit signed, with a range of -32768 to 32767 */ |
| 28 | typedef short blip_sample_tcxx . . $ blip_sample_t#; |
| 29 | enumcxx . . $ $anonymous_type_9fbbe4a08064f381_0# { blip_sample_maxcxx . . $ blip_sample_max. = 32767 }; |
| 30 | |
| 31 | typedef const char* blargg_err_tcxx . . $ blargg_err_t#; |
| 32 | typedef blip_u64cxx . . $ blip_u64# blip_resampled_time_tcxx . . $ blip_resampled_time_t#; |
| 33 | typedef blip_resampled_time_tcxx . . $ blip_resampled_time_t# resampled_time_tcxx . . $ resampled_time_t#; |
| 34 | typedef blip_time_tcxx . . $ blip_time_t# blip_buf_t_cxx . . $ blip_buf_t_#; |
| 35 | |
| 36 | typedef structcxx . . $ $anonymous_type_9fbbe4a08064f381_1# |
| 37 | { |
| 38 | blip_u64cxx . . $ blip_u64# factorcxx . . $ $anonymous_type_9fbbe4a08064f381_1#factor.; |
| 39 | blip_resampled_time_tcxx . . $ blip_resampled_time_t# offsetcxx . . $ $anonymous_type_9fbbe4a08064f381_1#offset.; |
| 40 | blip_buf_t_cxx . . $ blip_buf_t_#* buffercxx . . $ $anonymous_type_9fbbe4a08064f381_1#buffer.; |
| 41 | blip_longcxx . . $ blip_long# buffer_sizecxx . . $ $anonymous_type_9fbbe4a08064f381_1#buffer_size.; |
| 42 | blip_longcxx . . $ blip_long# reader_accumcxx . . $ $anonymous_type_9fbbe4a08064f381_1#reader_accum.; |
| 43 | int bass_shiftcxx . . $ $anonymous_type_9fbbe4a08064f381_1#bass_shift.; |
| 44 | long sample_ratecxx . . $ $anonymous_type_9fbbe4a08064f381_1#sample_rate.; |
| 45 | long clock_ratecxx . . $ $anonymous_type_9fbbe4a08064f381_1#clock_rate.; |
| 46 | int bass_freqcxx . . $ $anonymous_type_9fbbe4a08064f381_1#bass_freq.; |
| 47 | int lengthcxx . . $ $anonymous_type_9fbbe4a08064f381_1#length.; |
| 48 | int modifiedcxx . . $ $anonymous_type_9fbbe4a08064f381_1#modified.; |
| 49 | } Blip_Buffercxx . . $ Blip_Buffer#; |
| 50 | |
| 51 | void Blip_Buffer_initcxx . . $ Blip_Buffer_init(e49e6e859576dfef).(Blip_Buffercxx . . $ Blip_Buffer#* bbuf); |
| 52 | void Blip_Buffer_deinitcxx . . $ Blip_Buffer_deinit(e49e6e859576dfef).(Blip_Buffercxx . . $ 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_tcxx . . $ blargg_err_t# Blip_Buffer_set_sample_ratecxx . . $ Blip_Buffer_set_sample_rate(33010d6d4f65ccd3).(Blip_Buffercxx . . $ 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_framecxx . . $ Blip_Buffer_end_frame(f1fd343ab937da1b).(Blip_Buffercxx . . $ Blip_Buffer#* bbuf, blip_time_tcxx . . $ 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_samplescxx . . $ Blip_Buffer_read_samples(3914bcd8cd4abe38).(Blip_Buffercxx . . $ Blip_Buffer#* bbuf, blip_sample_tcxx . . $ 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_freqcxx . . $ Blip_Buffer_bass_freq(f1fd343ab937da1b).(Blip_Buffercxx . . $ 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_clearcxx . . $ Blip_Buffer_clear(f1fd343ab937da1b).(Blip_Buffercxx . . $ Blip_Buffer#* bbuf, int entire_buffer /*= 1*/); |
| 84 | |
| 85 | /* Number of samples available for reading with read_samples() */ |
| 86 | static INLINEcxx . . $ `<built-in>:421:9`! long Blip_Buffer_samples_availcxx . . $ Blip_Buffer_samples_avail(834ca95a49b29351).(Blip_Buffercxx . . $ Blip_Buffer#* bbuf); |
| 87 | |
| 88 | /* Remove 'count' samples from those waiting to be read */ |
| 89 | void Blip_Buffer_remove_samplescxx . . $ Blip_Buffer_remove_samples(d1345b21825f9f3a).(Blip_Buffercxx . . $ 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_samplescxx . . $ Blip_Buffer_count_samples(ce2306510376f86).(Blip_Buffercxx . . $ Blip_Buffer#* bbuf, blip_time_tcxx . . $ blip_time_t# duration); |
| 95 | |
| 96 | /* Mix 'count' samples from 'buf' into buffer. */ |
| 97 | void Blip_Buffer_mix_samplescxx . . $ Blip_Buffer_mix_samples(4cd0ea6bce341a79).(Blip_Buffercxx . . $ Blip_Buffer#* bbuf, blip_sample_tcxx . . $ blip_sample_t# const* buf, |
| 98 | long count); |
| 99 | |
| 100 | void Blip_Buffer_remove_silencecxx . . $ Blip_Buffer_remove_silence(d1345b21825f9f3a).(Blip_Buffercxx . . $ Blip_Buffer#* bbuf, long count); |
| 101 | |
| 102 | static INLINEcxx . . $ `<built-in>:421:9`! blip_resampled_time_tcxx . . $ blip_resampled_time_t# Blip_Buffer_resampled_timecxx . . $ Blip_Buffer_resampled_time(efae54d6144220f4).( |
| 103 | Blip_Buffercxx . . $ Blip_Buffer#* bbuf, blip_time_tcxx . . $ blip_time_t# t) |
| 104 | { |
| 105 | return t * bbuf->factorcxx . . $ $anonymous_type_9fbbe4a08064f381_1#factor. + bbuf->offsetcxx . . $ $anonymous_type_9fbbe4a08064f381_1#offset.; |
| 106 | } |
| 107 | blip_resampled_time_tcxx . . $ blip_resampled_time_t# Blip_Buffer_clock_rate_factorcxx . . $ Blip_Buffer_clock_rate_factor(f14257d8cb40dffc).(Blip_Buffercxx . . $ Blip_Buffer#* bbuf, |
| 108 | long clock_rate); |
| 109 | |
| 110 | |
| 111 | #define BLIP_BUFFER_ACCURACYcxx . . $ `mednafen/include/blip/Blip_Buffer.h:111:9`! 32 |
| 112 | #define BLIP_PHASE_BITScxx . . $ `mednafen/include/blip/Blip_Buffer.h:112:9`! 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_cxx . . $ `mednafen/include/blip/Blip_Buffer.h:124:9`! 16 |
| 125 | #define blip_buffer_extra_cxx . . $ `mednafen/include/blip/Blip_Buffer.h:125:9`! (blip_widest_impulse_cxx . . $ `mednafen/include/blip/Blip_Buffer.h:124:9`! + 2) |
| 126 | #define blip_rescxx . . $ `mednafen/include/blip/Blip_Buffer.h:126:9`! (1 << BLIP_PHASE_BITScxx . . $ `mednafen/include/blip/Blip_Buffer.h:112:9`!) |
| 127 | #define blip_sample_bitscxx . . $ `mednafen/include/blip/Blip_Buffer.h:127:9`! 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 structcxx . . $ $anonymous_type_9fbbe4a08064f381_2# |
| 136 | { |
| 137 | int delta_factorcxx . . $ $anonymous_type_9fbbe4a08064f381_2#delta_factor.; |
| 138 | } Blip_Synthcxx . . $ Blip_Synth#; |
| 139 | |
| 140 | static INLINEcxx . . $ `<built-in>:421:9`! void Blip_Synth_set_volumecxx . . $ Blip_Synth_set_volume(77258eebb41af760).(Blip_Synthcxx . . $ Blip_Synth#* synth, double v, int range) |
| 141 | { |
| 142 | synth->delta_factorcxx . . $ $anonymous_type_9fbbe4a08064f381_2#delta_factor. = ((v * (1.0 / (range < 0 ? -range : range))) * (1L << blip_sample_bitscxx . . $ `mednafen/include/blip/Blip_Buffer.h:127:9`!) + 0.5); |
| 143 | } |
| 144 | /* Works directly in terms of fractional output samples. Contact author for more info. */ |
| 145 | static INLINEcxx . . $ `<built-in>:421:9`! void Blip_Synth_offset_resampledcxx . . $ Blip_Synth_offset_resampled(3e19769f25418ec7).(Blip_Synthcxx . . $ Blip_Synth#* synth, blip_resampled_time_tcxx . . $ blip_resampled_time_t#, |
| 146 | int delta, Blip_Buffercxx . . $ 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 INLINEcxx . . $ `<built-in>:421:9`! void Blip_Synth_offsetcxx . . $ Blip_Synth_offset(274adf3324c166ac).(Blip_Synthcxx . . $ Blip_Synth#* synth, blip_time_tcxx . . $ blip_time_t# t, int delta, |
| 154 | Blip_Buffercxx . . $ Blip_Buffer#* buf) |
| 155 | { |
| 156 | Blip_Synth_offset_resampledcxx . . $ Blip_Synth_offset_resampled(3e19769f25418ec7).(synth, t * buf->factorcxx . . $ $anonymous_type_9fbbe4a08064f381_1#factor. + buf->offsetcxx . . $ $anonymous_type_9fbbe4a08064f381_1#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_BEGINcxx . . $ `mednafen/include/blip/Blip_Buffer.h:162:9`!( 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_BASScxx . . $ `mednafen/include/blip/Blip_Buffer.h:167:9`!( 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_basscxx . . $ `mednafen/include/blip/Blip_Buffer.h:172:9`! 9 |
| 173 | |
| 174 | /* Current sample */ |
| 175 | #define BLIP_READER_READcxx . . $ `mednafen/include/blip/Blip_Buffer.h:175:9`!( name ) (name##_reader_accum >> (blip_sample_bitscxx . . $ `mednafen/include/blip/Blip_Buffer.h:127:9`! - 16)) |
| 176 | |
| 177 | /* Current raw sample in full internal resolution */ |
| 178 | #define BLIP_READER_READ_RAWcxx . . $ `mednafen/include/blip/Blip_Buffer.h:178:9`!( name ) (name##_reader_accum) |
| 179 | |
| 180 | /* Advance to next sample */ |
| 181 | #define BLIP_READER_NEXTcxx . . $ `mednafen/include/blip/Blip_Buffer.h:181:9`!( 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_ENDcxx . . $ `mednafen/include/blip/Blip_Buffer.h:186:9`!( name, blip_buffer ) \ |
| 187 | (void) ((blip_buffer).reader_accum = name##_reader_accum) |
| 188 | |
| 189 | /* End of public interface */ |
| 190 | |
| 191 | static INLINEcxx . . $ `<built-in>:421:9`! void Blip_Synth_offset_resampledcxx . . $ Blip_Synth_offset_resampled(3e19769f25418ec7).( |
| 192 | Blip_Synthcxx . . $ Blip_Synth#* synth, |
| 193 | blip_resampled_time_tcxx . . $ blip_resampled_time_t# time, |
| 194 | int delta, Blip_Buffercxx . . $ Blip_Buffer#* blip_buf) |
| 195 | { |
| 196 | int phase; |
| 197 | blip_longcxx . . $ blip_long#* buf, left, right; |
| 198 | |
| 199 | delta *= synth->delta_factorcxx . . $ $anonymous_type_9fbbe4a08064f381_2#delta_factor.; |
| 200 | buf = blip_buf->buffercxx . . $ $anonymous_type_9fbbe4a08064f381_1#buffer. + (time >> BLIP_BUFFER_ACCURACYcxx . . $ `mednafen/include/blip/Blip_Buffer.h:111:9`!); |
| 201 | phase = (int)(time >> (BLIP_BUFFER_ACCURACYcxx . . $ `mednafen/include/blip/Blip_Buffer.h:111:9`! - BLIP_PHASE_BITScxx . . $ `mednafen/include/blip/Blip_Buffer.h:112:9`!) & |
| 202 | (blip_rescxx . . $ `mednafen/include/blip/Blip_Buffer.h:126:9`! - 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_BITScxx . . $ `mednafen/include/blip/Blip_Buffer.h:112:9`!) * phase; |
| 211 | left -= right; |
| 212 | right += buf [1]; |
| 213 | |
| 214 | buf [0] = left; |
| 215 | buf [1] = right; |
| 216 | } |
| 217 | |
| 218 | static INLINEcxx . . $ `<built-in>:421:9`! long Blip_Buffer_samples_availcxx . . $ Blip_Buffer_samples_avail(834ca95a49b29351).(Blip_Buffercxx . . $ Blip_Buffer#* bbuf) |
| 219 | { |
| 220 | return (long)(bbuf->offsetcxx . . $ $anonymous_type_9fbbe4a08064f381_1#offset. >> BLIP_BUFFER_ACCURACYcxx . . $ `mednafen/include/blip/Blip_Buffer.h:111:9`!); |
| 221 | } |
| 222 | |
| 223 | static INLINEcxx . . $ `<built-in>:421:9`! void Blip_Buffer_set_clock_ratecxx . . $ Blip_Buffer_set_clock_rate(d1345b21825f9f3a).(Blip_Buffercxx . . $ Blip_Buffer#* bbuf, long cps) |
| 224 | { |
| 225 | bbuf->factorcxx . . $ $anonymous_type_9fbbe4a08064f381_1#factor. = Blip_Buffer_clock_rate_factorcxx . . $ Blip_Buffer_clock_rate_factor(f14257d8cb40dffc).(bbuf, bbuf->clock_ratecxx . . $ $anonymous_type_9fbbe4a08064f381_1#clock_rate. = cps); |
| 226 | } |
| 227 | |
| 228 | #ifdef __cplusplus |
| 229 | } |
| 230 | #endif |
| 231 | |
| 232 | #endif |