2 Copyright (C) 2000 Paul Davis
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #define _ISOC9X_SOURCE 1
21 #define _ISOC99_SOURCE 1
23 #define __USE_ISOC9X 1
24 #define __USE_ISOC99 1
35 #include <jack/memops.h>
37 /* Notes about these *_SCALING values.
39 the MAX_<N>BIT values are floating point. when multiplied by
40 a full-scale normalized floating point sample value (-1.0..+1.0)
41 they should give the maxium value representable with an integer
42 sample type of N bits. Note that this is asymmetric. Sample ranges
43 for signed integer, 2's complement values are -(2^(N-1) to +(2^(N-1)-1)
47 If we use +2^(N-1) for the scaling factors, we run into a problem:
49 if we start with a normalized float value of -1.0, scaling
50 to 24 bits would give -8388608 (-2^23), which is ideal.
51 But with +1.0, we get +8388608, which is technically out of range.
53 We never multiply a full range normalized value by this constant,
54 but we could multiply it by a positive value that is close enough to +1.0
55 to produce a value > +(2^(N-1)-1.
57 There is no way around this paradox without wasting CPU cycles to determine
58 which scaling factor to use (i.e. determine if its negative or not,
59 use the right factor).
61 So, for now (October 2008) we use 2^(N-1)-1 as the scaling factor.
64 #define SAMPLE_24BIT_SCALING 8388607.0f
65 #define SAMPLE_16BIT_SCALING 32767.0f
67 /* these are just values to use if the floating point value was out of range
69 advice from Fons Adriaensen: make the limits symmetrical
72 #define SAMPLE_24BIT_MAX 8388607
73 #define SAMPLE_24BIT_MIN -8388607
75 #define SAMPLE_16BIT_MAX 32767
76 #define SAMPLE_16BIT_MIN -32767
78 /* these mark the outer edges of the range considered "within" range
79 for a floating point sample value. values outside (and on the boundaries)
80 of this range will be clipped before conversion; values within this
81 range will be scaled to appropriate values for the target sample
85 #define NORMALIZED_FLOAT_MIN -1.0f
86 #define NORMALIZED_FLOAT_MAX 1.0f
88 /* define this in case we end up on a platform that is missing
89 the real lrintf functions
92 #define f_round(f) lrintf(f)
94 /* Linear Congruential noise generator. From the music-dsp list
95 * less random than rand(), but good enough and 10x faster
98 inline unsigned int fast_rand() {
99 static unsigned int seed
= 22222;
100 seed
= (seed
* 96314165) + 907633515;
106 /* functions for native float sample data */
108 void sample_move_floatLE_sSs (jack_default_audio_sample_t
*dst
, char *src
, unsigned long nsamples
, unsigned long src_skip
) {
110 *dst
= *((float *) src
);
116 void sample_move_dS_floatLE (char *dst
, jack_default_audio_sample_t
*src
, unsigned long nsamples
, unsigned long dst_skip
, dither_state_t
*state
) {
118 *((float *) dst
) = *src
;
124 /* NOTES on function naming:
126 foo_bar_d<TYPE>_s<TYPE>
128 the "d<TYPE>" component defines the destination type for the operation
129 the "s<TYPE>" component defines the source type for the operation
133 S - sample is a jack_default_audio_sample_t, currently (October 2008) a 32 bit floating point value
134 Ss - like S but reverse endian from the host CPU
135 32u24 - sample is an signed 32 bit integer value, but data is in upper 24 bits only
136 32u24s - like 32u24 but reverse endian from the host CPU
137 24 - sample is an signed 24 bit integer value
138 24s - like 24 but reverse endian from the host CPU
139 16 - sample is an signed 16 bit integer value
140 16s - like 16 but reverse endian from the host CPU
142 For obvious reasons, the reverse endian versions only show as source types.
144 This covers all known sample formats at 16 bits or larger.
147 /* functions for native integer sample data */
149 void sample_move_d32u24_sSs (char *dst
, jack_default_audio_sample_t
*src
, unsigned long nsamples
, unsigned long dst_skip
, dither_state_t
*state
)
154 if (*src
<= NORMALIZED_FLOAT_MIN
) {
155 z
= SAMPLE_24BIT_MIN
;
156 } else if (*src
>= NORMALIZED_FLOAT_MAX
) {
157 z
= SAMPLE_24BIT_MAX
;
159 z
= f_round (*src
* SAMPLE_24BIT_SCALING
) << 8;
161 #if __BYTE_ORDER == __LITTLE_ENDIAN
162 dst
[0]=(char)(z
>>24);
163 dst
[1]=(char)(z
>>16);
166 #elif __BYTE_ORDER == __BIG_ENDIAN
169 dst
[2]=(char)(z
>>16);
170 dst
[3]=(char)(z
>>24);
177 void sample_move_d32u24_sS (char *dst
, jack_default_audio_sample_t
*src
, unsigned long nsamples
, unsigned long dst_skip
, dither_state_t
*state
)
180 if (*src
<= NORMALIZED_FLOAT_MIN
) {
181 *((int32_t*) dst
) = SAMPLE_24BIT_MIN
;
182 } else if (*src
>= NORMALIZED_FLOAT_MAX
) {
183 *((int32_t*) dst
) = SAMPLE_24BIT_MAX
;
185 *((int32_t*) dst
) = f_round (*src
* SAMPLE_24BIT_SCALING
) << 8;
192 void sample_move_dS_s32u24s (jack_default_audio_sample_t
*dst
, char *src
, unsigned long nsamples
, unsigned long src_skip
)
194 /* ALERT: signed sign-extension portability !!! */
198 #if __BYTE_ORDER == __LITTLE_ENDIAN
199 x
= (unsigned char)(src
[0]);
201 x
|= (unsigned char)(src
[1]);
203 x
|= (unsigned char)(src
[2]);
205 x
|= (unsigned char)(src
[3]);
206 #elif __BYTE_ORDER == __BIG_ENDIAN
207 x
= (unsigned char)(src
[3]);
209 x
|= (unsigned char)(src
[2]);
211 x
|= (unsigned char)(src
[1]);
213 x
|= (unsigned char)(src
[0]);
215 *dst
= (x
>> 8) / SAMPLE_24BIT_SCALING
;
221 void sample_move_dS_s32u24 (jack_default_audio_sample_t
*dst
, char *src
, unsigned long nsamples
, unsigned long src_skip
)
223 /* ALERT: signed sign-extension portability !!! */
226 *dst
= (*((int *) src
) >> 8) / SAMPLE_24BIT_SCALING
;
232 void sample_move_d24_sSs (char *dst
, jack_default_audio_sample_t
*src
, unsigned long nsamples
, unsigned long dst_skip
, dither_state_t
*state
)
237 if (*src
<= NORMALIZED_FLOAT_MIN
) {
238 z
= SAMPLE_24BIT_MIN
;
239 } else if (*src
>= NORMALIZED_FLOAT_MAX
) {
240 z
= SAMPLE_24BIT_MAX
;
242 z
= (int32_t)f_round (*src
* SAMPLE_24BIT_SCALING
);
245 #if __BYTE_ORDER == __LITTLE_ENDIAN
246 dst
[0]=(char)(z
>>16);
249 #elif __BYTE_ORDER == __BIG_ENDIAN
252 dst
[2]=(char)(z
>>16);
259 void sample_move_d24_sS (char *dst
, jack_default_audio_sample_t
*src
, unsigned long nsamples
, unsigned long dst_skip
, dither_state_t
*state
)
264 if (*src
<= NORMALIZED_FLOAT_MIN
) {
265 z
= SAMPLE_24BIT_MIN
;
266 } else if (*src
>= NORMALIZED_FLOAT_MAX
) {
267 z
= SAMPLE_24BIT_MAX
;
269 z
= (int32_t) f_round (*src
* SAMPLE_24BIT_SCALING
);
272 #if __BYTE_ORDER == __LITTLE_ENDIAN
274 #elif __BYTE_ORDER == __BIG_ENDIAN
275 memcpy (dst
, (char *)&z
+ 1, 3);
282 void sample_move_dS_s24s (jack_default_audio_sample_t
*dst
, char *src
, unsigned long nsamples
, unsigned long src_skip
)
284 /* ALERT: signed sign-extension portability !!! */
288 #if __BYTE_ORDER == __LITTLE_ENDIAN
289 x
= (unsigned char)(src
[0]);
291 x
|= (unsigned char)(src
[1]);
293 x
|= (unsigned char)(src
[2]);
294 /* correct sign bit and the rest of the top byte */
298 #elif __BYTE_ORDER == __BIG_ENDIAN
299 x
= (unsigned char)(src
[2]);
301 x
|= (unsigned char)(src
[1]);
303 x
|= (unsigned char)(src
[0]);
304 /* correct sign bit and the rest of the top byte */
309 *dst
= x
/ SAMPLE_24BIT_SCALING
;
315 void sample_move_dS_s24 (jack_default_audio_sample_t
*dst
, char *src
, unsigned long nsamples
, unsigned long src_skip
)
317 /* ALERT: signed sign-extension portability !!! */
321 #if __BYTE_ORDER == __LITTLE_ENDIAN
322 memcpy((char*)&x
+ 1, src
, 3);
323 #elif __BYTE_ORDER == __BIG_ENDIAN
327 *dst
= x
/ SAMPLE_24BIT_SCALING
;
334 void sample_move_d16_sSs (char *dst
, jack_default_audio_sample_t
*src
, unsigned long nsamples
, unsigned long dst_skip
, dither_state_t
*state
)
338 /* ALERT: signed sign-extension portability !!! */
341 if (*src
<= NORMALIZED_FLOAT_MIN
) {
342 tmp
= SAMPLE_16BIT_MIN
;
343 } else if (*src
>= NORMALIZED_FLOAT_MAX
) {
344 tmp
= SAMPLE_16BIT_MAX
;
346 tmp
= (int16_t) f_round (*src
* SAMPLE_16BIT_SCALING
);
348 #if __BYTE_ORDER == __LITTLE_ENDIAN
349 dst
[0]=(char)(tmp
>>8);
351 #elif __BYTE_ORDER == __BIG_ENDIAN
353 dst
[1]=(char)(tmp
>>8);
360 void sample_move_d16_sS (char *dst
, jack_default_audio_sample_t
*src
, unsigned long nsamples
, unsigned long dst_skip
, dither_state_t
*state
)
362 /* ALERT: signed sign-extension portability !!! */
365 if (*src
<= NORMALIZED_FLOAT_MIN
) {
366 *((int16_t*) dst
) = SAMPLE_16BIT_MIN
;
367 } else if (*src
>= NORMALIZED_FLOAT_MAX
) {
368 *((int16_t*) dst
) = SAMPLE_16BIT_MAX
;
370 *((int16_t*) dst
) = (int16_t) f_round (*src
* SAMPLE_16BIT_SCALING
);
377 void sample_move_dither_rect_d16_sSs (char *dst
, jack_default_audio_sample_t
*src
, unsigned long nsamples
, unsigned long dst_skip
, dither_state_t
*state
)
379 jack_default_audio_sample_t val
;
383 val
= *src
- (fast_rand()/((float)INT_MAX
-1.0f
));
384 if (val
<= NORMALIZED_FLOAT_MIN
) {
385 tmp
= SAMPLE_16BIT_MIN
;
386 } else if (val
>= NORMALIZED_FLOAT_MAX
) {
387 tmp
= SAMPLE_16BIT_MAX
;
389 tmp
= (int16_t) f_round(*src
* SAMPLE_16BIT_SCALING
);
391 #if __BYTE_ORDER == __LITTLE_ENDIAN
392 dst
[0]=(char)(tmp
>>8);
394 #elif __BYTE_ORDER == __BIG_ENDIAN
396 dst
[1]=(char)(tmp
>>8);
403 void sample_move_dither_rect_d16_sS (char *dst
, jack_default_audio_sample_t
*src
, unsigned long nsamples
, unsigned long dst_skip
, dither_state_t
*state
)
405 jack_default_audio_sample_t val
;
408 val
= *src
- (fast_rand()/((float)INT_MAX
- 1.0f
));
409 if (val
<= NORMALIZED_FLOAT_MIN
) {
410 *((int16_t*) dst
) = SAMPLE_16BIT_MIN
;
411 } else if (val
>= NORMALIZED_FLOAT_MAX
) {
412 *((int16_t*) dst
) = SAMPLE_16BIT_MAX
;
414 *((int16_t*) dst
) = (int16_t) f_round(*src
* SAMPLE_16BIT_SCALING
);
421 void sample_move_dither_tri_d16_sSs (char *dst
, jack_default_audio_sample_t
*src
, unsigned long nsamples
, unsigned long dst_skip
, dither_state_t
*state
)
423 jack_default_audio_sample_t x
;
425 float rm1
= state
->rm1
;
429 x
= *src
* SAMPLE_16BIT_SCALING
;
430 r
= 2.0f
* (float)fast_rand() / (float)INT_MAX
- 1.0f
;
434 if (x
<= NORMALIZED_FLOAT_MIN
) {
435 y
= SAMPLE_16BIT_MIN
;
436 } else if (x
>= NORMALIZED_FLOAT_MAX
) {
437 y
= SAMPLE_16BIT_MAX
;
442 #if __BYTE_ORDER == __LITTLE_ENDIAN
445 #elif __BYTE_ORDER == __BIG_ENDIAN
455 void sample_move_dither_tri_d16_sS (char *dst
, jack_default_audio_sample_t
*src
, unsigned long nsamples
, unsigned long dst_skip
, dither_state_t
*state
)
457 jack_default_audio_sample_t x
;
459 float rm1
= state
->rm1
;
462 x
= *src
* SAMPLE_16BIT_SCALING
;
463 r
= 2.0f
* (float)fast_rand() / (float)INT_MAX
- 1.0f
;
467 if (x
<= NORMALIZED_FLOAT_MIN
) {
468 *((int16_t*)dst
) = SAMPLE_16BIT_MIN
;
469 } else if (x
>= NORMALIZED_FLOAT_MAX
) {
470 *((int16_t*)dst
) = SAMPLE_16BIT_MAX
;
472 *((int16_t*)dst
) = (int16_t) f_round (x
* SAMPLE_16BIT_SCALING
);
481 void sample_move_dither_shaped_d16_sSs (char *dst
, jack_default_audio_sample_t
*src
, unsigned long nsamples
, unsigned long dst_skip
, dither_state_t
*state
)
483 jack_default_audio_sample_t x
;
484 jack_default_audio_sample_t xe
; /* the innput sample - filtered error */
485 jack_default_audio_sample_t xp
; /* x' */
487 float rm1
= state
->rm1
;
488 unsigned int idx
= state
->idx
;
492 x
= *src
* SAMPLE_16BIT_SCALING
;
493 r
= 2.0f
* (float)fast_rand() / (float)INT_MAX
- 1.0f
;
494 /* Filter the error with Lipshitz's minimally audible FIR:
495 [2.033 -2.165 1.959 -1.590 0.6149] */
497 - state
->e
[idx
] * 2.033f
498 + state
->e
[(idx
- 1) & DITHER_BUF_MASK
] * 2.165f
499 - state
->e
[(idx
- 2) & DITHER_BUF_MASK
] * 1.959f
500 + state
->e
[(idx
- 3) & DITHER_BUF_MASK
] * 1.590f
501 - state
->e
[(idx
- 4) & DITHER_BUF_MASK
] * 0.6149f
;
505 if (xp
<= NORMALIZED_FLOAT_MIN
) {
506 tmp
= SAMPLE_16BIT_MIN
;
507 } else if (xp
>= NORMALIZED_FLOAT_MAX
) {
508 tmp
= SAMPLE_16BIT_MAX
;
510 tmp
= (int16_t) f_round (xp
* SAMPLE_16BIT_SCALING
);
513 /* Intrinsic z^-1 delay */
514 idx
= (idx
+ 1) & DITHER_BUF_MASK
;
515 state
->e
[idx
] = xp
- xe
;
517 #if __BYTE_ORDER == __LITTLE_ENDIAN
518 dst
[0]=(char)(tmp
>>8);
520 #elif __BYTE_ORDER == __BIG_ENDIAN
522 dst
[1]=(char)(tmp
>>8);
531 void sample_move_dither_shaped_d16_sS (char *dst
, jack_default_audio_sample_t
*src
, unsigned long nsamples
, unsigned long dst_skip
, dither_state_t
*state
)
533 jack_default_audio_sample_t x
;
534 jack_default_audio_sample_t xe
; /* the innput sample - filtered error */
535 jack_default_audio_sample_t xp
; /* x' */
537 float rm1
= state
->rm1
;
538 unsigned int idx
= state
->idx
;
541 x
= *src
* SAMPLE_16BIT_SCALING
;
542 r
= 2.0f
* (float)fast_rand() / (float)INT_MAX
- 1.0f
;
543 /* Filter the error with Lipshitz's minimally audible FIR:
544 [2.033 -2.165 1.959 -1.590 0.6149] */
546 - state
->e
[idx
] * 2.033f
547 + state
->e
[(idx
- 1) & DITHER_BUF_MASK
] * 2.165f
548 - state
->e
[(idx
- 2) & DITHER_BUF_MASK
] * 1.959f
549 + state
->e
[(idx
- 3) & DITHER_BUF_MASK
] * 1.590f
550 - state
->e
[(idx
- 4) & DITHER_BUF_MASK
] * 0.6149f
;
554 if (xp
<= NORMALIZED_FLOAT_MIN
) {
555 *((int16_t*) dst
) = SAMPLE_16BIT_MIN
;
556 } else if (xp
>= NORMALIZED_FLOAT_MIN
) {
557 *((int16_t*) dst
) = SAMPLE_16BIT_MAX
;
559 *((int16_t*) dst
) = (int16_t) f_round (*src
* SAMPLE_16BIT_SCALING
);
562 /* Intrinsic z^-1 delay */
563 idx
= (idx
+ 1) & DITHER_BUF_MASK
;
564 state
->e
[idx
] = *((int16_t*) dst
) - xe
;
573 void sample_move_dS_s16s (jack_default_audio_sample_t
*dst
, char *src
, unsigned long nsamples
, unsigned long src_skip
)
577 /* ALERT: signed sign-extension portability !!! */
579 #if __BYTE_ORDER == __LITTLE_ENDIAN
580 z
= (unsigned char)(src
[0]);
582 z
|= (unsigned char)(src
[1]);
583 #elif __BYTE_ORDER == __BIG_ENDIAN
584 z
= (unsigned char)(src
[1]);
586 z
|= (unsigned char)(src
[0]);
588 *dst
= z
/ SAMPLE_16BIT_SCALING
;
594 void sample_move_dS_s16 (jack_default_audio_sample_t
*dst
, char *src
, unsigned long nsamples
, unsigned long src_skip
)
597 /* ALERT: signed sign-extension portability !!! */
599 *dst
= (*((short *) src
)) / SAMPLE_16BIT_SCALING
;
605 void memset_interleave (char *dst
, char val
, unsigned long bytes
,
606 unsigned long unit_bytes
,
607 unsigned long skip_bytes
)
609 switch (unit_bytes
) {
618 *((short *) dst
) = (short) val
;
625 *((int *) dst
) = (int) val
;
632 memset(dst
, val
, unit_bytes
);
640 /* COPY FUNCTIONS: used to move data from an input channel to an
641 output channel. Note that we assume that the skip distance
642 is the same for both channels. This is completely fine
643 unless the input and output were on different audio interfaces that
644 were interleaved differently. We don't try to handle that.
648 memcpy_fake (char *dst
, char *src
, unsigned long src_bytes
, unsigned long foo
, unsigned long bar
)
650 memcpy (dst
, src
, src_bytes
);
654 memcpy_interleave_d16_s16 (char *dst
, char *src
, unsigned long src_bytes
,
655 unsigned long dst_skip_bytes
, unsigned long src_skip_bytes
)
658 *((short *) dst
) = *((short *) src
);
659 dst
+= dst_skip_bytes
;
660 src
+= src_skip_bytes
;
666 memcpy_interleave_d24_s24 (char *dst
, char *src
, unsigned long src_bytes
,
667 unsigned long dst_skip_bytes
, unsigned long src_skip_bytes
)
672 dst
+= dst_skip_bytes
;
673 src
+= src_skip_bytes
;
679 memcpy_interleave_d32_s32 (char *dst
, char *src
, unsigned long src_bytes
,
680 unsigned long dst_skip_bytes
, unsigned long src_skip_bytes
)
684 *((int *) dst
) = *((int *) src
);
685 dst
+= dst_skip_bytes
;
686 src
+= src_skip_bytes
;