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.

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