mixeng.c 9.8 KB

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