mixeng.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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/osdep.h"
  26. #include "qemu-common.h"
  27. #include "qemu/bswap.h"
  28. #include "qemu/error-report.h"
  29. #include "audio.h"
  30. #define AUDIO_CAP "mixeng"
  31. #include "audio_int.h"
  32. /* 8 bit */
  33. #define ENDIAN_CONVERSION natural
  34. #define ENDIAN_CONVERT(v) (v)
  35. /* Signed 8 bit */
  36. #define BSIZE 8
  37. #define ITYPE int
  38. #define IN_MIN SCHAR_MIN
  39. #define IN_MAX SCHAR_MAX
  40. #define SIGNED
  41. #define SHIFT 8
  42. #include "mixeng_template.h"
  43. #undef SIGNED
  44. #undef IN_MAX
  45. #undef IN_MIN
  46. #undef BSIZE
  47. #undef ITYPE
  48. #undef SHIFT
  49. /* Unsigned 8 bit */
  50. #define BSIZE 8
  51. #define ITYPE uint
  52. #define IN_MIN 0
  53. #define IN_MAX UCHAR_MAX
  54. #define SHIFT 8
  55. #include "mixeng_template.h"
  56. #undef IN_MAX
  57. #undef IN_MIN
  58. #undef BSIZE
  59. #undef ITYPE
  60. #undef SHIFT
  61. #undef ENDIAN_CONVERT
  62. #undef ENDIAN_CONVERSION
  63. /* Signed 16 bit */
  64. #define BSIZE 16
  65. #define ITYPE int
  66. #define IN_MIN SHRT_MIN
  67. #define IN_MAX SHRT_MAX
  68. #define SIGNED
  69. #define SHIFT 16
  70. #define ENDIAN_CONVERSION natural
  71. #define ENDIAN_CONVERT(v) (v)
  72. #include "mixeng_template.h"
  73. #undef ENDIAN_CONVERT
  74. #undef ENDIAN_CONVERSION
  75. #define ENDIAN_CONVERSION swap
  76. #define ENDIAN_CONVERT(v) bswap16 (v)
  77. #include "mixeng_template.h"
  78. #undef ENDIAN_CONVERT
  79. #undef ENDIAN_CONVERSION
  80. #undef SIGNED
  81. #undef IN_MAX
  82. #undef IN_MIN
  83. #undef BSIZE
  84. #undef ITYPE
  85. #undef SHIFT
  86. /* Unsigned 16 bit */
  87. #define BSIZE 16
  88. #define ITYPE uint
  89. #define IN_MIN 0
  90. #define IN_MAX USHRT_MAX
  91. #define SHIFT 16
  92. #define ENDIAN_CONVERSION natural
  93. #define ENDIAN_CONVERT(v) (v)
  94. #include "mixeng_template.h"
  95. #undef ENDIAN_CONVERT
  96. #undef ENDIAN_CONVERSION
  97. #define ENDIAN_CONVERSION swap
  98. #define ENDIAN_CONVERT(v) bswap16 (v)
  99. #include "mixeng_template.h"
  100. #undef ENDIAN_CONVERT
  101. #undef ENDIAN_CONVERSION
  102. #undef IN_MAX
  103. #undef IN_MIN
  104. #undef BSIZE
  105. #undef ITYPE
  106. #undef SHIFT
  107. /* Signed 32 bit */
  108. #define BSIZE 32
  109. #define ITYPE int
  110. #define IN_MIN INT32_MIN
  111. #define IN_MAX INT32_MAX
  112. #define SIGNED
  113. #define SHIFT 32
  114. #define ENDIAN_CONVERSION natural
  115. #define ENDIAN_CONVERT(v) (v)
  116. #include "mixeng_template.h"
  117. #undef ENDIAN_CONVERT
  118. #undef ENDIAN_CONVERSION
  119. #define ENDIAN_CONVERSION swap
  120. #define ENDIAN_CONVERT(v) bswap32 (v)
  121. #include "mixeng_template.h"
  122. #undef ENDIAN_CONVERT
  123. #undef ENDIAN_CONVERSION
  124. #undef SIGNED
  125. #undef IN_MAX
  126. #undef IN_MIN
  127. #undef BSIZE
  128. #undef ITYPE
  129. #undef SHIFT
  130. /* Unsigned 32 bit */
  131. #define BSIZE 32
  132. #define ITYPE uint
  133. #define IN_MIN 0
  134. #define IN_MAX UINT32_MAX
  135. #define SHIFT 32
  136. #define ENDIAN_CONVERSION natural
  137. #define ENDIAN_CONVERT(v) (v)
  138. #include "mixeng_template.h"
  139. #undef ENDIAN_CONVERT
  140. #undef ENDIAN_CONVERSION
  141. #define ENDIAN_CONVERSION swap
  142. #define ENDIAN_CONVERT(v) bswap32 (v)
  143. #include "mixeng_template.h"
  144. #undef ENDIAN_CONVERT
  145. #undef ENDIAN_CONVERSION
  146. #undef IN_MAX
  147. #undef IN_MIN
  148. #undef BSIZE
  149. #undef ITYPE
  150. #undef SHIFT
  151. t_sample *mixeng_conv[2][2][2][3] = {
  152. {
  153. {
  154. {
  155. conv_natural_uint8_t_to_mono,
  156. conv_natural_uint16_t_to_mono,
  157. conv_natural_uint32_t_to_mono
  158. },
  159. {
  160. conv_natural_uint8_t_to_mono,
  161. conv_swap_uint16_t_to_mono,
  162. conv_swap_uint32_t_to_mono,
  163. }
  164. },
  165. {
  166. {
  167. conv_natural_int8_t_to_mono,
  168. conv_natural_int16_t_to_mono,
  169. conv_natural_int32_t_to_mono
  170. },
  171. {
  172. conv_natural_int8_t_to_mono,
  173. conv_swap_int16_t_to_mono,
  174. conv_swap_int32_t_to_mono
  175. }
  176. }
  177. },
  178. {
  179. {
  180. {
  181. conv_natural_uint8_t_to_stereo,
  182. conv_natural_uint16_t_to_stereo,
  183. conv_natural_uint32_t_to_stereo
  184. },
  185. {
  186. conv_natural_uint8_t_to_stereo,
  187. conv_swap_uint16_t_to_stereo,
  188. conv_swap_uint32_t_to_stereo
  189. }
  190. },
  191. {
  192. {
  193. conv_natural_int8_t_to_stereo,
  194. conv_natural_int16_t_to_stereo,
  195. conv_natural_int32_t_to_stereo
  196. },
  197. {
  198. conv_natural_int8_t_to_stereo,
  199. conv_swap_int16_t_to_stereo,
  200. conv_swap_int32_t_to_stereo,
  201. }
  202. }
  203. }
  204. };
  205. f_sample *mixeng_clip[2][2][2][3] = {
  206. {
  207. {
  208. {
  209. clip_natural_uint8_t_from_mono,
  210. clip_natural_uint16_t_from_mono,
  211. clip_natural_uint32_t_from_mono
  212. },
  213. {
  214. clip_natural_uint8_t_from_mono,
  215. clip_swap_uint16_t_from_mono,
  216. clip_swap_uint32_t_from_mono
  217. }
  218. },
  219. {
  220. {
  221. clip_natural_int8_t_from_mono,
  222. clip_natural_int16_t_from_mono,
  223. clip_natural_int32_t_from_mono
  224. },
  225. {
  226. clip_natural_int8_t_from_mono,
  227. clip_swap_int16_t_from_mono,
  228. clip_swap_int32_t_from_mono
  229. }
  230. }
  231. },
  232. {
  233. {
  234. {
  235. clip_natural_uint8_t_from_stereo,
  236. clip_natural_uint16_t_from_stereo,
  237. clip_natural_uint32_t_from_stereo
  238. },
  239. {
  240. clip_natural_uint8_t_from_stereo,
  241. clip_swap_uint16_t_from_stereo,
  242. clip_swap_uint32_t_from_stereo
  243. }
  244. },
  245. {
  246. {
  247. clip_natural_int8_t_from_stereo,
  248. clip_natural_int16_t_from_stereo,
  249. clip_natural_int32_t_from_stereo
  250. },
  251. {
  252. clip_natural_int8_t_from_stereo,
  253. clip_swap_int16_t_from_stereo,
  254. clip_swap_int32_t_from_stereo
  255. }
  256. }
  257. }
  258. };
  259. void audio_sample_to_uint64(void *samples, int pos,
  260. uint64_t *left, uint64_t *right)
  261. {
  262. struct st_sample *sample = samples;
  263. sample += pos;
  264. #ifdef FLOAT_MIXENG
  265. error_report(
  266. "Coreaudio and floating point samples are not supported by replay yet");
  267. abort();
  268. #else
  269. *left = sample->l;
  270. *right = sample->r;
  271. #endif
  272. }
  273. void audio_sample_from_uint64(void *samples, int pos,
  274. uint64_t left, uint64_t right)
  275. {
  276. struct st_sample *sample = samples;
  277. sample += pos;
  278. #ifdef FLOAT_MIXENG
  279. error_report(
  280. "Coreaudio and floating point samples are not supported by replay yet");
  281. abort();
  282. #else
  283. sample->l = left;
  284. sample->r = right;
  285. #endif
  286. }
  287. /*
  288. * August 21, 1998
  289. * Copyright 1998 Fabrice Bellard.
  290. *
  291. * [Rewrote completely the code of Lance Norskog And Sundry
  292. * Contributors with a more efficient algorithm.]
  293. *
  294. * This source code is freely redistributable and may be used for
  295. * any purpose. This copyright notice must be maintained.
  296. * Lance Norskog And Sundry Contributors are not responsible for
  297. * the consequences of using this software.
  298. */
  299. /*
  300. * Sound Tools rate change effect file.
  301. */
  302. /*
  303. * Linear Interpolation.
  304. *
  305. * The use of fractional increment allows us to use no buffer. It
  306. * avoid the problems at the end of the buffer we had with the old
  307. * method which stored a possibly big buffer of size
  308. * lcm(in_rate,out_rate).
  309. *
  310. * Limited to 16 bit samples and sampling frequency <= 65535 Hz. If
  311. * the input & output frequencies are equal, a delay of one sample is
  312. * introduced. Limited to processing 32-bit count worth of samples.
  313. *
  314. * 1 << FRAC_BITS evaluating to zero in several places. Changed with
  315. * an (unsigned long) cast to make it safe. MarkMLl 2/1/99
  316. */
  317. /* Private data */
  318. struct rate {
  319. uint64_t opos;
  320. uint64_t opos_inc;
  321. uint32_t ipos; /* position in the input stream (integer) */
  322. struct st_sample ilast; /* last sample in the input stream */
  323. };
  324. /*
  325. * Prepare processing.
  326. */
  327. void *st_rate_start (int inrate, int outrate)
  328. {
  329. struct rate *rate = audio_calloc (AUDIO_FUNC, 1, sizeof (*rate));
  330. if (!rate) {
  331. dolog ("Could not allocate resampler (%zu bytes)\n", sizeof (*rate));
  332. return NULL;
  333. }
  334. rate->opos = 0;
  335. /* increment */
  336. rate->opos_inc = ((uint64_t) inrate << 32) / outrate;
  337. rate->ipos = 0;
  338. rate->ilast.l = 0;
  339. rate->ilast.r = 0;
  340. return rate;
  341. }
  342. #define NAME st_rate_flow_mix
  343. #define OP(a, b) a += b
  344. #include "rate_template.h"
  345. #define NAME st_rate_flow
  346. #define OP(a, b) a = b
  347. #include "rate_template.h"
  348. void st_rate_stop (void *opaque)
  349. {
  350. g_free (opaque);
  351. }
  352. void mixeng_clear (struct st_sample *buf, int len)
  353. {
  354. memset (buf, 0, len * sizeof (struct st_sample));
  355. }
  356. void mixeng_volume (struct st_sample *buf, int len, struct mixeng_volume *vol)
  357. {
  358. if (vol->mute) {
  359. mixeng_clear (buf, len);
  360. return;
  361. }
  362. while (len--) {
  363. #ifdef FLOAT_MIXENG
  364. buf->l = buf->l * vol->l;
  365. buf->r = buf->r * vol->r;
  366. #else
  367. buf->l = (buf->l * vol->l) >> 32;
  368. buf->r = (buf->r * vol->r) >> 32;
  369. #endif
  370. buf += 1;
  371. }
  372. }