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.

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