jack1 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.

804 lines
21KB

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