Debian Codegraph @

android/app/jni/extern/blip_buf/blip_buf.c

raw
1 /* blip_buf $vers. http://www.slack.net/~ant/ */
2
3 #include "blip_buf.h"
4
5 #include <assert.h>
6 #include <limits.h>
7 #include <string.h>
8 #include <stdlib.h>
9
10 /* Library Copyright (C) 2003-2009 Shay Green. This library is free software;
11 you can redistribute it and/or modify it under the terms of the GNU Lesser
12 General Public License as published by the Free Software Foundation; either
13 version 2.1 of the License, or (at your option) any later version. This
14 library is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
17 details. You should have received a copy of the GNU Lesser General Public
18 License along with this module; if not, write to the Free Software Foundation,
19 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */
20
21 #if defined (BLARGG_TEST) && BLARGG_TEST
22 #include "blargg_test.h"
23 #endif
24
25 /* Equivalent to ULONG_MAX >= 0xFFFFFFFF00000000.
26 Avoids constants that don't fit in 32 bits. */
27 #if ULONG_MAX/0xFFFFFFFF > 0xFFFFFFFF
28 typedef unsigned long fixed_t;
29 enum { pre_shift = 32 };
30
31 #elif defined(ULLONG_MAX)
32 typedef unsigned long long fixed_t;
33 enum { pre_shift = 32 };
34
35 #else
36 typedef unsigned fixed_t;
37 enum { pre_shift = 0 };
38
39 #endif
40
41 enum { time_bits = pre_shift + 20 };
42
43 static fixed_t const time_unit = (fixed_t) 1 << time_bits;
44
45 enum { bass_shift = 9 }; /* affects high-pass filter breakpoint frequency */
46 enum { end_frame_extra = 2 }; /* allows deltas slightly after frame length */
47
48 enum { half_width = 8 };
49 enum { buf_extra = half_width*2 + end_frame_extra };
50 enum { phase_bits = 5 };
51 enum { phase_count = 1 << phase_bits };
52 enum { delta_bits = 15 };
53 enum { delta_unit = 1 << delta_bits };
54 enum { frac_bits = time_bits - pre_shift };
55
56 /* We could eliminate avail and encode whole samples in offset, but that would
57 limit the total buffered samples to blip_max_frame. That could only be
58 increased by decreasing time_bits, which would reduce resample ratio accuracy.
59 */
60
61 /** Sample buffer that resamples to output rate and accumulates samples
62 until they're read out */
63 struct blip_t
64 {
65 fixed_t factor;
66 fixed_t offset;
67 int avail;
68 int size;
69 int integrator;
70 unsigned char hipass;
71 };
72
73 typedef int buf_t;
74
75 /* probably not totally portable */
76 #define SAMPLES( buf ) ((buf_t*) ((buf) + 1))
77
78 /* Arithmetic (sign-preserving) right shift */
79 #define ARITH_SHIFT( n, shift ) \
80 ((n) >> (shift))
81
82 enum { max_sample = +32767 };
83 enum { min_sample = -32768 };
84
85 #define CLAMP( n ) \
86 {\
87 if ( (short) n != n )\
88 n = ARITH_SHIFT( n, 16 ) ^ max_sample;\
89 }
90
91 static void check_assumptions( void )
92 {
93 int n;
94
95 #if INT_MAX < 0x7FFFFFFF || UINT_MAX < 0xFFFFFFFF
96 #error "int must be at least 32 bits"
97 #endif
98
99 assert( (-3 >> 1) == -2 ); /* right shift must preserve sign */
100
101 n = max_sample * 2;
102 CLAMP( n );
103 assert( n == max_sample );
104
105 n = min_sample * 2;
106 CLAMP( n );
107 assert( n == min_sample );
108
109 assert( blip_max_ratio <= time_unit );
110 assert( blip_max_frame <= (fixed_t) -1 >> time_bits );
111 }
112
113 blip_t* blip_new( int size )
114 {
115 blip_t* m;
116 assert( size >= 0 );
117
118 m = (blip_t*) malloc( sizeof *m + (size + buf_extra) * sizeof (buf_t) );
119 if ( m )
120 {
121 m->factor = time_unit / blip_max_ratio;
122 m->size = size;
123 m->hipass = 1;
124 blip_clear( m );
125 check_assumptions();
126 }
127 return m;
128 }
129
130 void blip_delete( blip_t* m )
131 {
132 if ( m != NULL )
133 {
134 /* Clear fields in case user tries to use after freeing */
135 memset( m, 0, sizeof *m );
136 free( m );
137 }
138 }
139
140 void blip_set_dc( blip_t* m, unsigned char enable ) {
141 m->hipass=enable;
142 }
143
144 void blip_set_rates( blip_t* m, double clock_rate, double sample_rate )
145 {
146 double factor = time_unit * sample_rate / clock_rate;
147 m->factor = (fixed_t) factor;
148
149 /* Fails if clock_rate exceeds maximum, relative to sample_rate */
150 assert( 0 <= factor - m->factor && factor - m->factor < 1 );
151
152 /* Avoid requiring math.h. Equivalent to
153 m->factor = (int) ceil( factor ) */
154 if ( m->factor < factor )
155 m->factor++;
156
157 /* At this point, factor is most likely rounded up, but could still
158 have been rounded down in the floating-point calculation. */
159 }
160
161 void blip_clear( blip_t* m )
162 {
163 /* We could set offset to 0, factor/2, or factor-1. 0 is suitable if
164 factor is rounded up. factor-1 is suitable if factor is rounded down.
165 Since we don't know rounding direction, factor/2 accommodates either,
166 with the slight loss of showing an error in half the time. Since for
167 a 64-bit factor this is years, the halving isn't a problem. */
168
169 m->offset = m->factor / 2;
170 m->avail = 0;
171 m->integrator = 0;
172 memset( SAMPLES( m ), 0, (m->size + buf_extra) * sizeof (buf_t) );
173 }
174
175 int blip_clocks_needed( const blip_t* m, int samples )
176 {
177 fixed_t needed;
178
179 /* Fails if buffer can't hold that many more samples */
180 assert( samples >= 0 && m->avail + samples <= m->size );
181
182 needed = (fixed_t) samples * time_unit;
183 if ( needed < m->offset )
184 return 0;
185
186 return (needed - m->offset + m->factor - 1) / m->factor;
187 }
188
189 void blip_end_frame( blip_t* m, unsigned t )
190 {
191 fixed_t off = t * m->factor + m->offset;
192 m->avail += off >> time_bits;
193 m->offset = off & (time_unit - 1);
194
195 /* Fails if buffer size was exceeded */
196 assert( m->avail <= m->size );
197 }
198
199 int blip_samples_avail( const blip_t* m )
200 {
201 return m->avail;
202 }
203
204 static void remove_samples( blip_t* m, int count )
205 {
206 buf_t* buf = SAMPLES( m );
207 int remain = m->avail + buf_extra - count;
208 m->avail -= count;
209
210 memmove( &buf [0], &buf [count], remain * sizeof buf [0] );
211 memset( &buf [remain], 0, count * sizeof buf [0] );
212 }
213
214 int blip_read_samples( blip_t* m, short out [], int count, int stereo )
215 {
216 assert( count >= 0 );
217
218 if ( count > m->avail )
219 count = m->avail;
220
221 if ( count )
222 {
223 int const step = stereo ? 2 : 1;
224 buf_t const* in = SAMPLES( m );
225 buf_t const* end = in + count;
226 int sum = m->integrator;
227 do
228 {
229 /* Eliminate fraction */
230 int s = ARITH_SHIFT( sum, delta_bits );
231
232 sum += *in++;
233
234 CLAMP( s );
235
236 *out = s;
237 out += step;
238
239 /* High-pass filter */
240 if (m->hipass) {
241 sum -= s << (delta_bits - bass_shift);
242 }
243 }
244 while ( in != end );
245 m->integrator = sum;
246
247 remove_samples( m, count );
248 }
249
250 return count;
251 }
252
253 /* Things that didn't help performance on x86:
254 __attribute__((aligned(128)))
255 #define short int
256 restrict
257 */
258
259 /* Sinc_Generator( 0.9, 0.55, 4.5 ) */
260 static short const bl_step [phase_count + 1] [half_width] =
261 {
262 { 43, -115, 350, -488, 1136, -914, 5861,21022},
263 { 44, -118, 348, -473, 1076, -799, 5274,21001},
264 { 45, -121, 344, -454, 1011, -677, 4706,20936},
265 { 46, -122, 336, -431, 942, -549, 4156,20829},
266 { 47, -123, 327, -404, 868, -418, 3629,20679},
267 { 47, -122, 316, -375, 792, -285, 3124,20488},
268 { 47, -120, 303, -344, 714, -151, 2644,20256},
269 { 46, -117, 289, -310, 634, -17, 2188,19985},
270 { 46, -114, 273, -275, 553, 117, 1758,19675},
271 { 44, -108, 255, -237, 471, 247, 1356,19327},
272 { 43, -103, 237, -199, 390, 373, 981,18944},
273 { 42, -98, 218, -160, 310, 495, 633,18527},
274 { 40, -91, 198, -121, 231, 611, 314,18078},
275 { 38, -84, 178, -81, 153, 722, 22,17599},
276 { 36, -76, 157, -43, 80, 824, -241,17092},
277 { 34, -68, 135, -3, 8, 919, -476,16558},
278 { 32, -61, 115, 34, -60, 1006, -683,16001},
279 { 29, -52, 94, 70, -123, 1083, -862,15422},
280 { 27, -44, 73, 106, -184, 1152,-1015,14824},
281 { 25, -36, 53, 139, -239, 1211,-1142,14210},
282 { 22, -27, 34, 170, -290, 1261,-1244,13582},
283 { 20, -20, 16, 199, -335, 1301,-1322,12942},
284 { 18, -12, -3, 226, -375, 1331,-1376,12293},
285 { 15, -4, -19, 250, -410, 1351,-1408,11638},
286 { 13, 3, -35, 272, -439, 1361,-1419,10979},
287 { 11, 9, -49, 292, -464, 1362,-1410,10319},
288 { 9, 16, -63, 309, -483, 1354,-1383, 9660},
289 { 7, 22, -75, 322, -496, 1337,-1339, 9005},
290 { 6, 26, -85, 333, -504, 1312,-1280, 8355},
291 { 4, 31, -94, 341, -507, 1278,-1205, 7713},
292 { 3, 35, -102, 347, -506, 1238,-1119, 7082},
293 { 1, 40, -110, 350, -499, 1190,-1021, 6464},
294 { 0, 43, -115, 350, -488, 1136, -914, 5861}
295 };
296
297 /* Shifting by pre_shift allows calculation using unsigned int rather than
298 possibly-wider fixed_t. On 32-bit platforms, this is likely more efficient.
299 And by having pre_shift 32, a 32-bit platform can easily do the shift by
300 simply ignoring the low half. */
301
302 void blip_add_delta_slow( blip_t* m, unsigned time, int delta )
303 {
304 unsigned fixed = (unsigned) ((time * m->factor + m->offset) >> pre_shift);
305 buf_t* out = SAMPLES( m ) + m->avail + (fixed >> frac_bits);
306
307 int const phase_shift = frac_bits - phase_bits;
308 int phase = fixed >> phase_shift & (phase_count - 1);
309 short const* in = bl_step [phase];
310 short const* rev = bl_step [phase_count - phase];
311
312 int interp = fixed >> (phase_shift - delta_bits) & (delta_unit - 1);
313 int delta2 = (delta * interp) >> delta_bits;
314 delta -= delta2;
315
316 /* Fails if buffer size was exceeded */
317 assert( out <= &SAMPLES( m ) [m->size + end_frame_extra] );
318
319 out [0] += in[0]*delta + in[half_width+0]*delta2;
320 out [1] += in[1]*delta + in[half_width+1]*delta2;
321 out [2] += in[2]*delta + in[half_width+2]*delta2;
322 out [3] += in[3]*delta + in[half_width+3]*delta2;
323 out [4] += in[4]*delta + in[half_width+4]*delta2;
324 out [5] += in[5]*delta + in[half_width+5]*delta2;
325 out [6] += in[6]*delta + in[half_width+6]*delta2;
326 out [7] += in[7]*delta + in[half_width+7]*delta2;
327
328 in = rev;
329 out [ 8] += in[7]*delta + in[7-half_width]*delta2;
330 out [ 9] += in[6]*delta + in[6-half_width]*delta2;
331 out [10] += in[5]*delta + in[5-half_width]*delta2;
332 out [11] += in[4]*delta + in[4-half_width]*delta2;
333 out [12] += in[3]*delta + in[3-half_width]*delta2;
334 out [13] += in[2]*delta + in[2-half_width]*delta2;
335 out [14] += in[1]*delta + in[1-half_width]*delta2;
336 out [15] += in[0]*delta + in[0-half_width]*delta2;
337 }
338
339 void blip_add_delta_fast( blip_t* m, unsigned time, int delta )
340 {
341 unsigned fixed = (unsigned) ((time * m->factor + m->offset) >> pre_shift);
342 buf_t* out = SAMPLES( m ) + m->avail + (fixed >> frac_bits);
343
344 int interp = fixed >> (frac_bits - delta_bits) & (delta_unit - 1);
345 int delta2 = delta * interp;
346
347 /* Fails if buffer size was exceeded */
348 assert( out <= &SAMPLES( m ) [m->size + end_frame_extra] );
349
350 out [7] += delta * delta_unit - delta2;
351 out [8] += delta2;
352 }
353
354 /* (tildearrow) ability to change blip_add_delta at runtime */
355 void (*blip_add_delta)( blip_t*, unsigned int, int )=blip_add_delta_slow;