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.

274 lines
6.3KB

  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)
  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)
  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)
  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_dS_s16 (sample_t *dst, char *src, unsigned long nsamples, unsigned long src_skip)
  93. {
  94. /* ALERT: signed sign-extension portability !!! */
  95. while (nsamples--) {
  96. *dst = (*((short *) src)) / SAMPLE_MAX_16BIT;
  97. dst++;
  98. src += src_skip;
  99. }
  100. }
  101. void sample_merge_d16_sS (char *dst, sample_t *src, unsigned long nsamples, unsigned long dst_skip)
  102. {
  103. short val;
  104. /* ALERT: signed sign-extension portability !!! */
  105. while (nsamples--) {
  106. val = (short) (*src * SAMPLE_MAX_16BIT);
  107. if (val > SHRT_MAX - *((short *) dst)) {
  108. *((short *)dst) = SHRT_MAX;
  109. } else if (val < SHRT_MIN - *((short *) dst)) {
  110. *((short *)dst) = SHRT_MIN;
  111. } else {
  112. *((short *) dst) += val;
  113. }
  114. dst += dst_skip;
  115. src++;
  116. }
  117. }
  118. void sample_merge_d32u24_sS (char *dst, sample_t *src, unsigned long nsamples, unsigned long dst_skip)
  119. {
  120. /* ALERT: signed sign-extension portability !!! */
  121. while (nsamples--) {
  122. *((int *) dst) += (((int) (*src * SAMPLE_MAX_24BIT)) << 8);
  123. dst += dst_skip;
  124. src++;
  125. }
  126. }
  127. void memset_interleave (char *dst, char val, unsigned long bytes,
  128. unsigned long unit_bytes,
  129. unsigned long skip_bytes)
  130. {
  131. switch (unit_bytes) {
  132. case 1:
  133. while (bytes--) {
  134. *dst = val;
  135. dst += skip_bytes;
  136. }
  137. break;
  138. case 2:
  139. while (bytes) {
  140. *((short *) dst) = (short) val;
  141. dst += skip_bytes;
  142. bytes -= 2;
  143. }
  144. break;
  145. case 4:
  146. while (bytes) {
  147. *((int *) dst) = (int) val;
  148. dst += skip_bytes;
  149. bytes -= 4;
  150. }
  151. break;
  152. }
  153. }
  154. /* COPY FUNCTIONS: used to move data from an input channel to an
  155. output channel. Note that we assume that the skip distance
  156. is the same for both channels. This is completely fine
  157. unless the input and output were on different audio interfaces that
  158. were interleaved differently. We don't try to handle that.
  159. */
  160. void
  161. memcpy_fake (char *dst, char *src, unsigned long src_bytes, unsigned long foo, unsigned long bar)
  162. {
  163. memcpy (dst, src, src_bytes);
  164. }
  165. void
  166. merge_memcpy_d16_s16 (char *dst, char *src, unsigned long src_bytes,
  167. unsigned long dst_skip_bytes, unsigned long src_skip_bytes)
  168. {
  169. while (src_bytes) {
  170. *((short *) dst) += *((short *) src);
  171. dst += 2;
  172. src += 2;
  173. src_bytes -= 2;
  174. }
  175. }
  176. void
  177. merge_memcpy_d32_s32 (char *dst, char *src, unsigned long src_bytes,
  178. unsigned long dst_skip_bytes, unsigned long src_skip_bytes)
  179. {
  180. while (src_bytes) {
  181. *((int *) dst) += *((int *) src);
  182. dst += 4;
  183. src += 4;
  184. src_bytes -= 4;
  185. }
  186. }
  187. void
  188. merge_memcpy_interleave_d16_s16 (char *dst, char *src, unsigned long src_bytes,
  189. unsigned long dst_skip_bytes, unsigned long src_skip_bytes)
  190. {
  191. while (src_bytes) {
  192. *((short *) dst) += *((short *) src);
  193. dst += dst_skip_bytes;
  194. src += src_skip_bytes;
  195. src_bytes -= 2;
  196. }
  197. }
  198. void
  199. merge_memcpy_interleave_d32_s32 (char *dst, char *src, unsigned long src_bytes,
  200. unsigned long dst_skip_bytes, unsigned long src_skip_bytes)
  201. {
  202. while (src_bytes) {
  203. *((int *) dst) += *((int *) src);
  204. dst += dst_skip_bytes;
  205. src += src_skip_bytes;
  206. src_bytes -= 4;
  207. }
  208. }
  209. void
  210. memcpy_interleave_d16_s16 (char *dst, char *src, unsigned long src_bytes,
  211. unsigned long dst_skip_bytes, unsigned long src_skip_bytes)
  212. {
  213. while (src_bytes) {
  214. *((short *) dst) = *((short *) src);
  215. dst += dst_skip_bytes;
  216. src += src_skip_bytes;
  217. src_bytes -= 2;
  218. }
  219. }
  220. void
  221. memcpy_interleave_d32_s32 (char *dst, char *src, unsigned long src_bytes,
  222. unsigned long dst_skip_bytes, unsigned long src_skip_bytes)
  223. {
  224. while (src_bytes) {
  225. *((int *) dst) = *((int *) src);
  226. dst += dst_skip_bytes;
  227. src += src_skip_bytes;
  228. src_bytes -= 4;
  229. }
  230. }