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.

350 lines
8.2KB

  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. #include <stdio.h>
  17. #include <string.h>
  18. #include <math.h>
  19. #include <memory.h>
  20. #include <stdlib.h>
  21. #include <limits.h>
  22. #include <jack/memops.h>
  23. #define SAMPLE_MAX_24BIT 8388608.0f
  24. #define SAMPLE_MAX_16BIT 32767.0f
  25. /* Round float to int (can't go in memops.h otherwise it gets multiply
  26. defined) */
  27. inline int f_round(float f) {
  28. f += (3<<22);
  29. return *((int*)&f) - 0x4b400000;
  30. }
  31. /* XXX we could use rint(), but for now we'll accept whatever default
  32. floating-point => int conversion the compiler provides.
  33. */
  34. void sample_move_d32u24_sS (char *dst, sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state)
  35. {
  36. /* ALERT: signed sign-extension portability !!! */
  37. while (nsamples--) {
  38. *((int *) dst) = ((int) (*src * SAMPLE_MAX_24BIT)) << 8;
  39. dst += dst_skip;
  40. src++;
  41. }
  42. }
  43. void sample_move_dS_s32u24 (sample_t *dst, char *src, unsigned long nsamples, unsigned long src_skip)
  44. {
  45. /* ALERT: signed sign-extension portability !!! */
  46. while (nsamples--) {
  47. *dst = (*((int *) src) >> 8) / SAMPLE_MAX_24BIT;
  48. dst++;
  49. src += src_skip;
  50. }
  51. }
  52. void sample_move_d16_sS (char *dst, sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state)
  53. {
  54. sample_t val;
  55. /* ALERT: signed sign-extension portability !!! */
  56. /* XXX good to use x86 assembler here, since float->short
  57. sucks that h/w.
  58. */
  59. while (nsamples--) {
  60. val = *src;
  61. if (val > 1.0f) {
  62. *((short *)dst) = SHRT_MAX;
  63. } else if (val < -1.0f) {
  64. *((short *)dst) = SHRT_MIN;
  65. } else {
  66. *((short *) dst) = (short) (val * SAMPLE_MAX_16BIT);
  67. }
  68. dst += dst_skip;
  69. src++;
  70. }
  71. }
  72. void sample_move_dither_rect_d16_sS (char *dst, sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state)
  73. {
  74. sample_t val;
  75. int tmp;
  76. while (nsamples--) {
  77. val = *src * (float)SAMPLE_MAX_16BIT;
  78. val -= (float)rand() / (float)RAND_MAX;
  79. /* swh: This could be some inline asm on x86 */
  80. tmp = f_round(val);
  81. if (tmp > SHRT_MAX) {
  82. *((short *)dst) = SHRT_MAX;
  83. } else if (tmp < SHRT_MIN) {
  84. *((short *)dst) = SHRT_MIN;
  85. } else {
  86. *((short *) dst) = (short)tmp;
  87. }
  88. dst += dst_skip;
  89. src++;
  90. }
  91. }
  92. void sample_move_dither_tri_d16_sS (char *dst, sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state)
  93. {
  94. sample_t x;
  95. float r;
  96. float rm1 = state->rm1;
  97. int y;
  98. while (nsamples--) {
  99. x = *src * (float)SAMPLE_MAX_16BIT;
  100. r = 2.0f * (float)rand() / (float)RAND_MAX - 1.0f;
  101. x += r - rm1;
  102. rm1 = r;
  103. /* swh: This could be some inline asm on x86 */
  104. y = f_round(x);
  105. if (y > SHRT_MAX) {
  106. *((short *)dst) = SHRT_MAX;
  107. } else if (y < SHRT_MIN) {
  108. *((short *)dst) = SHRT_MIN;
  109. } else {
  110. *((short *) dst) = (short)y;
  111. }
  112. dst += dst_skip;
  113. src++;
  114. }
  115. state->rm1 = rm1;
  116. }
  117. void sample_move_dither_shaped_d16_sS (char *dst, sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state)
  118. {
  119. sample_t x;
  120. sample_t xe; /* the innput sample - filtered error */
  121. sample_t xp; /* x' */
  122. float r;
  123. float rm1 = state->rm1;
  124. unsigned int idx = state->idx;
  125. int y;
  126. while (nsamples--) {
  127. x = *src * (float)SAMPLE_MAX_16BIT;
  128. r = 2.0f * (float)rand() / (float)RAND_MAX - 1.0f;
  129. /* Filter the error with Lipshitz's minimally audible FIR:
  130. [2.033 -2.165 1.959 -1.590 0.6149] */
  131. xe = x
  132. - state->e[idx] * 2.033f
  133. + state->e[(idx - 1) & DITHER_BUF_MASK] * 2.165f
  134. - state->e[(idx - 2) & DITHER_BUF_MASK] * 1.959f
  135. + state->e[(idx - 3) & DITHER_BUF_MASK] * 1.590f
  136. - state->e[(idx - 4) & DITHER_BUF_MASK] * 0.6149f;
  137. xp = xe + r - rm1;
  138. rm1 = r;
  139. /* This could be some inline asm on x86 */
  140. y = f_round(xp);
  141. /* Intrinsic z^-1 delay */
  142. idx = (idx + 1) & DITHER_BUF_MASK;
  143. state->e[idx] = y - xe;
  144. if (y > SHRT_MAX) {
  145. *((short *)dst) = SHRT_MAX;
  146. } else if (y < SHRT_MIN) {
  147. *((short *)dst) = SHRT_MIN;
  148. } else {
  149. *((short *) dst) = (short)y;
  150. }
  151. dst += dst_skip;
  152. src++;
  153. }
  154. state->rm1 = rm1;
  155. state->idx = idx;
  156. }
  157. void sample_move_dS_s16 (sample_t *dst, char *src, unsigned long nsamples, unsigned long src_skip)
  158. {
  159. /* ALERT: signed sign-extension portability !!! */
  160. while (nsamples--) {
  161. *dst = (*((short *) src)) / SAMPLE_MAX_16BIT;
  162. dst++;
  163. src += src_skip;
  164. }
  165. }
  166. void sample_merge_d16_sS (char *dst, sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state)
  167. {
  168. short val;
  169. /* ALERT: signed sign-extension portability !!! */
  170. while (nsamples--) {
  171. val = (short) (*src * SAMPLE_MAX_16BIT);
  172. if (val > SHRT_MAX - *((short *) dst)) {
  173. *((short *)dst) = SHRT_MAX;
  174. } else if (val < SHRT_MIN - *((short *) dst)) {
  175. *((short *)dst) = SHRT_MIN;
  176. } else {
  177. *((short *) dst) += val;
  178. }
  179. dst += dst_skip;
  180. src++;
  181. }
  182. }
  183. void sample_merge_d32u24_sS (char *dst, sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state)
  184. {
  185. /* ALERT: signed sign-extension portability !!! */
  186. while (nsamples--) {
  187. *((int *) dst) += (((int) (*src * SAMPLE_MAX_24BIT)) << 8);
  188. dst += dst_skip;
  189. src++;
  190. }
  191. }
  192. void memset_interleave (char *dst, char val, unsigned long bytes,
  193. unsigned long unit_bytes,
  194. unsigned long skip_bytes)
  195. {
  196. switch (unit_bytes) {
  197. case 1:
  198. while (bytes--) {
  199. *dst = val;
  200. dst += skip_bytes;
  201. }
  202. break;
  203. case 2:
  204. while (bytes) {
  205. *((short *) dst) = (short) val;
  206. dst += skip_bytes;
  207. bytes -= 2;
  208. }
  209. break;
  210. case 4:
  211. while (bytes) {
  212. *((int *) dst) = (int) val;
  213. dst += skip_bytes;
  214. bytes -= 4;
  215. }
  216. break;
  217. }
  218. }
  219. /* COPY FUNCTIONS: used to move data from an input channel to an
  220. output channel. Note that we assume that the skip distance
  221. is the same for both channels. This is completely fine
  222. unless the input and output were on different audio interfaces that
  223. were interleaved differently. We don't try to handle that.
  224. */
  225. void
  226. memcpy_fake (char *dst, char *src, unsigned long src_bytes, unsigned long foo, unsigned long bar)
  227. {
  228. memcpy (dst, src, src_bytes);
  229. }
  230. void
  231. merge_memcpy_d16_s16 (char *dst, char *src, unsigned long src_bytes,
  232. unsigned long dst_skip_bytes, unsigned long src_skip_bytes)
  233. {
  234. while (src_bytes) {
  235. *((short *) dst) += *((short *) src);
  236. dst += 2;
  237. src += 2;
  238. src_bytes -= 2;
  239. }
  240. }
  241. void
  242. merge_memcpy_d32_s32 (char *dst, char *src, unsigned long src_bytes,
  243. unsigned long dst_skip_bytes, unsigned long src_skip_bytes)
  244. {
  245. while (src_bytes) {
  246. *((int *) dst) += *((int *) src);
  247. dst += 4;
  248. src += 4;
  249. src_bytes -= 4;
  250. }
  251. }
  252. void
  253. merge_memcpy_interleave_d16_s16 (char *dst, char *src, unsigned long src_bytes,
  254. unsigned long dst_skip_bytes, unsigned long src_skip_bytes)
  255. {
  256. while (src_bytes) {
  257. *((short *) dst) += *((short *) src);
  258. dst += dst_skip_bytes;
  259. src += src_skip_bytes;
  260. src_bytes -= 2;
  261. }
  262. }
  263. void
  264. merge_memcpy_interleave_d32_s32 (char *dst, char *src, unsigned long src_bytes,
  265. unsigned long dst_skip_bytes, unsigned long src_skip_bytes)
  266. {
  267. while (src_bytes) {
  268. *((int *) dst) += *((int *) src);
  269. dst += dst_skip_bytes;
  270. src += src_skip_bytes;
  271. src_bytes -= 4;
  272. }
  273. }
  274. void
  275. memcpy_interleave_d16_s16 (char *dst, char *src, unsigned long src_bytes,
  276. unsigned long dst_skip_bytes, unsigned long src_skip_bytes)
  277. {
  278. while (src_bytes) {
  279. *((short *) dst) = *((short *) src);
  280. dst += dst_skip_bytes;
  281. src += src_skip_bytes;
  282. src_bytes -= 2;
  283. }
  284. }
  285. void
  286. memcpy_interleave_d32_s32 (char *dst, char *src, unsigned long src_bytes,
  287. unsigned long dst_skip_bytes, unsigned long src_skip_bytes)
  288. {
  289. while (src_bytes) {
  290. *((int *) dst) = *((int *) src);
  291. dst += dst_skip_bytes;
  292. src += src_skip_bytes;
  293. src_bytes -= 4;
  294. }
  295. }