1 /* blip_buf $vers. http://www.slack.net/~ant/                       */
2 
3 /* Modified for Genesis Plus GX by EkeEke                           */
4 /*  - disabled assertions checks (define #BLIP_ASSERT to re-enable) */
5 /*  - fixed multiple time-frames support & removed m->avail         */
6 /*  - added blip_mix_samples function (see blip_buf.h)              */
7 /*  - added stereo buffer support (define #BLIP_MONO to disable)    */
8 /*  - added inverted stereo output (define #BLIP_INVERT to enable)*/
9 
10 #include "blip_buf.h"
11 
12 #ifdef BLIP_ASSERT
13 #include <assert.h>
14 #endif
15 #include <limits.h>
16 #include <string.h>
17 #include <stdlib.h>
18 
19 /* Library Copyright (C) 2003-2009 Shay Green. This library is free software;
20 you can redistribute it and/or modify it under the terms of the GNU Lesser
21 General Public License as published by the Free Software Foundation; either
22 version 2.1 of the License, or (at your option) any later version. This
23 library is distributed in the hope that it will be useful, but WITHOUT ANY
24 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
25 A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
26 details. You should have received a copy of the GNU Lesser General Public
27 License along with this module; if not, write to the Free Software Foundation,
28 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */
29 
30 
31 #if defined (BLARGG_TEST) && BLARGG_TEST
32 	#include "blargg_test.h"
33 #endif
34 
35 /* Equivalent to ULONG_MAX >= 0xFFFFFFFF00000000.
36 Avoids constants that don't fit in 32 bits. */
37 #if ULONG_MAX/0xFFFFFFFF > 0xFFFFFFFF
38 	typedef unsigned long fixed_t;
39 	enum { pre_shift = 32 };
40 
41 #elif defined(ULLONG_MAX)
42 	typedef unsigned long long fixed_t;
43 	enum { pre_shift = 32 };
44 
45 #else
46 	typedef unsigned fixed_t;
47 	enum { pre_shift = 0 };
48 
49 #endif
50 
51 enum { time_bits = pre_shift + 20 };
52 
53 static fixed_t const time_unit = (fixed_t) 1 << time_bits;
54 
55 enum { bass_shift  = 9 }; /* affects high-pass filter breakpoint frequency */
56 enum { end_frame_extra = 2 }; /* allows deltas slightly after frame length */
57 
58 enum { half_width  = 8 };
59 enum { buf_extra   = half_width*2 + end_frame_extra };
60 enum { phase_bits  = 5 };
61 enum { phase_count = 1 << phase_bits };
62 enum { delta_bits  = 15 };
63 enum { delta_unit  = 1 << delta_bits };
64 enum { frac_bits = time_bits - pre_shift };
65 enum { phase_shift = frac_bits - phase_bits };
66 
67 /* We could eliminate avail and encode whole samples in offset, but that would
68 limit the total buffered samples to blip_max_frame. That could only be
69 increased by decreasing time_bits, which would reduce resample ratio accuracy.
70 */
71 
72 typedef int buf_t;
73 
74 struct blip_t
75 {
76 	fixed_t factor;
77 	fixed_t offset;
78 	int size;
79 #ifdef BLIP_MONO
80 	int integrator;
81 #else
82   int integrator[2];
83   buf_t* buffer[2];
84 #endif
85 };
86 
87 #define BLIP_BUFFER_STATE_BUFFER_SIZE 16
88 
89 typedef struct blip_buffer_state_t
90 {
91 	fixed_t offset;
92 #ifdef BLIP_MONO
93 	int integrator;
94 	buf_t buffer[BLIP_BUFFER_STATE_BUFFER_SIZE];
95 #else
96 	int integrator[2];
97 	buf_t buffer[2][BLIP_BUFFER_STATE_BUFFER_SIZE];
98 #endif
99 };
100 
101 #ifdef BLIP_MONO
102 /* probably not totally portable */
103 #define SAMPLES( blip ) ((buf_t*) ((blip) + 1))
104 #endif
105 
106 /* Arithmetic (sign-preserving) right shift */
107 #define ARITH_SHIFT( n, shift ) \
108 	((n) >> (shift))
109 
110 enum { max_sample = +32767 };
111 enum { min_sample = -32768 };
112 
113 #define CLAMP( n ) \
114 	{\
115 		if ( n > max_sample ) n = max_sample;\
116     else if ( n < min_sample) n = min_sample;\
117 	}
118 
119 #ifdef BLIP_ASSERT
check_assumptions(void)120 static void check_assumptions( void )
121 {
122 	int n;
123 
124 	#if INT_MAX < 0x7FFFFFFF || UINT_MAX < 0xFFFFFFFF
125 		#error "int must be at least 32 bits"
126 	#endif
127 
128 	assert( (-3 >> 1) == -2 ); /* right shift must preserve sign */
129 
130 	n = max_sample * 2;
131 	CLAMP( n );
132 	assert( n == max_sample );
133 
134 	n = min_sample * 2;
135 	CLAMP( n );
136 	assert( n == min_sample );
137 
138 	assert( blip_max_ratio <= time_unit );
139 	assert( blip_max_frame <= (fixed_t) -1 >> time_bits );
140 }
141 #endif
142 
blip_new(int size)143 blip_t* blip_new( int size )
144 {
145 	blip_t* m;
146 #ifdef BLIP_ASSERT
147 	assert( size >= 0 );
148 #endif
149 
150 #ifdef BLIP_MONO
151 	m = (blip_t*) malloc( sizeof *m + (size + buf_extra) * sizeof (buf_t) );
152 #else
153 	m = (blip_t*) malloc( sizeof *m );
154 #endif
155 
156 	if ( m )
157 	{
158 #ifndef BLIP_MONO
159     m->buffer[0] = (buf_t*) malloc( (size + buf_extra) * sizeof (buf_t));
160     m->buffer[1] = (buf_t*) malloc( (size + buf_extra) * sizeof (buf_t));
161     if ((m->buffer[0] == NULL) || (m->buffer[1] == NULL))
162     {
163       blip_delete(m);
164       return 0;
165     }
166 #endif
167 		m->factor = time_unit / blip_max_ratio;
168 		m->size   = size;
169 		blip_clear( m );
170 #ifdef BLIP_ASSERT
171 		check_assumptions();
172 #endif
173   }
174 	return m;
175 }
176 
blip_delete(blip_t * m)177 void blip_delete( blip_t* m )
178 {
179 	if ( m != NULL )
180 	{
181 #ifndef BLIP_MONO
182     if (m->buffer[0] != NULL)
183       free(m->buffer[0]);
184     if (m->buffer[1] != NULL)
185       free(m->buffer[1]);
186 #endif
187     /* Clear fields in case user tries to use after freeing */
188 		memset( m, 0, sizeof *m );
189 		free( m );
190 	}
191 }
192 
blip_set_rates(blip_t * m,double clock_rate,double sample_rate)193 void blip_set_rates( blip_t* m, double clock_rate, double sample_rate )
194 {
195 	double factor = time_unit * sample_rate / clock_rate;
196 	m->factor = (fixed_t) factor;
197 
198 #ifdef BLIP_ASSERT
199 	/* Fails if clock_rate exceeds maximum, relative to sample_rate */
200 	assert( 0 <= factor - m->factor && factor - m->factor < 1 );
201 #endif
202 
203 /* Avoid requiring math.h. Equivalent to
204 	m->factor = (int) ceil( factor ) */
205 	if ( m->factor < factor )
206 		m->factor++;
207 
208 	/* At this point, factor is most likely rounded up, but could still
209 	have been rounded down in the floating-point calculation. */
210 }
211 
blip_clear(blip_t * m)212 void blip_clear( blip_t* m )
213 {
214 	/* We could set offset to 0, factor/2, or factor-1. 0 is suitable if
215 	factor is rounded up. factor-1 is suitable if factor is rounded down.
216 	Since we don't know rounding direction, factor/2 accommodates either,
217 	with the slight loss of showing an error in half the time. Since for
218 	a 64-bit factor this is years, the halving isn't a problem. */
219 
220 	m->offset = m->factor / 2;
221 #ifdef BLIP_MONO
222 	m->integrator = 0;
223 	memset( SAMPLES( m ), 0, (m->size + buf_extra) * sizeof (buf_t) );
224 #else
225 	m->integrator[0] = 0;
226 	m->integrator[1] = 0;
227 	memset( m->buffer[0], 0, (m->size + buf_extra) * sizeof (buf_t) );
228 	memset( m->buffer[1], 0, (m->size + buf_extra) * sizeof (buf_t) );
229 #endif
230 }
231 
blip_clocks_needed(const blip_t * m,int samples)232 int blip_clocks_needed( const blip_t* m, int samples )
233 {
234 	fixed_t needed;
235 
236 #ifdef BLIP_ASSERT
237 	/* Fails if buffer can't hold that many more samples */
238 	assert( (samples >= 0) && (((m->offset >> time_bits) + samples) <= m->size) );
239 #endif
240 
241   needed = (fixed_t) samples * time_unit;
242 	if ( needed < m->offset )
243 		return 0;
244 
245 	return (needed - m->offset + m->factor - 1) / m->factor;
246 }
247 
blip_end_frame(blip_t * m,unsigned t)248 void blip_end_frame( blip_t* m, unsigned t )
249 {
250 	m->offset += t * m->factor;
251 
252 #ifdef BLIP_ASSERT
253 	/* Fails if buffer size was exceeded */
254   assert( (m->offset >> time_bits) <= m->size );
255 #endif
256 }
257 
blip_samples_avail(const blip_t * m)258 int blip_samples_avail( const blip_t* m )
259 {
260 	return (m->offset >> time_bits);
261 }
262 
remove_samples(blip_t * m,int count)263 static void remove_samples( blip_t* m, int count )
264 {
265 #ifdef BLIP_MONO
266 	buf_t* buf = SAMPLES( m );
267 #else
268 	buf_t* buf = m->buffer[0];
269 #endif
270   int remain = (m->offset >> time_bits) + buf_extra - count;
271   m->offset -= count * time_unit;
272 
273 	memmove( &buf [0], &buf [count], remain * sizeof (buf_t) );
274 	memset( &buf [remain], 0, count * sizeof (buf_t) );
275 #ifndef BLIP_MONO
276 	buf = m->buffer[1];
277 	memmove( &buf [0], &buf [count], remain * sizeof (buf_t) );
278 	memset( &buf [remain], 0, count * sizeof (buf_t) );
279 #endif
280 }
281 
blip_discard_samples_dirty(blip_t * m,int count)282 int blip_discard_samples_dirty(blip_t* m, int count)
283 {
284 	if (count > (m->offset >> time_bits))
285 		count = m->offset >> time_bits;
286 
287 	m->offset -= count * time_unit;
288 }
289 
blip_read_samples(blip_t * m,short out[],int count)290 int blip_read_samples( blip_t* m, short out [], int count)
291 {
292 #ifdef BLIP_ASSERT
293 	assert( count >= 0 );
294 
295 	if ( count > (m->offset >> time_bits) )
296 		count = m->offset >> time_bits;
297 
298 	if ( count )
299 #endif
300   {
301 #ifdef BLIP_MONO
302 		buf_t const* in = SAMPLES( m );
303 		int sum = m->integrator;
304 #else
305 		buf_t const* in = m->buffer[0];
306 		buf_t const* in2 = m->buffer[1];
307 		int sum = m->integrator[0];
308 		int sum2 = m->integrator[1];
309 #endif
310 		buf_t const* end = in + count;
311 		do
312 		{
313 			/* Eliminate fraction */
314 			int s = ARITH_SHIFT( sum, delta_bits );
315 
316 			sum += *in++;
317 
318 			CLAMP( s );
319 
320 			*out++ = s;
321 
322 			/* High-pass filter */
323 			sum -= s << (delta_bits - bass_shift);
324 
325 #ifndef BLIP_MONO
326 			/* Eliminate fraction */
327 			s = ARITH_SHIFT( sum2, delta_bits );
328 
329 			sum2 += *in2++;
330 
331 			CLAMP( s );
332 
333 			*out++ = s;
334 
335 			/* High-pass filter */
336 			sum2 -= s << (delta_bits - bass_shift);
337 #endif
338 		}
339 		while ( in != end );
340 
341 #ifdef BLIP_MONO
342 		m->integrator = sum;
343 #else
344 		m->integrator[0] = sum;
345 		m->integrator[1] = sum2;
346 #endif
347 		remove_samples( m, count );
348 	}
349 
350 	return count;
351 }
352 
blip_mix_samples(blip_t * m1,blip_t * m2,blip_t * m3,short out[],int count)353 int blip_mix_samples( blip_t* m1, blip_t* m2, blip_t* m3, short out [], int count)
354 {
355 #ifdef BLIP_ASSERT
356   assert( count >= 0 );
357 
358   if ( count > (m1->offset >> time_bits) )
359     count = m1->offset >> time_bits;
360   if ( count > (m2->offset >> time_bits) )
361     count = m2->offset >> time_bits;
362   if ( count > (m3->offset >> time_bits) )
363     count = m3->offset >> time_bits;
364 
365   if ( count )
366 #endif
367   {
368     buf_t const* end;
369     buf_t const* in[3];
370 #ifdef BLIP_MONO
371     int sum = m1->integrator;
372     in[0] = SAMPLES( m1 );
373     in[1] = SAMPLES( m2 );
374     in[2] = SAMPLES( m3 );
375 #else
376     int sum = m1->integrator[0];
377     int sum2 = m1->integrator[1];
378     buf_t const* in2[3];
379     in[0] = m1->buffer[0];
380     in[1] = m2->buffer[0];
381     in[2] = m3->buffer[0];
382     in2[0] = m1->buffer[1];
383     in2[1] = m2->buffer[1];
384     in2[2] = m3->buffer[1];
385 #endif
386 
387     end = in[0] + count;
388     do
389     {
390       /* Eliminate fraction */
391       int s = ARITH_SHIFT( sum, delta_bits );
392 
393       sum += *in[0]++;
394       sum += *in[1]++;
395       sum += *in[2]++;
396 
397       CLAMP( s );
398 
399       *out++ = s;
400 
401       /* High-pass filter */
402       sum -= s << (delta_bits - bass_shift);
403 
404 #ifndef BLIP_MONO
405       /* Eliminate fraction */
406       s = ARITH_SHIFT( sum2, delta_bits );
407 
408       sum2 += *in2[0]++;
409       sum2 += *in2[1]++;
410       sum2 += *in2[2]++;
411 
412       CLAMP( s );
413 
414       *out++ = s;
415 
416       /* High-pass filter */
417       sum2 -= s << (delta_bits - bass_shift);
418 #endif
419     }
420     while ( in[0] != end );
421 
422 #ifdef BLIP_MONO
423     m1->integrator = sum;
424 #else
425     m1->integrator[0] = sum;
426     m1->integrator[1] = sum2;
427 #endif
428     remove_samples( m1, count );
429     remove_samples( m2, count );
430     remove_samples( m3, count );
431   }
432 
433   return count;
434 }
435 
436 /* Things that didn't help performance on x86:
437 	__attribute__((aligned(128)))
438 	#define short int
439 	restrict
440 */
441 
442 /* Sinc_Generator( 0.9, 0.55, 4.5 ) */
443 static short const bl_step [phase_count + 1] [half_width] =
444 {
445 {   43, -115,  350, -488, 1136, -914, 5861,21022},
446 {   44, -118,  348, -473, 1076, -799, 5274,21001},
447 {   45, -121,  344, -454, 1011, -677, 4706,20936},
448 {   46, -122,  336, -431,  942, -549, 4156,20829},
449 {   47, -123,  327, -404,  868, -418, 3629,20679},
450 {   47, -122,  316, -375,  792, -285, 3124,20488},
451 {   47, -120,  303, -344,  714, -151, 2644,20256},
452 {   46, -117,  289, -310,  634,  -17, 2188,19985},
453 {   46, -114,  273, -275,  553,  117, 1758,19675},
454 {   44, -108,  255, -237,  471,  247, 1356,19327},
455 {   43, -103,  237, -199,  390,  373,  981,18944},
456 {   42,  -98,  218, -160,  310,  495,  633,18527},
457 {   40,  -91,  198, -121,  231,  611,  314,18078},
458 {   38,  -84,  178,  -81,  153,  722,   22,17599},
459 {   36,  -76,  157,  -43,   80,  824, -241,17092},
460 {   34,  -68,  135,   -3,    8,  919, -476,16558},
461 {   32,  -61,  115,   34,  -60, 1006, -683,16001},
462 {   29,  -52,   94,   70, -123, 1083, -862,15422},
463 {   27,  -44,   73,  106, -184, 1152,-1015,14824},
464 {   25,  -36,   53,  139, -239, 1211,-1142,14210},
465 {   22,  -27,   34,  170, -290, 1261,-1244,13582},
466 {   20,  -20,   16,  199, -335, 1301,-1322,12942},
467 {   18,  -12,   -3,  226, -375, 1331,-1376,12293},
468 {   15,   -4,  -19,  250, -410, 1351,-1408,11638},
469 {   13,    3,  -35,  272, -439, 1361,-1419,10979},
470 {   11,    9,  -49,  292, -464, 1362,-1410,10319},
471 {    9,   16,  -63,  309, -483, 1354,-1383, 9660},
472 {    7,   22,  -75,  322, -496, 1337,-1339, 9005},
473 {    6,   26,  -85,  333, -504, 1312,-1280, 8355},
474 {    4,   31,  -94,  341, -507, 1278,-1205, 7713},
475 {    3,   35, -102,  347, -506, 1238,-1119, 7082},
476 {    1,   40, -110,  350, -499, 1190,-1021, 6464},
477 {    0,   43, -115,  350, -488, 1136, -914, 5861}
478 };
479 
480 /* Shifting by pre_shift allows calculation using unsigned int rather than
481 possibly-wider fixed_t. On 32-bit platforms, this is likely more efficient.
482 And by having pre_shift 32, a 32-bit platform can easily do the shift by
483 simply ignoring the low half. */
484 
485 #ifndef BLIP_MONO
486 
blip_add_delta(blip_t * m,unsigned time,int delta_l,int delta_r)487 void blip_add_delta( blip_t* m, unsigned time, int delta_l, int delta_r )
488 {
489   if (delta_l | delta_r)
490   {
491     unsigned fixed = (unsigned) ((time * m->factor + m->offset) >> pre_shift);
492     int phase = fixed >> phase_shift & (phase_count - 1);
493     short const* in  = bl_step [phase];
494     short const* rev = bl_step [phase_count - phase];
495     int interp = fixed >> (phase_shift - delta_bits) & (delta_unit - 1);
496     int pos = fixed >> frac_bits;
497 
498 #ifdef BLIP_INVERT
499     buf_t* out_l = m->buffer[1] + pos;
500     buf_t* out_r = m->buffer[0] + pos;
501 #else
502     buf_t* out_l = m->buffer[0] + pos;
503     buf_t* out_r = m->buffer[1] + pos;
504 #endif
505 
506     int delta;
507 
508 #ifdef BLIP_ASSERT
509     /* Fails if buffer size was exceeded */
510     assert( pos <= m->size + end_frame_extra );
511 #endif
512 
513     if (delta_l == delta_r)
514     {
515       buf_t out;
516       delta = (delta_l * interp) >> delta_bits;
517       delta_l -= delta;
518       out = in[0]*delta_l + in[half_width+0]*delta;
519       out_l[0] += out;
520       out_r[0] += out;
521       out = in[1]*delta_l + in[half_width+1]*delta;
522       out_l[1] += out;
523       out_r[1] += out;
524       out = in[2]*delta_l + in[half_width+2]*delta;
525       out_l[2] += out;
526       out_r[2] += out;
527       out = in[3]*delta_l + in[half_width+3]*delta;
528       out_l[3] += out;
529       out_r[3] += out;
530       out = in[4]*delta_l + in[half_width+4]*delta;
531       out_l[4] += out;
532       out_r[4] += out;
533       out = in[5]*delta_l + in[half_width+5]*delta;
534       out_l[5] += out;
535       out_r[5] += out;
536       out = in[6]*delta_l + in[half_width+6]*delta;
537       out_l[6] += out;
538       out_r[6] += out;
539       out = in[7]*delta_l + in[half_width+7]*delta;
540       out_l[7] += out;
541       out_r[7] += out;
542       out = rev[7]*delta_l + rev[7-half_width]*delta;
543       out_l[8] += out;
544       out_r[8] += out;
545       out = rev[6]*delta_l + rev[6-half_width]*delta;
546       out_l[9] += out;
547       out_r[9] += out;
548       out = rev[5]*delta_l + rev[5-half_width]*delta;
549       out_l[10] += out;
550       out_r[10] += out;
551       out = rev[4]*delta_l + rev[4-half_width]*delta;
552       out_l[11] += out;
553       out_r[11] += out;
554       out = rev[3]*delta_l + rev[3-half_width]*delta;
555       out_l[12] += out;
556       out_r[12] += out;
557       out = rev[2]*delta_l + rev[2-half_width]*delta;
558       out_l[13] += out;
559       out_r[13] += out;
560       out = rev[1]*delta_l + rev[1-half_width]*delta;
561       out_l[14] += out;
562       out_r[14] += out;
563       out = rev[0]*delta_l + rev[0-half_width]*delta;
564       out_l[15] += out;
565       out_r[15] += out;
566     }
567     else
568     {
569       delta = (delta_l * interp) >> delta_bits;
570       delta_l -= delta;
571       out_l [0] += in[0]*delta_l + in[half_width+0]*delta;
572       out_l [1] += in[1]*delta_l + in[half_width+1]*delta;
573       out_l [2] += in[2]*delta_l + in[half_width+2]*delta;
574       out_l [3] += in[3]*delta_l + in[half_width+3]*delta;
575       out_l [4] += in[4]*delta_l + in[half_width+4]*delta;
576       out_l [5] += in[5]*delta_l + in[half_width+5]*delta;
577       out_l [6] += in[6]*delta_l + in[half_width+6]*delta;
578       out_l [7] += in[7]*delta_l + in[half_width+7]*delta;
579       out_l [8] += rev[7]*delta_l + rev[7-half_width]*delta;
580       out_l [9] += rev[6]*delta_l + rev[6-half_width]*delta;
581       out_l [10] += rev[5]*delta_l + rev[5-half_width]*delta;
582       out_l [11] += rev[4]*delta_l + rev[4-half_width]*delta;
583       out_l [12] += rev[3]*delta_l + rev[3-half_width]*delta;
584       out_l [13] += rev[2]*delta_l + rev[2-half_width]*delta;
585       out_l [14] += rev[1]*delta_l + rev[1-half_width]*delta;
586       out_l [15] += rev[0]*delta_l + rev[0-half_width]*delta;
587 
588       delta = (delta_r * interp) >> delta_bits;
589       delta_r -= delta;
590       out_r [0] += in[0]*delta_r + in[half_width+0]*delta;
591       out_r [1] += in[1]*delta_r + in[half_width+1]*delta;
592       out_r [2] += in[2]*delta_r + in[half_width+2]*delta;
593       out_r [3] += in[3]*delta_r + in[half_width+3]*delta;
594       out_r [4] += in[4]*delta_r + in[half_width+4]*delta;
595       out_r [5] += in[5]*delta_r + in[half_width+5]*delta;
596       out_r [6] += in[6]*delta_r + in[half_width+6]*delta;
597       out_r [7] += in[7]*delta_r + in[half_width+7]*delta;
598       out_r [8] += rev[7]*delta_r + rev[7-half_width]*delta;
599       out_r [9] += rev[6]*delta_r + rev[6-half_width]*delta;
600       out_r [10] += rev[5]*delta_r + rev[5-half_width]*delta;
601       out_r [11] += rev[4]*delta_r + rev[4-half_width]*delta;
602       out_r [12] += rev[3]*delta_r + rev[3-half_width]*delta;
603       out_r [13] += rev[2]*delta_r + rev[2-half_width]*delta;
604       out_r [14] += rev[1]*delta_r + rev[1-half_width]*delta;
605       out_r [15] += rev[0]*delta_r + rev[0-half_width]*delta;
606     }
607   }
608 }
609 
blip_add_delta_fast(blip_t * m,unsigned time,int delta_l,int delta_r)610 void blip_add_delta_fast( blip_t* m, unsigned time, int delta_l, int delta_r )
611 {
612   if (delta_l | delta_r)
613   {
614     unsigned fixed = (unsigned) ((time * m->factor + m->offset) >> pre_shift);
615     int interp = fixed >> (frac_bits - delta_bits) & (delta_unit - 1);
616     int pos = fixed >> frac_bits;
617 
618 #ifdef STEREO_INVERT
619     buf_t* out_l = m->buffer[1] + pos;
620     buf_t* out_r = m->buffer[0] + pos;
621 #else
622     buf_t* out_l = m->buffer[0] + pos;
623     buf_t* out_r = m->buffer[1] + pos;
624 #endif
625 
626     int delta = delta_l * interp;
627 
628 #ifdef BLIP_ASSERT
629     /* Fails if buffer size was exceeded */
630     assert( pos <= m->size + end_frame_extra );
631 #endif
632 
633     if (delta_l == delta_r)
634     {
635       delta_l = delta_l * delta_unit - delta;
636       out_l[7] += delta_l;
637       out_l[8] += delta;
638       out_r[7] += delta_l;
639       out_r[8] += delta;
640     }
641     else
642     {
643       out_l[7] += delta_l * delta_unit - delta;
644       out_l[8] += delta;
645       delta = delta_r * interp;
646       out_r[7] += delta_r * delta_unit - delta;
647       out_r[8] += delta;
648     }
649   }
650 }
651 
652 #else
653 
blip_add_delta(blip_t * m,unsigned time,int delta)654 void blip_add_delta( blip_t* m, unsigned time, int delta )
655 {
656 	unsigned fixed = (unsigned) ((time * m->factor + m->offset) >> pre_shift);
657 	buf_t* out = SAMPLES( m ) + (fixed >> frac_bits);
658 
659 	int phase = fixed >> phase_shift & (phase_count - 1);
660 	short const* in  = bl_step [phase];
661 	short const* rev = bl_step [phase_count - phase];
662 
663 	int interp = fixed >> (phase_shift - delta_bits) & (delta_unit - 1);
664 	int delta2 = (delta * interp) >> delta_bits;
665 	delta -= delta2;
666 
667 #ifdef BLIP_ASSERT
668 	/* Fails if buffer size was exceeded */
669 	assert( out <= &SAMPLES( m ) [m->size + end_frame_extra] );
670 #endif
671 
672 	out [0] += in[0]*delta + in[half_width+0]*delta2;
673 	out [1] += in[1]*delta + in[half_width+1]*delta2;
674 	out [2] += in[2]*delta + in[half_width+2]*delta2;
675 	out [3] += in[3]*delta + in[half_width+3]*delta2;
676 	out [4] += in[4]*delta + in[half_width+4]*delta2;
677 	out [5] += in[5]*delta + in[half_width+5]*delta2;
678 	out [6] += in[6]*delta + in[half_width+6]*delta2;
679 	out [7] += in[7]*delta + in[half_width+7]*delta2;
680 
681 	in = rev;
682 	out [ 8] += in[7]*delta + in[7-half_width]*delta2;
683 	out [ 9] += in[6]*delta + in[6-half_width]*delta2;
684 	out [10] += in[5]*delta + in[5-half_width]*delta2;
685 	out [11] += in[4]*delta + in[4-half_width]*delta2;
686 	out [12] += in[3]*delta + in[3-half_width]*delta2;
687 	out [13] += in[2]*delta + in[2-half_width]*delta2;
688 	out [14] += in[1]*delta + in[1-half_width]*delta2;
689 	out [15] += in[0]*delta + in[0-half_width]*delta2;
690 }
691 
blip_add_delta_fast(blip_t * m,unsigned time,int delta)692 void blip_add_delta_fast( blip_t* m, unsigned time, int delta )
693 {
694 	unsigned fixed = (unsigned) ((time * m->factor + m->offset) >> pre_shift);
695 	buf_t* out = SAMPLES( m ) + (fixed >> frac_bits);
696 
697 	int interp = fixed >> (frac_bits - delta_bits) & (delta_unit - 1);
698 	int delta2 = delta * interp;
699 
700 #ifdef BLIP_ASSERT
701   /* Fails if buffer size was exceeded */
702 	assert( out <= &SAMPLES( m ) [m->size + end_frame_extra] );
703 #endif
704 
705 	out [7] += delta * delta_unit - delta2;
706 	out [8] += delta2;
707 }
708 #endif
709 
blip_save_buffer_state(const blip_t * buf,blip_buffer_state_t * state)710 void blip_save_buffer_state(const blip_t *buf, blip_buffer_state_t *state)
711 {
712 #ifdef BLIP_MONO
713 	state->integrator = buf->integrator;
714 	if (buf->buffer && buf->size >= BLIPSTATE_BUFFER_SIZE)
715 	{
716 		memcpy(state->buffer, buf->buffer, sizeof(state->buffer));
717 	}
718 #else
719 	int c;
720 	for (c = 0; c < 2; c++)
721 	{
722 		state->integrator[c] = buf->integrator[c];
723 		if (buf->buffer[c] && buf->size >= BLIP_BUFFER_STATE_BUFFER_SIZE)
724 		{
725 			memcpy(state->buffer[c], buf->buffer[c], sizeof(state->buffer[c]));
726 		}
727 	}
728 #endif
729 	state->offset = buf->offset;
730 }
731 
blip_load_buffer_state(blip_t * buf,const blip_buffer_state_t * state)732 void blip_load_buffer_state(blip_t *buf, const blip_buffer_state_t *state)
733 {
734 #ifdef BLIP_MONO
735 	state->integrator = buf->integrator;
736 	if (buf->buffer && buf->size >= BLIPSTATE_BUFFER_SIZE)
737 	{
738 		memcpy(state->buffer, buf->buffer, sizeof(state->buffer));
739 	}
740 #else
741 	int c;
742 	for (c = 0; c < 2; c++)
743 	{
744 		buf->integrator[c] = state->integrator[c];
745 		if (buf->buffer[c] && buf->size >= BLIP_BUFFER_STATE_BUFFER_SIZE)
746 		{
747 			memcpy(buf->buffer[c], state->buffer[c], sizeof(state->buffer[c]));
748 		}
749 	}
750 #endif
751 	buf->offset = (fixed_t)state->offset;
752 }
753 
blip_new_buffer_state()754 blip_buffer_state_t* blip_new_buffer_state()
755 {
756 	return (blip_buffer_state_t*)calloc(1, sizeof(blip_buffer_state_t));
757 }
758 
blip_delete_buffer_state(blip_buffer_state_t * state)759 void blip_delete_buffer_state(blip_buffer_state_t *state)
760 {
761 	if (state == NULL) return;
762 	memset(state, 0, sizeof(blip_buffer_state_t));
763 	free(state);
764 }
765