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.

1199 lines
29KB

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