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.

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