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.

292 lines
6.6KB

  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. /* XXX we could use rint(), but for now we'll accept whatever default
  26. floating-point => int conversion the compiler provides.
  27. */
  28. void sample_move_d32u24_sS (char *dst, sample_t *src, unsigned long nsamples, unsigned long dst_skip, gain_t gain)
  29. {
  30. /* ALERT: signed sign-extension portability !!! */
  31. if (gain == 1.0) {
  32. while (nsamples--) {
  33. *((int *) dst) = ((int) (*src * SAMPLE_MAX_24BIT)) << 8;
  34. dst += dst_skip;
  35. src++;
  36. }
  37. } else {
  38. while (nsamples--) {
  39. *((int *) dst) = ((int) ((*src * gain) * SAMPLE_MAX_24BIT)) << 8;
  40. dst += dst_skip;
  41. src++;
  42. }
  43. }
  44. }
  45. void sample_move_dS_s32u24 (sample_t *dst, char *src, unsigned long nsamples, unsigned long src_skip)
  46. {
  47. /* ALERT: signed sign-extension portability !!! */
  48. while (nsamples--) {
  49. *dst = (*((int *) src) >> 8) / SAMPLE_MAX_24BIT;
  50. dst++;
  51. src += src_skip;
  52. }
  53. }
  54. void sample_move_d16_sS (char *dst, sample_t *src, unsigned long nsamples, unsigned long dst_skip, gain_t gain)
  55. {
  56. sample_t val;
  57. /* ALERT: signed sign-extension portability !!! */
  58. /* XXX good to use x86 assembler here, since float->short
  59. sucks that h/w.
  60. */
  61. if (gain == 1.0) {
  62. while (nsamples--) {
  63. val = *src;
  64. if (val > 1.0f) {
  65. *((short *)dst) = SHRT_MAX;
  66. } else if (val < -1.0f) {
  67. *((short *)dst) = SHRT_MIN;
  68. } else {
  69. *((short *) dst) = (short) (val * SAMPLE_MAX_16BIT);
  70. }
  71. dst += dst_skip;
  72. src++;
  73. }
  74. } else {
  75. while (nsamples--) {
  76. val = *src * gain;
  77. if (val > 1.0f) {
  78. *((short *)dst) = SHRT_MAX;
  79. } else if (val < -1.0f) {
  80. *((short *)dst) = SHRT_MIN;
  81. } else {
  82. *((short *) dst) = (short) (val * SAMPLE_MAX_16BIT);
  83. }
  84. dst += dst_skip;
  85. src++;
  86. }
  87. }
  88. }
  89. void sample_move_dS_s16 (sample_t *dst, char *src, unsigned long nsamples, unsigned long src_skip)
  90. {
  91. /* ALERT: signed sign-extension portability !!! */
  92. while (nsamples--) {
  93. *dst = (*((short *) src)) / SAMPLE_MAX_16BIT;
  94. dst++;
  95. src += src_skip;
  96. }
  97. }
  98. void sample_merge_d16_sS (char *dst, sample_t *src, unsigned long nsamples, unsigned long dst_skip, gain_t gain)
  99. {
  100. short val;
  101. /* ALERT: signed sign-extension portability !!! */
  102. if (gain == 1.0) {
  103. while (nsamples--) {
  104. val = (short) (*src * SAMPLE_MAX_16BIT);
  105. if (val > SHRT_MAX - *((short *) dst)) {
  106. *((short *)dst) = SHRT_MAX;
  107. } else if (val < SHRT_MIN - *((short *) dst)) {
  108. *((short *)dst) = SHRT_MIN;
  109. } else {
  110. *((short *) dst) += val;
  111. }
  112. dst += dst_skip;
  113. src++;
  114. }
  115. } else {
  116. while (nsamples--) {
  117. val = (short) (*src * gain * SAMPLE_MAX_16BIT);
  118. if (val > SHRT_MAX - *((short *) dst)) {
  119. *((short *)dst) = SHRT_MAX;
  120. } else if (val < SHRT_MIN - *((short *) dst)) {
  121. *((short *)dst) = SHRT_MIN;
  122. } else {
  123. *((short *) dst) += val;
  124. }
  125. dst += dst_skip;
  126. src++;
  127. }
  128. }
  129. }
  130. void sample_merge_d32u24_sS (char *dst, sample_t *src, unsigned long nsamples, unsigned long dst_skip, gain_t gain)
  131. {
  132. /* ALERT: signed sign-extension portability !!! */
  133. if (gain == 1.0) {
  134. while (nsamples--) {
  135. *((int *) dst) += (((int) (*src * SAMPLE_MAX_24BIT)) << 8);
  136. dst += dst_skip;
  137. src++;
  138. }
  139. } else {
  140. while (nsamples--) {
  141. *((int *) dst) += (((int) ((*src * gain) * SAMPLE_MAX_24BIT)) << 8);
  142. dst += dst_skip;
  143. src++;
  144. }
  145. }
  146. }
  147. void memset_interleave (char *dst, char val, unsigned long bytes,
  148. unsigned long unit_bytes,
  149. unsigned long skip_bytes)
  150. {
  151. switch (unit_bytes) {
  152. case 1:
  153. while (bytes--) {
  154. *dst = val;
  155. dst += skip_bytes;
  156. }
  157. break;
  158. case 2:
  159. while (bytes) {
  160. *((short *) dst) = (short) val;
  161. dst += skip_bytes;
  162. bytes -= 2;
  163. }
  164. break;
  165. case 4:
  166. while (bytes) {
  167. *((int *) dst) = (int) val;
  168. dst += skip_bytes;
  169. bytes -= 4;
  170. }
  171. break;
  172. }
  173. }
  174. /* COPY FUNCTIONS: used to move data from an input channel to an
  175. output channel. Note that we assume that the skip distance
  176. is the same for both channels. This is completely fine
  177. unless the input and output were on different audio interfaces that
  178. were interleaved differently. We don't try to handle that.
  179. */
  180. void
  181. memcpy_fake (char *dst, char *src, unsigned long src_bytes, unsigned long foo, unsigned long bar)
  182. {
  183. memcpy (dst, src, src_bytes);
  184. }
  185. void
  186. merge_memcpy_d16_s16 (char *dst, char *src, unsigned long src_bytes,
  187. unsigned long dst_skip_bytes, unsigned long src_skip_bytes)
  188. {
  189. while (src_bytes) {
  190. *((short *) dst) += *((short *) src);
  191. dst += 2;
  192. src += 2;
  193. src_bytes -= 2;
  194. }
  195. }
  196. void
  197. merge_memcpy_d32_s32 (char *dst, char *src, unsigned long src_bytes,
  198. unsigned long dst_skip_bytes, unsigned long src_skip_bytes)
  199. {
  200. while (src_bytes) {
  201. *((int *) dst) += *((int *) src);
  202. dst += 4;
  203. src += 4;
  204. src_bytes -= 4;
  205. }
  206. }
  207. void
  208. merge_memcpy_interleave_d16_s16 (char *dst, char *src, unsigned long src_bytes,
  209. unsigned long dst_skip_bytes, unsigned long src_skip_bytes)
  210. {
  211. while (src_bytes) {
  212. *((short *) dst) += *((short *) src);
  213. dst += dst_skip_bytes;
  214. src += src_skip_bytes;
  215. src_bytes -= 2;
  216. }
  217. }
  218. void
  219. merge_memcpy_interleave_d32_s32 (char *dst, char *src, unsigned long src_bytes,
  220. unsigned long dst_skip_bytes, unsigned long src_skip_bytes)
  221. {
  222. while (src_bytes) {
  223. *((int *) dst) += *((int *) src);
  224. dst += dst_skip_bytes;
  225. src += src_skip_bytes;
  226. src_bytes -= 4;
  227. }
  228. }
  229. void
  230. memcpy_interleave_d16_s16 (char *dst, char *src, unsigned long src_bytes,
  231. unsigned long dst_skip_bytes, unsigned long src_skip_bytes)
  232. {
  233. while (src_bytes) {
  234. *((short *) dst) = *((short *) src);
  235. dst += dst_skip_bytes;
  236. src += src_skip_bytes;
  237. src_bytes -= 2;
  238. }
  239. }
  240. void
  241. memcpy_interleave_d32_s32 (char *dst, char *src, unsigned long src_bytes,
  242. unsigned long dst_skip_bytes, unsigned long src_skip_bytes)
  243. {
  244. while (src_bytes) {
  245. *((int *) dst) = *((int *) src);
  246. dst += dst_skip_bytes;
  247. src += src_skip_bytes;
  248. src_bytes -= 4;
  249. }
  250. }