jack2 codebase
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

839 lines
22KB

  1. /*
  2. Copyright (C) 2000 Paul Davis
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  14. */
  15. #define _ISOC9X_SOURCE 1
  16. #define _ISOC99_SOURCE 1
  17. #define __USE_ISOC9X 1
  18. #define __USE_ISOC99 1
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <math.h>
  22. #include <memory.h>
  23. #include <stdlib.h>
  24. #include <stdint.h>
  25. #include <limits.h>
  26. #ifdef __linux__
  27. #include <endian.h>
  28. #endif
  29. #include "memops.h"
  30. #if defined (__SSE2__) && !defined (__sun__)
  31. #include <emmintrin.h>
  32. #endif
  33. /* Notes about these *_SCALING values.
  34. the MAX_<N>BIT values are floating point. when multiplied by
  35. a full-scale normalized floating point sample value (-1.0..+1.0)
  36. they should give the maxium value representable with an integer
  37. sample type of N bits. Note that this is asymmetric. Sample ranges
  38. for signed integer, 2's complement values are -(2^(N-1) to +(2^(N-1)-1)
  39. Complications
  40. -------------
  41. If we use +2^(N-1) for the scaling factors, we run into a problem:
  42. if we start with a normalized float value of -1.0, scaling
  43. to 24 bits would give -8388608 (-2^23), which is ideal.
  44. But with +1.0, we get +8388608, which is technically out of range.
  45. We never multiply a full range normalized value by this constant,
  46. but we could multiply it by a positive value that is close enough to +1.0
  47. to produce a value > +(2^(N-1)-1.
  48. There is no way around this paradox without wasting CPU cycles to determine
  49. which scaling factor to use (i.e. determine if its negative or not,
  50. use the right factor).
  51. So, for now (October 2008) we use 2^(N-1)-1 as the scaling factor.
  52. */
  53. #define SAMPLE_24BIT_SCALING 8388607.0f
  54. #define SAMPLE_16BIT_SCALING 32767.0f
  55. /* these are just values to use if the floating point value was out of range
  56. advice from Fons Adriaensen: make the limits symmetrical
  57. */
  58. #define SAMPLE_24BIT_MAX 8388607
  59. #define SAMPLE_24BIT_MIN -8388607
  60. #define SAMPLE_24BIT_MAX_F 8388607.0f
  61. #define SAMPLE_24BIT_MIN_F -8388607.0f
  62. #define SAMPLE_16BIT_MAX 32767
  63. #define SAMPLE_16BIT_MIN -32767
  64. #define SAMPLE_16BIT_MAX_F 32767.0f
  65. #define SAMPLE_16BIT_MIN_F -32767.0f
  66. /* these mark the outer edges of the range considered "within" range
  67. for a floating point sample value. values outside (and on the boundaries)
  68. of this range will be clipped before conversion; values within this
  69. range will be scaled to appropriate values for the target sample
  70. type.
  71. */
  72. #define NORMALIZED_FLOAT_MIN -1.0f
  73. #define NORMALIZED_FLOAT_MAX 1.0f
  74. /* define this in case we end up on a platform that is missing
  75. the real lrintf functions
  76. */
  77. #define f_round(f) lrintf(f)
  78. #define float_16(s, d)\
  79. if ((s) <= NORMALIZED_FLOAT_MIN) {\
  80. (d) = SAMPLE_16BIT_MIN;\
  81. } else if ((s) >= NORMALIZED_FLOAT_MAX) {\
  82. (d) = SAMPLE_16BIT_MAX;\
  83. } else {\
  84. (d) = f_round ((s) * SAMPLE_16BIT_SCALING);\
  85. }
  86. /* call this when "s" has already been scaled (e.g. when dithering)
  87. */
  88. #define float_16_scaled(s, d)\
  89. if ((s) <= SAMPLE_16BIT_MIN_F) {\
  90. (d) = SAMPLE_16BIT_MIN_F;\
  91. } else if ((s) >= SAMPLE_16BIT_MAX_F) { \
  92. (d) = SAMPLE_16BIT_MAX;\
  93. } else {\
  94. (d) = f_round ((s));\
  95. }
  96. #define float_24u32(s, d) \
  97. if ((s) <= NORMALIZED_FLOAT_MIN) {\
  98. (d) = SAMPLE_24BIT_MIN << 8;\
  99. } else if ((s) >= NORMALIZED_FLOAT_MAX) {\
  100. (d) = SAMPLE_24BIT_MAX << 8;\
  101. } else {\
  102. (d) = f_round ((s) * SAMPLE_24BIT_SCALING) << 8;\
  103. }
  104. /* call this when "s" has already been scaled (e.g. when dithering)
  105. */
  106. #define float_24u32_scaled(s, d)\
  107. if ((s) <= SAMPLE_24BIT_MIN_F) {\
  108. (d) = SAMPLE_24BIT_MIN << 8;\
  109. } else if ((s) >= SAMPLE_24BIT_MAX_F) { \
  110. (d) = SAMPLE_24BIT_MAX << 8; \
  111. } else {\
  112. (d) = f_round ((s)) << 8; \
  113. }
  114. #define float_24(s, d) \
  115. if ((s) <= NORMALIZED_FLOAT_MIN) {\
  116. (d) = SAMPLE_24BIT_MIN;\
  117. } else if ((s) >= NORMALIZED_FLOAT_MAX) {\
  118. (d) = SAMPLE_24BIT_MAX;\
  119. } else {\
  120. (d) = f_round ((s) * SAMPLE_24BIT_SCALING);\
  121. }
  122. /* call this when "s" has already been scaled (e.g. when dithering)
  123. */
  124. #define float_24_scaled(s, d)\
  125. if ((s) <= SAMPLE_24BIT_MIN_F) {\
  126. (d) = SAMPLE_24BIT_MIN;\
  127. } else if ((s) >= SAMPLE_24BIT_MAX_F) { \
  128. (d) = SAMPLE_24BIT_MAX; \
  129. } else {\
  130. (d) = f_round ((s)); \
  131. }
  132. #if defined (__SSE2__) && !defined (__sun__)
  133. /* generates same as _mm_set_ps(1.f, 1.f, 1f., 1f) but faster */
  134. static inline __m128 gen_one(void)
  135. {
  136. volatile __m128i x;
  137. __m128i ones = _mm_cmpeq_epi32(x, x);
  138. return (__m128)_mm_slli_epi32 (_mm_srli_epi32(ones, 25), 23);
  139. }
  140. static inline __m128 clip(__m128 s, __m128 min, __m128 max)
  141. {
  142. return _mm_min_ps(max, _mm_max_ps(s, min));
  143. }
  144. static inline __m128i float_24_sse(__m128 s)
  145. {
  146. const __m128 upper_bound = gen_one(); /* NORMALIZED_FLOAT_MAX */
  147. const __m128 lower_bound = _mm_sub_ps(_mm_setzero_ps(), upper_bound);
  148. __m128 clipped = clip(s, lower_bound, upper_bound);
  149. __m128 scaled = _mm_mul_ps(clipped, _mm_set1_ps(SAMPLE_24BIT_SCALING));
  150. return _mm_cvtps_epi32(scaled);
  151. }
  152. #endif
  153. /* Linear Congruential noise generator. From the music-dsp list
  154. * less random than rand(), but good enough and 10x faster
  155. */
  156. static unsigned int seed = 22222;
  157. inline unsigned int fast_rand() {
  158. seed = (seed * 96314165) + 907633515;
  159. return seed;
  160. }
  161. /* functions for native float sample data */
  162. void sample_move_floatLE_sSs (jack_default_audio_sample_t *dst, char *src, unsigned long nsamples, unsigned long src_skip) {
  163. while (nsamples--) {
  164. *dst = *((float *) src);
  165. dst++;
  166. src += src_skip;
  167. }
  168. }
  169. void sample_move_dS_floatLE (char *dst, jack_default_audio_sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state) {
  170. while (nsamples--) {
  171. *((float *) dst) = *src;
  172. dst += dst_skip;
  173. src++;
  174. }
  175. }
  176. /* NOTES on function naming:
  177. foo_bar_d<TYPE>_s<TYPE>
  178. the "d<TYPE>" component defines the destination type for the operation
  179. the "s<TYPE>" component defines the source type for the operation
  180. TYPE can be one of:
  181. S - sample is a jack_default_audio_sample_t, currently (October 2008) a 32 bit floating point value
  182. Ss - like S but reverse endian from the host CPU
  183. 32u24 - sample is an signed 32 bit integer value, but data is in upper 24 bits only
  184. 32u24s - like 32u24 but reverse endian from the host CPU
  185. 24 - sample is an signed 24 bit integer value
  186. 24s - like 24 but reverse endian from the host CPU
  187. 16 - sample is an signed 16 bit integer value
  188. 16s - like 16 but reverse endian from the host CPU
  189. For obvious reasons, the reverse endian versions only show as source types.
  190. This covers all known sample formats at 16 bits or larger.
  191. */
  192. /* functions for native integer sample data */
  193. void sample_move_d32u24_sSs (char *dst, jack_default_audio_sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state)
  194. {
  195. int32_t z;
  196. while (nsamples--) {
  197. float_24u32 (*src, z);
  198. #if __BYTE_ORDER == __LITTLE_ENDIAN
  199. dst[0]=(char)(z>>24);
  200. dst[1]=(char)(z>>16);
  201. dst[2]=(char)(z>>8);
  202. dst[3]=(char)(z);
  203. #elif __BYTE_ORDER == __BIG_ENDIAN
  204. dst[0]=(char)(z);
  205. dst[1]=(char)(z>>8);
  206. dst[2]=(char)(z>>16);
  207. dst[3]=(char)(z>>24);
  208. #endif
  209. dst += dst_skip;
  210. src++;
  211. }
  212. }
  213. void sample_move_d32u24_sS (char *dst, jack_default_audio_sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state)
  214. {
  215. #if defined (__SSE2__) && !defined (__sun__)
  216. __m128 int_max = _mm_set1_ps(SAMPLE_24BIT_MAX_F);
  217. __m128 int_min = _mm_sub_ps(_mm_setzero_ps(), int_max);
  218. __m128 factor = int_max;
  219. unsigned long unrolled = nsamples / 4;
  220. nsamples = nsamples & 3;
  221. while (unrolled--) {
  222. __m128 in = _mm_load_ps(src);
  223. __m128 scaled = _mm_mul_ps(in, factor);
  224. __m128 clipped = clip(scaled, int_min, int_max);
  225. __m128i y = _mm_cvttps_epi32(clipped);
  226. __m128i shifted = _mm_slli_epi32(y, 8);
  227. __m128i shuffled1 = _mm_shuffle_epi32(shifted, _MM_SHUFFLE(0, 3, 2, 1));
  228. __m128i shuffled2 = _mm_shuffle_epi32(shifted, _MM_SHUFFLE(1, 0, 3, 2));
  229. __m128i shuffled3 = _mm_shuffle_epi32(shifted, _MM_SHUFFLE(2, 1, 0, 3));
  230. _mm_store_ss((float*)dst, (__m128)shifted);
  231. _mm_store_ss((float*)(dst+dst_skip), (__m128)shuffled1);
  232. _mm_store_ss((float*)(dst+2*dst_skip), (__m128)shuffled2);
  233. _mm_store_ss((float*)(dst+3*dst_skip), (__m128)shuffled3);
  234. dst += 4*dst_skip;
  235. src+= 4;
  236. }
  237. while (nsamples--) {
  238. __m128 in = _mm_load_ss(src);
  239. __m128 scaled = _mm_mul_ss(in, factor);
  240. __m128 clipped = _mm_min_ss(int_max, _mm_max_ss(scaled, int_min));
  241. int y = _mm_cvttss_si32(clipped);
  242. *((int *) dst) = y<<8;
  243. dst += dst_skip;
  244. src++;
  245. }
  246. #else
  247. while (nsamples--) {
  248. float_24u32 (*src, *((int32_t*) dst));
  249. dst += dst_skip;
  250. src++;
  251. }
  252. #endif
  253. }
  254. void sample_move_dS_s32u24s (jack_default_audio_sample_t *dst, char *src, unsigned long nsamples, unsigned long src_skip)
  255. {
  256. /* ALERT: signed sign-extension portability !!! */
  257. const jack_default_audio_sample_t scaling = 1.0/SAMPLE_24BIT_SCALING;
  258. while (nsamples--) {
  259. int x;
  260. #if __BYTE_ORDER == __LITTLE_ENDIAN
  261. x = (unsigned char)(src[0]);
  262. x <<= 8;
  263. x |= (unsigned char)(src[1]);
  264. x <<= 8;
  265. x |= (unsigned char)(src[2]);
  266. x <<= 8;
  267. x |= (unsigned char)(src[3]);
  268. #elif __BYTE_ORDER == __BIG_ENDIAN
  269. x = (unsigned char)(src[3]);
  270. x <<= 8;
  271. x |= (unsigned char)(src[2]);
  272. x <<= 8;
  273. x |= (unsigned char)(src[1]);
  274. x <<= 8;
  275. x |= (unsigned char)(src[0]);
  276. #endif
  277. *dst = (x >> 8) * scaling;
  278. dst++;
  279. src += src_skip;
  280. }
  281. }
  282. void sample_move_dS_s32u24 (jack_default_audio_sample_t *dst, char *src, unsigned long nsamples, unsigned long src_skip)
  283. {
  284. #if defined (__SSE2__) && !defined (__sun__)
  285. unsigned long unrolled = nsamples / 4;
  286. static float inv_sample_max_24bit = 1.0 / SAMPLE_24BIT_SCALING;
  287. __m128 factor = _mm_set1_ps(inv_sample_max_24bit);
  288. while (unrolled--)
  289. {
  290. int i1 = *((int *) src);
  291. src+= src_skip;
  292. int i2 = *((int *) src);
  293. src+= src_skip;
  294. int i3 = *((int *) src);
  295. src+= src_skip;
  296. int i4 = *((int *) src);
  297. src+= src_skip;
  298. __m128i src = _mm_set_epi32(i4, i3, i2, i1);
  299. __m128i shifted = _mm_srai_epi32(src, 8);
  300. __m128 as_float = _mm_cvtepi32_ps(shifted);
  301. __m128 divided = _mm_mul_ps(as_float, factor);
  302. _mm_storeu_ps(dst, divided);
  303. dst += 4;
  304. }
  305. nsamples = nsamples & 3;
  306. #endif
  307. /* ALERT: signed sign-extension portability !!! */
  308. const jack_default_audio_sample_t scaling = 1.0/SAMPLE_24BIT_SCALING;
  309. while (nsamples--) {
  310. *dst = (*((int *) src) >> 8) * scaling;
  311. dst++;
  312. src += src_skip;
  313. }
  314. }
  315. void sample_move_d24_sSs (char *dst, jack_default_audio_sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state)
  316. {
  317. int32_t z;
  318. while (nsamples--) {
  319. float_24 (*src, z);
  320. #if __BYTE_ORDER == __LITTLE_ENDIAN
  321. dst[0]=(char)(z>>16);
  322. dst[1]=(char)(z>>8);
  323. dst[2]=(char)(z);
  324. #elif __BYTE_ORDER == __BIG_ENDIAN
  325. dst[0]=(char)(z);
  326. dst[1]=(char)(z>>8);
  327. dst[2]=(char)(z>>16);
  328. #endif
  329. dst += dst_skip;
  330. src++;
  331. }
  332. }
  333. void sample_move_d24_sS (char *dst, jack_default_audio_sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state)
  334. {
  335. #if defined (__SSE2__) && !defined (__sun__)
  336. _MM_SET_ROUNDING_MODE(_MM_ROUND_NEAREST);
  337. while (nsamples >= 4) {
  338. int i;
  339. int32_t z[4];
  340. __m128 samples = _mm_loadu_ps(src);
  341. __m128i converted = float_24_sse(samples);
  342. __m128i shuffled1 = _mm_shuffle_epi32(converted, _MM_SHUFFLE(0, 3, 2, 1));
  343. __m128i shuffled2 = _mm_shuffle_epi32(converted, _MM_SHUFFLE(1, 0, 3, 2));
  344. __m128i shuffled3 = _mm_shuffle_epi32(converted, _MM_SHUFFLE(2, 1, 0, 3));
  345. _mm_store_ss((float*)z, (__m128)converted);
  346. _mm_store_ss((float*)z+1, (__m128)shuffled1);
  347. _mm_store_ss((float*)z+2, (__m128)shuffled2);
  348. _mm_store_ss((float*)z+3, (__m128)shuffled3);
  349. for (i = 0; i != 4; ++i) {
  350. #if __BYTE_ORDER == __LITTLE_ENDIAN
  351. memcpy (dst, z+i, 3);
  352. #elif __BYTE_ORDER == __BIG_ENDIAN
  353. memcpy (dst, (float*)((char *)&z + 1)+i, 3);
  354. #endif
  355. dst += dst_skip;
  356. }
  357. nsamples -= 4;
  358. src += 4;
  359. }
  360. #endif
  361. int32_t z;
  362. while (nsamples--) {
  363. float_24 (*src, z);
  364. #if __BYTE_ORDER == __LITTLE_ENDIAN
  365. memcpy (dst, &z, 3);
  366. #elif __BYTE_ORDER == __BIG_ENDIAN
  367. memcpy (dst, (char *)&z + 1, 3);
  368. #endif
  369. dst += dst_skip;
  370. src++;
  371. }
  372. }
  373. void sample_move_dS_s24s (jack_default_audio_sample_t *dst, char *src, unsigned long nsamples, unsigned long src_skip)
  374. {
  375. /* ALERT: signed sign-extension portability !!! */
  376. const jack_default_audio_sample_t scaling = 1.0/SAMPLE_24BIT_SCALING;
  377. while (nsamples--) {
  378. int x;
  379. #if __BYTE_ORDER == __LITTLE_ENDIAN
  380. x = (unsigned char)(src[0]);
  381. x <<= 8;
  382. x |= (unsigned char)(src[1]);
  383. x <<= 8;
  384. x |= (unsigned char)(src[2]);
  385. /* correct sign bit and the rest of the top byte */
  386. if (src[0] & 0x80) {
  387. x |= 0xff << 24;
  388. }
  389. #elif __BYTE_ORDER == __BIG_ENDIAN
  390. x = (unsigned char)(src[2]);
  391. x <<= 8;
  392. x |= (unsigned char)(src[1]);
  393. x <<= 8;
  394. x |= (unsigned char)(src[0]);
  395. /* correct sign bit and the rest of the top byte */
  396. if (src[0] & 0x80) {
  397. x |= 0xff << 24;
  398. }
  399. #endif
  400. *dst = x * scaling;
  401. dst++;
  402. src += src_skip;
  403. }
  404. }
  405. void sample_move_dS_s24 (jack_default_audio_sample_t *dst, char *src, unsigned long nsamples, unsigned long src_skip)
  406. {
  407. const jack_default_audio_sample_t scaling = 1.f/SAMPLE_24BIT_SCALING;
  408. #if defined (__SSE2__) && !defined (__sun__)
  409. const __m128 scaling_block = _mm_set_ps1(scaling);
  410. while (nsamples >= 4) {
  411. int x0, x1, x2, x3;
  412. #if __BYTE_ORDER == __LITTLE_ENDIAN
  413. memcpy((char*)&x0 + 1, src, 3);
  414. memcpy((char*)&x1 + 1, src+src_skip, 3);
  415. memcpy((char*)&x2 + 1, src+2*src_skip, 3);
  416. memcpy((char*)&x3 + 1, src+3*src_skip, 3);
  417. #elif __BYTE_ORDER == __BIG_ENDIAN
  418. memcpy(&x0, src, 3);
  419. memcpy(&x1, src+src_skip, 3);
  420. memcpy(&x2, src+2*src_skip, 3);
  421. memcpy(&x3, src+3*src_skip, 3);
  422. #endif
  423. src += 4 * src_skip;
  424. const __m128i block_i = _mm_set_epi32(x3, x2, x1, x0);
  425. const __m128i shifted = _mm_srai_epi32(block_i, 8);
  426. const __m128 converted = _mm_cvtepi32_ps (shifted);
  427. const __m128 scaled = _mm_mul_ps(converted, scaling_block);
  428. _mm_storeu_ps(dst, scaled);
  429. dst += 4;
  430. nsamples -= 4;
  431. }
  432. #endif
  433. while (nsamples--) {
  434. int x;
  435. #if __BYTE_ORDER == __LITTLE_ENDIAN
  436. memcpy((char*)&x + 1, src, 3);
  437. #elif __BYTE_ORDER == __BIG_ENDIAN
  438. memcpy(&x, src, 3);
  439. #endif
  440. x >>= 8;
  441. *dst = x * scaling;
  442. dst++;
  443. src += src_skip;
  444. }
  445. }
  446. void sample_move_d16_sSs (char *dst, jack_default_audio_sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state)
  447. {
  448. int16_t tmp;
  449. while (nsamples--) {
  450. // float_16 (*src, tmp);
  451. if (*src <= NORMALIZED_FLOAT_MIN) {
  452. tmp = SAMPLE_16BIT_MIN;
  453. } else if (*src >= NORMALIZED_FLOAT_MAX) {
  454. tmp = SAMPLE_16BIT_MAX;
  455. } else {
  456. tmp = (int16_t) f_round (*src * SAMPLE_16BIT_SCALING);
  457. }
  458. #if __BYTE_ORDER == __LITTLE_ENDIAN
  459. dst[0]=(char)(tmp>>8);
  460. dst[1]=(char)(tmp);
  461. #elif __BYTE_ORDER == __BIG_ENDIAN
  462. dst[0]=(char)(tmp);
  463. dst[1]=(char)(tmp>>8);
  464. #endif
  465. dst += dst_skip;
  466. src++;
  467. }
  468. }
  469. void sample_move_d16_sS (char *dst, jack_default_audio_sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state)
  470. {
  471. while (nsamples--) {
  472. float_16 (*src, *((int16_t*) dst));
  473. dst += dst_skip;
  474. src++;
  475. }
  476. }
  477. 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)
  478. {
  479. jack_default_audio_sample_t val;
  480. int16_t tmp;
  481. while (nsamples--) {
  482. val = (*src * SAMPLE_16BIT_SCALING) + fast_rand() / (float) UINT_MAX - 0.5f;
  483. float_16_scaled (val, tmp);
  484. #if __BYTE_ORDER == __LITTLE_ENDIAN
  485. dst[0]=(char)(tmp>>8);
  486. dst[1]=(char)(tmp);
  487. #elif __BYTE_ORDER == __BIG_ENDIAN
  488. dst[0]=(char)(tmp);
  489. dst[1]=(char)(tmp>>8);
  490. #endif
  491. dst += dst_skip;
  492. src++;
  493. }
  494. }
  495. 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)
  496. {
  497. jack_default_audio_sample_t val;
  498. while (nsamples--) {
  499. val = (*src * SAMPLE_16BIT_SCALING) + fast_rand() / (float)UINT_MAX - 0.5f;
  500. float_16_scaled (val, *((int16_t*) dst));
  501. dst += dst_skip;
  502. src++;
  503. }
  504. }
  505. 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)
  506. {
  507. jack_default_audio_sample_t val;
  508. int16_t tmp;
  509. while (nsamples--) {
  510. val = (*src * SAMPLE_16BIT_SCALING) + ((float)fast_rand() + (float)fast_rand()) / (float)UINT_MAX - 1.0f;
  511. float_16_scaled (val, tmp);
  512. #if __BYTE_ORDER == __LITTLE_ENDIAN
  513. dst[0]=(char)(tmp>>8);
  514. dst[1]=(char)(tmp);
  515. #elif __BYTE_ORDER == __BIG_ENDIAN
  516. dst[0]=(char)(tmp);
  517. dst[1]=(char)(tmp>>8);
  518. #endif
  519. dst += dst_skip;
  520. src++;
  521. }
  522. }
  523. 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)
  524. {
  525. jack_default_audio_sample_t val;
  526. while (nsamples--) {
  527. val = (*src * SAMPLE_16BIT_SCALING) + ((float)fast_rand() + (float)fast_rand()) / (float)UINT_MAX - 1.0f;
  528. float_16_scaled (val, *((int16_t*) dst));
  529. dst += dst_skip;
  530. src++;
  531. }
  532. }
  533. 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)
  534. {
  535. jack_default_audio_sample_t x;
  536. jack_default_audio_sample_t xe; /* the innput sample - filtered error */
  537. jack_default_audio_sample_t xp; /* x' */
  538. float r;
  539. float rm1 = state->rm1;
  540. unsigned int idx = state->idx;
  541. int16_t tmp;
  542. while (nsamples--) {
  543. x = *src * SAMPLE_16BIT_SCALING;
  544. r = ((float)fast_rand() + (float)fast_rand()) / (float)UINT_MAX - 1.0f;
  545. /* Filter the error with Lipshitz's minimally audible FIR:
  546. [2.033 -2.165 1.959 -1.590 0.6149] */
  547. xe = x
  548. - state->e[idx] * 2.033f
  549. + state->e[(idx - 1) & DITHER_BUF_MASK] * 2.165f
  550. - state->e[(idx - 2) & DITHER_BUF_MASK] * 1.959f
  551. + state->e[(idx - 3) & DITHER_BUF_MASK] * 1.590f
  552. - state->e[(idx - 4) & DITHER_BUF_MASK] * 0.6149f;
  553. xp = xe + r - rm1;
  554. rm1 = r;
  555. float_16_scaled (xp, tmp);
  556. /* Intrinsic z^-1 delay */
  557. idx = (idx + 1) & DITHER_BUF_MASK;
  558. state->e[idx] = xp - xe;
  559. #if __BYTE_ORDER == __LITTLE_ENDIAN
  560. dst[0]=(char)(tmp>>8);
  561. dst[1]=(char)(tmp);
  562. #elif __BYTE_ORDER == __BIG_ENDIAN
  563. dst[0]=(char)(tmp);
  564. dst[1]=(char)(tmp>>8);
  565. #endif
  566. dst += dst_skip;
  567. src++;
  568. }
  569. state->rm1 = rm1;
  570. state->idx = idx;
  571. }
  572. 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)
  573. {
  574. jack_default_audio_sample_t x;
  575. jack_default_audio_sample_t xe; /* the innput sample - filtered error */
  576. jack_default_audio_sample_t xp; /* x' */
  577. float r;
  578. float rm1 = state->rm1;
  579. unsigned int idx = state->idx;
  580. while (nsamples--) {
  581. x = *src * SAMPLE_16BIT_SCALING;
  582. r = ((float)fast_rand() + (float)fast_rand()) / (float)UINT_MAX - 1.0f;
  583. /* Filter the error with Lipshitz's minimally audible FIR:
  584. [2.033 -2.165 1.959 -1.590 0.6149] */
  585. xe = x
  586. - state->e[idx] * 2.033f
  587. + state->e[(idx - 1) & DITHER_BUF_MASK] * 2.165f
  588. - state->e[(idx - 2) & DITHER_BUF_MASK] * 1.959f
  589. + state->e[(idx - 3) & DITHER_BUF_MASK] * 1.590f
  590. - state->e[(idx - 4) & DITHER_BUF_MASK] * 0.6149f;
  591. xp = xe + r - rm1;
  592. rm1 = r;
  593. float_16_scaled (xp, *((int16_t*) dst));
  594. /* Intrinsic z^-1 delay */
  595. idx = (idx + 1) & DITHER_BUF_MASK;
  596. state->e[idx] = *((int16_t*) dst) - xe;
  597. dst += dst_skip;
  598. src++;
  599. }
  600. state->rm1 = rm1;
  601. state->idx = idx;
  602. }
  603. void sample_move_dS_s16s (jack_default_audio_sample_t *dst, char *src, unsigned long nsamples, unsigned long src_skip)
  604. {
  605. short z;
  606. const jack_default_audio_sample_t scaling = 1.0/SAMPLE_16BIT_SCALING;
  607. /* ALERT: signed sign-extension portability !!! */
  608. while (nsamples--) {
  609. #if __BYTE_ORDER == __LITTLE_ENDIAN
  610. z = (unsigned char)(src[0]);
  611. z <<= 8;
  612. z |= (unsigned char)(src[1]);
  613. #elif __BYTE_ORDER == __BIG_ENDIAN
  614. z = (unsigned char)(src[1]);
  615. z <<= 8;
  616. z |= (unsigned char)(src[0]);
  617. #endif
  618. *dst = z * scaling;
  619. dst++;
  620. src += src_skip;
  621. }
  622. }
  623. void sample_move_dS_s16 (jack_default_audio_sample_t *dst, char *src, unsigned long nsamples, unsigned long src_skip)
  624. {
  625. /* ALERT: signed sign-extension portability !!! */
  626. const jack_default_audio_sample_t scaling = 1.0/SAMPLE_16BIT_SCALING;
  627. while (nsamples--) {
  628. *dst = (*((short *) src)) * scaling;
  629. dst++;
  630. src += src_skip;
  631. }
  632. }
  633. void memset_interleave (char *dst, char val, unsigned long bytes,
  634. unsigned long unit_bytes,
  635. unsigned long skip_bytes)
  636. {
  637. switch (unit_bytes) {
  638. case 1:
  639. while (bytes--) {
  640. *dst = val;
  641. dst += skip_bytes;
  642. }
  643. break;
  644. case 2:
  645. while (bytes) {
  646. *((short *) dst) = (short) val;
  647. dst += skip_bytes;
  648. bytes -= 2;
  649. }
  650. break;
  651. case 4:
  652. while (bytes) {
  653. *((int *) dst) = (int) val;
  654. dst += skip_bytes;
  655. bytes -= 4;
  656. }
  657. break;
  658. default:
  659. while (bytes) {
  660. memset(dst, val, unit_bytes);
  661. dst += skip_bytes;
  662. bytes -= unit_bytes;
  663. }
  664. break;
  665. }
  666. }
  667. /* COPY FUNCTIONS: used to move data from an input channel to an
  668. output channel. Note that we assume that the skip distance
  669. is the same for both channels. This is completely fine
  670. unless the input and output were on different audio interfaces that
  671. were interleaved differently. We don't try to handle that.
  672. */
  673. void
  674. memcpy_fake (char *dst, char *src, unsigned long src_bytes, unsigned long foo, unsigned long bar)
  675. {
  676. memcpy (dst, src, src_bytes);
  677. }
  678. void
  679. memcpy_interleave_d16_s16 (char *dst, char *src, unsigned long src_bytes,
  680. unsigned long dst_skip_bytes, unsigned long src_skip_bytes)
  681. {
  682. while (src_bytes) {
  683. *((short *) dst) = *((short *) src);
  684. dst += dst_skip_bytes;
  685. src += src_skip_bytes;
  686. src_bytes -= 2;
  687. }
  688. }
  689. void
  690. memcpy_interleave_d24_s24 (char *dst, char *src, unsigned long src_bytes,
  691. unsigned long dst_skip_bytes, unsigned long src_skip_bytes)
  692. {
  693. while (src_bytes) {
  694. memcpy(dst, src, 3);
  695. dst += dst_skip_bytes;
  696. src += src_skip_bytes;
  697. src_bytes -= 3;
  698. }
  699. }
  700. void
  701. memcpy_interleave_d32_s32 (char *dst, char *src, unsigned long src_bytes,
  702. unsigned long dst_skip_bytes, unsigned long src_skip_bytes)
  703. {
  704. while (src_bytes) {
  705. *((int *) dst) = *((int *) src);
  706. dst += dst_skip_bytes;
  707. src += src_skip_bytes;
  708. src_bytes -= 4;
  709. }
  710. }