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.

1227 lines
30KB

  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 <limits.h>
  25. #include <endian.h>
  26. #include <jack/memops.h>
  27. #define SAMPLE_MAX_24BIT 8388608.0f
  28. #define SAMPLE_MAX_16BIT 32768.0f
  29. /* define these in case we end up on a platform that is missing
  30. the real lrintf functions
  31. */
  32. #define f_round(f) lrintf(f)
  33. #define f_roundl(f) llrintf(f)
  34. /* Linear Congruential noise generator. From the music-dsp list
  35. * less random than rand(), but good enough and 10x faster */
  36. inline unsigned int fast_rand();
  37. inline unsigned int fast_rand() {
  38. static unsigned int seed = 22222;
  39. seed = (seed * 96314165) + 907633515;
  40. return seed;
  41. }
  42. /* functions for native float sample data */
  43. void sample_move_floatLE_sSs (jack_default_audio_sample_t *dst, char *src, unsigned long nsamples, unsigned long src_skip) {
  44. while (nsamples--) {
  45. *dst = *((float *) src);
  46. dst++;
  47. src += src_skip;
  48. }
  49. }
  50. void sample_move_dS_floatLE (char *dst, jack_default_audio_sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state) {
  51. while (nsamples--) {
  52. *((float *) dst) = *src;
  53. dst += dst_skip;
  54. src++;
  55. }
  56. }
  57. /* NOTES on function naming:
  58. foo_bar_d<TYPE>_s<TYPE>
  59. the "d<TYPE>" component defines the destination type for the operation
  60. the "s<TYPE>" component defines the source type for the operation
  61. TYPE can be one of:
  62. S - sample is a jack_default_audio_sample_t, currently (October 2008) a 32 bit floating point value
  63. Ss - like S but reverse endian from the host CPU
  64. 32u24 - sample is an signed 32 bit integer value, but data is in upper 24 bits only
  65. 32u24s - like 32u24 but reverse endian from the host CPU
  66. 24 - sample is an signed 24 bit integer value
  67. 24s - like 24 but reverse endian from the host CPU
  68. 16 - sample is an signed 16 bit integer value
  69. 16s - like 16 but reverse endian from the host CPU
  70. For obvious reasons, the reverse endian versions only show as source types.
  71. This covers all known sample formats at 16 bits or larger.
  72. */
  73. /* functions for native integer sample data */
  74. void sample_move_d32u24_sSs (char *dst, jack_default_audio_sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state)
  75. {
  76. long long y;
  77. int z;
  78. while (nsamples--) {
  79. y = f_roundl(*src * SAMPLE_MAX_24BIT) << 8;
  80. if (y > INT_MAX) {
  81. z = INT_MAX;
  82. } else if (y < INT_MIN) {
  83. z = INT_MIN;
  84. } else {
  85. z = (int)y;
  86. }
  87. #if __BYTE_ORDER == __LITTLE_ENDIAN
  88. dst[0]=(char)(z>>24);
  89. dst[1]=(char)(z>>16);
  90. dst[2]=(char)(z>>8);
  91. dst[3]=(char)(z);
  92. #elif __BYTE_ORDER == __BIG_ENDIAN
  93. dst[0]=(char)(z);
  94. dst[1]=(char)(z>>8);
  95. dst[2]=(char)(z>>16);
  96. dst[3]=(char)(z>>24);
  97. #endif
  98. dst += dst_skip;
  99. src++;
  100. }
  101. }
  102. void sample_move_d32u24_sS (char *dst, jack_default_audio_sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state)
  103. {
  104. long long y;
  105. while (nsamples--) {
  106. y = f_roundl(*src * SAMPLE_MAX_24BIT) << 8;
  107. if (y > INT_MAX) {
  108. *((int *) dst) = INT_MAX;
  109. } else if (y < INT_MIN) {
  110. *((int *) dst) = INT_MIN;
  111. } else {
  112. *((int *) dst) = (int)y;
  113. }
  114. dst += dst_skip;
  115. src++;
  116. }
  117. }
  118. void sample_move_dS_s32u24s (jack_default_audio_sample_t *dst, char *src, unsigned long nsamples, unsigned long src_skip)
  119. {
  120. /* ALERT: signed sign-extension portability !!! */
  121. while (nsamples--) {
  122. int x;
  123. #if __BYTE_ORDER == __LITTLE_ENDIAN
  124. x = (unsigned char)(src[0]);
  125. x <<= 8;
  126. x |= (unsigned char)(src[1]);
  127. x <<= 8;
  128. x |= (unsigned char)(src[2]);
  129. x <<= 8;
  130. x |= (unsigned char)(src[3]);
  131. #elif __BYTE_ORDER == __BIG_ENDIAN
  132. x = (unsigned char)(src[3]);
  133. x <<= 8;
  134. x |= (unsigned char)(src[2]);
  135. x <<= 8;
  136. x |= (unsigned char)(src[1]);
  137. x <<= 8;
  138. x |= (unsigned char)(src[0]);
  139. #endif
  140. *dst = (x >> 8) / SAMPLE_MAX_24BIT;
  141. dst++;
  142. src += src_skip;
  143. }
  144. }
  145. void sample_move_dS_s32u24 (jack_default_audio_sample_t *dst, char *src, unsigned long nsamples, unsigned long src_skip)
  146. {
  147. /* ALERT: signed sign-extension portability !!! */
  148. while (nsamples--) {
  149. *dst = (*((int *) src) >> 8) / SAMPLE_MAX_24BIT;
  150. dst++;
  151. src += src_skip;
  152. }
  153. }
  154. void sample_move_dither_rect_d32u24_sSs (char *dst, jack_default_audio_sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state)
  155. {
  156. /* ALERT: signed sign-extension portability !!! */
  157. jack_default_audio_sample_t x;
  158. long long y;
  159. int z;
  160. while (nsamples--) {
  161. x = *src * SAMPLE_MAX_16BIT;
  162. x -= (float)fast_rand() / (float)INT_MAX;
  163. y = f_roundl(x);
  164. y <<= 16;
  165. if (y > INT_MAX) {
  166. z = INT_MAX;
  167. } else if (y < INT_MIN) {
  168. z = INT_MIN;
  169. } else {
  170. z = (int) y;
  171. }
  172. #if __BYTE_ORDER == __LITTLE_ENDIAN
  173. dst[0]=(char)(z>>24);
  174. dst[1]=(char)(z>>16);
  175. dst[2]=(char)(z>>8);
  176. dst[3]=(char)(z);
  177. #elif __BYTE_ORDER == __BIG_ENDIAN
  178. dst[0]=(char)(z);
  179. dst[1]=(char)(z>>8);
  180. dst[2]=(char)(z>>16);
  181. dst[3]=(char)(z>>24);
  182. #endif
  183. dst += dst_skip;
  184. src++;
  185. }
  186. }
  187. void sample_move_dither_rect_d32u24_sS (char *dst, jack_default_audio_sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state)
  188. {
  189. /* ALERT: signed sign-extension portability !!! */
  190. jack_default_audio_sample_t x;
  191. long long y;
  192. while (nsamples--) {
  193. x = *src * SAMPLE_MAX_16BIT;
  194. x -= (float)fast_rand() / (float)INT_MAX;
  195. y = f_roundl(x);
  196. y <<= 16;
  197. if (y > INT_MAX) {
  198. *((int *) dst) = INT_MAX;
  199. } else if (y < INT_MIN) {
  200. *((int *) dst) = INT_MIN;
  201. } else {
  202. *((int *) dst) = (int) y;
  203. }
  204. dst += dst_skip;
  205. src++;
  206. }
  207. }
  208. void sample_move_dither_tri_d32u24_sSs (char *dst, jack_default_audio_sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state)
  209. {
  210. jack_default_audio_sample_t x;
  211. float r;
  212. float rm1 = state->rm1;
  213. long long y;
  214. int z;
  215. while (nsamples--) {
  216. x = *src * (float)SAMPLE_MAX_16BIT;
  217. r = 2.0f * (float)fast_rand() / (float)INT_MAX - 1.0f;
  218. x += r - rm1;
  219. rm1 = r;
  220. y = f_roundl(x);
  221. y <<= 16;
  222. if (y > INT_MAX) {
  223. z = INT_MAX;
  224. } else if (y < INT_MIN) {
  225. z = INT_MIN;
  226. } else {
  227. z = (int)y;
  228. }
  229. #if __BYTE_ORDER == __LITTLE_ENDIAN
  230. dst[0]=(char)(z>>24);
  231. dst[1]=(char)(z>>16);
  232. dst[2]=(char)(z>>8);
  233. dst[3]=(char)(z);
  234. #elif __BYTE_ORDER == __BIG_ENDIAN
  235. dst[0]=(char)(z);
  236. dst[1]=(char)(z>>8);
  237. dst[2]=(char)(z>>16);
  238. dst[3]=(char)(z>>24);
  239. #endif
  240. dst += dst_skip;
  241. src++;
  242. }
  243. state->rm1 = rm1;
  244. }
  245. void sample_move_dither_tri_d32u24_sS (char *dst, jack_default_audio_sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state)
  246. {
  247. jack_default_audio_sample_t x;
  248. float r;
  249. float rm1 = state->rm1;
  250. long long y;
  251. while (nsamples--) {
  252. x = *src * (float)SAMPLE_MAX_16BIT;
  253. r = 2.0f * (float)fast_rand() / (float)INT_MAX - 1.0f;
  254. x += r - rm1;
  255. rm1 = r;
  256. y = f_roundl(x);
  257. y <<= 16;
  258. if (y > INT_MAX) {
  259. *((int *) dst) = INT_MAX;
  260. } else if (y < INT_MIN) {
  261. *((int *) dst) = INT_MIN;
  262. } else {
  263. *((int *) dst) = (int)y;
  264. }
  265. dst += dst_skip;
  266. src++;
  267. }
  268. state->rm1 = rm1;
  269. }
  270. void sample_move_dither_shaped_d32u24_sSs (char *dst, jack_default_audio_sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state)
  271. {
  272. jack_default_audio_sample_t x;
  273. jack_default_audio_sample_t xe; /* the innput sample - filtered error */
  274. jack_default_audio_sample_t xp; /* x' */
  275. float r;
  276. float rm1 = state->rm1;
  277. unsigned int idx = state->idx;
  278. long long y;
  279. int z;
  280. while (nsamples--) {
  281. x = *src * (float)SAMPLE_MAX_16BIT;
  282. r = 2.0f * (float)fast_rand() / (float)INT_MAX - 1.0f;
  283. /* Filter the error with Lipshitz's minimally audible FIR:
  284. [2.033 -2.165 1.959 -1.590 0.6149] */
  285. xe = x
  286. - state->e[idx] * 2.033f
  287. + state->e[(idx - 1) & DITHER_BUF_MASK] * 2.165f
  288. - state->e[(idx - 2) & DITHER_BUF_MASK] * 1.959f
  289. + state->e[(idx - 3) & DITHER_BUF_MASK] * 1.590f
  290. - state->e[(idx - 4) & DITHER_BUF_MASK] * 0.6149f;
  291. xp = xe + r - rm1;
  292. rm1 = r;
  293. /* This could be some inline asm on x86 */
  294. y = f_roundl(xp);
  295. /* Intrinsic z^-1 delay */
  296. idx = (idx + 1) & DITHER_BUF_MASK;
  297. state->e[idx] = y - xe;
  298. y <<= 16;
  299. if (y > INT_MAX) {
  300. z = INT_MAX;
  301. } else if (y < INT_MIN) {
  302. z = INT_MIN;
  303. } else {
  304. z = (int)y;
  305. }
  306. #if __BYTE_ORDER == __LITTLE_ENDIAN
  307. dst[0]=(char)(z>>24);
  308. dst[1]=(char)(z>>16);
  309. dst[2]=(char)(z>>8);
  310. dst[3]=(char)(z);
  311. #elif __BYTE_ORDER == __BIG_ENDIAN
  312. dst[0]=(char)(z);
  313. dst[1]=(char)(z>>8);
  314. dst[2]=(char)(z>>16);
  315. dst[3]=(char)(z>>24);
  316. #endif
  317. dst += dst_skip;
  318. src++;
  319. }
  320. state->rm1 = rm1;
  321. state->idx = idx;
  322. }
  323. void sample_move_dither_shaped_d32u24_sS (char *dst, jack_default_audio_sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state)
  324. {
  325. jack_default_audio_sample_t x;
  326. jack_default_audio_sample_t xe; /* the innput sample - filtered error */
  327. jack_default_audio_sample_t xp; /* x' */
  328. float r;
  329. float rm1 = state->rm1;
  330. unsigned int idx = state->idx;
  331. long long y;
  332. while (nsamples--) {
  333. x = *src * (float)SAMPLE_MAX_16BIT;
  334. r = 2.0f * (float)fast_rand() / (float)INT_MAX - 1.0f;
  335. /* Filter the error with Lipshitz's minimally audible FIR:
  336. [2.033 -2.165 1.959 -1.590 0.6149] */
  337. xe = x
  338. - state->e[idx] * 2.033f
  339. + state->e[(idx - 1) & DITHER_BUF_MASK] * 2.165f
  340. - state->e[(idx - 2) & DITHER_BUF_MASK] * 1.959f
  341. + state->e[(idx - 3) & DITHER_BUF_MASK] * 1.590f
  342. - state->e[(idx - 4) & DITHER_BUF_MASK] * 0.6149f;
  343. xp = xe + r - rm1;
  344. rm1 = r;
  345. /* This could be some inline asm on x86 */
  346. y = f_roundl(xp);
  347. /* Intrinsic z^-1 delay */
  348. idx = (idx + 1) & DITHER_BUF_MASK;
  349. state->e[idx] = y - xe;
  350. y <<= 16;
  351. if (y > INT_MAX) {
  352. *((int *) dst) = INT_MAX;
  353. } else if (y < INT_MIN) {
  354. *((int *) dst) = INT_MIN;
  355. } else {
  356. *((int *) dst) = y;
  357. }
  358. dst += dst_skip;
  359. src++;
  360. }
  361. state->rm1 = rm1;
  362. state->idx = idx;
  363. }
  364. void sample_move_d24_sSs (char *dst, jack_default_audio_sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state)
  365. {
  366. long long y;
  367. int z;
  368. while (nsamples--) {
  369. y = (long long)(*src * SAMPLE_MAX_24BIT);
  370. if (y > (INT_MAX >> 8 )) {
  371. z = (INT_MAX >> 8);
  372. } else if (y < (INT_MIN >> 8 )) {
  373. z = (INT_MIN >> 8 );
  374. } else {
  375. z = (int)y;
  376. }
  377. #if __BYTE_ORDER == __LITTLE_ENDIAN
  378. dst[0]=(char)(z>>16);
  379. dst[1]=(char)(z>>8);
  380. dst[2]=(char)(z);
  381. #elif __BYTE_ORDER == __BIG_ENDIAN
  382. dst[0]=(char)(z);
  383. dst[1]=(char)(z>>8);
  384. dst[2]=(char)(z>>16);
  385. #endif
  386. dst += dst_skip;
  387. src++;
  388. }
  389. }
  390. void sample_move_d24_sS (char *dst, jack_default_audio_sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state)
  391. {
  392. long long y;
  393. while (nsamples--) {
  394. y = (long long)(*src * SAMPLE_MAX_24BIT);
  395. if (y > (INT_MAX >> 8 )) {
  396. y = (INT_MAX >> 8);
  397. } else if (y < (INT_MIN >> 8 )) {
  398. y = (INT_MIN >> 8 );
  399. }
  400. #if __BYTE_ORDER == __LITTLE_ENDIAN
  401. memcpy (dst, &y, 3);
  402. #elif __BYTE_ORDER == __BIG_ENDIAN
  403. memcpy (dst, (char *)&y + 5, 3);
  404. #endif
  405. dst += dst_skip;
  406. src++;
  407. }
  408. }
  409. void sample_move_dS_s24s (jack_default_audio_sample_t *dst, char *src, unsigned long nsamples, unsigned long src_skip)
  410. {
  411. /* ALERT: signed sign-extension portability !!! */
  412. while (nsamples--) {
  413. int x;
  414. #if __BYTE_ORDER == __LITTLE_ENDIAN
  415. x = (unsigned char)(src[0]);
  416. x <<= 8;
  417. x |= (unsigned char)(src[1]);
  418. x <<= 8;
  419. x |= (unsigned char)(src[2]);
  420. /* correct sign bit and the rest of the top byte */
  421. if (src[0] & 0x80) {
  422. x |= 0xff << 24;
  423. }
  424. #elif __BYTE_ORDER == __BIG_ENDIAN
  425. x = (unsigned char)(src[2]);
  426. x <<= 8;
  427. x |= (unsigned char)(src[1]);
  428. x <<= 8;
  429. x |= (unsigned char)(src[0]);
  430. /* correct sign bit and the rest of the top byte */
  431. if (src[0] & 0x80) {
  432. x |= 0xff << 24;
  433. }
  434. #endif
  435. *dst = x / SAMPLE_MAX_24BIT;
  436. dst++;
  437. src += src_skip;
  438. }
  439. }
  440. void sample_move_dS_s24 (jack_default_audio_sample_t *dst, char *src, unsigned long nsamples, unsigned long src_skip)
  441. {
  442. /* ALERT: signed sign-extension portability !!! */
  443. while (nsamples--) {
  444. int x;
  445. #if __BYTE_ORDER == __LITTLE_ENDIAN
  446. memcpy((char*)&x + 1, src, 3);
  447. #elif __BYTE_ORDER == __BIG_ENDIAN
  448. memcpy(&x, src, 3);
  449. #endif
  450. x >>= 8;
  451. *dst = x / SAMPLE_MAX_24BIT;
  452. dst++;
  453. src += src_skip;
  454. }
  455. }
  456. void sample_move_dither_rect_d24_sSs (char *dst, jack_default_audio_sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state)
  457. {
  458. /* ALERT: signed sign-extension portability !!! */
  459. jack_default_audio_sample_t x;
  460. long long y;
  461. int z;
  462. while (nsamples--) {
  463. x = *src * SAMPLE_MAX_16BIT;
  464. x -= (float)fast_rand() / (float)INT_MAX;
  465. y = f_roundl(x);
  466. y <<= 8;
  467. if (y > (INT_MAX >> 8)) {
  468. z = (INT_MAX >> 8);
  469. } else if (y < (INT_MIN >> 8)) {
  470. z = (INT_MIN >> 8);
  471. } else {
  472. z = (int)y;
  473. }
  474. #if __BYTE_ORDER == __LITTLE_ENDIAN
  475. dst[0]=(char)(z>>16);
  476. dst[1]=(char)(z>>8);
  477. dst[2]=(char)(z);
  478. #elif __BYTE_ORDER == __BIG_ENDIAN
  479. dst[0]=(char)(z);
  480. dst[1]=(char)(z>>8);
  481. dst[2]=(char)(z>>16);
  482. #endif
  483. dst += dst_skip;
  484. src++;
  485. }
  486. }
  487. void sample_move_dither_rect_d24_sS (char *dst, jack_default_audio_sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state)
  488. {
  489. /* ALERT: signed sign-extension portability !!! */
  490. jack_default_audio_sample_t x;
  491. long long y;
  492. while (nsamples--) {
  493. x = *src * SAMPLE_MAX_16BIT;
  494. x -= (float)fast_rand() / (float)INT_MAX;
  495. y = f_roundl(x);
  496. y <<= 8;
  497. if (y > (INT_MAX >> 8)) {
  498. y = (INT_MAX >> 8);
  499. } else if (y < (INT_MIN >> 8)) {
  500. y = (INT_MIN >> 8);
  501. }
  502. #if __BYTE_ORDER == __LITTLE_ENDIAN
  503. memcpy (dst, &y, 3);
  504. #elif __BYTE_ORDER == __BIG_ENDIAN
  505. memcpy (dst, (char *)&y + 5, 3);
  506. #endif
  507. dst += dst_skip;
  508. src++;
  509. }
  510. }
  511. void sample_move_dither_tri_d24_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 x;
  514. float r;
  515. float rm1 = state->rm1;
  516. long long y;
  517. int z;
  518. while (nsamples--) {
  519. x = *src * (float)SAMPLE_MAX_16BIT;
  520. r = 2.0f * (float)fast_rand() / (float)INT_MAX - 1.0f;
  521. x += r - rm1;
  522. rm1 = r;
  523. y = f_roundl(x);
  524. y <<= 8;
  525. if (y > (INT_MAX >> 8)) {
  526. z = (INT_MAX >> 8);
  527. } else if (y < (INT_MIN >> 8)) {
  528. z = (INT_MIN >> 8);
  529. } else {
  530. z = (int)y;
  531. }
  532. #if __BYTE_ORDER == __LITTLE_ENDIAN
  533. dst[0]=(char)(z>>16);
  534. dst[1]=(char)(z>>8);
  535. dst[2]=(char)(z);
  536. #elif __BYTE_ORDER == __BIG_ENDIAN
  537. dst[0]=(char)(z);
  538. dst[1]=(char)(z>>8);
  539. dst[2]=(char)(z>>16);
  540. #endif
  541. dst += dst_skip;
  542. src++;
  543. }
  544. state->rm1 = rm1;
  545. }
  546. void sample_move_dither_tri_d24_sS (char *dst, jack_default_audio_sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state)
  547. {
  548. jack_default_audio_sample_t x;
  549. float r;
  550. float rm1 = state->rm1;
  551. long long y;
  552. while (nsamples--) {
  553. x = *src * (float)SAMPLE_MAX_16BIT;
  554. r = 2.0f * (float)fast_rand() / (float)INT_MAX - 1.0f;
  555. x += r - rm1;
  556. rm1 = r;
  557. y = f_roundl(x);
  558. y <<= 8;
  559. if (y > (INT_MAX >> 8)) {
  560. y = (INT_MAX >> 8);
  561. } else if (y < (INT_MIN >> 8)) {
  562. y = (INT_MIN >> 8);
  563. }
  564. #if __BYTE_ORDER == __LITTLE_ENDIAN
  565. memcpy (dst, &y, 3);
  566. #elif __BYTE_ORDER == __BIG_ENDIAN
  567. memcpy (dst, (char *)&y + 5, 3);
  568. #endif
  569. dst += dst_skip;
  570. src++;
  571. }
  572. state->rm1 = rm1;
  573. }
  574. void sample_move_dither_shaped_d24_sSs (char *dst, jack_default_audio_sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state)
  575. {
  576. jack_default_audio_sample_t x;
  577. jack_default_audio_sample_t xe; /* the innput sample - filtered error */
  578. jack_default_audio_sample_t xp; /* x' */
  579. float r;
  580. float rm1 = state->rm1;
  581. unsigned int idx = state->idx;
  582. long long y;
  583. int z;
  584. while (nsamples--) {
  585. x = *src * (float)SAMPLE_MAX_16BIT;
  586. r = 2.0f * (float)fast_rand() / (float)INT_MAX - 1.0f;
  587. /* Filter the error with Lipshitz's minimally audible FIR:
  588. [2.033 -2.165 1.959 -1.590 0.6149] */
  589. xe = x
  590. - state->e[idx] * 2.033f
  591. + state->e[(idx - 1) & DITHER_BUF_MASK] * 2.165f
  592. - state->e[(idx - 2) & DITHER_BUF_MASK] * 1.959f
  593. + state->e[(idx - 3) & DITHER_BUF_MASK] * 1.590f
  594. - state->e[(idx - 4) & DITHER_BUF_MASK] * 0.6149f;
  595. xp = xe + r - rm1;
  596. rm1 = r;
  597. /* This could be some inline asm on x86 */
  598. y = f_roundl(xp);
  599. /* Intrinsic z^-1 delay */
  600. idx = (idx + 1) & DITHER_BUF_MASK;
  601. state->e[idx] = y - xe;
  602. y <<= 8;
  603. if (y > (INT_MAX >> 8)) {
  604. z = (INT_MAX >> 8);
  605. } else if (y < (INT_MIN >> 8)) {
  606. z = (INT_MIN >> 8);
  607. } else {
  608. z = (int)y;
  609. }
  610. #if __BYTE_ORDER == __LITTLE_ENDIAN
  611. dst[0]=(char)(z>>16);
  612. dst[1]=(char)(z>>8);
  613. dst[2]=(char)(z);
  614. #elif __BYTE_ORDER == __BIG_ENDIAN
  615. dst[0]=(char)(z);
  616. dst[1]=(char)(z>>8);
  617. dst[2]=(char)(z>>16);
  618. #endif
  619. dst += dst_skip;
  620. src++;
  621. }
  622. state->rm1 = rm1;
  623. state->idx = idx;
  624. }
  625. void sample_move_dither_shaped_d24_sS (char *dst, jack_default_audio_sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state)
  626. {
  627. jack_default_audio_sample_t x;
  628. jack_default_audio_sample_t xe; /* the innput sample - filtered error */
  629. jack_default_audio_sample_t xp; /* x' */
  630. float r;
  631. float rm1 = state->rm1;
  632. unsigned int idx = state->idx;
  633. long long y;
  634. while (nsamples--) {
  635. x = *src * (float)SAMPLE_MAX_16BIT;
  636. r = 2.0f * (float)fast_rand() / (float)INT_MAX - 1.0f;
  637. /* Filter the error with Lipshitz's minimally audible FIR:
  638. [2.033 -2.165 1.959 -1.590 0.6149] */
  639. xe = x
  640. - state->e[idx] * 2.033f
  641. + state->e[(idx - 1) & DITHER_BUF_MASK] * 2.165f
  642. - state->e[(idx - 2) & DITHER_BUF_MASK] * 1.959f
  643. + state->e[(idx - 3) & DITHER_BUF_MASK] * 1.590f
  644. - state->e[(idx - 4) & DITHER_BUF_MASK] * 0.6149f;
  645. xp = xe + r - rm1;
  646. rm1 = r;
  647. /* This could be some inline asm on x86 */
  648. y = (long long)f_round(xp);
  649. /* Intrinsic z^-1 delay */
  650. idx = (idx + 1) & DITHER_BUF_MASK;
  651. state->e[idx] = y - xe;
  652. y <<= 8;
  653. if (y > (INT_MAX >> 8)) {
  654. y = (INT_MAX >> 8);
  655. } else if (y < (INT_MIN >> 8)) {
  656. y = (INT_MIN >> 8);
  657. }
  658. #if __BYTE_ORDER == __LITTLE_ENDIAN
  659. memcpy (dst, &y, 3);
  660. #elif __BYTE_ORDER == __BIG_ENDIAN
  661. memcpy (dst, (char *)&y + 5, 3);
  662. #endif
  663. dst += dst_skip;
  664. src++;
  665. }
  666. state->rm1 = rm1;
  667. state->idx = idx;
  668. }
  669. void sample_move_d16_sSs (char *dst, jack_default_audio_sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state)
  670. {
  671. int tmp;
  672. /* ALERT: signed sign-extension portability !!! */
  673. while (nsamples--) {
  674. tmp = f_round(*src * SAMPLE_MAX_16BIT);
  675. if (tmp > SHRT_MAX) {
  676. tmp = SHRT_MAX;
  677. } else if (tmp < SHRT_MIN) {
  678. tmp = SHRT_MIN;
  679. }
  680. #if __BYTE_ORDER == __LITTLE_ENDIAN
  681. dst[0]=(char)(tmp>>8);
  682. dst[1]=(char)(tmp);
  683. #elif __BYTE_ORDER == __BIG_ENDIAN
  684. dst[0]=(char)(tmp);
  685. dst[1]=(char)(tmp>>8);
  686. #endif
  687. dst += dst_skip;
  688. src++;
  689. }
  690. }
  691. void sample_move_d16_sS (char *dst, jack_default_audio_sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state)
  692. {
  693. int tmp;
  694. /* ALERT: signed sign-extension portability !!! */
  695. while (nsamples--) {
  696. tmp = f_round(*src * SAMPLE_MAX_16BIT);
  697. if (tmp > SHRT_MAX) {
  698. *((short *)dst) = SHRT_MAX;
  699. } else if (tmp < SHRT_MIN) {
  700. *((short *)dst) = SHRT_MIN;
  701. } else {
  702. *((short *) dst) = (short) tmp;
  703. }
  704. dst += dst_skip;
  705. src++;
  706. }
  707. }
  708. 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)
  709. {
  710. jack_default_audio_sample_t val;
  711. int tmp;
  712. while (nsamples--) {
  713. val = *src * (float)SAMPLE_MAX_16BIT;
  714. val -= (float)fast_rand() / (float)INT_MAX;
  715. tmp = f_round(val);
  716. if (tmp > SHRT_MAX) {
  717. tmp = SHRT_MAX;
  718. } else if (tmp < SHRT_MIN) {
  719. tmp = SHRT_MIN;
  720. }
  721. #if __BYTE_ORDER == __LITTLE_ENDIAN
  722. dst[0]=(char)(tmp>>8);
  723. dst[1]=(char)(tmp);
  724. #elif __BYTE_ORDER == __BIG_ENDIAN
  725. dst[0]=(char)(tmp);
  726. dst[1]=(char)(tmp>>8);
  727. #endif
  728. dst += dst_skip;
  729. src++;
  730. }
  731. }
  732. 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)
  733. {
  734. jack_default_audio_sample_t val;
  735. int tmp;
  736. while (nsamples--) {
  737. val = *src * (float)SAMPLE_MAX_16BIT;
  738. val -= (float)fast_rand() / (float)INT_MAX;
  739. tmp = f_round(val);
  740. if (tmp > SHRT_MAX) {
  741. *((short *)dst) = SHRT_MAX;
  742. } else if (tmp < SHRT_MIN) {
  743. *((short *)dst) = SHRT_MIN;
  744. } else {
  745. *((short *) dst) = (short)tmp;
  746. }
  747. dst += dst_skip;
  748. src++;
  749. }
  750. }
  751. 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)
  752. {
  753. jack_default_audio_sample_t x;
  754. float r;
  755. float rm1 = state->rm1;
  756. int y;
  757. while (nsamples--) {
  758. x = *src * (float)SAMPLE_MAX_16BIT;
  759. r = 2.0f * (float)fast_rand() / (float)INT_MAX - 1.0f;
  760. x += r - rm1;
  761. rm1 = r;
  762. y = f_round(x);
  763. if (y > SHRT_MAX) {
  764. y = SHRT_MAX;
  765. } else if (y < SHRT_MIN) {
  766. y = SHRT_MIN;
  767. }
  768. #if __BYTE_ORDER == __LITTLE_ENDIAN
  769. dst[0]=(char)(y>>8);
  770. dst[1]=(char)(y);
  771. #elif __BYTE_ORDER == __BIG_ENDIAN
  772. dst[0]=(char)(y);
  773. dst[1]=(char)(y>>8);
  774. #endif
  775. dst += dst_skip;
  776. src++;
  777. }
  778. state->rm1 = rm1;
  779. }
  780. 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)
  781. {
  782. jack_default_audio_sample_t x;
  783. float r;
  784. float rm1 = state->rm1;
  785. int y;
  786. while (nsamples--) {
  787. x = *src * (float)SAMPLE_MAX_16BIT;
  788. r = 2.0f * (float)fast_rand() / (float)INT_MAX - 1.0f;
  789. x += r - rm1;
  790. rm1 = r;
  791. y = f_round(x);
  792. if (y > SHRT_MAX) {
  793. *((short *)dst) = SHRT_MAX;
  794. } else if (y < SHRT_MIN) {
  795. *((short *)dst) = SHRT_MIN;
  796. } else {
  797. *((short *) dst) = (short)y;
  798. }
  799. dst += dst_skip;
  800. src++;
  801. }
  802. state->rm1 = rm1;
  803. }
  804. 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)
  805. {
  806. jack_default_audio_sample_t x;
  807. jack_default_audio_sample_t xe; /* the innput sample - filtered error */
  808. jack_default_audio_sample_t xp; /* x' */
  809. float r;
  810. float rm1 = state->rm1;
  811. unsigned int idx = state->idx;
  812. int y;
  813. while (nsamples--) {
  814. x = *src * (float)SAMPLE_MAX_16BIT;
  815. r = 2.0f * (float)fast_rand() / (float)INT_MAX - 1.0f;
  816. /* Filter the error with Lipshitz's minimally audible FIR:
  817. [2.033 -2.165 1.959 -1.590 0.6149] */
  818. xe = x
  819. - state->e[idx] * 2.033f
  820. + state->e[(idx - 1) & DITHER_BUF_MASK] * 2.165f
  821. - state->e[(idx - 2) & DITHER_BUF_MASK] * 1.959f
  822. + state->e[(idx - 3) & DITHER_BUF_MASK] * 1.590f
  823. - state->e[(idx - 4) & DITHER_BUF_MASK] * 0.6149f;
  824. xp = xe + r - rm1;
  825. rm1 = r;
  826. /* This could be some inline asm on x86 */
  827. y = f_round(xp);
  828. /* Intrinsic z^-1 delay */
  829. idx = (idx + 1) & DITHER_BUF_MASK;
  830. state->e[idx] = y - xe;
  831. if (y > SHRT_MAX) {
  832. y = SHRT_MAX;
  833. } else if (y < SHRT_MIN) {
  834. y = SHRT_MIN;
  835. }
  836. #if __BYTE_ORDER == __LITTLE_ENDIAN
  837. dst[0]=(char)(y>>8);
  838. dst[1]=(char)(y);
  839. #elif __BYTE_ORDER == __BIG_ENDIAN
  840. dst[0]=(char)(y);
  841. dst[1]=(char)(y>>8);
  842. #endif
  843. dst += dst_skip;
  844. src++;
  845. }
  846. state->rm1 = rm1;
  847. state->idx = idx;
  848. }
  849. 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)
  850. {
  851. jack_default_audio_sample_t x;
  852. jack_default_audio_sample_t xe; /* the innput sample - filtered error */
  853. jack_default_audio_sample_t xp; /* x' */
  854. float r;
  855. float rm1 = state->rm1;
  856. unsigned int idx = state->idx;
  857. int y;
  858. while (nsamples--) {
  859. x = *src * (float)SAMPLE_MAX_16BIT;
  860. r = 2.0f * (float)fast_rand() / (float)INT_MAX - 1.0f;
  861. /* Filter the error with Lipshitz's minimally audible FIR:
  862. [2.033 -2.165 1.959 -1.590 0.6149] */
  863. xe = x
  864. - state->e[idx] * 2.033f
  865. + state->e[(idx - 1) & DITHER_BUF_MASK] * 2.165f
  866. - state->e[(idx - 2) & DITHER_BUF_MASK] * 1.959f
  867. + state->e[(idx - 3) & DITHER_BUF_MASK] * 1.590f
  868. - state->e[(idx - 4) & DITHER_BUF_MASK] * 0.6149f;
  869. xp = xe + r - rm1;
  870. rm1 = r;
  871. /* This could be some inline asm on x86 */
  872. y = f_round(xp);
  873. /* Intrinsic z^-1 delay */
  874. idx = (idx + 1) & DITHER_BUF_MASK;
  875. state->e[idx] = y - xe;
  876. if (y > SHRT_MAX) {
  877. *((short *)dst) = SHRT_MAX;
  878. } else if (y < SHRT_MIN) {
  879. *((short *)dst) = SHRT_MIN;
  880. } else {
  881. *((short *) dst) = (short)y;
  882. }
  883. dst += dst_skip;
  884. src++;
  885. }
  886. state->rm1 = rm1;
  887. state->idx = idx;
  888. }
  889. void sample_move_dS_s16s (jack_default_audio_sample_t *dst, char *src, unsigned long nsamples, unsigned long src_skip)
  890. {
  891. short z;
  892. /* ALERT: signed sign-extension portability !!! */
  893. while (nsamples--) {
  894. #if __BYTE_ORDER == __LITTLE_ENDIAN
  895. z = (unsigned char)(src[0]);
  896. z <<= 8;
  897. z |= (unsigned char)(src[1]);
  898. #elif __BYTE_ORDER == __BIG_ENDIAN
  899. z = (unsigned char)(src[1]);
  900. z <<= 8;
  901. z |= (unsigned char)(src[0]);
  902. #endif
  903. *dst = z / SAMPLE_MAX_16BIT;
  904. dst++;
  905. src += src_skip;
  906. }
  907. }
  908. void sample_move_dS_s16 (jack_default_audio_sample_t *dst, char *src, unsigned long nsamples, unsigned long src_skip)
  909. {
  910. /* ALERT: signed sign-extension portability !!! */
  911. while (nsamples--) {
  912. *dst = (*((short *) src)) / SAMPLE_MAX_16BIT;
  913. dst++;
  914. src += src_skip;
  915. }
  916. }
  917. void sample_merge_d16_sS (char *dst, jack_default_audio_sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state)
  918. {
  919. short val;
  920. /* ALERT: signed sign-extension portability !!! */
  921. while (nsamples--) {
  922. val = (short) (*src * SAMPLE_MAX_16BIT);
  923. if (val > SHRT_MAX - *((short *) dst)) {
  924. *((short *)dst) = SHRT_MAX;
  925. } else if (val < SHRT_MIN - *((short *) dst)) {
  926. *((short *)dst) = SHRT_MIN;
  927. } else {
  928. *((short *) dst) += val;
  929. }
  930. dst += dst_skip;
  931. src++;
  932. }
  933. }
  934. void sample_merge_d32u24_sS (char *dst, jack_default_audio_sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state)
  935. {
  936. /* ALERT: signed sign-extension portability !!! */
  937. while (nsamples--) {
  938. *((int *) dst) += (((int) (*src * SAMPLE_MAX_24BIT)) << 8);
  939. dst += dst_skip;
  940. src++;
  941. }
  942. }
  943. void memset_interleave (char *dst, char val, unsigned long bytes,
  944. unsigned long unit_bytes,
  945. unsigned long skip_bytes)
  946. {
  947. switch (unit_bytes) {
  948. case 1:
  949. while (bytes--) {
  950. *dst = val;
  951. dst += skip_bytes;
  952. }
  953. break;
  954. case 2:
  955. while (bytes) {
  956. *((short *) dst) = (short) val;
  957. dst += skip_bytes;
  958. bytes -= 2;
  959. }
  960. break;
  961. case 4:
  962. while (bytes) {
  963. *((int *) dst) = (int) val;
  964. dst += skip_bytes;
  965. bytes -= 4;
  966. }
  967. break;
  968. default:
  969. while (bytes) {
  970. memset(dst, val, unit_bytes);
  971. dst += skip_bytes;
  972. bytes -= unit_bytes;
  973. }
  974. break;
  975. }
  976. }
  977. /* COPY FUNCTIONS: used to move data from an input channel to an
  978. output channel. Note that we assume that the skip distance
  979. is the same for both channels. This is completely fine
  980. unless the input and output were on different audio interfaces that
  981. were interleaved differently. We don't try to handle that.
  982. */
  983. void
  984. memcpy_fake (char *dst, char *src, unsigned long src_bytes, unsigned long foo, unsigned long bar)
  985. {
  986. memcpy (dst, src, src_bytes);
  987. }
  988. void
  989. merge_memcpy_d16_s16 (char *dst, char *src, unsigned long src_bytes,
  990. unsigned long dst_skip_bytes, unsigned long src_skip_bytes)
  991. {
  992. while (src_bytes) {
  993. *((short *) dst) += *((short *) src);
  994. dst += 2;
  995. src += 2;
  996. src_bytes -= 2;
  997. }
  998. }
  999. void
  1000. merge_memcpy_d32_s32 (char *dst, char *src, unsigned long src_bytes,
  1001. unsigned long dst_skip_bytes, unsigned long src_skip_bytes)
  1002. {
  1003. while (src_bytes) {
  1004. *((int *) dst) += *((int *) src);
  1005. dst += 4;
  1006. src += 4;
  1007. src_bytes -= 4;
  1008. }
  1009. }
  1010. void
  1011. merge_memcpy_interleave_d16_s16 (char *dst, char *src, unsigned long src_bytes,
  1012. unsigned long dst_skip_bytes, unsigned long src_skip_bytes)
  1013. {
  1014. while (src_bytes) {
  1015. *((short *) dst) += *((short *) src);
  1016. dst += dst_skip_bytes;
  1017. src += src_skip_bytes;
  1018. src_bytes -= 2;
  1019. }
  1020. }
  1021. void
  1022. merge_memcpy_interleave_d32_s32 (char *dst, char *src, unsigned long src_bytes,
  1023. unsigned long dst_skip_bytes, unsigned long src_skip_bytes)
  1024. {
  1025. while (src_bytes) {
  1026. *((int *) dst) += *((int *) src);
  1027. dst += dst_skip_bytes;
  1028. src += src_skip_bytes;
  1029. src_bytes -= 4;
  1030. }
  1031. }
  1032. void
  1033. merge_memcpy_interleave_d24_s24 (char *dst, char *src, unsigned long src_bytes,
  1034. unsigned long dst_skip_bytes, unsigned long src_skip_bytes)
  1035. {
  1036. while (src_bytes) {
  1037. int acc = (*(int *)dst & 0xFFFFFF) + (*(int *)src & 0xFFFFFF);
  1038. memcpy(dst, &acc, 3);
  1039. dst += dst_skip_bytes;
  1040. src += src_skip_bytes;
  1041. src_bytes -= 3;
  1042. }
  1043. }
  1044. void
  1045. memcpy_interleave_d16_s16 (char *dst, char *src, unsigned long src_bytes,
  1046. unsigned long dst_skip_bytes, unsigned long src_skip_bytes)
  1047. {
  1048. while (src_bytes) {
  1049. *((short *) dst) = *((short *) src);
  1050. dst += dst_skip_bytes;
  1051. src += src_skip_bytes;
  1052. src_bytes -= 2;
  1053. }
  1054. }
  1055. void
  1056. memcpy_interleave_d24_s24 (char *dst, char *src, unsigned long src_bytes,
  1057. unsigned long dst_skip_bytes, unsigned long src_skip_bytes)
  1058. {
  1059. while (src_bytes) {
  1060. memcpy(dst, src, 3);
  1061. dst += dst_skip_bytes;
  1062. src += src_skip_bytes;
  1063. src_bytes -= 3;
  1064. }
  1065. }
  1066. void
  1067. memcpy_interleave_d32_s32 (char *dst, char *src, unsigned long src_bytes,
  1068. unsigned long dst_skip_bytes, unsigned long src_skip_bytes)
  1069. {
  1070. while (src_bytes) {
  1071. *((int *) dst) = *((int *) src);
  1072. dst += dst_skip_bytes;
  1073. src += src_skip_bytes;
  1074. src_bytes -= 4;
  1075. }
  1076. }