| 1 | // Blip_Buffer 0.4.1. http://www.slack.net/~ant/cxx . . $ `<file>/mednafen/sound/Blip_Buffer.c`/ |
| 2 | |
| 3 | #include "../include/blip/Blip_Buffer.h"cxx . . $ `<file>/mednafen/include/blip/Blip_Buffer.h`/ |
| 4 | |
| 5 | #include <limits.h> |
| 6 | #include <string.h> |
| 7 | #include <stdlib.h> |
| 8 | #include <math.h> |
| 9 | |
| 10 | #ifndef ULLONG_MAXcxx . . $ `<external>/67164224788392828/limits.h:146:10`! |
| 11 | #define ULLONG_MAX 18446744073709551615ULL |
| 12 | #endif |
| 13 | |
| 14 | /* Copyright (C) 2003-2006 Shay Green. This module is free software; you |
| 15 | can redistribute it and/or modify it under the terms of the GNU Lesser |
| 16 | General Public License as published by the Free Software Foundation; either |
| 17 | version 2.1 of the License, or (at your option) any later version. This |
| 18 | module is distributed in the hope that it will be useful, but WITHOUT ANY |
| 19 | WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 20 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
| 21 | details. You should have received a copy of the GNU Lesser General Public |
| 22 | License along with this module; if not, write to the Free Software Foundation, |
| 23 | Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ |
| 24 | |
| 25 | #ifdef BLARGG_ENABLE_OPTIMIZER |
| 26 | #include BLARGG_ENABLE_OPTIMIZER |
| 27 | #endif |
| 28 | |
| 29 | void Blip_Buffer_initcxx . . $ Blip_Buffer_init(e49e6e859576dfef).(Blip_Buffercxx . . $ Blip_Buffer#* bbuf) |
| 30 | { |
| 31 | bbuf->factorcxx . . $ $anonymous_type_9fbbe4a08064f381_1#factor. = (blip_u64cxx . . $ blip_u64#)ULLONG_MAXcxx . . $ `<external>/67164224788392828/limits.h:146:10`!; |
| 32 | bbuf->offsetcxx . . $ $anonymous_type_9fbbe4a08064f381_1#offset. = 0; |
| 33 | bbuf->buffercxx . . $ $anonymous_type_9fbbe4a08064f381_1#buffer. = 0; |
| 34 | bbuf->buffer_sizecxx . . $ $anonymous_type_9fbbe4a08064f381_1#buffer_size. = 0; |
| 35 | bbuf->sample_ratecxx . . $ $anonymous_type_9fbbe4a08064f381_1#sample_rate. = 0; |
| 36 | bbuf->reader_accumcxx . . $ $anonymous_type_9fbbe4a08064f381_1#reader_accum. = 0; |
| 37 | bbuf->bass_shiftcxx . . $ $anonymous_type_9fbbe4a08064f381_1#bass_shift. = 0; |
| 38 | bbuf->clock_ratecxx . . $ $anonymous_type_9fbbe4a08064f381_1#clock_rate. = 0; |
| 39 | bbuf->bass_freqcxx . . $ $anonymous_type_9fbbe4a08064f381_1#bass_freq. = 16; |
| 40 | bbuf->lengthcxx . . $ $anonymous_type_9fbbe4a08064f381_1#length. = 0; |
| 41 | } |
| 42 | |
| 43 | void Blip_Buffer_deinitcxx . . $ Blip_Buffer_deinit(e49e6e859576dfef).(Blip_Buffercxx . . $ Blip_Buffer#* bbuf) |
| 44 | { |
| 45 | if (bbuf->buffercxx . . $ $anonymous_type_9fbbe4a08064f381_1#buffer.) |
| 46 | freecxx . . $ free(5d22bdacc48458e8).(bbuf->buffercxx . . $ $anonymous_type_9fbbe4a08064f381_1#buffer.); |
| 47 | } |
| 48 | |
| 49 | void Blip_Buffer_clearcxx . . $ Blip_Buffer_clear(f1fd343ab937da1b).(Blip_Buffercxx . . $ Blip_Buffer#* bbuf, int entire_buffer) |
| 50 | { |
| 51 | bbuf->offsetcxx . . $ $anonymous_type_9fbbe4a08064f381_1#offset. = 0; |
| 52 | bbuf->reader_accumcxx . . $ $anonymous_type_9fbbe4a08064f381_1#reader_accum. = 0; |
| 53 | bbuf->modifiedcxx . . $ $anonymous_type_9fbbe4a08064f381_1#modified. = 0; |
| 54 | if (bbuf->buffercxx . . $ $anonymous_type_9fbbe4a08064f381_1#buffer.) |
| 55 | { |
| 56 | long count = (entire_buffer ? bbuf->buffer_sizecxx . . $ $anonymous_type_9fbbe4a08064f381_1#buffer_size. : Blip_Buffer_samples_availcxx . . $ Blip_Buffer_samples_avail(834ca95a49b29351).(bbuf)); |
| 57 | memsetcxx . . $ memset(d1de1e4fb215f1c6).(bbuf->buffercxx . . $ $anonymous_type_9fbbe4a08064f381_1#buffer., 0, (count + blip_buffer_extra_cxx . . $ `mednafen/include/blip/Blip_Buffer.h:125:9`!) * sizeof(blip_buf_t_cxx . . $ blip_buf_t_#)); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | blargg_err_tcxx . . $ blargg_err_t# Blip_Buffer_set_sample_ratecxx . . $ Blip_Buffer_set_sample_rate(33010d6d4f65ccd3).(Blip_Buffercxx . . $ Blip_Buffer#* bbuf, long new_rate, int msec) |
| 62 | { |
| 63 | // start with maximum length that resampled time can represent |
| 64 | blip_s64cxx . . $ blip_s64# new_size = (ULLONG_MAXcxx . . $ `<external>/67164224788392828/limits.h:146:10`! >> BLIP_BUFFER_ACCURACYcxx . . $ `mednafen/include/blip/Blip_Buffer.h:111:9`!) - blip_buffer_extra_cxx . . $ `mednafen/include/blip/Blip_Buffer.h:125:9`! - 64; |
| 65 | |
| 66 | // simple safety check, since code elsewhere may not be safe for sizes approaching (2 ^ 31). |
| 67 | if (new_size > ((1LL << 30) - 1)) |
| 68 | new_size = (1LL << 30) - 1; |
| 69 | |
| 70 | if (msec != 0) |
| 71 | { |
| 72 | blip_s64cxx . . $ blip_s64# s = ((blip_s64cxx . . $ blip_s64#)new_rate * (msec + 1) + 999) / 1000; |
| 73 | if (s < new_size) |
| 74 | new_size = s; |
| 75 | } |
| 76 | |
| 77 | if (bbuf->buffer_sizecxx . . $ $anonymous_type_9fbbe4a08064f381_1#buffer_size. != new_size) |
| 78 | { |
| 79 | void* p = realloccxx . . $ realloc(417ab160f6baadc6).(bbuf->buffercxx . . $ $anonymous_type_9fbbe4a08064f381_1#buffer., (new_size + blip_buffer_extra_cxx . . $ `mednafen/include/blip/Blip_Buffer.h:125:9`!) * sizeof (bbuf->buffercxx . . $ $anonymous_type_9fbbe4a08064f381_1#buffer.)); |
| 80 | if (!p) |
| 81 | return "Out of memory"; |
| 82 | |
| 83 | bbuf->buffercxx . . $ $anonymous_type_9fbbe4a08064f381_1#buffer. = (blip_buf_t_cxx . . $ blip_buf_t_#*) p; |
| 84 | } |
| 85 | |
| 86 | bbuf->buffer_sizecxx . . $ $anonymous_type_9fbbe4a08064f381_1#buffer_size. = new_size; |
| 87 | |
| 88 | // update things based on the sample rate |
| 89 | bbuf->sample_ratecxx . . $ $anonymous_type_9fbbe4a08064f381_1#sample_rate. = new_rate; |
| 90 | bbuf->lengthcxx . . $ $anonymous_type_9fbbe4a08064f381_1#length. = new_size * 1000 / new_rate - 1; |
| 91 | if (bbuf->clock_ratecxx . . $ $anonymous_type_9fbbe4a08064f381_1#clock_rate.) |
| 92 | Blip_Buffer_set_clock_ratecxx . . $ Blip_Buffer_set_clock_rate(d1345b21825f9f3a).(bbuf, bbuf->clock_ratecxx . . $ $anonymous_type_9fbbe4a08064f381_1#clock_rate.); |
| 93 | Blip_Buffer_bass_freqcxx . . $ Blip_Buffer_bass_freq(f1fd343ab937da1b).(bbuf, bbuf->bass_freqcxx . . $ $anonymous_type_9fbbe4a08064f381_1#bass_freq.); |
| 94 | |
| 95 | Blip_Buffer_clearcxx . . $ Blip_Buffer_clear(f1fd343ab937da1b).(bbuf, 1); |
| 96 | |
| 97 | return 0; // success |
| 98 | } |
| 99 | |
| 100 | blip_resampled_time_tcxx . . $ blip_resampled_time_t# Blip_Buffer_clock_rate_factorcxx . . $ Blip_Buffer_clock_rate_factor(f14257d8cb40dffc).(Blip_Buffercxx . . $ Blip_Buffer#* bbuf, |
| 101 | long rate) |
| 102 | { |
| 103 | double ratio = (double) bbuf->sample_ratecxx . . $ $anonymous_type_9fbbe4a08064f381_1#sample_rate. / rate; |
| 104 | blip_s64cxx . . $ blip_s64# factor = (blip_s64cxx . . $ blip_s64#) floorcxx . . $ floor(447c67c6676f75c3).(ratio * (1LL << BLIP_BUFFER_ACCURACYcxx . . $ `mednafen/include/blip/Blip_Buffer.h:111:9`!) + 0.5); |
| 105 | return (blip_resampled_time_tcxx . . $ blip_resampled_time_t#) factor; |
| 106 | } |
| 107 | |
| 108 | void Blip_Buffer_bass_freqcxx . . $ Blip_Buffer_bass_freq(f1fd343ab937da1b).(Blip_Buffercxx . . $ Blip_Buffer#* bbuf, int freq) |
| 109 | { |
| 110 | int shift = 31; |
| 111 | |
| 112 | bbuf->bass_freqcxx . . $ $anonymous_type_9fbbe4a08064f381_1#bass_freq. = freq; |
| 113 | if (freq > 0) |
| 114 | { |
| 115 | long f; |
| 116 | shift = 13; |
| 117 | f = (freq << 16) / bbuf->sample_ratecxx . . $ $anonymous_type_9fbbe4a08064f381_1#sample_rate.; |
| 118 | while ((f >>= 1) && --shift) { } |
| 119 | } |
| 120 | bbuf->bass_shiftcxx . . $ $anonymous_type_9fbbe4a08064f381_1#bass_shift. = shift; |
| 121 | } |
| 122 | |
| 123 | void Blip_Buffer_end_framecxx . . $ Blip_Buffer_end_frame(f1fd343ab937da1b).(Blip_Buffercxx . . $ Blip_Buffer#* bbuf, blip_time_tcxx . . $ blip_time_t# t) |
| 124 | { |
| 125 | bbuf->offsetcxx . . $ $anonymous_type_9fbbe4a08064f381_1#offset. += t * bbuf->factorcxx . . $ $anonymous_type_9fbbe4a08064f381_1#factor.; |
| 126 | } |
| 127 | |
| 128 | void Blip_Buffer_remove_silencecxx . . $ Blip_Buffer_remove_silence(d1345b21825f9f3a).(Blip_Buffercxx . . $ Blip_Buffer#* bbuf, long count) |
| 129 | { |
| 130 | bbuf->offsetcxx . . $ $anonymous_type_9fbbe4a08064f381_1#offset. -= (blip_resampled_time_tcxx . . $ blip_resampled_time_t#) count << BLIP_BUFFER_ACCURACYcxx . . $ `mednafen/include/blip/Blip_Buffer.h:111:9`!; |
| 131 | } |
| 132 | |
| 133 | long Blip_Buffer_count_samplescxx . . $ Blip_Buffer_count_samples(ce2306510376f86).(Blip_Buffercxx . . $ Blip_Buffer#* bbuf, blip_time_tcxx . . $ blip_time_t# t) |
| 134 | { |
| 135 | unsigned long last_sample = Blip_Buffer_resampled_timecxx . . $ Blip_Buffer_resampled_time(efae54d6144220f4).(bbuf, t) >> BLIP_BUFFER_ACCURACYcxx . . $ `mednafen/include/blip/Blip_Buffer.h:111:9`!; |
| 136 | unsigned long first_sample = bbuf->offsetcxx . . $ $anonymous_type_9fbbe4a08064f381_1#offset. >> BLIP_BUFFER_ACCURACYcxx . . $ `mednafen/include/blip/Blip_Buffer.h:111:9`!; |
| 137 | return (long)(last_sample - first_sample); |
| 138 | } |
| 139 | |
| 140 | void Blip_Buffer_remove_samplescxx . . $ Blip_Buffer_remove_samples(d1345b21825f9f3a).(Blip_Buffercxx . . $ Blip_Buffer#* bbuf, long count) |
| 141 | { |
| 142 | if (count) |
| 143 | { |
| 144 | long remain; |
| 145 | Blip_Buffer_remove_silencecxx . . $ Blip_Buffer_remove_silence(d1345b21825f9f3a).(bbuf, count); |
| 146 | |
| 147 | // copy remaining samples to beginning and clear old samples |
| 148 | remain = Blip_Buffer_samples_availcxx . . $ Blip_Buffer_samples_avail(834ca95a49b29351).(bbuf) + blip_buffer_extra_cxx . . $ `mednafen/include/blip/Blip_Buffer.h:125:9`!; |
| 149 | memmovecxx . . $ memmove(cc817cb175f282d).(bbuf->buffercxx . . $ $anonymous_type_9fbbe4a08064f381_1#buffer., bbuf->buffercxx . . $ $anonymous_type_9fbbe4a08064f381_1#buffer. + count, remain * sizeof(bbuf->buffercxx . . $ $anonymous_type_9fbbe4a08064f381_1#buffer.)); |
| 150 | memsetcxx . . $ memset(d1de1e4fb215f1c6).(bbuf->buffercxx . . $ $anonymous_type_9fbbe4a08064f381_1#buffer. + remain, 0, count * sizeof(bbuf->buffercxx . . $ $anonymous_type_9fbbe4a08064f381_1#buffer.)); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | long Blip_Buffer_read_samplescxx . . $ Blip_Buffer_read_samples(3914bcd8cd4abe38).(Blip_Buffercxx . . $ Blip_Buffer#* bbuf, blip_sample_tcxx . . $ blip_sample_t#* out, |
| 155 | long max_samples) |
| 156 | { |
| 157 | long count = Blip_Buffer_samples_availcxx . . $ Blip_Buffer_samples_avail(834ca95a49b29351).(bbuf); |
| 158 | if (count > max_samples) |
| 159 | count = max_samples; |
| 160 | |
| 161 | if (count) |
| 162 | { |
| 163 | int const bass = BLIP_READER_BASScxx . . $ `mednafen/include/blip/Blip_Buffer.h:167:9`!(*bbuf); |
| 164 | BLIP_READER_BEGINcxx . . $ `mednafen/include/blip/Blip_Buffer.h:162:9`!(reader, *bbuf); |
| 165 | |
| 166 | blip_longcxx . . $ blip_long# n; |
| 167 | |
| 168 | for (n = count; n; --n) |
| 169 | { |
| 170 | blip_longcxx . . $ blip_long# s = BLIP_READER_READcxx . . $ `mednafen/include/blip/Blip_Buffer.h:175:9`!(reader); |
| 171 | if ((blip_sample_tcxx . . $ blip_sample_t#) s != s) |
| 172 | s = 0x7FFF - (s >> 24); |
| 173 | *out = (blip_sample_tcxx . . $ blip_sample_t#) s; |
| 174 | out += 2; |
| 175 | BLIP_READER_NEXTcxx . . $ `mednafen/include/blip/Blip_Buffer.h:181:9`!(reader, bass); |
| 176 | } |
| 177 | |
| 178 | BLIP_READER_ENDcxx . . $ `mednafen/include/blip/Blip_Buffer.h:186:9`!(reader, *bbuf); |
| 179 | Blip_Buffer_remove_samplescxx . . $ Blip_Buffer_remove_samples(d1345b21825f9f3a).(bbuf, count); |
| 180 | } |
| 181 | return count; |
| 182 | } |
| 183 | |
| 184 | void Blip_Buffer_mix_samplescxx . . $ Blip_Buffer_mix_samples(4cd0ea6bce341a79).(Blip_Buffercxx . . $ Blip_Buffer#* bbuf, blip_sample_tcxx . . $ blip_sample_t# const* in, long count) |
| 185 | { |
| 186 | blip_buf_t_cxx . . $ blip_buf_t_#* out = bbuf->buffercxx . . $ $anonymous_type_9fbbe4a08064f381_1#buffer. + (bbuf->offsetcxx . . $ $anonymous_type_9fbbe4a08064f381_1#offset. >> BLIP_BUFFER_ACCURACYcxx . . $ `mednafen/include/blip/Blip_Buffer.h:111:9`!) + blip_widest_impulse_cxx . . $ `mednafen/include/blip/Blip_Buffer.h:124:9`! / 2; |
| 187 | |
| 188 | int const sample_shift = blip_sample_bitscxx . . $ `mednafen/include/blip/Blip_Buffer.h:127:9`! - 16; |
| 189 | int prev = 0; |
| 190 | while (count--) |
| 191 | { |
| 192 | blip_longcxx . . $ blip_long# s = (blip_longcxx . . $ blip_long#) * in++ << sample_shift; |
| 193 | *out += s - prev; |
| 194 | prev = s; |
| 195 | ++out; |
| 196 | } |
| 197 | *out -= prev; |
| 198 | } |