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.

1229 lines
29KB

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