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.

751 lines
20KB

  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. /* Linear Congruential noise generator. From the music-dsp list
  133. * less random than rand(), but good enough and 10x faster
  134. */
  135. static unsigned int seed = 22222;
  136. inline unsigned int fast_rand() {
  137. seed = (seed * 96314165) + 907633515;
  138. return seed;
  139. }
  140. /* functions for native float sample data */
  141. void sample_move_floatLE_sSs (jack_default_audio_sample_t *dst, char *src, unsigned long nsamples, unsigned long src_skip) {
  142. while (nsamples--) {
  143. *dst = *((float *) src);
  144. dst++;
  145. src += src_skip;
  146. }
  147. }
  148. void sample_move_dS_floatLE (char *dst, jack_default_audio_sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state) {
  149. while (nsamples--) {
  150. *((float *) dst) = *src;
  151. dst += dst_skip;
  152. src++;
  153. }
  154. }
  155. /* NOTES on function naming:
  156. foo_bar_d<TYPE>_s<TYPE>
  157. the "d<TYPE>" component defines the destination type for the operation
  158. the "s<TYPE>" component defines the source type for the operation
  159. TYPE can be one of:
  160. S - sample is a jack_default_audio_sample_t, currently (October 2008) a 32 bit floating point value
  161. Ss - like S but reverse endian from the host CPU
  162. 32u24 - sample is an signed 32 bit integer value, but data is in upper 24 bits only
  163. 32u24s - like 32u24 but reverse endian from the host CPU
  164. 24 - sample is an signed 24 bit integer value
  165. 24s - like 24 but reverse endian from the host CPU
  166. 16 - sample is an signed 16 bit integer value
  167. 16s - like 16 but reverse endian from the host CPU
  168. For obvious reasons, the reverse endian versions only show as source types.
  169. This covers all known sample formats at 16 bits or larger.
  170. */
  171. /* functions for native integer sample data */
  172. void sample_move_d32u24_sSs (char *dst, jack_default_audio_sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state)
  173. {
  174. int32_t z;
  175. while (nsamples--) {
  176. float_24u32 (*src, z);
  177. #if __BYTE_ORDER == __LITTLE_ENDIAN
  178. dst[0]=(char)(z>>24);
  179. dst[1]=(char)(z>>16);
  180. dst[2]=(char)(z>>8);
  181. dst[3]=(char)(z);
  182. #elif __BYTE_ORDER == __BIG_ENDIAN
  183. dst[0]=(char)(z);
  184. dst[1]=(char)(z>>8);
  185. dst[2]=(char)(z>>16);
  186. dst[3]=(char)(z>>24);
  187. #endif
  188. dst += dst_skip;
  189. src++;
  190. }
  191. }
  192. void sample_move_d32u24_sS (char *dst, jack_default_audio_sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state)
  193. {
  194. #if defined (__SSE2__) && !defined (__sun__)
  195. __m128 int_max = _mm_set1_ps(SAMPLE_24BIT_MAX_F);
  196. __m128 int_min = _mm_sub_ps(_mm_setzero_ps(), int_max);
  197. __m128 factor = int_max;
  198. unsigned long unrolled = nsamples / 4;
  199. nsamples = nsamples & 3;
  200. while (unrolled--) {
  201. __m128 in = _mm_load_ps(src);
  202. __m128 scaled = _mm_mul_ps(in, factor);
  203. __m128 clipped = _mm_min_ps(int_max, _mm_max_ps(scaled, int_min));
  204. __m128i y = _mm_cvttps_epi32(clipped);
  205. __m128i shifted = _mm_slli_epi32(y, 8);
  206. __m128i shuffled1 = _mm_shuffle_epi32(shifted, _MM_SHUFFLE(0, 3, 2, 1));
  207. __m128i shuffled2 = _mm_shuffle_epi32(shifted, _MM_SHUFFLE(1, 0, 3, 2));
  208. __m128i shuffled3 = _mm_shuffle_epi32(shifted, _MM_SHUFFLE(2, 1, 0, 3));
  209. _mm_store_ss((float*)dst, (__m128)shifted);
  210. dst += dst_skip;
  211. _mm_store_ss((float*)dst, (__m128)shuffled1);
  212. dst += dst_skip;
  213. _mm_store_ss((float*)dst, (__m128)shuffled2);
  214. dst += dst_skip;
  215. _mm_store_ss((float*)dst, (__m128)shuffled3);
  216. dst += dst_skip;
  217. src+= 4;
  218. }
  219. while (nsamples--) {
  220. __m128 in = _mm_load_ss(src);
  221. __m128 scaled = _mm_mul_ss(in, factor);
  222. __m128 clipped = _mm_min_ss(int_max, _mm_max_ss(scaled, int_min));
  223. int y = _mm_cvttss_si32(clipped);
  224. *((int *) dst) = y<<8;
  225. dst += dst_skip;
  226. src++;
  227. }
  228. #else
  229. while (nsamples--) {
  230. float_24u32 (*src, *((int32_t*) dst));
  231. dst += dst_skip;
  232. src++;
  233. }
  234. #endif
  235. }
  236. void sample_move_dS_s32u24s (jack_default_audio_sample_t *dst, char *src, unsigned long nsamples, unsigned long src_skip)
  237. {
  238. /* ALERT: signed sign-extension portability !!! */
  239. while (nsamples--) {
  240. int x;
  241. #if __BYTE_ORDER == __LITTLE_ENDIAN
  242. x = (unsigned char)(src[0]);
  243. x <<= 8;
  244. x |= (unsigned char)(src[1]);
  245. x <<= 8;
  246. x |= (unsigned char)(src[2]);
  247. x <<= 8;
  248. x |= (unsigned char)(src[3]);
  249. #elif __BYTE_ORDER == __BIG_ENDIAN
  250. x = (unsigned char)(src[3]);
  251. x <<= 8;
  252. x |= (unsigned char)(src[2]);
  253. x <<= 8;
  254. x |= (unsigned char)(src[1]);
  255. x <<= 8;
  256. x |= (unsigned char)(src[0]);
  257. #endif
  258. *dst = (x >> 8) / SAMPLE_24BIT_SCALING;
  259. dst++;
  260. src += src_skip;
  261. }
  262. }
  263. void sample_move_dS_s32u24 (jack_default_audio_sample_t *dst, char *src, unsigned long nsamples, unsigned long src_skip)
  264. {
  265. #if defined (__SSE2__) && !defined (__sun__)
  266. unsigned long unrolled = nsamples / 4;
  267. static float inv_sample_max_24bit = 1.0 / SAMPLE_24BIT_SCALING;
  268. __m128 factor = _mm_set1_ps(inv_sample_max_24bit);
  269. while (unrolled--)
  270. {
  271. int i1 = *((int *) src);
  272. src+= src_skip;
  273. int i2 = *((int *) src);
  274. src+= src_skip;
  275. int i3 = *((int *) src);
  276. src+= src_skip;
  277. int i4 = *((int *) src);
  278. src+= src_skip;
  279. __m128i src = _mm_set_epi32(i1, i2, i3, i4);
  280. __m128i shifted = _mm_srai_epi32(src, 8);
  281. __m128 as_float = _mm_cvtepi32_ps(shifted);
  282. __m128 divided = _mm_mul_ps(as_float, factor);
  283. _mm_storeu_ps(dst, divided);
  284. dst += 4;
  285. }
  286. nsamples = nsamples & 3;
  287. #endif
  288. /* ALERT: signed sign-extension portability !!! */
  289. while (nsamples--) {
  290. *dst = (*((int *) src) >> 8) / SAMPLE_24BIT_SCALING;
  291. dst++;
  292. src += src_skip;
  293. }
  294. }
  295. void sample_move_d24_sSs (char *dst, jack_default_audio_sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state)
  296. {
  297. int32_t z;
  298. while (nsamples--) {
  299. float_24 (*src, z);
  300. #if __BYTE_ORDER == __LITTLE_ENDIAN
  301. dst[0]=(char)(z>>16);
  302. dst[1]=(char)(z>>8);
  303. dst[2]=(char)(z);
  304. #elif __BYTE_ORDER == __BIG_ENDIAN
  305. dst[0]=(char)(z);
  306. dst[1]=(char)(z>>8);
  307. dst[2]=(char)(z>>16);
  308. #endif
  309. dst += dst_skip;
  310. src++;
  311. }
  312. }
  313. void sample_move_d24_sS (char *dst, jack_default_audio_sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state)
  314. {
  315. int32_t z;
  316. while (nsamples--) {
  317. float_24 (*src, z);
  318. #if __BYTE_ORDER == __LITTLE_ENDIAN
  319. memcpy (dst, &z, 3);
  320. #elif __BYTE_ORDER == __BIG_ENDIAN
  321. memcpy (dst, (char *)&z + 1, 3);
  322. #endif
  323. dst += dst_skip;
  324. src++;
  325. }
  326. }
  327. void sample_move_dS_s24s (jack_default_audio_sample_t *dst, char *src, unsigned long nsamples, unsigned long src_skip)
  328. {
  329. /* ALERT: signed sign-extension portability !!! */
  330. while (nsamples--) {
  331. int x;
  332. #if __BYTE_ORDER == __LITTLE_ENDIAN
  333. x = (unsigned char)(src[0]);
  334. x <<= 8;
  335. x |= (unsigned char)(src[1]);
  336. x <<= 8;
  337. x |= (unsigned char)(src[2]);
  338. /* correct sign bit and the rest of the top byte */
  339. if (src[0] & 0x80) {
  340. x |= 0xff << 24;
  341. }
  342. #elif __BYTE_ORDER == __BIG_ENDIAN
  343. x = (unsigned char)(src[2]);
  344. x <<= 8;
  345. x |= (unsigned char)(src[1]);
  346. x <<= 8;
  347. x |= (unsigned char)(src[0]);
  348. /* correct sign bit and the rest of the top byte */
  349. if (src[0] & 0x80) {
  350. x |= 0xff << 24;
  351. }
  352. #endif
  353. *dst = x / SAMPLE_24BIT_SCALING;
  354. dst++;
  355. src += src_skip;
  356. }
  357. }
  358. void sample_move_dS_s24 (jack_default_audio_sample_t *dst, char *src, unsigned long nsamples, unsigned long src_skip)
  359. {
  360. /* ALERT: signed sign-extension portability !!! */
  361. while (nsamples--) {
  362. int x;
  363. #if __BYTE_ORDER == __LITTLE_ENDIAN
  364. memcpy((char*)&x + 1, src, 3);
  365. #elif __BYTE_ORDER == __BIG_ENDIAN
  366. memcpy(&x, src, 3);
  367. #endif
  368. x >>= 8;
  369. *dst = x / SAMPLE_24BIT_SCALING;
  370. dst++;
  371. src += src_skip;
  372. }
  373. }
  374. void sample_move_d16_sSs (char *dst, jack_default_audio_sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state)
  375. {
  376. int16_t tmp;
  377. while (nsamples--) {
  378. // float_16 (*src, tmp);
  379. if (*src <= NORMALIZED_FLOAT_MIN) {
  380. tmp = SAMPLE_16BIT_MIN;
  381. } else if (*src >= NORMALIZED_FLOAT_MAX) {
  382. tmp = SAMPLE_16BIT_MAX;
  383. } else {
  384. tmp = (int16_t) f_round (*src * SAMPLE_16BIT_SCALING);
  385. }
  386. #if __BYTE_ORDER == __LITTLE_ENDIAN
  387. dst[0]=(char)(tmp>>8);
  388. dst[1]=(char)(tmp);
  389. #elif __BYTE_ORDER == __BIG_ENDIAN
  390. dst[0]=(char)(tmp);
  391. dst[1]=(char)(tmp>>8);
  392. #endif
  393. dst += dst_skip;
  394. src++;
  395. }
  396. }
  397. void sample_move_d16_sS (char *dst, jack_default_audio_sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state)
  398. {
  399. while (nsamples--) {
  400. float_16 (*src, *((int16_t*) dst));
  401. dst += dst_skip;
  402. src++;
  403. }
  404. }
  405. 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)
  406. {
  407. jack_default_audio_sample_t val;
  408. int16_t tmp;
  409. while (nsamples--) {
  410. val = (*src * SAMPLE_16BIT_SCALING) + fast_rand() / (float) UINT_MAX - 0.5f;
  411. float_16_scaled (val, tmp);
  412. #if __BYTE_ORDER == __LITTLE_ENDIAN
  413. dst[0]=(char)(tmp>>8);
  414. dst[1]=(char)(tmp);
  415. #elif __BYTE_ORDER == __BIG_ENDIAN
  416. dst[0]=(char)(tmp);
  417. dst[1]=(char)(tmp>>8);
  418. #endif
  419. dst += dst_skip;
  420. src++;
  421. }
  422. }
  423. 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)
  424. {
  425. jack_default_audio_sample_t val;
  426. while (nsamples--) {
  427. val = (*src * SAMPLE_16BIT_SCALING) + fast_rand() / (float)UINT_MAX - 0.5f;
  428. float_16_scaled (val, *((int16_t*) dst));
  429. dst += dst_skip;
  430. src++;
  431. }
  432. }
  433. 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)
  434. {
  435. jack_default_audio_sample_t val;
  436. int16_t tmp;
  437. while (nsamples--) {
  438. val = (*src * SAMPLE_16BIT_SCALING) + ((float)fast_rand() + (float)fast_rand()) / (float)UINT_MAX - 1.0f;
  439. float_16_scaled (val, tmp);
  440. #if __BYTE_ORDER == __LITTLE_ENDIAN
  441. dst[0]=(char)(tmp>>8);
  442. dst[1]=(char)(tmp);
  443. #elif __BYTE_ORDER == __BIG_ENDIAN
  444. dst[0]=(char)(tmp);
  445. dst[1]=(char)(tmp>>8);
  446. #endif
  447. dst += dst_skip;
  448. src++;
  449. }
  450. }
  451. 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)
  452. {
  453. jack_default_audio_sample_t val;
  454. while (nsamples--) {
  455. val = (*src * SAMPLE_16BIT_SCALING) + ((float)fast_rand() + (float)fast_rand()) / (float)UINT_MAX - 1.0f;
  456. float_16_scaled (val, *((int16_t*) dst));
  457. dst += dst_skip;
  458. src++;
  459. }
  460. }
  461. 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)
  462. {
  463. jack_default_audio_sample_t x;
  464. jack_default_audio_sample_t xe; /* the innput sample - filtered error */
  465. jack_default_audio_sample_t xp; /* x' */
  466. float r;
  467. float rm1 = state->rm1;
  468. unsigned int idx = state->idx;
  469. int16_t tmp;
  470. while (nsamples--) {
  471. x = *src * SAMPLE_16BIT_SCALING;
  472. r = ((float)fast_rand() + (float)fast_rand()) / (float)UINT_MAX - 1.0f;
  473. /* Filter the error with Lipshitz's minimally audible FIR:
  474. [2.033 -2.165 1.959 -1.590 0.6149] */
  475. xe = x
  476. - state->e[idx] * 2.033f
  477. + state->e[(idx - 1) & DITHER_BUF_MASK] * 2.165f
  478. - state->e[(idx - 2) & DITHER_BUF_MASK] * 1.959f
  479. + state->e[(idx - 3) & DITHER_BUF_MASK] * 1.590f
  480. - state->e[(idx - 4) & DITHER_BUF_MASK] * 0.6149f;
  481. xp = xe + r - rm1;
  482. rm1 = r;
  483. float_16_scaled (xp, tmp);
  484. /* Intrinsic z^-1 delay */
  485. idx = (idx + 1) & DITHER_BUF_MASK;
  486. state->e[idx] = xp - xe;
  487. #if __BYTE_ORDER == __LITTLE_ENDIAN
  488. dst[0]=(char)(tmp>>8);
  489. dst[1]=(char)(tmp);
  490. #elif __BYTE_ORDER == __BIG_ENDIAN
  491. dst[0]=(char)(tmp);
  492. dst[1]=(char)(tmp>>8);
  493. #endif
  494. dst += dst_skip;
  495. src++;
  496. }
  497. state->rm1 = rm1;
  498. state->idx = idx;
  499. }
  500. 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)
  501. {
  502. jack_default_audio_sample_t x;
  503. jack_default_audio_sample_t xe; /* the innput sample - filtered error */
  504. jack_default_audio_sample_t xp; /* x' */
  505. float r;
  506. float rm1 = state->rm1;
  507. unsigned int idx = state->idx;
  508. while (nsamples--) {
  509. x = *src * SAMPLE_16BIT_SCALING;
  510. r = ((float)fast_rand() + (float)fast_rand()) / (float)UINT_MAX - 1.0f;
  511. /* Filter the error with Lipshitz's minimally audible FIR:
  512. [2.033 -2.165 1.959 -1.590 0.6149] */
  513. xe = x
  514. - state->e[idx] * 2.033f
  515. + state->e[(idx - 1) & DITHER_BUF_MASK] * 2.165f
  516. - state->e[(idx - 2) & DITHER_BUF_MASK] * 1.959f
  517. + state->e[(idx - 3) & DITHER_BUF_MASK] * 1.590f
  518. - state->e[(idx - 4) & DITHER_BUF_MASK] * 0.6149f;
  519. xp = xe + r - rm1;
  520. rm1 = r;
  521. float_16_scaled (xp, *((int16_t*) dst));
  522. /* Intrinsic z^-1 delay */
  523. idx = (idx + 1) & DITHER_BUF_MASK;
  524. state->e[idx] = *((int16_t*) dst) - xe;
  525. dst += dst_skip;
  526. src++;
  527. }
  528. state->rm1 = rm1;
  529. state->idx = idx;
  530. }
  531. void sample_move_dS_s16s (jack_default_audio_sample_t *dst, char *src, unsigned long nsamples, unsigned long src_skip)
  532. {
  533. short z;
  534. /* ALERT: signed sign-extension portability !!! */
  535. while (nsamples--) {
  536. #if __BYTE_ORDER == __LITTLE_ENDIAN
  537. z = (unsigned char)(src[0]);
  538. z <<= 8;
  539. z |= (unsigned char)(src[1]);
  540. #elif __BYTE_ORDER == __BIG_ENDIAN
  541. z = (unsigned char)(src[1]);
  542. z <<= 8;
  543. z |= (unsigned char)(src[0]);
  544. #endif
  545. *dst = z / SAMPLE_16BIT_SCALING;
  546. dst++;
  547. src += src_skip;
  548. }
  549. }
  550. void sample_move_dS_s16 (jack_default_audio_sample_t *dst, char *src, unsigned long nsamples, unsigned long src_skip)
  551. {
  552. /* ALERT: signed sign-extension portability !!! */
  553. while (nsamples--) {
  554. *dst = (*((short *) src)) / SAMPLE_16BIT_SCALING;
  555. dst++;
  556. src += src_skip;
  557. }
  558. }
  559. void memset_interleave (char *dst, char val, unsigned long bytes,
  560. unsigned long unit_bytes,
  561. unsigned long skip_bytes)
  562. {
  563. switch (unit_bytes) {
  564. case 1:
  565. while (bytes--) {
  566. *dst = val;
  567. dst += skip_bytes;
  568. }
  569. break;
  570. case 2:
  571. while (bytes) {
  572. *((short *) dst) = (short) val;
  573. dst += skip_bytes;
  574. bytes -= 2;
  575. }
  576. break;
  577. case 4:
  578. while (bytes) {
  579. *((int *) dst) = (int) val;
  580. dst += skip_bytes;
  581. bytes -= 4;
  582. }
  583. break;
  584. default:
  585. while (bytes) {
  586. memset(dst, val, unit_bytes);
  587. dst += skip_bytes;
  588. bytes -= unit_bytes;
  589. }
  590. break;
  591. }
  592. }
  593. /* COPY FUNCTIONS: used to move data from an input channel to an
  594. output channel. Note that we assume that the skip distance
  595. is the same for both channels. This is completely fine
  596. unless the input and output were on different audio interfaces that
  597. were interleaved differently. We don't try to handle that.
  598. */
  599. void
  600. memcpy_fake (char *dst, char *src, unsigned long src_bytes, unsigned long foo, unsigned long bar)
  601. {
  602. memcpy (dst, src, src_bytes);
  603. }
  604. void
  605. memcpy_interleave_d16_s16 (char *dst, char *src, unsigned long src_bytes,
  606. unsigned long dst_skip_bytes, unsigned long src_skip_bytes)
  607. {
  608. while (src_bytes) {
  609. *((short *) dst) = *((short *) src);
  610. dst += dst_skip_bytes;
  611. src += src_skip_bytes;
  612. src_bytes -= 2;
  613. }
  614. }
  615. void
  616. memcpy_interleave_d24_s24 (char *dst, char *src, unsigned long src_bytes,
  617. unsigned long dst_skip_bytes, unsigned long src_skip_bytes)
  618. {
  619. while (src_bytes) {
  620. memcpy(dst, src, 3);
  621. dst += dst_skip_bytes;
  622. src += src_skip_bytes;
  623. src_bytes -= 3;
  624. }
  625. }
  626. void
  627. memcpy_interleave_d32_s32 (char *dst, char *src, unsigned long src_bytes,
  628. unsigned long dst_skip_bytes, unsigned long src_skip_bytes)
  629. {
  630. while (src_bytes) {
  631. *((int *) dst) = *((int *) src);
  632. dst += dst_skip_bytes;
  633. src += src_skip_bytes;
  634. src_bytes -= 4;
  635. }
  636. }