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

472 lines
11KB

  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$
  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 <jack/memops.h>
  27. #define SAMPLE_MAX_24BIT 8388607.0f
  28. #define SAMPLE_MAX_16BIT 32767.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. /* XXX we could use rint(), but for now we'll accept whatever default
  39. floating-point => int conversion the compiler provides.
  40. */
  41. void sample_move_d32u24_sS (char *dst, jack_default_audio_sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state)
  42. {
  43. long long y;
  44. while (nsamples--) {
  45. y = (long long)(*src * SAMPLE_MAX_24BIT) << 8;
  46. if (y > INT_MAX) {
  47. *((int *) dst) = INT_MAX;
  48. } else if (y < INT_MIN) {
  49. *((int *) dst) = INT_MIN;
  50. } else {
  51. *((int *) dst) = (int)y;
  52. }
  53. dst += dst_skip;
  54. src++;
  55. }
  56. }
  57. void sample_move_dS_s32u24 (jack_default_audio_sample_t *dst, char *src, unsigned long nsamples, unsigned long src_skip)
  58. {
  59. /* ALERT: signed sign-extension portability !!! */
  60. while (nsamples--) {
  61. *dst = (*((int *) src) >> 8) / SAMPLE_MAX_24BIT;
  62. dst++;
  63. src += src_skip;
  64. }
  65. }
  66. 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)
  67. {
  68. /* ALERT: signed sign-extension portability !!! */
  69. jack_default_audio_sample_t x;
  70. long long y;
  71. while (nsamples--) {
  72. x = *src * SAMPLE_MAX_16BIT;
  73. x -= (float)fast_rand() / (float)INT_MAX;
  74. y = (long long)f_round(x);
  75. y <<= 16;
  76. if (y > INT_MAX) {
  77. *((int *) dst) = INT_MAX;
  78. } else if (y < INT_MIN) {
  79. *((int *) dst) = INT_MIN;
  80. } else {
  81. *((int *) dst) = (int)y;
  82. }
  83. dst += dst_skip;
  84. src++;
  85. }
  86. }
  87. 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)
  88. {
  89. jack_default_audio_sample_t x;
  90. float r;
  91. float rm1 = state->rm1;
  92. long long y;
  93. while (nsamples--) {
  94. x = *src * (float)SAMPLE_MAX_16BIT;
  95. r = 2.0f * (float)fast_rand() / (float)INT_MAX - 1.0f;
  96. x += r - rm1;
  97. rm1 = r;
  98. /* swh: This could be some inline asm on x86 */
  99. y = (long long)f_round(x);
  100. y <<= 16;
  101. if (y > INT_MAX) {
  102. *((int *) dst) = INT_MAX;
  103. } else if (y < INT_MIN) {
  104. *((int *) dst) = INT_MIN;
  105. } else {
  106. *((int *) dst) = (int)y;
  107. }
  108. dst += dst_skip;
  109. src++;
  110. }
  111. state->rm1 = rm1;
  112. }
  113. 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)
  114. {
  115. jack_default_audio_sample_t x;
  116. jack_default_audio_sample_t xe; /* the innput sample - filtered error */
  117. jack_default_audio_sample_t xp; /* x' */
  118. float r;
  119. float rm1 = state->rm1;
  120. unsigned int idx = state->idx;
  121. long long y;
  122. while (nsamples--) {
  123. x = *src * (float)SAMPLE_MAX_16BIT;
  124. r = 2.0f * (float)fast_rand() / (float)INT_MAX - 1.0f;
  125. /* Filter the error with Lipshitz's minimally audible FIR:
  126. [2.033 -2.165 1.959 -1.590 0.6149] */
  127. xe = x
  128. - state->e[idx] * 2.033f
  129. + state->e[(idx - 1) & DITHER_BUF_MASK] * 2.165f
  130. - state->e[(idx - 2) & DITHER_BUF_MASK] * 1.959f
  131. + state->e[(idx - 3) & DITHER_BUF_MASK] * 1.590f
  132. - state->e[(idx - 4) & DITHER_BUF_MASK] * 0.6149f;
  133. xp = xe + r - rm1;
  134. rm1 = r;
  135. /* This could be some inline asm on x86 */
  136. y = (long long)f_round(xp);
  137. /* Intrinsic z^-1 delay */
  138. idx = (idx + 1) & DITHER_BUF_MASK;
  139. state->e[idx] = y - xe;
  140. y <<= 16;
  141. if (y > INT_MAX) {
  142. *((int *) dst) = INT_MAX;
  143. } else if (y < INT_MIN) {
  144. *((int *) dst) = INT_MIN;
  145. } else {
  146. *((int *) dst) = (int)y;
  147. }
  148. dst += dst_skip;
  149. src++;
  150. }
  151. state->rm1 = rm1;
  152. state->idx = idx;
  153. }
  154. void sample_move_d16_sS (char *dst, jack_default_audio_sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state)
  155. {
  156. jack_default_audio_sample_t val;
  157. /* ALERT: signed sign-extension portability !!! */
  158. /* XXX good to use x86 assembler here, since float->short
  159. sucks that h/w.
  160. */
  161. while (nsamples--) {
  162. val = *src;
  163. if (val > 1.0f) {
  164. *((short *)dst) = SHRT_MAX;
  165. } else if (val < -1.0f) {
  166. *((short *)dst) = SHRT_MIN;
  167. } else {
  168. *((short *) dst) = (short) (val * SAMPLE_MAX_16BIT);
  169. }
  170. dst += dst_skip;
  171. src++;
  172. }
  173. }
  174. 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)
  175. {
  176. jack_default_audio_sample_t val;
  177. int tmp;
  178. while (nsamples--) {
  179. val = *src * (float)SAMPLE_MAX_16BIT;
  180. val -= (float)fast_rand() / (float)INT_MAX;
  181. /* swh: This could be some inline asm on x86 */
  182. tmp = f_round(val);
  183. if (tmp > SHRT_MAX) {
  184. *((short *)dst) = SHRT_MAX;
  185. } else if (tmp < SHRT_MIN) {
  186. *((short *)dst) = SHRT_MIN;
  187. } else {
  188. *((short *) dst) = (short)tmp;
  189. }
  190. dst += dst_skip;
  191. src++;
  192. }
  193. }
  194. 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)
  195. {
  196. jack_default_audio_sample_t x;
  197. float r;
  198. float rm1 = state->rm1;
  199. int y;
  200. while (nsamples--) {
  201. x = *src * (float)SAMPLE_MAX_16BIT;
  202. r = 2.0f * (float)fast_rand() / (float)INT_MAX - 1.0f;
  203. x += r - rm1;
  204. rm1 = r;
  205. /* swh: This could be some inline asm on x86 */
  206. y = f_round(x);
  207. if (y > SHRT_MAX) {
  208. *((short *)dst) = SHRT_MAX;
  209. } else if (y < SHRT_MIN) {
  210. *((short *)dst) = SHRT_MIN;
  211. } else {
  212. *((short *) dst) = (short)y;
  213. }
  214. dst += dst_skip;
  215. src++;
  216. }
  217. state->rm1 = rm1;
  218. }
  219. 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)
  220. {
  221. jack_default_audio_sample_t x;
  222. jack_default_audio_sample_t xe; /* the innput sample - filtered error */
  223. jack_default_audio_sample_t xp; /* x' */
  224. float r;
  225. float rm1 = state->rm1;
  226. unsigned int idx = state->idx;
  227. int y;
  228. while (nsamples--) {
  229. x = *src * (float)SAMPLE_MAX_16BIT;
  230. r = 2.0f * (float)fast_rand() / (float)INT_MAX - 1.0f;
  231. /* Filter the error with Lipshitz's minimally audible FIR:
  232. [2.033 -2.165 1.959 -1.590 0.6149] */
  233. xe = x
  234. - state->e[idx] * 2.033f
  235. + state->e[(idx - 1) & DITHER_BUF_MASK] * 2.165f
  236. - state->e[(idx - 2) & DITHER_BUF_MASK] * 1.959f
  237. + state->e[(idx - 3) & DITHER_BUF_MASK] * 1.590f
  238. - state->e[(idx - 4) & DITHER_BUF_MASK] * 0.6149f;
  239. xp = xe + r - rm1;
  240. rm1 = r;
  241. /* This could be some inline asm on x86 */
  242. y = f_round(xp);
  243. /* Intrinsic z^-1 delay */
  244. idx = (idx + 1) & DITHER_BUF_MASK;
  245. state->e[idx] = y - xe;
  246. if (y > SHRT_MAX) {
  247. *((short *)dst) = SHRT_MAX;
  248. } else if (y < SHRT_MIN) {
  249. *((short *)dst) = SHRT_MIN;
  250. } else {
  251. *((short *) dst) = (short)y;
  252. }
  253. dst += dst_skip;
  254. src++;
  255. }
  256. state->rm1 = rm1;
  257. state->idx = idx;
  258. }
  259. void sample_move_dS_s16 (jack_default_audio_sample_t *dst, char *src, unsigned long nsamples, unsigned long src_skip)
  260. {
  261. /* ALERT: signed sign-extension portability !!! */
  262. while (nsamples--) {
  263. *dst = (*((short *) src)) / SAMPLE_MAX_16BIT;
  264. dst++;
  265. src += src_skip;
  266. }
  267. }
  268. void sample_merge_d16_sS (char *dst, jack_default_audio_sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state)
  269. {
  270. short val;
  271. /* ALERT: signed sign-extension portability !!! */
  272. while (nsamples--) {
  273. val = (short) (*src * SAMPLE_MAX_16BIT);
  274. if (val > SHRT_MAX - *((short *) dst)) {
  275. *((short *)dst) = SHRT_MAX;
  276. } else if (val < SHRT_MIN - *((short *) dst)) {
  277. *((short *)dst) = SHRT_MIN;
  278. } else {
  279. *((short *) dst) += val;
  280. }
  281. dst += dst_skip;
  282. src++;
  283. }
  284. }
  285. void sample_merge_d32u24_sS (char *dst, jack_default_audio_sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state)
  286. {
  287. /* ALERT: signed sign-extension portability !!! */
  288. while (nsamples--) {
  289. *((int *) dst) += (((int) (*src * SAMPLE_MAX_24BIT)) << 8);
  290. dst += dst_skip;
  291. src++;
  292. }
  293. }
  294. void memset_interleave (char *dst, char val, unsigned long bytes,
  295. unsigned long unit_bytes,
  296. unsigned long skip_bytes)
  297. {
  298. switch (unit_bytes) {
  299. case 1:
  300. while (bytes--) {
  301. *dst = val;
  302. dst += skip_bytes;
  303. }
  304. break;
  305. case 2:
  306. while (bytes) {
  307. *((short *) dst) = (short) val;
  308. dst += skip_bytes;
  309. bytes -= 2;
  310. }
  311. break;
  312. case 4:
  313. while (bytes) {
  314. *((int *) dst) = (int) val;
  315. dst += skip_bytes;
  316. bytes -= 4;
  317. }
  318. break;
  319. }
  320. }
  321. /* COPY FUNCTIONS: used to move data from an input channel to an
  322. output channel. Note that we assume that the skip distance
  323. is the same for both channels. This is completely fine
  324. unless the input and output were on different audio interfaces that
  325. were interleaved differently. We don't try to handle that.
  326. */
  327. void
  328. memcpy_fake (char *dst, char *src, unsigned long src_bytes, unsigned long foo, unsigned long bar)
  329. {
  330. memcpy (dst, src, src_bytes);
  331. }
  332. void
  333. merge_memcpy_d16_s16 (char *dst, char *src, unsigned long src_bytes,
  334. unsigned long dst_skip_bytes, unsigned long src_skip_bytes)
  335. {
  336. while (src_bytes) {
  337. *((short *) dst) += *((short *) src);
  338. dst += 2;
  339. src += 2;
  340. src_bytes -= 2;
  341. }
  342. }
  343. void
  344. merge_memcpy_d32_s32 (char *dst, char *src, unsigned long src_bytes,
  345. unsigned long dst_skip_bytes, unsigned long src_skip_bytes)
  346. {
  347. while (src_bytes) {
  348. *((int *) dst) += *((int *) src);
  349. dst += 4;
  350. src += 4;
  351. src_bytes -= 4;
  352. }
  353. }
  354. void
  355. merge_memcpy_interleave_d16_s16 (char *dst, char *src, unsigned long src_bytes,
  356. unsigned long dst_skip_bytes, unsigned long src_skip_bytes)
  357. {
  358. while (src_bytes) {
  359. *((short *) dst) += *((short *) src);
  360. dst += dst_skip_bytes;
  361. src += src_skip_bytes;
  362. src_bytes -= 2;
  363. }
  364. }
  365. void
  366. merge_memcpy_interleave_d32_s32 (char *dst, char *src, unsigned long src_bytes,
  367. unsigned long dst_skip_bytes, unsigned long src_skip_bytes)
  368. {
  369. while (src_bytes) {
  370. *((int *) dst) += *((int *) src);
  371. dst += dst_skip_bytes;
  372. src += src_skip_bytes;
  373. src_bytes -= 4;
  374. }
  375. }
  376. void
  377. memcpy_interleave_d16_s16 (char *dst, char *src, unsigned long src_bytes,
  378. unsigned long dst_skip_bytes, unsigned long src_skip_bytes)
  379. {
  380. while (src_bytes) {
  381. *((short *) dst) = *((short *) src);
  382. dst += dst_skip_bytes;
  383. src += src_skip_bytes;
  384. src_bytes -= 2;
  385. }
  386. }
  387. void
  388. memcpy_interleave_d32_s32 (char *dst, char *src, unsigned long src_bytes,
  389. unsigned long dst_skip_bytes, unsigned long src_skip_bytes)
  390. {
  391. while (src_bytes) {
  392. *((int *) dst) = *((int *) src);
  393. dst += dst_skip_bytes;
  394. src += src_skip_bytes;
  395. src_bytes -= 4;
  396. }
  397. }