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.

846 lines
23KB

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