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.

1204 lines
28KB

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