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.

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