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.

652 lines
15KB

  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 "memops.h"
  27. #define SAMPLE_MAX_24BIT 8388608.0f
  28. #define SAMPLE_MAX_16BIT 32768.0f
  29. #define f_round(f) lrintf(f)
  30. /* Linear Congruential noise generator. From the music-dsp list
  31. * less random than rand(), but good enough and 10x faster */
  32. inline unsigned int fast_rand();
  33. inline unsigned int fast_rand() {
  34. static unsigned int seed = 22222;
  35. seed = (seed * 96314165) + 907633515;
  36. return seed;
  37. }
  38. void sample_move_d32u24_sS (char *dst, jack_default_audio_sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state)
  39. {
  40. long long y;
  41. while (nsamples--) {
  42. y = (long long)(*src * SAMPLE_MAX_24BIT) << 8;
  43. if (y > INT_MAX) {
  44. *((int *) dst) = INT_MAX;
  45. } else if (y < INT_MIN) {
  46. *((int *) dst) = INT_MIN;
  47. } else {
  48. *((int *) dst) = (int)y;
  49. }
  50. dst += dst_skip;
  51. src++;
  52. }
  53. }
  54. void sample_move_dS_s32u24 (jack_default_audio_sample_t *dst, char *src, unsigned long nsamples, unsigned long src_skip)
  55. {
  56. /* ALERT: signed sign-extension portability !!! */
  57. while (nsamples--) {
  58. *dst = (*((int *) src) >> 8) / SAMPLE_MAX_24BIT;
  59. dst++;
  60. src += src_skip;
  61. }
  62. }
  63. 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)
  64. {
  65. /* ALERT: signed sign-extension portability !!! */
  66. jack_default_audio_sample_t x;
  67. long long y;
  68. while (nsamples--) {
  69. x = *src * SAMPLE_MAX_16BIT;
  70. x -= (float)fast_rand() / (float)INT_MAX;
  71. y = (long long)f_round(x);
  72. y <<= 16;
  73. if (y > INT_MAX) {
  74. *((int *) dst) = INT_MAX;
  75. } else if (y < INT_MIN) {
  76. *((int *) dst) = INT_MIN;
  77. } else {
  78. *((int *) dst) = (int)y;
  79. }
  80. dst += dst_skip;
  81. src++;
  82. }
  83. }
  84. 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)
  85. {
  86. jack_default_audio_sample_t x;
  87. float r;
  88. float rm1 = state->rm1;
  89. long long y;
  90. while (nsamples--) {
  91. x = *src * (float)SAMPLE_MAX_16BIT;
  92. r = 2.0f * (float)fast_rand() / (float)INT_MAX - 1.0f;
  93. x += r - rm1;
  94. rm1 = r;
  95. y = (long long)f_round(x);
  96. y <<= 16;
  97. if (y > INT_MAX) {
  98. *((int *) dst) = INT_MAX;
  99. } else if (y < INT_MIN) {
  100. *((int *) dst) = INT_MIN;
  101. } else {
  102. *((int *) dst) = (int)y;
  103. }
  104. dst += dst_skip;
  105. src++;
  106. }
  107. state->rm1 = rm1;
  108. }
  109. 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)
  110. {
  111. jack_default_audio_sample_t x;
  112. jack_default_audio_sample_t xe; /* the innput sample - filtered error */
  113. jack_default_audio_sample_t xp; /* x' */
  114. float r;
  115. float rm1 = state->rm1;
  116. unsigned int idx = state->idx;
  117. long long y;
  118. while (nsamples--) {
  119. x = *src * (float)SAMPLE_MAX_16BIT;
  120. r = 2.0f * (float)fast_rand() / (float)INT_MAX - 1.0f;
  121. /* Filter the error with Lipshitz's minimally audible FIR:
  122. [2.033 -2.165 1.959 -1.590 0.6149] */
  123. xe = x
  124. - state->e[idx] * 2.033f
  125. + state->e[(idx - 1) & DITHER_BUF_MASK] * 2.165f
  126. - state->e[(idx - 2) & DITHER_BUF_MASK] * 1.959f
  127. + state->e[(idx - 3) & DITHER_BUF_MASK] * 1.590f
  128. - state->e[(idx - 4) & DITHER_BUF_MASK] * 0.6149f;
  129. xp = xe + r - rm1;
  130. rm1 = r;
  131. /* This could be some inline asm on x86 */
  132. y = (long long)f_round(xp);
  133. /* Intrinsic z^-1 delay */
  134. idx = (idx + 1) & DITHER_BUF_MASK;
  135. state->e[idx] = y - xe;
  136. y <<= 16;
  137. if (y > INT_MAX) {
  138. *((int *) dst) = INT_MAX;
  139. } else if (y < INT_MIN) {
  140. *((int *) dst) = INT_MIN;
  141. } else {
  142. *((int *) dst) = y;
  143. }
  144. dst += dst_skip;
  145. src++;
  146. }
  147. state->rm1 = rm1;
  148. state->idx = idx;
  149. }
  150. void sample_move_d24_sS (char *dst, jack_default_audio_sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state)
  151. {
  152. long long y;
  153. while (nsamples--) {
  154. y = (long long)(*src * SAMPLE_MAX_24BIT);
  155. if (y > (INT_MAX >> 8 )) {
  156. y = (INT_MAX >> 8);
  157. } else if (y < (INT_MIN >> 8 )) {
  158. y = (INT_MIN >> 8 );
  159. }
  160. #if __BYTE_ORDER == __LITTLE_ENDIAN
  161. memcpy (dst, &y, 3);
  162. #elif __BYTE_ORDER == __BIG_ENDIAN
  163. memcpy (dst, (char *)&y + 5, 3);
  164. #endif
  165. dst += dst_skip;
  166. src++;
  167. }
  168. }
  169. void sample_move_dS_s24 (jack_default_audio_sample_t *dst, char *src, unsigned long nsamples, unsigned long src_skip)
  170. {
  171. /* ALERT: signed sign-extension portability !!! */
  172. while (nsamples--) {
  173. int x;
  174. #if __BYTE_ORDER == __LITTLE_ENDIAN
  175. memcpy((char*)&x + 1, src, 3);
  176. #elif __BYTE_ORDER == __BIG_ENDIAN
  177. memcpy(&x, src, 3);
  178. #endif
  179. x >>= 8;
  180. *dst = x / SAMPLE_MAX_24BIT;
  181. dst++;
  182. src += src_skip;
  183. }
  184. }
  185. 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)
  186. {
  187. /* ALERT: signed sign-extension portability !!! */
  188. jack_default_audio_sample_t x;
  189. long long y;
  190. while (nsamples--) {
  191. x = *src * SAMPLE_MAX_16BIT;
  192. x -= (float)fast_rand() / (float)INT_MAX;
  193. y = (long long)f_round(x);
  194. y <<= 8;
  195. if (y > (INT_MAX >> 8)) {
  196. y = (INT_MAX >> 8);
  197. } else if (y < (INT_MIN >> 8)) {
  198. y = (INT_MIN >> 8);
  199. }
  200. #if __BYTE_ORDER == __LITTLE_ENDIAN
  201. memcpy (dst, &y, 3);
  202. #elif __BYTE_ORDER == __BIG_ENDIAN
  203. memcpy (dst, (char *)&y + 5, 3);
  204. #endif
  205. dst += dst_skip;
  206. src++;
  207. }
  208. }
  209. 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)
  210. {
  211. jack_default_audio_sample_t x;
  212. float r;
  213. float rm1 = state->rm1;
  214. long long y;
  215. while (nsamples--) {
  216. x = *src * (float)SAMPLE_MAX_16BIT;
  217. r = 2.0f * (float)fast_rand() / (float)INT_MAX - 1.0f;
  218. x += r - rm1;
  219. rm1 = r;
  220. y = (long long)f_round(x);
  221. y <<= 8;
  222. if (y > (INT_MAX >> 8)) {
  223. y = (INT_MAX >> 8);
  224. } else if (y < (INT_MIN >> 8)) {
  225. y = (INT_MIN >> 8);
  226. }
  227. #if __BYTE_ORDER == __LITTLE_ENDIAN
  228. memcpy (dst, &y, 3);
  229. #elif __BYTE_ORDER == __BIG_ENDIAN
  230. memcpy (dst, (char *)&y + 5, 3);
  231. #endif
  232. dst += dst_skip;
  233. src++;
  234. }
  235. state->rm1 = rm1;
  236. }
  237. 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)
  238. {
  239. jack_default_audio_sample_t x;
  240. jack_default_audio_sample_t xe; /* the innput sample - filtered error */
  241. jack_default_audio_sample_t xp; /* x' */
  242. float r;
  243. float rm1 = state->rm1;
  244. unsigned int idx = state->idx;
  245. long long y;
  246. while (nsamples--) {
  247. x = *src * (float)SAMPLE_MAX_16BIT;
  248. r = 2.0f * (float)fast_rand() / (float)INT_MAX - 1.0f;
  249. /* Filter the error with Lipshitz's minimally audible FIR:
  250. [2.033 -2.165 1.959 -1.590 0.6149] */
  251. xe = x
  252. - state->e[idx] * 2.033f
  253. + state->e[(idx - 1) & DITHER_BUF_MASK] * 2.165f
  254. - state->e[(idx - 2) & DITHER_BUF_MASK] * 1.959f
  255. + state->e[(idx - 3) & DITHER_BUF_MASK] * 1.590f
  256. - state->e[(idx - 4) & DITHER_BUF_MASK] * 0.6149f;
  257. xp = xe + r - rm1;
  258. rm1 = r;
  259. /* This could be some inline asm on x86 */
  260. y = (long long)f_round(xp);
  261. /* Intrinsic z^-1 delay */
  262. idx = (idx + 1) & DITHER_BUF_MASK;
  263. state->e[idx] = y - xe;
  264. y <<= 8;
  265. if (y > (INT_MAX >> 8)) {
  266. y = (INT_MAX >> 8);
  267. } else if (y < (INT_MIN >> 8)) {
  268. y = (INT_MIN >> 8);
  269. }
  270. #if __BYTE_ORDER == __LITTLE_ENDIAN
  271. memcpy (dst, &y, 3);
  272. #elif __BYTE_ORDER == __BIG_ENDIAN
  273. memcpy (dst, (char *)&y + 5, 3);
  274. #endif
  275. dst += dst_skip;
  276. src++;
  277. }
  278. state->rm1 = rm1;
  279. state->idx = idx;
  280. }
  281. void sample_move_d16_sS (char *dst, jack_default_audio_sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state)
  282. {
  283. int tmp;
  284. /* ALERT: signed sign-extension portability !!! */
  285. while (nsamples--) {
  286. tmp = f_round(*src * SAMPLE_MAX_16BIT);
  287. if (tmp > SHRT_MAX) {
  288. *((short *)dst) = SHRT_MAX;
  289. } else if (tmp < SHRT_MIN) {
  290. *((short *)dst) = SHRT_MIN;
  291. } else {
  292. *((short *) dst) = (short) tmp;
  293. }
  294. dst += dst_skip;
  295. src++;
  296. }
  297. }
  298. 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)
  299. {
  300. jack_default_audio_sample_t val;
  301. int tmp;
  302. while (nsamples--) {
  303. val = *src * (float)SAMPLE_MAX_16BIT;
  304. val -= (float)fast_rand() / (float)INT_MAX;
  305. tmp = f_round(val);
  306. if (tmp > SHRT_MAX) {
  307. *((short *)dst) = SHRT_MAX;
  308. } else if (tmp < SHRT_MIN) {
  309. *((short *)dst) = SHRT_MIN;
  310. } else {
  311. *((short *) dst) = (short)tmp;
  312. }
  313. dst += dst_skip;
  314. src++;
  315. }
  316. }
  317. 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)
  318. {
  319. jack_default_audio_sample_t x;
  320. float r;
  321. float rm1 = state->rm1;
  322. int y;
  323. while (nsamples--) {
  324. x = *src * (float)SAMPLE_MAX_16BIT;
  325. r = 2.0f * (float)fast_rand() / (float)INT_MAX - 1.0f;
  326. x += r - rm1;
  327. rm1 = r;
  328. y = f_round(x);
  329. if (y > SHRT_MAX) {
  330. *((short *)dst) = SHRT_MAX;
  331. } else if (y < SHRT_MIN) {
  332. *((short *)dst) = SHRT_MIN;
  333. } else {
  334. *((short *) dst) = (short)y;
  335. }
  336. dst += dst_skip;
  337. src++;
  338. }
  339. state->rm1 = rm1;
  340. }
  341. 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)
  342. {
  343. jack_default_audio_sample_t x;
  344. jack_default_audio_sample_t xe; /* the innput sample - filtered error */
  345. jack_default_audio_sample_t xp; /* x' */
  346. float r;
  347. float rm1 = state->rm1;
  348. unsigned int idx = state->idx;
  349. int y;
  350. while (nsamples--) {
  351. x = *src * (float)SAMPLE_MAX_16BIT;
  352. r = 2.0f * (float)fast_rand() / (float)INT_MAX - 1.0f;
  353. /* Filter the error with Lipshitz's minimally audible FIR:
  354. [2.033 -2.165 1.959 -1.590 0.6149] */
  355. xe = x
  356. - state->e[idx] * 2.033f
  357. + state->e[(idx - 1) & DITHER_BUF_MASK] * 2.165f
  358. - state->e[(idx - 2) & DITHER_BUF_MASK] * 1.959f
  359. + state->e[(idx - 3) & DITHER_BUF_MASK] * 1.590f
  360. - state->e[(idx - 4) & DITHER_BUF_MASK] * 0.6149f;
  361. xp = xe + r - rm1;
  362. rm1 = r;
  363. /* This could be some inline asm on x86 */
  364. y = f_round(xp);
  365. /* Intrinsic z^-1 delay */
  366. idx = (idx + 1) & DITHER_BUF_MASK;
  367. state->e[idx] = y - xe;
  368. if (y > SHRT_MAX) {
  369. *((short *)dst) = SHRT_MAX;
  370. } else if (y < SHRT_MIN) {
  371. *((short *)dst) = SHRT_MIN;
  372. } else {
  373. *((short *) dst) = (short)y;
  374. }
  375. dst += dst_skip;
  376. src++;
  377. }
  378. state->rm1 = rm1;
  379. state->idx = idx;
  380. }
  381. void sample_move_dS_s16 (jack_default_audio_sample_t *dst, char *src, unsigned long nsamples, unsigned long src_skip)
  382. {
  383. /* ALERT: signed sign-extension portability !!! */
  384. while (nsamples--) {
  385. *dst = (*((short *) src)) / SAMPLE_MAX_16BIT;
  386. dst++;
  387. src += src_skip;
  388. }
  389. }
  390. void sample_merge_d16_sS (char *dst, jack_default_audio_sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state)
  391. {
  392. short val;
  393. /* ALERT: signed sign-extension portability !!! */
  394. while (nsamples--) {
  395. val = (short) (*src * SAMPLE_MAX_16BIT);
  396. if (val > SHRT_MAX - *((short *) dst)) {
  397. *((short *)dst) = SHRT_MAX;
  398. } else if (val < SHRT_MIN - *((short *) dst)) {
  399. *((short *)dst) = SHRT_MIN;
  400. } else {
  401. *((short *) dst) += val;
  402. }
  403. dst += dst_skip;
  404. src++;
  405. }
  406. }
  407. void sample_merge_d32u24_sS (char *dst, jack_default_audio_sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state)
  408. {
  409. /* ALERT: signed sign-extension portability !!! */
  410. while (nsamples--) {
  411. *((int *) dst) += (((int) (*src * SAMPLE_MAX_24BIT)) << 8);
  412. dst += dst_skip;
  413. src++;
  414. }
  415. }
  416. void memset_interleave (char *dst, char val, unsigned long bytes,
  417. unsigned long unit_bytes,
  418. unsigned long skip_bytes)
  419. {
  420. switch (unit_bytes) {
  421. case 1:
  422. while (bytes--) {
  423. *dst = val;
  424. dst += skip_bytes;
  425. }
  426. break;
  427. case 2:
  428. while (bytes) {
  429. *((short *) dst) = (short) val;
  430. dst += skip_bytes;
  431. bytes -= 2;
  432. }
  433. break;
  434. case 4:
  435. while (bytes) {
  436. *((int *) dst) = (int) val;
  437. dst += skip_bytes;
  438. bytes -= 4;
  439. }
  440. break;
  441. default:
  442. while (bytes) {
  443. memset(dst, val, unit_bytes);
  444. dst += skip_bytes;
  445. bytes -= unit_bytes;
  446. }
  447. break;
  448. }
  449. }
  450. /* COPY FUNCTIONS: used to move data from an input channel to an
  451. output channel. Note that we assume that the skip distance
  452. is the same for both channels. This is completely fine
  453. unless the input and output were on different audio interfaces that
  454. were interleaved differently. We don't try to handle that.
  455. */
  456. void
  457. memcpy_fake (char *dst, char *src, unsigned long src_bytes, unsigned long foo, unsigned long bar)
  458. {
  459. memcpy (dst, src, src_bytes);
  460. }
  461. void
  462. merge_memcpy_d16_s16 (char *dst, char *src, unsigned long src_bytes,
  463. unsigned long dst_skip_bytes, unsigned long src_skip_bytes)
  464. {
  465. while (src_bytes) {
  466. *((short *) dst) += *((short *) src);
  467. dst += 2;
  468. src += 2;
  469. src_bytes -= 2;
  470. }
  471. }
  472. void
  473. merge_memcpy_d32_s32 (char *dst, char *src, unsigned long src_bytes,
  474. unsigned long dst_skip_bytes, unsigned long src_skip_bytes)
  475. {
  476. while (src_bytes) {
  477. *((int *) dst) += *((int *) src);
  478. dst += 4;
  479. src += 4;
  480. src_bytes -= 4;
  481. }
  482. }
  483. void
  484. merge_memcpy_interleave_d16_s16 (char *dst, char *src, unsigned long src_bytes,
  485. unsigned long dst_skip_bytes, unsigned long src_skip_bytes)
  486. {
  487. while (src_bytes) {
  488. *((short *) dst) += *((short *) src);
  489. dst += dst_skip_bytes;
  490. src += src_skip_bytes;
  491. src_bytes -= 2;
  492. }
  493. }
  494. void
  495. merge_memcpy_interleave_d32_s32 (char *dst, char *src, unsigned long src_bytes,
  496. unsigned long dst_skip_bytes, unsigned long src_skip_bytes)
  497. {
  498. while (src_bytes) {
  499. *((int *) dst) += *((int *) src);
  500. dst += dst_skip_bytes;
  501. src += src_skip_bytes;
  502. src_bytes -= 4;
  503. }
  504. }
  505. void
  506. merge_memcpy_interleave_d24_s24 (char *dst, char *src, unsigned long src_bytes,
  507. unsigned long dst_skip_bytes, unsigned long src_skip_bytes)
  508. {
  509. while (src_bytes) {
  510. int acc = (*(int *)dst & 0xFFFFFF) + (*(int *)src & 0xFFFFFF);
  511. memcpy(dst, &acc, 3);
  512. dst += dst_skip_bytes;
  513. src += src_skip_bytes;
  514. src_bytes -= 3;
  515. }
  516. }
  517. void
  518. memcpy_interleave_d16_s16 (char *dst, char *src, unsigned long src_bytes,
  519. unsigned long dst_skip_bytes, unsigned long src_skip_bytes)
  520. {
  521. while (src_bytes) {
  522. *((short *) dst) = *((short *) src);
  523. dst += dst_skip_bytes;
  524. src += src_skip_bytes;
  525. src_bytes -= 2;
  526. }
  527. }
  528. void
  529. memcpy_interleave_d24_s24 (char *dst, char *src, unsigned long src_bytes,
  530. unsigned long dst_skip_bytes, unsigned long src_skip_bytes)
  531. {
  532. while (src_bytes) {
  533. memcpy(dst, src, 3);
  534. dst += dst_skip_bytes;
  535. src += src_skip_bytes;
  536. src_bytes -= 3;
  537. }
  538. }
  539. void
  540. memcpy_interleave_d32_s32 (char *dst, char *src, unsigned long src_bytes,
  541. unsigned long dst_skip_bytes, unsigned long src_skip_bytes)
  542. {
  543. while (src_bytes) {
  544. *((int *) dst) = *((int *) src);
  545. dst += dst_skip_bytes;
  546. src += src_skip_bytes;
  547. src_bytes -= 4;
  548. }
  549. }