blob: af4cdf60e7966283130cc725a6dfeafbda5ad934 [file] [log] [blame]
bellard85571bc2004-11-07 18:04:02 +00001/*
2 * QEMU Audio subsystem
bellard1d14ffa2005-10-30 18:58:22 +00003 *
4 * Copyright (c) 2003-2005 Vassili Karpov (malc)
5 *
bellard85571bc2004-11-07 18:04:02 +00006 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
pbrook87ecb682007-11-17 17:14:51 +000024#include "hw/hw.h"
25#include "audio.h"
Paolo Bonzini83c90892012-12-17 18:19:49 +010026#include "monitor/monitor.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010027#include "qemu/timer.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010028#include "sysemu/sysemu.h"
bellard85571bc2004-11-07 18:04:02 +000029
bellard1d14ffa2005-10-30 18:58:22 +000030#define AUDIO_CAP "audio"
31#include "audio_int.h"
bellard85571bc2004-11-07 18:04:02 +000032
bellard1d14ffa2005-10-30 18:58:22 +000033/* #define DEBUG_PLIVE */
34/* #define DEBUG_LIVE */
35/* #define DEBUG_OUT */
bellard8ead62c2006-07-04 16:51:32 +000036/* #define DEBUG_CAPTURE */
malc713a98f2009-09-12 02:28:45 +040037/* #define DEBUG_POLL */
bellard1d14ffa2005-10-30 18:58:22 +000038
bellardc0fe3822005-11-05 18:55:28 +000039#define SW_NAME(sw) (sw)->name ? (sw)->name : "unknown"
40
Juan Quintela2358a492009-07-27 16:13:25 +020041
42/* Order of CONFIG_AUDIO_DRIVERS is import.
43 The 1st one is the one used by default, that is the reason
44 that we generate the list.
45*/
bellard1d14ffa2005-10-30 18:58:22 +000046static struct audio_driver *drvtab[] = {
Gerd Hoffmann3e313752010-11-09 17:29:46 +010047#ifdef CONFIG_SPICE
48 &spice_audio_driver,
49#endif
Juan Quintela2358a492009-07-27 16:13:25 +020050 CONFIG_AUDIO_DRIVERS
bellard1d14ffa2005-10-30 18:58:22 +000051 &no_audio_driver,
52 &wav_audio_driver
53};
bellard85571bc2004-11-07 18:04:02 +000054
bellardc0fe3822005-11-05 18:55:28 +000055struct fixed_settings {
56 int enabled;
57 int nb_voices;
58 int greedy;
malc1ea879e2008-12-03 22:48:44 +000059 struct audsettings settings;
bellardc0fe3822005-11-05 18:55:28 +000060};
bellard1d14ffa2005-10-30 18:58:22 +000061
bellardc0fe3822005-11-05 18:55:28 +000062static struct {
63 struct fixed_settings fixed_out;
64 struct fixed_settings fixed_in;
65 union {
malcc310de82008-11-12 20:36:27 +000066 int hertz;
bellardc0fe3822005-11-05 18:55:28 +000067 int64_t ticks;
68 } period;
69 int plive;
bellard541e0842005-11-11 00:04:19 +000070 int log_to_monitor;
malc713a98f2009-09-12 02:28:45 +040071 int try_poll_in;
72 int try_poll_out;
bellardc0fe3822005-11-05 18:55:28 +000073} conf = {
Juan Quintela14658cd2009-07-20 20:57:00 +020074 .fixed_out = { /* DAC fixed settings */
75 .enabled = 1,
76 .nb_voices = 1,
77 .greedy = 1,
78 .settings = {
79 .freq = 44100,
80 .nchannels = 2,
81 .fmt = AUD_FMT_S16,
82 .endianness = AUDIO_HOST_ENDIANNESS,
bellardc0fe3822005-11-05 18:55:28 +000083 }
84 },
bellard1d14ffa2005-10-30 18:58:22 +000085
Juan Quintela14658cd2009-07-20 20:57:00 +020086 .fixed_in = { /* ADC fixed settings */
87 .enabled = 1,
88 .nb_voices = 1,
89 .greedy = 1,
90 .settings = {
91 .freq = 44100,
92 .nchannels = 2,
93 .fmt = AUD_FMT_S16,
94 .endianness = AUDIO_HOST_ENDIANNESS,
bellardc0fe3822005-11-05 18:55:28 +000095 }
96 },
bellard1d14ffa2005-10-30 18:58:22 +000097
malcaea86742009-07-24 05:16:12 +040098 .period = { .hertz = 250 },
Juan Quintela14658cd2009-07-20 20:57:00 +020099 .plive = 0,
100 .log_to_monitor = 0,
malc713a98f2009-09-12 02:28:45 +0400101 .try_poll_in = 1,
102 .try_poll_out = 1,
bellard1d14ffa2005-10-30 18:58:22 +0000103};
104
bellardc0fe3822005-11-05 18:55:28 +0000105static AudioState glob_audio_state;
106
Michael Walle00e07672011-01-05 01:05:47 +0100107const struct mixeng_volume nominal_volume = {
Juan Quintela14658cd2009-07-20 20:57:00 +0200108 .mute = 0,
bellard1d14ffa2005-10-30 18:58:22 +0000109#ifdef FLOAT_MIXENG
Juan Quintela14658cd2009-07-20 20:57:00 +0200110 .r = 1.0,
111 .l = 1.0,
bellard1d14ffa2005-10-30 18:58:22 +0000112#else
Juan Quintela14658cd2009-07-20 20:57:00 +0200113 .r = 1ULL << 32,
114 .l = 1ULL << 32,
bellard1d14ffa2005-10-30 18:58:22 +0000115#endif
bellard85571bc2004-11-07 18:04:02 +0000116};
117
bellard1d14ffa2005-10-30 18:58:22 +0000118#ifdef AUDIO_IS_FLAWLESS_AND_NO_CHECKS_ARE_REQURIED
119#error No its not
120#else
malc82584e22010-01-17 02:03:30 +0300121static void audio_print_options (const char *prefix,
122 struct audio_option *opt);
123
bellard1d14ffa2005-10-30 18:58:22 +0000124int audio_bug (const char *funcname, int cond)
bellard85571bc2004-11-07 18:04:02 +0000125{
bellard1d14ffa2005-10-30 18:58:22 +0000126 if (cond) {
127 static int shown;
128
bellard8ead62c2006-07-04 16:51:32 +0000129 AUD_log (NULL, "A bug was just triggered in %s\n", funcname);
bellard1d14ffa2005-10-30 18:58:22 +0000130 if (!shown) {
malc82584e22010-01-17 02:03:30 +0300131 struct audio_driver *d;
132
bellard1d14ffa2005-10-30 18:58:22 +0000133 shown = 1;
134 AUD_log (NULL, "Save all your work and restart without audio\n");
malc155a8ad2009-09-18 11:52:45 +0400135 AUD_log (NULL, "Please send bug report to av1474@comtv.ru\n");
bellard1d14ffa2005-10-30 18:58:22 +0000136 AUD_log (NULL, "I am sorry\n");
malc82584e22010-01-17 02:03:30 +0300137 d = glob_audio_state.drv;
138 if (d) {
139 audio_print_options (d->name, d->options);
140 }
bellard1d14ffa2005-10-30 18:58:22 +0000141 }
142 AUD_log (NULL, "Context:\n");
143
144#if defined AUDIO_BREAKPOINT_ON_BUG
145# if defined HOST_I386
146# if defined __GNUC__
147 __asm__ ("int3");
148# elif defined _MSC_VER
149 _asm _emit 0xcc;
150# else
151 abort ();
152# endif
153# else
154 abort ();
155# endif
156#endif
157 }
158
159 return cond;
160}
161#endif
162
thsf941aa22007-02-17 22:19:29 +0000163static inline int audio_bits_to_index (int bits)
164{
165 switch (bits) {
166 case 8:
167 return 0;
168
169 case 16:
170 return 1;
171
172 case 32:
173 return 2;
174
175 default:
176 audio_bug ("bits_to_index", 1);
177 AUD_log (NULL, "invalid bits %d\n", bits);
178 return 0;
179 }
180}
181
bellardc0fe3822005-11-05 18:55:28 +0000182void *audio_calloc (const char *funcname, int nmemb, size_t size)
183{
184 int cond;
185 size_t len;
186
187 len = nmemb * size;
188 cond = !nmemb || !size;
189 cond |= nmemb < 0;
190 cond |= len < size;
191
192 if (audio_bug ("audio_calloc", cond)) {
193 AUD_log (NULL, "%s passed invalid arguments to audio_calloc\n",
194 funcname);
bellard541e0842005-11-11 00:04:19 +0000195 AUD_log (NULL, "nmemb=%d size=%zu (len=%zu)\n", nmemb, size, len);
bellardc0fe3822005-11-05 18:55:28 +0000196 return NULL;
197 }
198
Anthony Liguori7267c092011-08-20 22:09:37 -0500199 return g_malloc0 (len);
bellardc0fe3822005-11-05 18:55:28 +0000200}
201
bellard1d14ffa2005-10-30 18:58:22 +0000202static char *audio_alloc_prefix (const char *s)
203{
204 const char qemu_prefix[] = "QEMU_";
aliguori090f1fa2009-02-05 22:05:58 +0000205 size_t len, i;
206 char *r, *u;
bellard1d14ffa2005-10-30 18:58:22 +0000207
208 if (!s) {
209 return NULL;
210 }
211
212 len = strlen (s);
Anthony Liguori7267c092011-08-20 22:09:37 -0500213 r = g_malloc (len + sizeof (qemu_prefix));
bellard1d14ffa2005-10-30 18:58:22 +0000214
aliguori090f1fa2009-02-05 22:05:58 +0000215 u = r + sizeof (qemu_prefix) - 1;
bellard1d14ffa2005-10-30 18:58:22 +0000216
aliguori090f1fa2009-02-05 22:05:58 +0000217 pstrcpy (r, len + sizeof (qemu_prefix), qemu_prefix);
218 pstrcat (r, len + sizeof (qemu_prefix), s);
bellard1d14ffa2005-10-30 18:58:22 +0000219
aliguori090f1fa2009-02-05 22:05:58 +0000220 for (i = 0; i < len; ++i) {
221 u[i] = qemu_toupper(u[i]);
bellard1d14ffa2005-10-30 18:58:22 +0000222 }
aliguori090f1fa2009-02-05 22:05:58 +0000223
bellard1d14ffa2005-10-30 18:58:22 +0000224 return r;
225}
226
pbrook9596ebb2007-11-18 01:44:38 +0000227static const char *audio_audfmt_to_string (audfmt_e fmt)
bellard1d14ffa2005-10-30 18:58:22 +0000228{
229 switch (fmt) {
230 case AUD_FMT_U8:
231 return "U8";
232
233 case AUD_FMT_U16:
234 return "U16";
235
236 case AUD_FMT_S8:
237 return "S8";
238
239 case AUD_FMT_S16:
240 return "S16";
thsf941aa22007-02-17 22:19:29 +0000241
242 case AUD_FMT_U32:
243 return "U32";
244
245 case AUD_FMT_S32:
246 return "S32";
bellard1d14ffa2005-10-30 18:58:22 +0000247 }
248
249 dolog ("Bogus audfmt %d returning S16\n", fmt);
250 return "S16";
251}
252
pbrook9596ebb2007-11-18 01:44:38 +0000253static audfmt_e audio_string_to_audfmt (const char *s, audfmt_e defval,
254 int *defaultp)
bellard1d14ffa2005-10-30 18:58:22 +0000255{
256 if (!strcasecmp (s, "u8")) {
257 *defaultp = 0;
258 return AUD_FMT_U8;
259 }
260 else if (!strcasecmp (s, "u16")) {
261 *defaultp = 0;
262 return AUD_FMT_U16;
263 }
thsf941aa22007-02-17 22:19:29 +0000264 else if (!strcasecmp (s, "u32")) {
265 *defaultp = 0;
266 return AUD_FMT_U32;
267 }
bellard1d14ffa2005-10-30 18:58:22 +0000268 else if (!strcasecmp (s, "s8")) {
269 *defaultp = 0;
270 return AUD_FMT_S8;
271 }
272 else if (!strcasecmp (s, "s16")) {
273 *defaultp = 0;
274 return AUD_FMT_S16;
275 }
thsf941aa22007-02-17 22:19:29 +0000276 else if (!strcasecmp (s, "s32")) {
277 *defaultp = 0;
278 return AUD_FMT_S32;
279 }
bellard1d14ffa2005-10-30 18:58:22 +0000280 else {
281 dolog ("Bogus audio format `%s' using %s\n",
282 s, audio_audfmt_to_string (defval));
283 *defaultp = 1;
284 return defval;
285 }
286}
287
288static audfmt_e audio_get_conf_fmt (const char *envname,
289 audfmt_e defval,
290 int *defaultp)
291{
292 const char *var = getenv (envname);
293 if (!var) {
294 *defaultp = 1;
295 return defval;
296 }
297 return audio_string_to_audfmt (var, defval, defaultp);
298}
299
300static int audio_get_conf_int (const char *key, int defval, int *defaultp)
301{
302 int val;
bellard85571bc2004-11-07 18:04:02 +0000303 char *strval;
304
305 strval = getenv (key);
306 if (strval) {
bellard1d14ffa2005-10-30 18:58:22 +0000307 *defaultp = 0;
bellard85571bc2004-11-07 18:04:02 +0000308 val = atoi (strval);
bellard1d14ffa2005-10-30 18:58:22 +0000309 return val;
bellard85571bc2004-11-07 18:04:02 +0000310 }
bellard1d14ffa2005-10-30 18:58:22 +0000311 else {
312 *defaultp = 1;
313 return defval;
314 }
bellard85571bc2004-11-07 18:04:02 +0000315}
316
bellard1d14ffa2005-10-30 18:58:22 +0000317static const char *audio_get_conf_str (const char *key,
318 const char *defval,
319 int *defaultp)
bellard85571bc2004-11-07 18:04:02 +0000320{
321 const char *val = getenv (key);
bellard1d14ffa2005-10-30 18:58:22 +0000322 if (!val) {
323 *defaultp = 1;
bellard85571bc2004-11-07 18:04:02 +0000324 return defval;
bellard1d14ffa2005-10-30 18:58:22 +0000325 }
326 else {
327 *defaultp = 0;
bellard85571bc2004-11-07 18:04:02 +0000328 return val;
bellard1d14ffa2005-10-30 18:58:22 +0000329 }
bellard85571bc2004-11-07 18:04:02 +0000330}
331
bellard541e0842005-11-11 00:04:19 +0000332void AUD_vlog (const char *cap, const char *fmt, va_list ap)
333{
334 if (conf.log_to_monitor) {
335 if (cap) {
Markus Armbruster8631b602010-02-18 11:41:55 +0100336 monitor_printf(default_mon, "%s: ", cap);
bellard541e0842005-11-11 00:04:19 +0000337 }
338
Markus Armbruster8631b602010-02-18 11:41:55 +0100339 monitor_vprintf(default_mon, fmt, ap);
bellard541e0842005-11-11 00:04:19 +0000340 }
341 else {
342 if (cap) {
343 fprintf (stderr, "%s: ", cap);
344 }
345
346 vfprintf (stderr, fmt, ap);
347 }
348}
349
bellardfb065182004-11-09 23:09:44 +0000350void AUD_log (const char *cap, const char *fmt, ...)
bellard85571bc2004-11-07 18:04:02 +0000351{
352 va_list ap;
bellard85571bc2004-11-07 18:04:02 +0000353
bellard541e0842005-11-11 00:04:19 +0000354 va_start (ap, fmt);
355 AUD_vlog (cap, fmt, ap);
356 va_end (ap);
bellard85571bc2004-11-07 18:04:02 +0000357}
358
bellard1d14ffa2005-10-30 18:58:22 +0000359static void audio_print_options (const char *prefix,
360 struct audio_option *opt)
bellard85571bc2004-11-07 18:04:02 +0000361{
bellard1d14ffa2005-10-30 18:58:22 +0000362 char *uprefix;
363
364 if (!prefix) {
365 dolog ("No prefix specified\n");
366 return;
367 }
368
369 if (!opt) {
370 dolog ("No options\n");
371 return;
372 }
373
374 uprefix = audio_alloc_prefix (prefix);
375
376 for (; opt->name; opt++) {
377 const char *state = "default";
378 printf (" %s_%s: ", uprefix, opt->name);
379
thsfe8f0962007-07-12 10:59:21 +0000380 if (opt->overriddenp && *opt->overriddenp) {
bellard1d14ffa2005-10-30 18:58:22 +0000381 state = "current";
382 }
383
384 switch (opt->tag) {
385 case AUD_OPT_BOOL:
386 {
387 int *intp = opt->valp;
388 printf ("boolean, %s = %d\n", state, *intp ? 1 : 0);
389 }
390 break;
391
392 case AUD_OPT_INT:
393 {
394 int *intp = opt->valp;
395 printf ("integer, %s = %d\n", state, *intp);
396 }
397 break;
398
399 case AUD_OPT_FMT:
400 {
401 audfmt_e *fmtp = opt->valp;
402 printf (
balrogca9cc282008-01-14 04:24:29 +0000403 "format, %s = %s, (one of: U8 S8 U16 S16 U32 S32)\n",
bellard1d14ffa2005-10-30 18:58:22 +0000404 state,
405 audio_audfmt_to_string (*fmtp)
406 );
407 }
408 break;
409
410 case AUD_OPT_STR:
411 {
412 const char **strp = opt->valp;
413 printf ("string, %s = %s\n",
414 state,
415 *strp ? *strp : "(not set)");
416 }
417 break;
418
419 default:
420 printf ("???\n");
421 dolog ("Bad value tag for option %s_%s %d\n",
422 uprefix, opt->name, opt->tag);
423 break;
424 }
425 printf (" %s\n", opt->descr);
426 }
427
Anthony Liguori7267c092011-08-20 22:09:37 -0500428 g_free (uprefix);
bellard85571bc2004-11-07 18:04:02 +0000429}
430
bellard1d14ffa2005-10-30 18:58:22 +0000431static void audio_process_options (const char *prefix,
432 struct audio_option *opt)
433{
434 char *optname;
435 const char qemu_prefix[] = "QEMU_";
blueswir1363a37d2008-08-21 17:58:08 +0000436 size_t preflen, optlen;
bellard1d14ffa2005-10-30 18:58:22 +0000437
438 if (audio_bug (AUDIO_FUNC, !prefix)) {
439 dolog ("prefix = NULL\n");
440 return;
441 }
442
443 if (audio_bug (AUDIO_FUNC, !opt)) {
444 dolog ("opt = NULL\n");
445 return;
446 }
447
448 preflen = strlen (prefix);
449
450 for (; opt->name; opt++) {
451 size_t len, i;
452 int def;
453
454 if (!opt->valp) {
455 dolog ("Option value pointer for `%s' is not set\n",
456 opt->name);
457 continue;
458 }
459
460 len = strlen (opt->name);
bellardc0fe3822005-11-05 18:55:28 +0000461 /* len of opt->name + len of prefix + size of qemu_prefix
462 * (includes trailing zero) + zero + underscore (on behalf of
463 * sizeof) */
blueswir1363a37d2008-08-21 17:58:08 +0000464 optlen = len + preflen + sizeof (qemu_prefix) + 1;
Anthony Liguori7267c092011-08-20 22:09:37 -0500465 optname = g_malloc (optlen);
bellard1d14ffa2005-10-30 18:58:22 +0000466
blueswir1363a37d2008-08-21 17:58:08 +0000467 pstrcpy (optname, optlen, qemu_prefix);
bellardc0fe3822005-11-05 18:55:28 +0000468
469 /* copy while upper-casing, including trailing zero */
bellard1d14ffa2005-10-30 18:58:22 +0000470 for (i = 0; i <= preflen; ++i) {
blueswir1cd390082008-11-16 13:53:32 +0000471 optname[i + sizeof (qemu_prefix) - 1] = qemu_toupper(prefix[i]);
bellard1d14ffa2005-10-30 18:58:22 +0000472 }
blueswir1363a37d2008-08-21 17:58:08 +0000473 pstrcat (optname, optlen, "_");
blueswir1363a37d2008-08-21 17:58:08 +0000474 pstrcat (optname, optlen, opt->name);
bellard1d14ffa2005-10-30 18:58:22 +0000475
476 def = 1;
477 switch (opt->tag) {
478 case AUD_OPT_BOOL:
479 case AUD_OPT_INT:
480 {
481 int *intp = opt->valp;
482 *intp = audio_get_conf_int (optname, *intp, &def);
483 }
484 break;
485
486 case AUD_OPT_FMT:
487 {
488 audfmt_e *fmtp = opt->valp;
489 *fmtp = audio_get_conf_fmt (optname, *fmtp, &def);
490 }
491 break;
492
493 case AUD_OPT_STR:
494 {
495 const char **strp = opt->valp;
496 *strp = audio_get_conf_str (optname, *strp, &def);
497 }
498 break;
499
500 default:
501 dolog ("Bad value tag for option `%s' - %d\n",
502 optname, opt->tag);
503 break;
504 }
505
thsfe8f0962007-07-12 10:59:21 +0000506 if (!opt->overriddenp) {
507 opt->overriddenp = &opt->overridden;
bellard1d14ffa2005-10-30 18:58:22 +0000508 }
thsfe8f0962007-07-12 10:59:21 +0000509 *opt->overriddenp = !def;
Anthony Liguori7267c092011-08-20 22:09:37 -0500510 g_free (optname);
bellard1d14ffa2005-10-30 18:58:22 +0000511 }
512}
513
malc1ea879e2008-12-03 22:48:44 +0000514static void audio_print_settings (struct audsettings *as)
bellardc0fe3822005-11-05 18:55:28 +0000515{
516 dolog ("frequency=%d nchannels=%d fmt=", as->freq, as->nchannels);
517
518 switch (as->fmt) {
519 case AUD_FMT_S8:
520 AUD_log (NULL, "S8");
521 break;
522 case AUD_FMT_U8:
523 AUD_log (NULL, "U8");
524 break;
525 case AUD_FMT_S16:
526 AUD_log (NULL, "S16");
527 break;
528 case AUD_FMT_U16:
529 AUD_log (NULL, "U16");
530 break;
malcd50997f2008-06-22 14:10:45 +0000531 case AUD_FMT_S32:
532 AUD_log (NULL, "S32");
533 break;
534 case AUD_FMT_U32:
535 AUD_log (NULL, "U32");
536 break;
bellardc0fe3822005-11-05 18:55:28 +0000537 default:
538 AUD_log (NULL, "invalid(%d)", as->fmt);
539 break;
540 }
bellardec36b692006-07-16 18:57:03 +0000541
542 AUD_log (NULL, " endianness=");
bellardd929eba2006-07-04 21:47:22 +0000543 switch (as->endianness) {
544 case 0:
545 AUD_log (NULL, "little");
546 break;
547 case 1:
548 AUD_log (NULL, "big");
549 break;
550 default:
551 AUD_log (NULL, "invalid");
552 break;
553 }
bellardc0fe3822005-11-05 18:55:28 +0000554 AUD_log (NULL, "\n");
555}
556
malc1ea879e2008-12-03 22:48:44 +0000557static int audio_validate_settings (struct audsettings *as)
bellardc0fe3822005-11-05 18:55:28 +0000558{
559 int invalid;
560
561 invalid = as->nchannels != 1 && as->nchannels != 2;
bellardd929eba2006-07-04 21:47:22 +0000562 invalid |= as->endianness != 0 && as->endianness != 1;
bellardc0fe3822005-11-05 18:55:28 +0000563
564 switch (as->fmt) {
565 case AUD_FMT_S8:
566 case AUD_FMT_U8:
567 case AUD_FMT_S16:
568 case AUD_FMT_U16:
thsf941aa22007-02-17 22:19:29 +0000569 case AUD_FMT_S32:
570 case AUD_FMT_U32:
bellardc0fe3822005-11-05 18:55:28 +0000571 break;
572 default:
573 invalid = 1;
574 break;
575 }
576
577 invalid |= as->freq <= 0;
bellardd929eba2006-07-04 21:47:22 +0000578 return invalid ? -1 : 0;
bellardc0fe3822005-11-05 18:55:28 +0000579}
580
malc1ea879e2008-12-03 22:48:44 +0000581static int audio_pcm_info_eq (struct audio_pcm_info *info, struct audsettings *as)
bellard1d14ffa2005-10-30 18:58:22 +0000582{
583 int bits = 8, sign = 0;
584
bellardc0fe3822005-11-05 18:55:28 +0000585 switch (as->fmt) {
bellard1d14ffa2005-10-30 18:58:22 +0000586 case AUD_FMT_S8:
587 sign = 1;
Stefan Weilb4bd0b12012-02-25 14:46:55 +0100588 /* fall through */
bellard1d14ffa2005-10-30 18:58:22 +0000589 case AUD_FMT_U8:
590 break;
591
592 case AUD_FMT_S16:
593 sign = 1;
Stefan Weilb4bd0b12012-02-25 14:46:55 +0100594 /* fall through */
bellard1d14ffa2005-10-30 18:58:22 +0000595 case AUD_FMT_U16:
596 bits = 16;
597 break;
thsf941aa22007-02-17 22:19:29 +0000598
599 case AUD_FMT_S32:
600 sign = 1;
Stefan Weilb4bd0b12012-02-25 14:46:55 +0100601 /* fall through */
thsf941aa22007-02-17 22:19:29 +0000602 case AUD_FMT_U32:
603 bits = 32;
604 break;
bellard1d14ffa2005-10-30 18:58:22 +0000605 }
bellardc0fe3822005-11-05 18:55:28 +0000606 return info->freq == as->freq
607 && info->nchannels == as->nchannels
bellard1d14ffa2005-10-30 18:58:22 +0000608 && info->sign == sign
bellardd929eba2006-07-04 21:47:22 +0000609 && info->bits == bits
610 && info->swap_endianness == (as->endianness != AUDIO_HOST_ENDIANNESS);
bellard1d14ffa2005-10-30 18:58:22 +0000611}
612
malc1ea879e2008-12-03 22:48:44 +0000613void audio_pcm_init_info (struct audio_pcm_info *info, struct audsettings *as)
bellard85571bc2004-11-07 18:04:02 +0000614{
thsf941aa22007-02-17 22:19:29 +0000615 int bits = 8, sign = 0, shift = 0;
bellard85571bc2004-11-07 18:04:02 +0000616
bellardc0fe3822005-11-05 18:55:28 +0000617 switch (as->fmt) {
bellard85571bc2004-11-07 18:04:02 +0000618 case AUD_FMT_S8:
619 sign = 1;
620 case AUD_FMT_U8:
621 break;
622
623 case AUD_FMT_S16:
624 sign = 1;
625 case AUD_FMT_U16:
626 bits = 16;
thsf941aa22007-02-17 22:19:29 +0000627 shift = 1;
628 break;
629
630 case AUD_FMT_S32:
631 sign = 1;
632 case AUD_FMT_U32:
633 bits = 32;
634 shift = 2;
bellard85571bc2004-11-07 18:04:02 +0000635 break;
636 }
637
bellardc0fe3822005-11-05 18:55:28 +0000638 info->freq = as->freq;
bellard1d14ffa2005-10-30 18:58:22 +0000639 info->bits = bits;
640 info->sign = sign;
bellardc0fe3822005-11-05 18:55:28 +0000641 info->nchannels = as->nchannels;
thsf941aa22007-02-17 22:19:29 +0000642 info->shift = (as->nchannels == 2) + shift;
bellard1d14ffa2005-10-30 18:58:22 +0000643 info->align = (1 << info->shift) - 1;
644 info->bytes_per_second = info->freq << info->shift;
bellardd929eba2006-07-04 21:47:22 +0000645 info->swap_endianness = (as->endianness != AUDIO_HOST_ENDIANNESS);
bellard85571bc2004-11-07 18:04:02 +0000646}
647
bellard1d14ffa2005-10-30 18:58:22 +0000648void audio_pcm_info_clear_buf (struct audio_pcm_info *info, void *buf, int len)
bellard85571bc2004-11-07 18:04:02 +0000649{
bellard1d14ffa2005-10-30 18:58:22 +0000650 if (!len) {
651 return;
652 }
653
654 if (info->sign) {
bellarde2f909b2006-08-03 20:39:40 +0000655 memset (buf, 0x00, len << info->shift);
bellard1d14ffa2005-10-30 18:58:22 +0000656 }
657 else {
thsf941aa22007-02-17 22:19:29 +0000658 switch (info->bits) {
659 case 8:
bellarde2f909b2006-08-03 20:39:40 +0000660 memset (buf, 0x80, len << info->shift);
thsf941aa22007-02-17 22:19:29 +0000661 break;
bellard1d14ffa2005-10-30 18:58:22 +0000662
thsf941aa22007-02-17 22:19:29 +0000663 case 16:
664 {
665 int i;
666 uint16_t *p = buf;
667 int shift = info->nchannels - 1;
668 short s = INT16_MAX;
bellard1d14ffa2005-10-30 18:58:22 +0000669
thsf941aa22007-02-17 22:19:29 +0000670 if (info->swap_endianness) {
671 s = bswap16 (s);
672 }
673
674 for (i = 0; i < len << shift; i++) {
675 p[i] = s;
676 }
bellard1d14ffa2005-10-30 18:58:22 +0000677 }
thsf941aa22007-02-17 22:19:29 +0000678 break;
679
680 case 32:
681 {
682 int i;
683 uint32_t *p = buf;
684 int shift = info->nchannels - 1;
685 int32_t s = INT32_MAX;
686
687 if (info->swap_endianness) {
688 s = bswap32 (s);
689 }
690
691 for (i = 0; i < len << shift; i++) {
692 p[i] = s;
693 }
694 }
695 break;
696
697 default:
698 AUD_log (NULL, "audio_pcm_info_clear_buf: invalid bits %d\n",
699 info->bits);
700 break;
bellard1d14ffa2005-10-30 18:58:22 +0000701 }
702 }
bellard85571bc2004-11-07 18:04:02 +0000703}
704
bellard1d14ffa2005-10-30 18:58:22 +0000705/*
bellard8ead62c2006-07-04 16:51:32 +0000706 * Capture
707 */
Michael Walle00e07672011-01-05 01:05:47 +0100708static void noop_conv (struct st_sample *dst, const void *src, int samples)
bellard8ead62c2006-07-04 16:51:32 +0000709{
710 (void) src;
711 (void) dst;
712 (void) samples;
bellard8ead62c2006-07-04 16:51:32 +0000713}
714
715static CaptureVoiceOut *audio_pcm_capture_find_specific (
malc1ea879e2008-12-03 22:48:44 +0000716 struct audsettings *as
bellard8ead62c2006-07-04 16:51:32 +0000717 )
718{
719 CaptureVoiceOut *cap;
malc1a7dafc2009-05-14 03:11:35 +0400720 AudioState *s = &glob_audio_state;
bellard8ead62c2006-07-04 16:51:32 +0000721
722 for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) {
bellardd929eba2006-07-04 21:47:22 +0000723 if (audio_pcm_info_eq (&cap->hw.info, as)) {
bellard8ead62c2006-07-04 16:51:32 +0000724 return cap;
725 }
726 }
727 return NULL;
728}
729
bellardec36b692006-07-16 18:57:03 +0000730static void audio_notify_capture (CaptureVoiceOut *cap, audcnotification_e cmd)
731{
732 struct capture_callback *cb;
733
734#ifdef DEBUG_CAPTURE
735 dolog ("notification %d sent\n", cmd);
736#endif
737 for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
738 cb->ops.notify (cb->opaque, cmd);
739 }
740}
741
742static void audio_capture_maybe_changed (CaptureVoiceOut *cap, int enabled)
bellard8ead62c2006-07-04 16:51:32 +0000743{
744 if (cap->hw.enabled != enabled) {
bellardec36b692006-07-16 18:57:03 +0000745 audcnotification_e cmd;
bellard8ead62c2006-07-04 16:51:32 +0000746 cap->hw.enabled = enabled;
bellardec36b692006-07-16 18:57:03 +0000747 cmd = enabled ? AUD_CNOTIFY_ENABLE : AUD_CNOTIFY_DISABLE;
748 audio_notify_capture (cap, cmd);
bellard8ead62c2006-07-04 16:51:32 +0000749 }
750}
751
752static void audio_recalc_and_notify_capture (CaptureVoiceOut *cap)
753{
754 HWVoiceOut *hw = &cap->hw;
755 SWVoiceOut *sw;
756 int enabled = 0;
757
bellardec36b692006-07-16 18:57:03 +0000758 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
bellard8ead62c2006-07-04 16:51:32 +0000759 if (sw->active) {
760 enabled = 1;
761 break;
762 }
763 }
bellardec36b692006-07-16 18:57:03 +0000764 audio_capture_maybe_changed (cap, enabled);
bellard8ead62c2006-07-04 16:51:32 +0000765}
766
767static void audio_detach_capture (HWVoiceOut *hw)
768{
bellardec36b692006-07-16 18:57:03 +0000769 SWVoiceCap *sc = hw->cap_head.lh_first;
bellard8ead62c2006-07-04 16:51:32 +0000770
bellardec36b692006-07-16 18:57:03 +0000771 while (sc) {
772 SWVoiceCap *sc1 = sc->entries.le_next;
773 SWVoiceOut *sw = &sc->sw;
774 CaptureVoiceOut *cap = sc->cap;
775 int was_active = sw->active;
776
bellard8ead62c2006-07-04 16:51:32 +0000777 if (sw->rate) {
778 st_rate_stop (sw->rate);
779 sw->rate = NULL;
780 }
781
Blue Swirl72cf2d42009-09-12 07:36:22 +0000782 QLIST_REMOVE (sw, entries);
783 QLIST_REMOVE (sc, entries);
Anthony Liguori7267c092011-08-20 22:09:37 -0500784 g_free (sc);
bellardec36b692006-07-16 18:57:03 +0000785 if (was_active) {
786 /* We have removed soft voice from the capture:
787 this might have changed the overall status of the capture
788 since this might have been the only active voice */
789 audio_recalc_and_notify_capture (cap);
790 }
791 sc = sc1;
bellard8ead62c2006-07-04 16:51:32 +0000792 }
793}
794
malc1a7dafc2009-05-14 03:11:35 +0400795static int audio_attach_capture (HWVoiceOut *hw)
bellard8ead62c2006-07-04 16:51:32 +0000796{
malc1a7dafc2009-05-14 03:11:35 +0400797 AudioState *s = &glob_audio_state;
bellard8ead62c2006-07-04 16:51:32 +0000798 CaptureVoiceOut *cap;
799
800 audio_detach_capture (hw);
801 for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) {
bellardec36b692006-07-16 18:57:03 +0000802 SWVoiceCap *sc;
bellard8ead62c2006-07-04 16:51:32 +0000803 SWVoiceOut *sw;
bellardec36b692006-07-16 18:57:03 +0000804 HWVoiceOut *hw_cap = &cap->hw;
bellard8ead62c2006-07-04 16:51:32 +0000805
bellardec36b692006-07-16 18:57:03 +0000806 sc = audio_calloc (AUDIO_FUNC, 1, sizeof (*sc));
807 if (!sc) {
bellard8ead62c2006-07-04 16:51:32 +0000808 dolog ("Could not allocate soft capture voice (%zu bytes)\n",
bellardec36b692006-07-16 18:57:03 +0000809 sizeof (*sc));
bellard8ead62c2006-07-04 16:51:32 +0000810 return -1;
811 }
812
bellardec36b692006-07-16 18:57:03 +0000813 sc->cap = cap;
814 sw = &sc->sw;
bellard8ead62c2006-07-04 16:51:32 +0000815 sw->hw = hw_cap;
bellardec36b692006-07-16 18:57:03 +0000816 sw->info = hw->info;
bellard8ead62c2006-07-04 16:51:32 +0000817 sw->empty = 1;
818 sw->active = hw->enabled;
819 sw->conv = noop_conv;
820 sw->ratio = ((int64_t) hw_cap->info.freq << 32) / sw->info.freq;
malc83617102012-07-16 18:08:36 +0400821 sw->vol = nominal_volume;
bellard8ead62c2006-07-04 16:51:32 +0000822 sw->rate = st_rate_start (sw->info.freq, hw_cap->info.freq);
823 if (!sw->rate) {
824 dolog ("Could not start rate conversion for `%s'\n", SW_NAME (sw));
Anthony Liguori7267c092011-08-20 22:09:37 -0500825 g_free (sw);
bellard8ead62c2006-07-04 16:51:32 +0000826 return -1;
827 }
Blue Swirl72cf2d42009-09-12 07:36:22 +0000828 QLIST_INSERT_HEAD (&hw_cap->sw_head, sw, entries);
829 QLIST_INSERT_HEAD (&hw->cap_head, sc, entries);
bellardec36b692006-07-16 18:57:03 +0000830#ifdef DEBUG_CAPTURE
Stefan Weil457b6542013-01-16 18:17:33 +0100831 sw->name = g_strdup_printf ("for %p %d,%d,%d",
832 hw, sw->info.freq, sw->info.bits,
833 sw->info.nchannels);
bellardec36b692006-07-16 18:57:03 +0000834 dolog ("Added %s active = %d\n", sw->name, sw->active);
835#endif
bellard8ead62c2006-07-04 16:51:32 +0000836 if (sw->active) {
bellardec36b692006-07-16 18:57:03 +0000837 audio_capture_maybe_changed (cap, 1);
bellard8ead62c2006-07-04 16:51:32 +0000838 }
839 }
840 return 0;
841}
842
843/*
bellard1d14ffa2005-10-30 18:58:22 +0000844 * Hard voice (capture)
845 */
bellardc0fe3822005-11-05 18:55:28 +0000846static int audio_pcm_hw_find_min_in (HWVoiceIn *hw)
bellard85571bc2004-11-07 18:04:02 +0000847{
bellard1d14ffa2005-10-30 18:58:22 +0000848 SWVoiceIn *sw;
849 int m = hw->total_samples_captured;
bellard85571bc2004-11-07 18:04:02 +0000850
bellard1d14ffa2005-10-30 18:58:22 +0000851 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
852 if (sw->active) {
853 m = audio_MIN (m, sw->total_hw_samples_acquired);
bellard85571bc2004-11-07 18:04:02 +0000854 }
855 }
bellard1d14ffa2005-10-30 18:58:22 +0000856 return m;
bellard85571bc2004-11-07 18:04:02 +0000857}
858
bellard1d14ffa2005-10-30 18:58:22 +0000859int audio_pcm_hw_get_live_in (HWVoiceIn *hw)
bellard85571bc2004-11-07 18:04:02 +0000860{
bellard1d14ffa2005-10-30 18:58:22 +0000861 int live = hw->total_samples_captured - audio_pcm_hw_find_min_in (hw);
862 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
863 dolog ("live=%d hw->samples=%d\n", live, hw->samples);
864 return 0;
865 }
866 return live;
867}
bellard85571bc2004-11-07 18:04:02 +0000868
malcddabec72009-09-18 10:59:38 +0400869int audio_pcm_hw_clip_out (HWVoiceOut *hw, void *pcm_buf,
870 int live, int pending)
871{
872 int left = hw->samples - pending;
873 int len = audio_MIN (left, live);
874 int clipped = 0;
875
876 while (len) {
877 struct st_sample *src = hw->mix_buf + hw->rpos;
878 uint8_t *dst = advance (pcm_buf, hw->rpos << hw->info.shift);
879 int samples_till_end_of_buf = hw->samples - hw->rpos;
880 int samples_to_clip = audio_MIN (len, samples_till_end_of_buf);
881
882 hw->clip (dst, src, samples_to_clip);
883
884 hw->rpos = (hw->rpos + samples_to_clip) % hw->samples;
885 len -= samples_to_clip;
886 clipped += samples_to_clip;
887 }
888 return clipped;
889}
890
bellard1d14ffa2005-10-30 18:58:22 +0000891/*
892 * Soft voice (capture)
893 */
bellard1d14ffa2005-10-30 18:58:22 +0000894static int audio_pcm_sw_get_rpos_in (SWVoiceIn *sw)
895{
896 HWVoiceIn *hw = sw->hw;
897 int live = hw->total_samples_captured - sw->total_hw_samples_acquired;
898 int rpos;
899
900 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
901 dolog ("live=%d hw->samples=%d\n", live, hw->samples);
902 return 0;
903 }
904
905 rpos = hw->wpos - live;
906 if (rpos >= 0) {
907 return rpos;
908 }
909 else {
910 return hw->samples + rpos;
bellard85571bc2004-11-07 18:04:02 +0000911 }
912}
913
bellard1d14ffa2005-10-30 18:58:22 +0000914int audio_pcm_sw_read (SWVoiceIn *sw, void *buf, int size)
bellard85571bc2004-11-07 18:04:02 +0000915{
bellard1d14ffa2005-10-30 18:58:22 +0000916 HWVoiceIn *hw = sw->hw;
917 int samples, live, ret = 0, swlim, isamp, osamp, rpos, total = 0;
malc1ea879e2008-12-03 22:48:44 +0000918 struct st_sample *src, *dst = sw->buf;
bellard85571bc2004-11-07 18:04:02 +0000919
bellard1d14ffa2005-10-30 18:58:22 +0000920 rpos = audio_pcm_sw_get_rpos_in (sw) % hw->samples;
bellard85571bc2004-11-07 18:04:02 +0000921
bellard1d14ffa2005-10-30 18:58:22 +0000922 live = hw->total_samples_captured - sw->total_hw_samples_acquired;
923 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
924 dolog ("live_in=%d hw->samples=%d\n", live, hw->samples);
925 return 0;
bellard85571bc2004-11-07 18:04:02 +0000926 }
bellard1d14ffa2005-10-30 18:58:22 +0000927
928 samples = size >> sw->info.shift;
929 if (!live) {
930 return 0;
931 }
932
933 swlim = (live * sw->ratio) >> 32;
bellard85571bc2004-11-07 18:04:02 +0000934 swlim = audio_MIN (swlim, samples);
935
bellard1d14ffa2005-10-30 18:58:22 +0000936 while (swlim) {
937 src = hw->conv_buf + rpos;
938 isamp = hw->wpos - rpos;
939 /* XXX: <= ? */
940 if (isamp <= 0) {
941 isamp = hw->samples - rpos;
942 }
943
944 if (!isamp) {
945 break;
946 }
947 osamp = swlim;
948
949 if (audio_bug (AUDIO_FUNC, osamp < 0)) {
950 dolog ("osamp=%d\n", osamp);
bellardc0fe3822005-11-05 18:55:28 +0000951 return 0;
bellard1d14ffa2005-10-30 18:58:22 +0000952 }
953
954 st_rate_flow (sw->rate, src, dst, &isamp, &osamp);
955 swlim -= osamp;
956 rpos = (rpos + isamp) % hw->samples;
957 dst += osamp;
958 ret += osamp;
959 total += isamp;
960 }
961
Marc-André Lureauc01b2452012-04-17 14:32:36 +0200962 if (!(hw->ctl_caps & VOICE_VOLUME_CAP)) {
963 mixeng_volume (sw->buf, ret, &sw->vol);
964 }
Michael Walle00e07672011-01-05 01:05:47 +0100965
bellard571ec3d2005-11-20 16:24:34 +0000966 sw->clip (buf, sw->buf, ret);
bellard1d14ffa2005-10-30 18:58:22 +0000967 sw->total_hw_samples_acquired += total;
968 return ret << sw->info.shift;
969}
970
971/*
972 * Hard voice (playback)
973 */
974static int audio_pcm_hw_find_min_out (HWVoiceOut *hw, int *nb_livep)
975{
976 SWVoiceOut *sw;
977 int m = INT_MAX;
978 int nb_live = 0;
979
980 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
981 if (sw->active || !sw->empty) {
982 m = audio_MIN (m, sw->total_hw_samples_mixed);
983 nb_live += 1;
984 }
985 }
986
987 *nb_livep = nb_live;
988 return m;
989}
990
malcbdff2532009-09-18 11:37:39 +0400991static int audio_pcm_hw_get_live_out (HWVoiceOut *hw, int *nb_live)
bellard1d14ffa2005-10-30 18:58:22 +0000992{
993 int smin;
malcbdff2532009-09-18 11:37:39 +0400994 int nb_live1;
bellard1d14ffa2005-10-30 18:58:22 +0000995
malcbdff2532009-09-18 11:37:39 +0400996 smin = audio_pcm_hw_find_min_out (hw, &nb_live1);
997 if (nb_live) {
998 *nb_live = nb_live1;
bellard1d14ffa2005-10-30 18:58:22 +0000999 }
malcbdff2532009-09-18 11:37:39 +04001000
1001 if (nb_live1) {
bellard1d14ffa2005-10-30 18:58:22 +00001002 int live = smin;
1003
1004 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
1005 dolog ("live=%d hw->samples=%d\n", live, hw->samples);
1006 return 0;
1007 }
1008 return live;
1009 }
malcbdff2532009-09-18 11:37:39 +04001010 return 0;
bellard1d14ffa2005-10-30 18:58:22 +00001011}
1012
1013/*
1014 * Soft voice (playback)
1015 */
bellard1d14ffa2005-10-30 18:58:22 +00001016int audio_pcm_sw_write (SWVoiceOut *sw, void *buf, int size)
1017{
1018 int hwsamples, samples, isamp, osamp, wpos, live, dead, left, swlim, blck;
1019 int ret = 0, pos = 0, total = 0;
1020
1021 if (!sw) {
1022 return size;
1023 }
1024
1025 hwsamples = sw->hw->samples;
1026
1027 live = sw->total_hw_samples_mixed;
1028 if (audio_bug (AUDIO_FUNC, live < 0 || live > hwsamples)){
1029 dolog ("live=%d hw->samples=%d\n", live, hwsamples);
1030 return 0;
1031 }
1032
1033 if (live == hwsamples) {
bellardec36b692006-07-16 18:57:03 +00001034#ifdef DEBUG_OUT
1035 dolog ("%s is full %d\n", sw->name, live);
1036#endif
bellard1d14ffa2005-10-30 18:58:22 +00001037 return 0;
1038 }
1039
1040 wpos = (sw->hw->rpos + live) % hwsamples;
1041 samples = size >> sw->info.shift;
1042
1043 dead = hwsamples - live;
1044 swlim = ((int64_t) dead << 32) / sw->ratio;
1045 swlim = audio_MIN (swlim, samples);
1046 if (swlim) {
Michael Walle00e07672011-01-05 01:05:47 +01001047 sw->conv (sw->buf, buf, swlim);
Marc-André Lureauc01b2452012-04-17 14:32:36 +02001048
1049 if (!(sw->hw->ctl_caps & VOICE_VOLUME_CAP)) {
1050 mixeng_volume (sw->buf, swlim, &sw->vol);
1051 }
bellard1d14ffa2005-10-30 18:58:22 +00001052 }
bellard85571bc2004-11-07 18:04:02 +00001053
1054 while (swlim) {
1055 dead = hwsamples - live;
1056 left = hwsamples - wpos;
1057 blck = audio_MIN (dead, left);
1058 if (!blck) {
bellard85571bc2004-11-07 18:04:02 +00001059 break;
1060 }
1061 isamp = swlim;
1062 osamp = blck;
bellard1d14ffa2005-10-30 18:58:22 +00001063 st_rate_flow_mix (
1064 sw->rate,
1065 sw->buf + pos,
1066 sw->hw->mix_buf + wpos,
1067 &isamp,
1068 &osamp
1069 );
bellard85571bc2004-11-07 18:04:02 +00001070 ret += isamp;
1071 swlim -= isamp;
1072 pos += isamp;
1073 live += osamp;
1074 wpos = (wpos + osamp) % hwsamples;
bellard1d14ffa2005-10-30 18:58:22 +00001075 total += osamp;
bellard85571bc2004-11-07 18:04:02 +00001076 }
1077
bellard1d14ffa2005-10-30 18:58:22 +00001078 sw->total_hw_samples_mixed += total;
1079 sw->empty = sw->total_hw_samples_mixed == 0;
1080
1081#ifdef DEBUG_OUT
1082 dolog (
bellardc0fe3822005-11-05 18:55:28 +00001083 "%s: write size %d ret %d total sw %d\n",
1084 SW_NAME (sw),
bellard1d14ffa2005-10-30 18:58:22 +00001085 size >> sw->info.shift,
1086 ret,
bellardc0fe3822005-11-05 18:55:28 +00001087 sw->total_hw_samples_mixed
bellard1d14ffa2005-10-30 18:58:22 +00001088 );
1089#endif
1090
1091 return ret << sw->info.shift;
bellard85571bc2004-11-07 18:04:02 +00001092}
1093
bellard1d14ffa2005-10-30 18:58:22 +00001094#ifdef DEBUG_AUDIO
1095static void audio_pcm_print_info (const char *cap, struct audio_pcm_info *info)
bellard85571bc2004-11-07 18:04:02 +00001096{
bellard1d14ffa2005-10-30 18:58:22 +00001097 dolog ("%s: bits %d, sign %d, freq %d, nchan %d\n",
1098 cap, info->bits, info->sign, info->freq, info->nchannels);
bellard85571bc2004-11-07 18:04:02 +00001099}
bellard1d14ffa2005-10-30 18:58:22 +00001100#endif
bellard85571bc2004-11-07 18:04:02 +00001101
bellard1d14ffa2005-10-30 18:58:22 +00001102#define DAC
1103#include "audio_template.h"
1104#undef DAC
1105#include "audio_template.h"
bellard85571bc2004-11-07 18:04:02 +00001106
malc713a98f2009-09-12 02:28:45 +04001107/*
1108 * Timer
1109 */
malc713a98f2009-09-12 02:28:45 +04001110static int audio_is_timer_needed (void)
1111{
1112 HWVoiceIn *hwi = NULL;
1113 HWVoiceOut *hwo = NULL;
1114
1115 while ((hwo = audio_pcm_hw_find_any_enabled_out (hwo))) {
1116 if (!hwo->poll_mode) return 1;
1117 }
1118 while ((hwi = audio_pcm_hw_find_any_enabled_in (hwi))) {
1119 if (!hwi->poll_mode) return 1;
1120 }
1121 return 0;
1122}
1123
malc39deb1e2010-11-18 14:30:12 +03001124static void audio_reset_timer (AudioState *s)
malc713a98f2009-09-12 02:28:45 +04001125{
malc713a98f2009-09-12 02:28:45 +04001126 if (audio_is_timer_needed ()) {
Alex Blighbc72ad62013-08-21 16:03:08 +01001127 timer_mod (s->ts, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + 1);
malc713a98f2009-09-12 02:28:45 +04001128 }
1129 else {
Alex Blighbc72ad62013-08-21 16:03:08 +01001130 timer_del (s->ts);
malc713a98f2009-09-12 02:28:45 +04001131 }
1132}
1133
malc39deb1e2010-11-18 14:30:12 +03001134static void audio_timer (void *opaque)
1135{
1136 audio_run ("timer");
1137 audio_reset_timer (opaque);
1138}
1139
malc713a98f2009-09-12 02:28:45 +04001140/*
1141 * Public API
1142 */
bellard1d14ffa2005-10-30 18:58:22 +00001143int AUD_write (SWVoiceOut *sw, void *buf, int size)
bellard85571bc2004-11-07 18:04:02 +00001144{
1145 int bytes;
1146
bellard1d14ffa2005-10-30 18:58:22 +00001147 if (!sw) {
1148 /* XXX: Consider options */
1149 return size;
1150 }
1151
1152 if (!sw->hw->enabled) {
bellardc0fe3822005-11-05 18:55:28 +00001153 dolog ("Writing to disabled voice %s\n", SW_NAME (sw));
bellard1d14ffa2005-10-30 18:58:22 +00001154 return 0;
1155 }
1156
bellard85571bc2004-11-07 18:04:02 +00001157 bytes = sw->hw->pcm_ops->write (sw, buf, size);
1158 return bytes;
1159}
1160
bellard1d14ffa2005-10-30 18:58:22 +00001161int AUD_read (SWVoiceIn *sw, void *buf, int size)
bellard85571bc2004-11-07 18:04:02 +00001162{
bellard1d14ffa2005-10-30 18:58:22 +00001163 int bytes;
bellard85571bc2004-11-07 18:04:02 +00001164
bellard1d14ffa2005-10-30 18:58:22 +00001165 if (!sw) {
1166 /* XXX: Consider options */
1167 return size;
bellard85571bc2004-11-07 18:04:02 +00001168 }
bellard1d14ffa2005-10-30 18:58:22 +00001169
1170 if (!sw->hw->enabled) {
bellardc0fe3822005-11-05 18:55:28 +00001171 dolog ("Reading from disabled voice %s\n", SW_NAME (sw));
bellard1d14ffa2005-10-30 18:58:22 +00001172 return 0;
1173 }
1174
1175 bytes = sw->hw->pcm_ops->read (sw, buf, size);
1176 return bytes;
bellard85571bc2004-11-07 18:04:02 +00001177}
1178
bellard1d14ffa2005-10-30 18:58:22 +00001179int AUD_get_buffer_size_out (SWVoiceOut *sw)
bellard85571bc2004-11-07 18:04:02 +00001180{
bellardc0fe3822005-11-05 18:55:28 +00001181 return sw->hw->samples << sw->hw->info.shift;
bellard85571bc2004-11-07 18:04:02 +00001182}
1183
bellard1d14ffa2005-10-30 18:58:22 +00001184void AUD_set_active_out (SWVoiceOut *sw, int on)
bellard85571bc2004-11-07 18:04:02 +00001185{
bellard1d14ffa2005-10-30 18:58:22 +00001186 HWVoiceOut *hw;
1187
1188 if (!sw) {
bellard85571bc2004-11-07 18:04:02 +00001189 return;
bellard85571bc2004-11-07 18:04:02 +00001190 }
1191
bellard85571bc2004-11-07 18:04:02 +00001192 hw = sw->hw;
bellard85571bc2004-11-07 18:04:02 +00001193 if (sw->active != on) {
malc978dd632009-02-18 20:44:04 +00001194 AudioState *s = &glob_audio_state;
bellard1d14ffa2005-10-30 18:58:22 +00001195 SWVoiceOut *temp_sw;
bellardec36b692006-07-16 18:57:03 +00001196 SWVoiceCap *sc;
bellard1d14ffa2005-10-30 18:58:22 +00001197
bellard85571bc2004-11-07 18:04:02 +00001198 if (on) {
1199 hw->pending_disable = 0;
1200 if (!hw->enabled) {
1201 hw->enabled = 1;
malc978dd632009-02-18 20:44:04 +00001202 if (s->vm_running) {
malc713a98f2009-09-12 02:28:45 +04001203 hw->pcm_ops->ctl_out (hw, VOICE_ENABLE, conf.try_poll_out);
malc39deb1e2010-11-18 14:30:12 +03001204 audio_reset_timer (s);
malc978dd632009-02-18 20:44:04 +00001205 }
bellard1d14ffa2005-10-30 18:58:22 +00001206 }
bellard85571bc2004-11-07 18:04:02 +00001207 }
1208 else {
bellard1d14ffa2005-10-30 18:58:22 +00001209 if (hw->enabled) {
bellard85571bc2004-11-07 18:04:02 +00001210 int nb_active = 0;
bellard1d14ffa2005-10-30 18:58:22 +00001211
1212 for (temp_sw = hw->sw_head.lh_first; temp_sw;
1213 temp_sw = temp_sw->entries.le_next) {
1214 nb_active += temp_sw->active != 0;
1215 }
1216
1217 hw->pending_disable = nb_active == 1;
1218 }
1219 }
bellardec36b692006-07-16 18:57:03 +00001220
1221 for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) {
1222 sc->sw.active = hw->enabled;
bellard8ead62c2006-07-04 16:51:32 +00001223 if (hw->enabled) {
bellardec36b692006-07-16 18:57:03 +00001224 audio_capture_maybe_changed (sc->cap, 1);
bellard8ead62c2006-07-04 16:51:32 +00001225 }
1226 }
bellard1d14ffa2005-10-30 18:58:22 +00001227 sw->active = on;
1228 }
1229}
1230
1231void AUD_set_active_in (SWVoiceIn *sw, int on)
1232{
1233 HWVoiceIn *hw;
1234
1235 if (!sw) {
1236 return;
1237 }
1238
1239 hw = sw->hw;
1240 if (sw->active != on) {
malc978dd632009-02-18 20:44:04 +00001241 AudioState *s = &glob_audio_state;
bellard1d14ffa2005-10-30 18:58:22 +00001242 SWVoiceIn *temp_sw;
1243
1244 if (on) {
1245 if (!hw->enabled) {
1246 hw->enabled = 1;
malc978dd632009-02-18 20:44:04 +00001247 if (s->vm_running) {
malc713a98f2009-09-12 02:28:45 +04001248 hw->pcm_ops->ctl_in (hw, VOICE_ENABLE, conf.try_poll_in);
malc39deb1e2010-11-18 14:30:12 +03001249 audio_reset_timer (s);
malc978dd632009-02-18 20:44:04 +00001250 }
bellard1d14ffa2005-10-30 18:58:22 +00001251 }
1252 sw->total_hw_samples_acquired = hw->total_samples_captured;
1253 }
1254 else {
1255 if (hw->enabled) {
1256 int nb_active = 0;
1257
1258 for (temp_sw = hw->sw_head.lh_first; temp_sw;
1259 temp_sw = temp_sw->entries.le_next) {
1260 nb_active += temp_sw->active != 0;
bellard85571bc2004-11-07 18:04:02 +00001261 }
1262
1263 if (nb_active == 1) {
bellard1d14ffa2005-10-30 18:58:22 +00001264 hw->enabled = 0;
1265 hw->pcm_ops->ctl_in (hw, VOICE_DISABLE);
bellard85571bc2004-11-07 18:04:02 +00001266 }
1267 }
1268 }
1269 sw->active = on;
1270 }
1271}
1272
bellard1d14ffa2005-10-30 18:58:22 +00001273static int audio_get_avail (SWVoiceIn *sw)
bellard85571bc2004-11-07 18:04:02 +00001274{
bellard1d14ffa2005-10-30 18:58:22 +00001275 int live;
1276
1277 if (!sw) {
1278 return 0;
1279 }
1280
1281 live = sw->hw->total_samples_captured - sw->total_hw_samples_acquired;
1282 if (audio_bug (AUDIO_FUNC, live < 0 || live > sw->hw->samples)) {
1283 dolog ("live=%d sw->hw->samples=%d\n", live, sw->hw->samples);
1284 return 0;
1285 }
1286
1287 ldebug (
bellard26a76462006-06-25 18:15:32 +00001288 "%s: get_avail live %d ret %" PRId64 "\n",
bellardc0fe3822005-11-05 18:55:28 +00001289 SW_NAME (sw),
bellard1d14ffa2005-10-30 18:58:22 +00001290 live, (((int64_t) live << 32) / sw->ratio) << sw->info.shift
1291 );
1292
1293 return (((int64_t) live << 32) / sw->ratio) << sw->info.shift;
1294}
1295
1296static int audio_get_free (SWVoiceOut *sw)
1297{
1298 int live, dead;
1299
1300 if (!sw) {
1301 return 0;
1302 }
1303
1304 live = sw->total_hw_samples_mixed;
1305
1306 if (audio_bug (AUDIO_FUNC, live < 0 || live > sw->hw->samples)) {
1307 dolog ("live=%d sw->hw->samples=%d\n", live, sw->hw->samples);
bellardc0fe3822005-11-05 18:55:28 +00001308 return 0;
bellard1d14ffa2005-10-30 18:58:22 +00001309 }
1310
1311 dead = sw->hw->samples - live;
1312
1313#ifdef DEBUG_OUT
bellard26a76462006-06-25 18:15:32 +00001314 dolog ("%s: get_free live %d dead %d ret %" PRId64 "\n",
bellardc0fe3822005-11-05 18:55:28 +00001315 SW_NAME (sw),
bellard1d14ffa2005-10-30 18:58:22 +00001316 live, dead, (((int64_t) dead << 32) / sw->ratio) << sw->info.shift);
1317#endif
1318
1319 return (((int64_t) dead << 32) / sw->ratio) << sw->info.shift;
1320}
1321
bellard8ead62c2006-07-04 16:51:32 +00001322static void audio_capture_mix_and_clear (HWVoiceOut *hw, int rpos, int samples)
1323{
1324 int n;
1325
1326 if (hw->enabled) {
bellardec36b692006-07-16 18:57:03 +00001327 SWVoiceCap *sc;
bellard8ead62c2006-07-04 16:51:32 +00001328
bellardec36b692006-07-16 18:57:03 +00001329 for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) {
1330 SWVoiceOut *sw = &sc->sw;
bellard8ead62c2006-07-04 16:51:32 +00001331 int rpos2 = rpos;
1332
1333 n = samples;
1334 while (n) {
1335 int till_end_of_hw = hw->samples - rpos2;
1336 int to_write = audio_MIN (till_end_of_hw, n);
1337 int bytes = to_write << hw->info.shift;
1338 int written;
1339
1340 sw->buf = hw->mix_buf + rpos2;
1341 written = audio_pcm_sw_write (sw, NULL, bytes);
1342 if (written - bytes) {
bellardec36b692006-07-16 18:57:03 +00001343 dolog ("Could not mix %d bytes into a capture "
1344 "buffer, mixed %d\n",
1345 bytes, written);
bellard8ead62c2006-07-04 16:51:32 +00001346 break;
1347 }
1348 n -= to_write;
1349 rpos2 = (rpos2 + to_write) % hw->samples;
1350 }
1351 }
1352 }
1353
1354 n = audio_MIN (samples, hw->samples - rpos);
1355 mixeng_clear (hw->mix_buf + rpos, n);
1356 mixeng_clear (hw->mix_buf, samples - n);
1357}
1358
bellardc0fe3822005-11-05 18:55:28 +00001359static void audio_run_out (AudioState *s)
bellard1d14ffa2005-10-30 18:58:22 +00001360{
1361 HWVoiceOut *hw = NULL;
1362 SWVoiceOut *sw;
1363
malc1a7dafc2009-05-14 03:11:35 +04001364 while ((hw = audio_pcm_hw_find_any_enabled_out (hw))) {
bellard1d14ffa2005-10-30 18:58:22 +00001365 int played;
bellard8ead62c2006-07-04 16:51:32 +00001366 int live, free, nb_live, cleanup_required, prev_rpos;
bellard1d14ffa2005-10-30 18:58:22 +00001367
malcbdff2532009-09-18 11:37:39 +04001368 live = audio_pcm_hw_get_live_out (hw, &nb_live);
bellard1d14ffa2005-10-30 18:58:22 +00001369 if (!nb_live) {
1370 live = 0;
bellard85571bc2004-11-07 18:04:02 +00001371 }
bellardc0fe3822005-11-05 18:55:28 +00001372
bellard1d14ffa2005-10-30 18:58:22 +00001373 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
1374 dolog ("live=%d hw->samples=%d\n", live, hw->samples);
bellardc0fe3822005-11-05 18:55:28 +00001375 continue;
bellard85571bc2004-11-07 18:04:02 +00001376 }
bellard1d14ffa2005-10-30 18:58:22 +00001377
1378 if (hw->pending_disable && !nb_live) {
bellardec36b692006-07-16 18:57:03 +00001379 SWVoiceCap *sc;
bellard1d14ffa2005-10-30 18:58:22 +00001380#ifdef DEBUG_OUT
1381 dolog ("Disabling voice\n");
1382#endif
1383 hw->enabled = 0;
1384 hw->pending_disable = 0;
1385 hw->pcm_ops->ctl_out (hw, VOICE_DISABLE);
bellardec36b692006-07-16 18:57:03 +00001386 for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) {
1387 sc->sw.active = 0;
1388 audio_recalc_and_notify_capture (sc->cap);
bellard8ead62c2006-07-04 16:51:32 +00001389 }
bellard1d14ffa2005-10-30 18:58:22 +00001390 continue;
1391 }
1392
1393 if (!live) {
1394 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1395 if (sw->active) {
1396 free = audio_get_free (sw);
1397 if (free > 0) {
1398 sw->callback.fn (sw->callback.opaque, free);
1399 }
1400 }
1401 }
1402 continue;
1403 }
1404
bellard8ead62c2006-07-04 16:51:32 +00001405 prev_rpos = hw->rpos;
malcbdff2532009-09-18 11:37:39 +04001406 played = hw->pcm_ops->run_out (hw, live);
bellard1d14ffa2005-10-30 18:58:22 +00001407 if (audio_bug (AUDIO_FUNC, hw->rpos >= hw->samples)) {
1408 dolog ("hw->rpos=%d hw->samples=%d played=%d\n",
1409 hw->rpos, hw->samples, played);
1410 hw->rpos = 0;
1411 }
1412
1413#ifdef DEBUG_OUT
bellardc0fe3822005-11-05 18:55:28 +00001414 dolog ("played=%d\n", played);
bellard1d14ffa2005-10-30 18:58:22 +00001415#endif
1416
1417 if (played) {
1418 hw->ts_helper += played;
bellard8ead62c2006-07-04 16:51:32 +00001419 audio_capture_mix_and_clear (hw, prev_rpos, played);
bellard1d14ffa2005-10-30 18:58:22 +00001420 }
1421
bellardc0fe3822005-11-05 18:55:28 +00001422 cleanup_required = 0;
bellard1d14ffa2005-10-30 18:58:22 +00001423 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
bellard1d14ffa2005-10-30 18:58:22 +00001424 if (!sw->active && sw->empty) {
1425 continue;
1426 }
1427
1428 if (audio_bug (AUDIO_FUNC, played > sw->total_hw_samples_mixed)) {
1429 dolog ("played=%d sw->total_hw_samples_mixed=%d\n",
1430 played, sw->total_hw_samples_mixed);
1431 played = sw->total_hw_samples_mixed;
1432 }
1433
1434 sw->total_hw_samples_mixed -= played;
1435
1436 if (!sw->total_hw_samples_mixed) {
1437 sw->empty = 1;
bellardc0fe3822005-11-05 18:55:28 +00001438 cleanup_required |= !sw->active && !sw->callback.fn;
bellard1d14ffa2005-10-30 18:58:22 +00001439 }
1440
1441 if (sw->active) {
1442 free = audio_get_free (sw);
1443 if (free > 0) {
1444 sw->callback.fn (sw->callback.opaque, free);
1445 }
1446 }
bellard85571bc2004-11-07 18:04:02 +00001447 }
bellardc0fe3822005-11-05 18:55:28 +00001448
1449 if (cleanup_required) {
bellardec36b692006-07-16 18:57:03 +00001450 SWVoiceOut *sw1;
1451
1452 sw = hw->sw_head.lh_first;
1453 while (sw) {
1454 sw1 = sw->entries.le_next;
bellardc0fe3822005-11-05 18:55:28 +00001455 if (!sw->active && !sw->callback.fn) {
1456#ifdef DEBUG_PLIVE
1457 dolog ("Finishing with old voice\n");
1458#endif
malc1a7dafc2009-05-14 03:11:35 +04001459 audio_close_out (sw);
bellardc0fe3822005-11-05 18:55:28 +00001460 }
bellardec36b692006-07-16 18:57:03 +00001461 sw = sw1;
bellardc0fe3822005-11-05 18:55:28 +00001462 }
1463 }
bellard85571bc2004-11-07 18:04:02 +00001464 }
bellard1d14ffa2005-10-30 18:58:22 +00001465}
1466
bellardc0fe3822005-11-05 18:55:28 +00001467static void audio_run_in (AudioState *s)
bellard1d14ffa2005-10-30 18:58:22 +00001468{
1469 HWVoiceIn *hw = NULL;
1470
malc1a7dafc2009-05-14 03:11:35 +04001471 while ((hw = audio_pcm_hw_find_any_enabled_in (hw))) {
bellard1d14ffa2005-10-30 18:58:22 +00001472 SWVoiceIn *sw;
1473 int captured, min;
1474
1475 captured = hw->pcm_ops->run_in (hw);
1476
1477 min = audio_pcm_hw_find_min_in (hw);
1478 hw->total_samples_captured += captured - min;
1479 hw->ts_helper += captured;
1480
1481 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1482 sw->total_hw_samples_acquired -= min;
1483
1484 if (sw->active) {
1485 int avail;
1486
1487 avail = audio_get_avail (sw);
1488 if (avail > 0) {
1489 sw->callback.fn (sw->callback.opaque, avail);
1490 }
1491 }
1492 }
1493 }
1494}
1495
bellard8ead62c2006-07-04 16:51:32 +00001496static void audio_run_capture (AudioState *s)
1497{
1498 CaptureVoiceOut *cap;
1499
1500 for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) {
1501 int live, rpos, captured;
1502 HWVoiceOut *hw = &cap->hw;
1503 SWVoiceOut *sw;
1504
malcbdff2532009-09-18 11:37:39 +04001505 captured = live = audio_pcm_hw_get_live_out (hw, NULL);
bellard8ead62c2006-07-04 16:51:32 +00001506 rpos = hw->rpos;
1507 while (live) {
1508 int left = hw->samples - rpos;
1509 int to_capture = audio_MIN (live, left);
malc1ea879e2008-12-03 22:48:44 +00001510 struct st_sample *src;
bellard8ead62c2006-07-04 16:51:32 +00001511 struct capture_callback *cb;
1512
1513 src = hw->mix_buf + rpos;
1514 hw->clip (cap->buf, src, to_capture);
1515 mixeng_clear (src, to_capture);
1516
1517 for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
1518 cb->ops.capture (cb->opaque, cap->buf,
1519 to_capture << hw->info.shift);
1520 }
1521 rpos = (rpos + to_capture) % hw->samples;
1522 live -= to_capture;
1523 }
1524 hw->rpos = rpos;
1525
1526 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1527 if (!sw->active && sw->empty) {
1528 continue;
1529 }
1530
1531 if (audio_bug (AUDIO_FUNC, captured > sw->total_hw_samples_mixed)) {
1532 dolog ("captured=%d sw->total_hw_samples_mixed=%d\n",
1533 captured, sw->total_hw_samples_mixed);
1534 captured = sw->total_hw_samples_mixed;
1535 }
1536
1537 sw->total_hw_samples_mixed -= captured;
1538 sw->empty = sw->total_hw_samples_mixed == 0;
1539 }
1540 }
1541}
1542
malc713a98f2009-09-12 02:28:45 +04001543void audio_run (const char *msg)
bellard571ec3d2005-11-20 16:24:34 +00001544{
malc713a98f2009-09-12 02:28:45 +04001545 AudioState *s = &glob_audio_state;
bellard571ec3d2005-11-20 16:24:34 +00001546
1547 audio_run_out (s);
1548 audio_run_in (s);
bellard8ead62c2006-07-04 16:51:32 +00001549 audio_run_capture (s);
malc713a98f2009-09-12 02:28:45 +04001550#ifdef DEBUG_POLL
1551 {
1552 static double prevtime;
1553 double currtime;
1554 struct timeval tv;
bellard571ec3d2005-11-20 16:24:34 +00001555
malc713a98f2009-09-12 02:28:45 +04001556 if (gettimeofday (&tv, NULL)) {
1557 perror ("audio_run: gettimeofday");
1558 return;
1559 }
1560
1561 currtime = tv.tv_sec + tv.tv_usec * 1e-6;
1562 dolog ("Elapsed since last %s: %f\n", msg, currtime - prevtime);
1563 prevtime = currtime;
1564 }
1565#endif
bellard571ec3d2005-11-20 16:24:34 +00001566}
1567
bellard1d14ffa2005-10-30 18:58:22 +00001568static struct audio_option audio_options[] = {
1569 /* DAC */
malc98f9f482009-08-11 20:48:02 +04001570 {
1571 .name = "DAC_FIXED_SETTINGS",
1572 .tag = AUD_OPT_BOOL,
1573 .valp = &conf.fixed_out.enabled,
1574 .descr = "Use fixed settings for host DAC"
1575 },
1576 {
1577 .name = "DAC_FIXED_FREQ",
1578 .tag = AUD_OPT_INT,
1579 .valp = &conf.fixed_out.settings.freq,
1580 .descr = "Frequency for fixed host DAC"
1581 },
1582 {
1583 .name = "DAC_FIXED_FMT",
1584 .tag = AUD_OPT_FMT,
1585 .valp = &conf.fixed_out.settings.fmt,
1586 .descr = "Format for fixed host DAC"
1587 },
1588 {
1589 .name = "DAC_FIXED_CHANNELS",
1590 .tag = AUD_OPT_INT,
1591 .valp = &conf.fixed_out.settings.nchannels,
1592 .descr = "Number of channels for fixed DAC (1 - mono, 2 - stereo)"
1593 },
1594 {
1595 .name = "DAC_VOICES",
1596 .tag = AUD_OPT_INT,
1597 .valp = &conf.fixed_out.nb_voices,
1598 .descr = "Number of voices for DAC"
1599 },
malc713a98f2009-09-12 02:28:45 +04001600 {
1601 .name = "DAC_TRY_POLL",
1602 .tag = AUD_OPT_BOOL,
1603 .valp = &conf.try_poll_out,
1604 .descr = "Attempt using poll mode for DAC"
1605 },
bellard1d14ffa2005-10-30 18:58:22 +00001606 /* ADC */
malc98f9f482009-08-11 20:48:02 +04001607 {
1608 .name = "ADC_FIXED_SETTINGS",
1609 .tag = AUD_OPT_BOOL,
1610 .valp = &conf.fixed_in.enabled,
1611 .descr = "Use fixed settings for host ADC"
1612 },
1613 {
1614 .name = "ADC_FIXED_FREQ",
1615 .tag = AUD_OPT_INT,
1616 .valp = &conf.fixed_in.settings.freq,
1617 .descr = "Frequency for fixed host ADC"
1618 },
1619 {
1620 .name = "ADC_FIXED_FMT",
1621 .tag = AUD_OPT_FMT,
1622 .valp = &conf.fixed_in.settings.fmt,
1623 .descr = "Format for fixed host ADC"
1624 },
1625 {
1626 .name = "ADC_FIXED_CHANNELS",
1627 .tag = AUD_OPT_INT,
1628 .valp = &conf.fixed_in.settings.nchannels,
1629 .descr = "Number of channels for fixed ADC (1 - mono, 2 - stereo)"
1630 },
1631 {
1632 .name = "ADC_VOICES",
1633 .tag = AUD_OPT_INT,
1634 .valp = &conf.fixed_in.nb_voices,
1635 .descr = "Number of voices for ADC"
1636 },
malc713a98f2009-09-12 02:28:45 +04001637 {
1638 .name = "ADC_TRY_POLL",
1639 .tag = AUD_OPT_BOOL,
Jan Kiszka0a90e342009-09-13 22:24:46 +04001640 .valp = &conf.try_poll_in,
malc713a98f2009-09-12 02:28:45 +04001641 .descr = "Attempt using poll mode for ADC"
1642 },
bellard1d14ffa2005-10-30 18:58:22 +00001643 /* Misc */
malc98f9f482009-08-11 20:48:02 +04001644 {
1645 .name = "TIMER_PERIOD",
1646 .tag = AUD_OPT_INT,
1647 .valp = &conf.period.hertz,
1648 .descr = "Timer period in HZ (0 - use lowest possible)"
1649 },
1650 {
1651 .name = "PLIVE",
1652 .tag = AUD_OPT_BOOL,
1653 .valp = &conf.plive,
1654 .descr = "(undocumented)"
1655 },
1656 {
1657 .name = "LOG_TO_MONITOR",
1658 .tag = AUD_OPT_BOOL,
1659 .valp = &conf.log_to_monitor,
malc713a98f2009-09-12 02:28:45 +04001660 .descr = "Print logging messages to monitor instead of stderr"
malc98f9f482009-08-11 20:48:02 +04001661 },
Juan Quintela2700efa2009-08-11 02:31:16 +02001662 { /* End of list */ }
bellard1d14ffa2005-10-30 18:58:22 +00001663};
1664
bellard571ec3d2005-11-20 16:24:34 +00001665static void audio_pp_nb_voices (const char *typ, int nb)
1666{
1667 switch (nb) {
1668 case 0:
1669 printf ("Does not support %s\n", typ);
1670 break;
1671 case 1:
1672 printf ("One %s voice\n", typ);
1673 break;
1674 case INT_MAX:
1675 printf ("Theoretically supports many %s voices\n", typ);
1676 break;
1677 default:
Stefan Weile7d81002011-12-10 00:19:46 +01001678 printf ("Theoretically supports up to %d %s voices\n", nb, typ);
bellard571ec3d2005-11-20 16:24:34 +00001679 break;
1680 }
1681
1682}
1683
bellard1d14ffa2005-10-30 18:58:22 +00001684void AUD_help (void)
1685{
1686 size_t i;
1687
1688 audio_process_options ("AUDIO", audio_options);
malcb1503cd2008-12-22 20:33:55 +00001689 for (i = 0; i < ARRAY_SIZE (drvtab); i++) {
bellard1d14ffa2005-10-30 18:58:22 +00001690 struct audio_driver *d = drvtab[i];
1691 if (d->options) {
1692 audio_process_options (d->name, d->options);
1693 }
1694 }
1695
1696 printf ("Audio options:\n");
1697 audio_print_options ("AUDIO", audio_options);
1698 printf ("\n");
1699
1700 printf ("Available drivers:\n");
1701
malcb1503cd2008-12-22 20:33:55 +00001702 for (i = 0; i < ARRAY_SIZE (drvtab); i++) {
bellard1d14ffa2005-10-30 18:58:22 +00001703 struct audio_driver *d = drvtab[i];
1704
1705 printf ("Name: %s\n", d->name);
1706 printf ("Description: %s\n", d->descr);
1707
bellard571ec3d2005-11-20 16:24:34 +00001708 audio_pp_nb_voices ("playback", d->max_voices_out);
1709 audio_pp_nb_voices ("capture", d->max_voices_in);
bellard1d14ffa2005-10-30 18:58:22 +00001710
1711 if (d->options) {
1712 printf ("Options:\n");
1713 audio_print_options (d->name, d->options);
1714 }
1715 else {
1716 printf ("No options\n");
1717 }
1718 printf ("\n");
1719 }
1720
1721 printf (
1722 "Options are settable through environment variables.\n"
1723 "Example:\n"
1724#ifdef _WIN32
1725 " set QEMU_AUDIO_DRV=wav\n"
bellard571ec3d2005-11-20 16:24:34 +00001726 " set QEMU_WAV_PATH=c:\\tune.wav\n"
bellard1d14ffa2005-10-30 18:58:22 +00001727#else
1728 " export QEMU_AUDIO_DRV=wav\n"
1729 " export QEMU_WAV_PATH=$HOME/tune.wav\n"
1730 "(for csh replace export with setenv in the above)\n"
1731#endif
1732 " qemu ...\n\n"
1733 );
1734}
1735
bellardc0fe3822005-11-05 18:55:28 +00001736static int audio_driver_init (AudioState *s, struct audio_driver *drv)
bellard1d14ffa2005-10-30 18:58:22 +00001737{
1738 if (drv->options) {
1739 audio_process_options (drv->name, drv->options);
1740 }
bellardc0fe3822005-11-05 18:55:28 +00001741 s->drv_opaque = drv->init ();
bellard1d14ffa2005-10-30 18:58:22 +00001742
bellardc0fe3822005-11-05 18:55:28 +00001743 if (s->drv_opaque) {
malc1a7dafc2009-05-14 03:11:35 +04001744 audio_init_nb_voices_out (drv);
1745 audio_init_nb_voices_in (drv);
bellardc0fe3822005-11-05 18:55:28 +00001746 s->drv = drv;
bellard85571bc2004-11-07 18:04:02 +00001747 return 0;
1748 }
bellard1d14ffa2005-10-30 18:58:22 +00001749 else {
1750 dolog ("Could not init `%s' audio driver\n", drv->name);
1751 return -1;
1752 }
bellard85571bc2004-11-07 18:04:02 +00001753}
1754
aliguori9781e042009-01-22 17:15:29 +00001755static void audio_vm_change_state_handler (void *opaque, int running,
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001756 RunState state)
bellard85571bc2004-11-07 18:04:02 +00001757{
bellardc0fe3822005-11-05 18:55:28 +00001758 AudioState *s = opaque;
bellard1d14ffa2005-10-30 18:58:22 +00001759 HWVoiceOut *hwo = NULL;
1760 HWVoiceIn *hwi = NULL;
bellard541e0842005-11-11 00:04:19 +00001761 int op = running ? VOICE_ENABLE : VOICE_DISABLE;
bellard85571bc2004-11-07 18:04:02 +00001762
malc978dd632009-02-18 20:44:04 +00001763 s->vm_running = running;
malc1a7dafc2009-05-14 03:11:35 +04001764 while ((hwo = audio_pcm_hw_find_any_enabled_out (hwo))) {
malc713a98f2009-09-12 02:28:45 +04001765 hwo->pcm_ops->ctl_out (hwo, op, conf.try_poll_out);
bellard1d14ffa2005-10-30 18:58:22 +00001766 }
1767
malc1a7dafc2009-05-14 03:11:35 +04001768 while ((hwi = audio_pcm_hw_find_any_enabled_in (hwi))) {
malc713a98f2009-09-12 02:28:45 +04001769 hwi->pcm_ops->ctl_in (hwi, op, conf.try_poll_in);
bellard85571bc2004-11-07 18:04:02 +00001770 }
malc39deb1e2010-11-18 14:30:12 +03001771 audio_reset_timer (s);
bellard85571bc2004-11-07 18:04:02 +00001772}
1773
1774static void audio_atexit (void)
1775{
bellardc0fe3822005-11-05 18:55:28 +00001776 AudioState *s = &glob_audio_state;
bellard1d14ffa2005-10-30 18:58:22 +00001777 HWVoiceOut *hwo = NULL;
1778 HWVoiceIn *hwi = NULL;
bellard85571bc2004-11-07 18:04:02 +00001779
Jan Kiszkaaeb29b62012-05-24 12:05:15 -03001780 while ((hwo = audio_pcm_hw_find_any_out (hwo))) {
bellardec36b692006-07-16 18:57:03 +00001781 SWVoiceCap *sc;
bellard8ead62c2006-07-04 16:51:32 +00001782
Jan Kiszkaaeb29b62012-05-24 12:05:15 -03001783 if (hwo->enabled) {
1784 hwo->pcm_ops->ctl_out (hwo, VOICE_DISABLE);
1785 }
bellard1d14ffa2005-10-30 18:58:22 +00001786 hwo->pcm_ops->fini_out (hwo);
bellard8ead62c2006-07-04 16:51:32 +00001787
bellardec36b692006-07-16 18:57:03 +00001788 for (sc = hwo->cap_head.lh_first; sc; sc = sc->entries.le_next) {
1789 CaptureVoiceOut *cap = sc->cap;
1790 struct capture_callback *cb;
1791
1792 for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
1793 cb->ops.destroy (cb->opaque);
1794 }
bellard8ead62c2006-07-04 16:51:32 +00001795 }
bellard1d14ffa2005-10-30 18:58:22 +00001796 }
1797
Jan Kiszkaaeb29b62012-05-24 12:05:15 -03001798 while ((hwi = audio_pcm_hw_find_any_in (hwi))) {
1799 if (hwi->enabled) {
1800 hwi->pcm_ops->ctl_in (hwi, VOICE_DISABLE);
1801 }
bellard1d14ffa2005-10-30 18:58:22 +00001802 hwi->pcm_ops->fini_in (hwi);
bellard85571bc2004-11-07 18:04:02 +00001803 }
bellardc0fe3822005-11-05 18:55:28 +00001804
1805 if (s->drv) {
1806 s->drv->fini (s->drv_opaque);
1807 }
bellard85571bc2004-11-07 18:04:02 +00001808}
1809
Juan Quintelad959fce2009-12-02 11:49:34 +01001810static const VMStateDescription vmstate_audio = {
1811 .name = "audio",
1812 .version_id = 1,
1813 .minimum_version_id = 1,
1814 .minimum_version_id_old = 1,
1815 .fields = (VMStateField []) {
1816 VMSTATE_END_OF_LIST()
bellard1d14ffa2005-10-30 18:58:22 +00001817 }
Juan Quintelad959fce2009-12-02 11:49:34 +01001818};
bellard85571bc2004-11-07 18:04:02 +00001819
malc1a7dafc2009-05-14 03:11:35 +04001820static void audio_init (void)
bellard85571bc2004-11-07 18:04:02 +00001821{
bellard1d14ffa2005-10-30 18:58:22 +00001822 size_t i;
bellard85571bc2004-11-07 18:04:02 +00001823 int done = 0;
1824 const char *drvname;
malc713a98f2009-09-12 02:28:45 +04001825 VMChangeStateEntry *e;
bellardc0fe3822005-11-05 18:55:28 +00001826 AudioState *s = &glob_audio_state;
bellard85571bc2004-11-07 18:04:02 +00001827
Paul Brook0d9acba2009-05-12 12:02:38 +01001828 if (s->drv) {
malc1a7dafc2009-05-14 03:11:35 +04001829 return;
Paul Brook0d9acba2009-05-12 12:02:38 +01001830 }
1831
Blue Swirl72cf2d42009-09-12 07:36:22 +00001832 QLIST_INIT (&s->hw_head_out);
1833 QLIST_INIT (&s->hw_head_in);
1834 QLIST_INIT (&s->cap_head);
bellard571ec3d2005-11-20 16:24:34 +00001835 atexit (audio_atexit);
1836
Alex Blighbc72ad62013-08-21 16:03:08 +01001837 s->ts = timer_new_ns(QEMU_CLOCK_VIRTUAL, audio_timer, s);
bellard571ec3d2005-11-20 16:24:34 +00001838 if (!s->ts) {
Paul Brook0d9acba2009-05-12 12:02:38 +01001839 hw_error("Could not create audio timer\n");
bellard571ec3d2005-11-20 16:24:34 +00001840 }
1841
bellard1d14ffa2005-10-30 18:58:22 +00001842 audio_process_options ("AUDIO", audio_options);
bellard85571bc2004-11-07 18:04:02 +00001843
bellardc0fe3822005-11-05 18:55:28 +00001844 s->nb_hw_voices_out = conf.fixed_out.nb_voices;
1845 s->nb_hw_voices_in = conf.fixed_in.nb_voices;
1846
bellard1d14ffa2005-10-30 18:58:22 +00001847 if (s->nb_hw_voices_out <= 0) {
bellard571ec3d2005-11-20 16:24:34 +00001848 dolog ("Bogus number of playback voices %d, setting to 1\n",
bellard1d14ffa2005-10-30 18:58:22 +00001849 s->nb_hw_voices_out);
1850 s->nb_hw_voices_out = 1;
bellard85571bc2004-11-07 18:04:02 +00001851 }
1852
bellard1d14ffa2005-10-30 18:58:22 +00001853 if (s->nb_hw_voices_in <= 0) {
bellard571ec3d2005-11-20 16:24:34 +00001854 dolog ("Bogus number of capture voices %d, setting to 0\n",
bellard1d14ffa2005-10-30 18:58:22 +00001855 s->nb_hw_voices_in);
bellard571ec3d2005-11-20 16:24:34 +00001856 s->nb_hw_voices_in = 0;
bellard1d14ffa2005-10-30 18:58:22 +00001857 }
1858
1859 {
1860 int def;
1861 drvname = audio_get_conf_str ("QEMU_AUDIO_DRV", NULL, &def);
1862 }
1863
bellard85571bc2004-11-07 18:04:02 +00001864 if (drvname) {
1865 int found = 0;
bellard1d14ffa2005-10-30 18:58:22 +00001866
malcb1503cd2008-12-22 20:33:55 +00001867 for (i = 0; i < ARRAY_SIZE (drvtab); i++) {
bellard85571bc2004-11-07 18:04:02 +00001868 if (!strcmp (drvname, drvtab[i]->name)) {
bellardc0fe3822005-11-05 18:55:28 +00001869 done = !audio_driver_init (s, drvtab[i]);
bellard85571bc2004-11-07 18:04:02 +00001870 found = 1;
1871 break;
1872 }
1873 }
bellard1d14ffa2005-10-30 18:58:22 +00001874
bellard85571bc2004-11-07 18:04:02 +00001875 if (!found) {
1876 dolog ("Unknown audio driver `%s'\n", drvname);
bellard1d14ffa2005-10-30 18:58:22 +00001877 dolog ("Run with -audio-help to list available drivers\n");
bellard85571bc2004-11-07 18:04:02 +00001878 }
1879 }
1880
bellard85571bc2004-11-07 18:04:02 +00001881 if (!done) {
malcb1503cd2008-12-22 20:33:55 +00001882 for (i = 0; !done && i < ARRAY_SIZE (drvtab); i++) {
bellard1d14ffa2005-10-30 18:58:22 +00001883 if (drvtab[i]->can_be_default) {
bellardc0fe3822005-11-05 18:55:28 +00001884 done = !audio_driver_init (s, drvtab[i]);
bellard1d14ffa2005-10-30 18:58:22 +00001885 }
bellard85571bc2004-11-07 18:04:02 +00001886 }
1887 }
1888
bellard85571bc2004-11-07 18:04:02 +00001889 if (!done) {
bellardc0fe3822005-11-05 18:55:28 +00001890 done = !audio_driver_init (s, &no_audio_driver);
1891 if (!done) {
Paul Brook0d9acba2009-05-12 12:02:38 +01001892 hw_error("Could not initialize audio subsystem\n");
bellard1d14ffa2005-10-30 18:58:22 +00001893 }
1894 else {
bellardc0fe3822005-11-05 18:55:28 +00001895 dolog ("warning: Using timer based audio emulation\n");
bellard1d14ffa2005-10-30 18:58:22 +00001896 }
bellard85571bc2004-11-07 18:04:02 +00001897 }
bellard1d14ffa2005-10-30 18:58:22 +00001898
Paul Brook0d9acba2009-05-12 12:02:38 +01001899 if (conf.period.hertz <= 0) {
1900 if (conf.period.hertz < 0) {
1901 dolog ("warning: Timer period is negative - %d "
1902 "treating as zero\n",
1903 conf.period.hertz);
bellard1d14ffa2005-10-30 18:58:22 +00001904 }
Paul Brook0d9acba2009-05-12 12:02:38 +01001905 conf.period.ticks = 1;
1906 } else {
malc4f4cc0e2009-09-18 08:16:03 +04001907 conf.period.ticks =
1908 muldiv64 (1, get_ticks_per_sec (), conf.period.hertz);
bellard1d14ffa2005-10-30 18:58:22 +00001909 }
Paul Brook0d9acba2009-05-12 12:02:38 +01001910
1911 e = qemu_add_vm_change_state_handler (audio_vm_change_state_handler, s);
1912 if (!e) {
1913 dolog ("warning: Could not register change state handler\n"
1914 "(Audio can continue looping even after stopping the VM)\n");
bellard1d14ffa2005-10-30 18:58:22 +00001915 }
1916
Blue Swirl72cf2d42009-09-12 07:36:22 +00001917 QLIST_INIT (&s->card_head);
Alex Williamson0be71e32010-06-25 11:09:07 -06001918 vmstate_register (NULL, 0, &vmstate_audio, s);
bellard85571bc2004-11-07 18:04:02 +00001919}
bellard8ead62c2006-07-04 16:51:32 +00001920
malc1a7dafc2009-05-14 03:11:35 +04001921void AUD_register_card (const char *name, QEMUSoundCard *card)
1922{
1923 audio_init ();
Anthony Liguori7267c092011-08-20 22:09:37 -05001924 card->name = g_strdup (name);
malc1a7dafc2009-05-14 03:11:35 +04001925 memset (&card->entries, 0, sizeof (card->entries));
Blue Swirl72cf2d42009-09-12 07:36:22 +00001926 QLIST_INSERT_HEAD (&glob_audio_state.card_head, card, entries);
malc1a7dafc2009-05-14 03:11:35 +04001927}
1928
1929void AUD_remove_card (QEMUSoundCard *card)
1930{
Blue Swirl72cf2d42009-09-12 07:36:22 +00001931 QLIST_REMOVE (card, entries);
Anthony Liguori7267c092011-08-20 22:09:37 -05001932 g_free (card->name);
malc1a7dafc2009-05-14 03:11:35 +04001933}
1934
1935
bellardec36b692006-07-16 18:57:03 +00001936CaptureVoiceOut *AUD_add_capture (
malc1ea879e2008-12-03 22:48:44 +00001937 struct audsettings *as,
bellard8ead62c2006-07-04 16:51:32 +00001938 struct audio_capture_ops *ops,
1939 void *cb_opaque
1940 )
1941{
malc1a7dafc2009-05-14 03:11:35 +04001942 AudioState *s = &glob_audio_state;
bellard8ead62c2006-07-04 16:51:32 +00001943 CaptureVoiceOut *cap;
1944 struct capture_callback *cb;
1945
bellardec36b692006-07-16 18:57:03 +00001946 if (audio_validate_settings (as)) {
bellard8ead62c2006-07-04 16:51:32 +00001947 dolog ("Invalid settings were passed when trying to add capture\n");
1948 audio_print_settings (as);
bellardec36b692006-07-16 18:57:03 +00001949 goto err0;
bellard8ead62c2006-07-04 16:51:32 +00001950 }
1951
1952 cb = audio_calloc (AUDIO_FUNC, 1, sizeof (*cb));
1953 if (!cb) {
1954 dolog ("Could not allocate capture callback information, size %zu\n",
1955 sizeof (*cb));
1956 goto err0;
1957 }
1958 cb->ops = *ops;
1959 cb->opaque = cb_opaque;
1960
malc1a7dafc2009-05-14 03:11:35 +04001961 cap = audio_pcm_capture_find_specific (as);
bellard8ead62c2006-07-04 16:51:32 +00001962 if (cap) {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001963 QLIST_INSERT_HEAD (&cap->cb_head, cb, entries);
bellardec36b692006-07-16 18:57:03 +00001964 return cap;
bellard8ead62c2006-07-04 16:51:32 +00001965 }
1966 else {
1967 HWVoiceOut *hw;
1968 CaptureVoiceOut *cap;
1969
1970 cap = audio_calloc (AUDIO_FUNC, 1, sizeof (*cap));
1971 if (!cap) {
1972 dolog ("Could not allocate capture voice, size %zu\n",
1973 sizeof (*cap));
1974 goto err1;
1975 }
1976
1977 hw = &cap->hw;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001978 QLIST_INIT (&hw->sw_head);
1979 QLIST_INIT (&cap->cb_head);
bellard8ead62c2006-07-04 16:51:32 +00001980
1981 /* XXX find a more elegant way */
1982 hw->samples = 4096 * 4;
1983 hw->mix_buf = audio_calloc (AUDIO_FUNC, hw->samples,
malc1ea879e2008-12-03 22:48:44 +00001984 sizeof (struct st_sample));
bellard8ead62c2006-07-04 16:51:32 +00001985 if (!hw->mix_buf) {
1986 dolog ("Could not allocate capture mix buffer (%d samples)\n",
1987 hw->samples);
1988 goto err2;
1989 }
1990
bellardd929eba2006-07-04 21:47:22 +00001991 audio_pcm_init_info (&hw->info, as);
bellard8ead62c2006-07-04 16:51:32 +00001992
1993 cap->buf = audio_calloc (AUDIO_FUNC, hw->samples, 1 << hw->info.shift);
1994 if (!cap->buf) {
1995 dolog ("Could not allocate capture buffer "
1996 "(%d samples, each %d bytes)\n",
1997 hw->samples, 1 << hw->info.shift);
1998 goto err3;
1999 }
2000
2001 hw->clip = mixeng_clip
2002 [hw->info.nchannels == 2]
2003 [hw->info.sign]
bellardd929eba2006-07-04 21:47:22 +00002004 [hw->info.swap_endianness]
thsf941aa22007-02-17 22:19:29 +00002005 [audio_bits_to_index (hw->info.bits)];
bellard8ead62c2006-07-04 16:51:32 +00002006
Blue Swirl72cf2d42009-09-12 07:36:22 +00002007 QLIST_INSERT_HEAD (&s->cap_head, cap, entries);
2008 QLIST_INSERT_HEAD (&cap->cb_head, cb, entries);
bellard8ead62c2006-07-04 16:51:32 +00002009
2010 hw = NULL;
malc1a7dafc2009-05-14 03:11:35 +04002011 while ((hw = audio_pcm_hw_find_any_out (hw))) {
2012 audio_attach_capture (hw);
bellard8ead62c2006-07-04 16:51:32 +00002013 }
bellardec36b692006-07-16 18:57:03 +00002014 return cap;
bellard8ead62c2006-07-04 16:51:32 +00002015
2016 err3:
Anthony Liguori7267c092011-08-20 22:09:37 -05002017 g_free (cap->hw.mix_buf);
bellard8ead62c2006-07-04 16:51:32 +00002018 err2:
Anthony Liguori7267c092011-08-20 22:09:37 -05002019 g_free (cap);
bellard8ead62c2006-07-04 16:51:32 +00002020 err1:
Anthony Liguori7267c092011-08-20 22:09:37 -05002021 g_free (cb);
bellard8ead62c2006-07-04 16:51:32 +00002022 err0:
bellardec36b692006-07-16 18:57:03 +00002023 return NULL;
2024 }
2025}
2026
2027void AUD_del_capture (CaptureVoiceOut *cap, void *cb_opaque)
2028{
2029 struct capture_callback *cb;
2030
2031 for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
2032 if (cb->opaque == cb_opaque) {
2033 cb->ops.destroy (cb_opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00002034 QLIST_REMOVE (cb, entries);
Anthony Liguori7267c092011-08-20 22:09:37 -05002035 g_free (cb);
bellardec36b692006-07-16 18:57:03 +00002036
2037 if (!cap->cb_head.lh_first) {
2038 SWVoiceOut *sw = cap->hw.sw_head.lh_first, *sw1;
bellarda3c25992006-07-18 21:09:59 +00002039
bellardec36b692006-07-16 18:57:03 +00002040 while (sw) {
bellarda3c25992006-07-18 21:09:59 +00002041 SWVoiceCap *sc = (SWVoiceCap *) sw;
bellardec36b692006-07-16 18:57:03 +00002042#ifdef DEBUG_CAPTURE
2043 dolog ("freeing %s\n", sw->name);
2044#endif
bellarda3c25992006-07-18 21:09:59 +00002045
bellardec36b692006-07-16 18:57:03 +00002046 sw1 = sw->entries.le_next;
2047 if (sw->rate) {
2048 st_rate_stop (sw->rate);
2049 sw->rate = NULL;
2050 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00002051 QLIST_REMOVE (sw, entries);
2052 QLIST_REMOVE (sc, entries);
Anthony Liguori7267c092011-08-20 22:09:37 -05002053 g_free (sc);
bellardec36b692006-07-16 18:57:03 +00002054 sw = sw1;
2055 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00002056 QLIST_REMOVE (cap, entries);
Anthony Liguori7267c092011-08-20 22:09:37 -05002057 g_free (cap);
bellardec36b692006-07-16 18:57:03 +00002058 }
2059 return;
2060 }
bellard8ead62c2006-07-04 16:51:32 +00002061 }
2062}
balrog683efdc2008-05-04 10:21:03 +00002063
2064void AUD_set_volume_out (SWVoiceOut *sw, int mute, uint8_t lvol, uint8_t rvol)
2065{
2066 if (sw) {
Marc-André Lureau6c95ab92012-04-17 14:32:35 +02002067 HWVoiceOut *hw = sw->hw;
2068
balrog683efdc2008-05-04 10:21:03 +00002069 sw->vol.mute = mute;
2070 sw->vol.l = nominal_volume.l * lvol / 255;
2071 sw->vol.r = nominal_volume.r * rvol / 255;
Marc-André Lureau6c95ab92012-04-17 14:32:35 +02002072
2073 if (hw->pcm_ops->ctl_out) {
2074 hw->pcm_ops->ctl_out (hw, VOICE_VOLUME, sw);
2075 }
balrog683efdc2008-05-04 10:21:03 +00002076 }
2077}
2078
2079void AUD_set_volume_in (SWVoiceIn *sw, int mute, uint8_t lvol, uint8_t rvol)
2080{
2081 if (sw) {
Marc-André Lureau6c95ab92012-04-17 14:32:35 +02002082 HWVoiceIn *hw = sw->hw;
2083
balrog683efdc2008-05-04 10:21:03 +00002084 sw->vol.mute = mute;
2085 sw->vol.l = nominal_volume.l * lvol / 255;
2086 sw->vol.r = nominal_volume.r * rvol / 255;
Marc-André Lureau6c95ab92012-04-17 14:32:35 +02002087
2088 if (hw->pcm_ops->ctl_in) {
2089 hw->pcm_ops->ctl_in (hw, VOICE_VOLUME, sw);
2090 }
balrog683efdc2008-05-04 10:21:03 +00002091 }
2092}