mixeng.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /*
  2. * QEMU Mixing engine
  3. *
  4. * Copyright (c) 2004-2005 Vassili Karpov (malc)
  5. * Copyright (c) 1998 Fabrice Bellard
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  20. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. * THE SOFTWARE.
  24. */
  25. #include "qemu-common.h"
  26. #include "audio.h"
  27. #define AUDIO_CAP "mixeng"
  28. #include "audio_int.h"
  29. /* 8 bit */
  30. #define ENDIAN_CONVERSION natural
  31. #define ENDIAN_CONVERT(v) (v)
  32. /* Signed 8 bit */
  33. #define IN_T int8_t
  34. #define IN_MIN SCHAR_MIN
  35. #define IN_MAX SCHAR_MAX
  36. #define SIGNED
  37. #define SHIFT 8
  38. #include "mixeng_template.h"
  39. #undef SIGNED
  40. #undef IN_MAX
  41. #undef IN_MIN
  42. #undef IN_T
  43. #undef SHIFT
  44. /* Unsigned 8 bit */
  45. #define IN_T uint8_t
  46. #define IN_MIN 0
  47. #define IN_MAX UCHAR_MAX
  48. #define SHIFT 8
  49. #include "mixeng_template.h"
  50. #undef IN_MAX
  51. #undef IN_MIN
  52. #undef IN_T
  53. #undef SHIFT
  54. #undef ENDIAN_CONVERT
  55. #undef ENDIAN_CONVERSION
  56. /* Signed 16 bit */
  57. #define IN_T int16_t
  58. #define IN_MIN SHRT_MIN
  59. #define IN_MAX SHRT_MAX
  60. #define SIGNED
  61. #define SHIFT 16
  62. #define ENDIAN_CONVERSION natural
  63. #define ENDIAN_CONVERT(v) (v)
  64. #include "mixeng_template.h"
  65. #undef ENDIAN_CONVERT
  66. #undef ENDIAN_CONVERSION
  67. #define ENDIAN_CONVERSION swap
  68. #define ENDIAN_CONVERT(v) bswap16 (v)
  69. #include "mixeng_template.h"
  70. #undef ENDIAN_CONVERT
  71. #undef ENDIAN_CONVERSION
  72. #undef SIGNED
  73. #undef IN_MAX
  74. #undef IN_MIN
  75. #undef IN_T
  76. #undef SHIFT
  77. /* Unsigned 16 bit */
  78. #define IN_T uint16_t
  79. #define IN_MIN 0
  80. #define IN_MAX USHRT_MAX
  81. #define SHIFT 16
  82. #define ENDIAN_CONVERSION natural
  83. #define ENDIAN_CONVERT(v) (v)
  84. #include "mixeng_template.h"
  85. #undef ENDIAN_CONVERT
  86. #undef ENDIAN_CONVERSION
  87. #define ENDIAN_CONVERSION swap
  88. #define ENDIAN_CONVERT(v) bswap16 (v)
  89. #include "mixeng_template.h"
  90. #undef ENDIAN_CONVERT
  91. #undef ENDIAN_CONVERSION
  92. #undef IN_MAX
  93. #undef IN_MIN
  94. #undef IN_T
  95. #undef SHIFT
  96. /* Signed 32 bit */
  97. #define IN_T int32_t
  98. #define IN_MIN INT32_MIN
  99. #define IN_MAX INT32_MAX
  100. #define SIGNED
  101. #define SHIFT 32
  102. #define ENDIAN_CONVERSION natural
  103. #define ENDIAN_CONVERT(v) (v)
  104. #include "mixeng_template.h"
  105. #undef ENDIAN_CONVERT
  106. #undef ENDIAN_CONVERSION
  107. #define ENDIAN_CONVERSION swap
  108. #define ENDIAN_CONVERT(v) bswap32 (v)
  109. #include "mixeng_template.h"
  110. #undef ENDIAN_CONVERT
  111. #undef ENDIAN_CONVERSION
  112. #undef SIGNED
  113. #undef IN_MAX
  114. #undef IN_MIN
  115. #undef IN_T
  116. #undef SHIFT
  117. /* Unsigned 32 bit */
  118. #define IN_T uint32_t
  119. #define IN_MIN 0
  120. #define IN_MAX UINT32_MAX
  121. #define SHIFT 32
  122. #define ENDIAN_CONVERSION natural
  123. #define ENDIAN_CONVERT(v) (v)
  124. #include "mixeng_template.h"
  125. #undef ENDIAN_CONVERT
  126. #undef ENDIAN_CONVERSION
  127. #define ENDIAN_CONVERSION swap
  128. #define ENDIAN_CONVERT(v) bswap32 (v)
  129. #include "mixeng_template.h"
  130. #undef ENDIAN_CONVERT
  131. #undef ENDIAN_CONVERSION
  132. #undef IN_MAX
  133. #undef IN_MIN
  134. #undef IN_T
  135. #undef SHIFT
  136. t_sample *mixeng_conv[2][2][2][3] = {
  137. {
  138. {
  139. {
  140. conv_natural_uint8_t_to_mono,
  141. conv_natural_uint16_t_to_mono,
  142. conv_natural_uint32_t_to_mono
  143. },
  144. {
  145. conv_natural_uint8_t_to_mono,
  146. conv_swap_uint16_t_to_mono,
  147. conv_swap_uint32_t_to_mono,
  148. }
  149. },
  150. {
  151. {
  152. conv_natural_int8_t_to_mono,
  153. conv_natural_int16_t_to_mono,
  154. conv_natural_int32_t_to_mono
  155. },
  156. {
  157. conv_natural_int8_t_to_mono,
  158. conv_swap_int16_t_to_mono,
  159. conv_swap_int32_t_to_mono
  160. }
  161. }
  162. },
  163. {
  164. {
  165. {
  166. conv_natural_uint8_t_to_stereo,
  167. conv_natural_uint16_t_to_stereo,
  168. conv_natural_uint32_t_to_stereo
  169. },
  170. {
  171. conv_natural_uint8_t_to_stereo,
  172. conv_swap_uint16_t_to_stereo,
  173. conv_swap_uint32_t_to_stereo
  174. }
  175. },
  176. {
  177. {
  178. conv_natural_int8_t_to_stereo,
  179. conv_natural_int16_t_to_stereo,
  180. conv_natural_int32_t_to_stereo
  181. },
  182. {
  183. conv_natural_int8_t_to_stereo,
  184. conv_swap_int16_t_to_stereo,
  185. conv_swap_int32_t_to_stereo,
  186. }
  187. }
  188. }
  189. };
  190. f_sample *mixeng_clip[2][2][2][3] = {
  191. {
  192. {
  193. {
  194. clip_natural_uint8_t_from_mono,
  195. clip_natural_uint16_t_from_mono,
  196. clip_natural_uint32_t_from_mono
  197. },
  198. {
  199. clip_natural_uint8_t_from_mono,
  200. clip_swap_uint16_t_from_mono,
  201. clip_swap_uint32_t_from_mono
  202. }
  203. },
  204. {
  205. {
  206. clip_natural_int8_t_from_mono,
  207. clip_natural_int16_t_from_mono,
  208. clip_natural_int32_t_from_mono
  209. },
  210. {
  211. clip_natural_int8_t_from_mono,
  212. clip_swap_int16_t_from_mono,
  213. clip_swap_int32_t_from_mono
  214. }
  215. }
  216. },
  217. {
  218. {
  219. {
  220. clip_natural_uint8_t_from_stereo,
  221. clip_natural_uint16_t_from_stereo,
  222. clip_natural_uint32_t_from_stereo
  223. },
  224. {
  225. clip_natural_uint8_t_from_stereo,
  226. clip_swap_uint16_t_from_stereo,
  227. clip_swap_uint32_t_from_stereo
  228. }
  229. },
  230. {
  231. {
  232. clip_natural_int8_t_from_stereo,
  233. clip_natural_int16_t_from_stereo,
  234. clip_natural_int32_t_from_stereo
  235. },
  236. {
  237. clip_natural_int8_t_from_stereo,
  238. clip_swap_int16_t_from_stereo,
  239. clip_swap_int32_t_from_stereo
  240. }
  241. }
  242. }
  243. };
  244. /*
  245. * August 21, 1998
  246. * Copyright 1998 Fabrice Bellard.
  247. *
  248. * [Rewrote completly the code of Lance Norskog And Sundry
  249. * Contributors with a more efficient algorithm.]
  250. *
  251. * This source code is freely redistributable and may be used for
  252. * any purpose. This copyright notice must be maintained.
  253. * Lance Norskog And Sundry Contributors are not responsible for
  254. * the consequences of using this software.
  255. */
  256. /*
  257. * Sound Tools rate change effect file.
  258. */
  259. /*
  260. * Linear Interpolation.
  261. *
  262. * The use of fractional increment allows us to use no buffer. It
  263. * avoid the problems at the end of the buffer we had with the old
  264. * method which stored a possibly big buffer of size
  265. * lcm(in_rate,out_rate).
  266. *
  267. * Limited to 16 bit samples and sampling frequency <= 65535 Hz. If
  268. * the input & output frequencies are equal, a delay of one sample is
  269. * introduced. Limited to processing 32-bit count worth of samples.
  270. *
  271. * 1 << FRAC_BITS evaluating to zero in several places. Changed with
  272. * an (unsigned long) cast to make it safe. MarkMLl 2/1/99
  273. */
  274. /* Private data */
  275. struct rate {
  276. uint64_t opos;
  277. uint64_t opos_inc;
  278. uint32_t ipos; /* position in the input stream (integer) */
  279. struct st_sample ilast; /* last sample in the input stream */
  280. };
  281. /*
  282. * Prepare processing.
  283. */
  284. void *st_rate_start (int inrate, int outrate)
  285. {
  286. struct rate *rate = audio_calloc (AUDIO_FUNC, 1, sizeof (*rate));
  287. if (!rate) {
  288. dolog ("Could not allocate resampler (%zu bytes)\n", sizeof (*rate));
  289. return NULL;
  290. }
  291. rate->opos = 0;
  292. /* increment */
  293. rate->opos_inc = ((uint64_t) inrate << 32) / outrate;
  294. rate->ipos = 0;
  295. rate->ilast.l = 0;
  296. rate->ilast.r = 0;
  297. return rate;
  298. }
  299. #define NAME st_rate_flow_mix
  300. #define OP(a, b) a += b
  301. #include "rate_template.h"
  302. #define NAME st_rate_flow
  303. #define OP(a, b) a = b
  304. #include "rate_template.h"
  305. void st_rate_stop (void *opaque)
  306. {
  307. g_free (opaque);
  308. }
  309. void mixeng_clear (struct st_sample *buf, int len)
  310. {
  311. memset (buf, 0, len * sizeof (struct st_sample));
  312. }
  313. void mixeng_volume (struct st_sample *buf, int len, struct mixeng_volume *vol)
  314. {
  315. #ifdef CONFIG_MIXEMU
  316. if (vol->mute) {
  317. mixeng_clear (buf, len);
  318. return;
  319. }
  320. while (len--) {
  321. #ifdef FLOAT_MIXENG
  322. buf->l = buf->l * vol->l;
  323. buf->r = buf->r * vol->r;
  324. #else
  325. buf->l = (buf->l * vol->l) >> 32;
  326. buf->r = (buf->r * vol->r) >> 32;
  327. #endif
  328. buf += 1;
  329. }
  330. #else
  331. (void) buf;
  332. (void) len;
  333. (void) vol;
  334. #endif
  335. }