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

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