]> git.proxmox.com Git - qemu.git/blame - audio/audio.c
Merge remote branch 'qmp/for-anthony' into staging
[qemu.git] / audio / audio.c
CommitLineData
85571bc7
FB
1/*
2 * QEMU Audio subsystem
1d14ffa9
FB
3 *
4 * Copyright (c) 2003-2005 Vassili Karpov (malc)
5 *
85571bc7
FB
6 * 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 */
87ecb68b
PB
24#include "hw/hw.h"
25#include "audio.h"
376253ec 26#include "monitor.h"
87ecb68b
PB
27#include "qemu-timer.h"
28#include "sysemu.h"
85571bc7 29
1d14ffa9
FB
30#define AUDIO_CAP "audio"
31#include "audio_int.h"
85571bc7 32
1d14ffa9
FB
33/* #define DEBUG_PLIVE */
34/* #define DEBUG_LIVE */
35/* #define DEBUG_OUT */
8ead62cf 36/* #define DEBUG_CAPTURE */
713a98f8 37/* #define DEBUG_POLL */
85571bc7 38
c0fe3827
FB
39#define SW_NAME(sw) (sw)->name ? (sw)->name : "unknown"
40
2358a494
JQ
41
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*/
1d14ffa9 46static struct audio_driver *drvtab[] = {
3e313753
GH
47#ifdef CONFIG_SPICE
48 &spice_audio_driver,
49#endif
2358a494 50 CONFIG_AUDIO_DRIVERS
1d14ffa9
FB
51 &no_audio_driver,
52 &wav_audio_driver
53};
85571bc7 54
c0fe3827
FB
55struct fixed_settings {
56 int enabled;
57 int nb_voices;
58 int greedy;
1ea879e5 59 struct audsettings settings;
c0fe3827
FB
60};
61
62static struct {
63 struct fixed_settings fixed_out;
64 struct fixed_settings fixed_in;
65 union {
c310de86 66 int hertz;
c0fe3827
FB
67 int64_t ticks;
68 } period;
69 int plive;
541e0844 70 int log_to_monitor;
713a98f8 71 int try_poll_in;
72 int try_poll_out;
c0fe3827 73} conf = {
14658cd1
JQ
74 .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,
c0fe3827
FB
83 }
84 },
85
14658cd1
JQ
86 .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,
c0fe3827
FB
95 }
96 },
97
aea86747 98 .period = { .hertz = 250 },
14658cd1
JQ
99 .plive = 0,
100 .log_to_monitor = 0,
713a98f8 101 .try_poll_in = 1,
102 .try_poll_out = 1,
1d14ffa9
FB
103};
104
c0fe3827
FB
105static AudioState glob_audio_state;
106
1ea879e5 107struct mixeng_volume nominal_volume = {
14658cd1 108 .mute = 0,
1d14ffa9 109#ifdef FLOAT_MIXENG
14658cd1
JQ
110 .r = 1.0,
111 .l = 1.0,
1d14ffa9 112#else
14658cd1
JQ
113 .r = 1ULL << 32,
114 .l = 1ULL << 32,
1d14ffa9 115#endif
85571bc7
FB
116};
117
1d14ffa9
FB
118#ifdef AUDIO_IS_FLAWLESS_AND_NO_CHECKS_ARE_REQURIED
119#error No its not
120#else
82584e21 121static void audio_print_options (const char *prefix,
122 struct audio_option *opt);
123
1d14ffa9 124int audio_bug (const char *funcname, int cond)
85571bc7 125{
1d14ffa9
FB
126 if (cond) {
127 static int shown;
128
8ead62cf 129 AUD_log (NULL, "A bug was just triggered in %s\n", funcname);
1d14ffa9 130 if (!shown) {
82584e21 131 struct audio_driver *d;
132
1d14ffa9
FB
133 shown = 1;
134 AUD_log (NULL, "Save all your work and restart without audio\n");
155a8ad3 135 AUD_log (NULL, "Please send bug report to av1474@comtv.ru\n");
1d14ffa9 136 AUD_log (NULL, "I am sorry\n");
82584e21 137 d = glob_audio_state.drv;
138 if (d) {
139 audio_print_options (d->name, d->options);
140 }
1d14ffa9
FB
141 }
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
85571bc7
FB
157 }
158
1d14ffa9 159 return cond;
85571bc7 160}
1d14ffa9 161#endif
85571bc7 162
f941aa25
TS
163static 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
c0fe3827
FB
182void *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);
541e0844 195 AUD_log (NULL, "nmemb=%d size=%zu (len=%zu)\n", nmemb, size, len);
c0fe3827
FB
196 return NULL;
197 }
198
199 return qemu_mallocz (len);
200}
201
1d14ffa9 202static char *audio_alloc_prefix (const char *s)
85571bc7 203{
1d14ffa9 204 const char qemu_prefix[] = "QEMU_";
090f1fa3
AL
205 size_t len, i;
206 char *r, *u;
85571bc7 207
1d14ffa9
FB
208 if (!s) {
209 return NULL;
210 }
85571bc7 211
1d14ffa9 212 len = strlen (s);
a3772d4d 213 r = qemu_malloc (len + sizeof (qemu_prefix));
85571bc7 214
090f1fa3 215 u = r + sizeof (qemu_prefix) - 1;
85571bc7 216
090f1fa3
AL
217 pstrcpy (r, len + sizeof (qemu_prefix), qemu_prefix);
218 pstrcat (r, len + sizeof (qemu_prefix), s);
1d14ffa9 219
090f1fa3
AL
220 for (i = 0; i < len; ++i) {
221 u[i] = qemu_toupper(u[i]);
85571bc7 222 }
090f1fa3 223
1d14ffa9 224 return r;
85571bc7
FB
225}
226
9596ebb7 227static const char *audio_audfmt_to_string (audfmt_e fmt)
85571bc7 228{
1d14ffa9
FB
229 switch (fmt) {
230 case AUD_FMT_U8:
231 return "U8";
85571bc7 232
1d14ffa9
FB
233 case AUD_FMT_U16:
234 return "U16";
85571bc7 235
85571bc7 236 case AUD_FMT_S8:
1d14ffa9 237 return "S8";
85571bc7
FB
238
239 case AUD_FMT_S16:
1d14ffa9 240 return "S16";
f941aa25
TS
241
242 case AUD_FMT_U32:
243 return "U32";
244
245 case AUD_FMT_S32:
246 return "S32";
85571bc7
FB
247 }
248
1d14ffa9
FB
249 dolog ("Bogus audfmt %d returning S16\n", fmt);
250 return "S16";
85571bc7
FB
251}
252
9596ebb7
PB
253static audfmt_e audio_string_to_audfmt (const char *s, audfmt_e defval,
254 int *defaultp)
85571bc7 255{
1d14ffa9
FB
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 }
f941aa25
TS
264 else if (!strcasecmp (s, "u32")) {
265 *defaultp = 0;
266 return AUD_FMT_U32;
267 }
1d14ffa9
FB
268 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 }
f941aa25
TS
276 else if (!strcasecmp (s, "s32")) {
277 *defaultp = 0;
278 return AUD_FMT_S32;
279 }
1d14ffa9
FB
280 else {
281 dolog ("Bogus audio format `%s' using %s\n",
282 s, audio_audfmt_to_string (defval));
283 *defaultp = 1;
284 return defval;
285 }
85571bc7
FB
286}
287
1d14ffa9
FB
288static audfmt_e audio_get_conf_fmt (const char *envname,
289 audfmt_e defval,
290 int *defaultp)
85571bc7 291{
1d14ffa9
FB
292 const char *var = getenv (envname);
293 if (!var) {
294 *defaultp = 1;
295 return defval;
85571bc7 296 }
1d14ffa9 297 return audio_string_to_audfmt (var, defval, defaultp);
85571bc7
FB
298}
299
1d14ffa9 300static int audio_get_conf_int (const char *key, int defval, int *defaultp)
85571bc7 301{
1d14ffa9
FB
302 int val;
303 char *strval;
85571bc7 304
1d14ffa9
FB
305 strval = getenv (key);
306 if (strval) {
307 *defaultp = 0;
308 val = atoi (strval);
309 return val;
310 }
311 else {
312 *defaultp = 1;
313 return defval;
314 }
85571bc7
FB
315}
316
1d14ffa9
FB
317static const char *audio_get_conf_str (const char *key,
318 const char *defval,
319 int *defaultp)
85571bc7 320{
1d14ffa9
FB
321 const char *val = getenv (key);
322 if (!val) {
323 *defaultp = 1;
324 return defval;
325 }
326 else {
327 *defaultp = 0;
328 return val;
85571bc7 329 }
85571bc7
FB
330}
331
541e0844 332void AUD_vlog (const char *cap, const char *fmt, va_list ap)
85571bc7 333{
541e0844
FB
334 if (conf.log_to_monitor) {
335 if (cap) {
8631b608 336 monitor_printf(default_mon, "%s: ", cap);
541e0844
FB
337 }
338
8631b608 339 monitor_vprintf(default_mon, fmt, ap);
541e0844
FB
340 }
341 else {
342 if (cap) {
343 fprintf (stderr, "%s: ", cap);
344 }
345
346 vfprintf (stderr, fmt, ap);
85571bc7 347 }
85571bc7
FB
348}
349
541e0844 350void AUD_log (const char *cap, const char *fmt, ...)
85571bc7 351{
541e0844
FB
352 va_list ap;
353
354 va_start (ap, fmt);
355 AUD_vlog (cap, fmt, ap);
356 va_end (ap);
85571bc7
FB
357}
358
1d14ffa9
FB
359static void audio_print_options (const char *prefix,
360 struct audio_option *opt)
85571bc7 361{
1d14ffa9
FB
362 char *uprefix;
363
364 if (!prefix) {
365 dolog ("No prefix specified\n");
366 return;
367 }
368
369 if (!opt) {
370 dolog ("No options\n");
85571bc7 371 return;
1d14ffa9 372 }
85571bc7 373
1d14ffa9 374 uprefix = audio_alloc_prefix (prefix);
85571bc7 375
1d14ffa9
FB
376 for (; opt->name; opt++) {
377 const char *state = "default";
378 printf (" %s_%s: ", uprefix, opt->name);
85571bc7 379
fe8f096b 380 if (opt->overriddenp && *opt->overriddenp) {
1d14ffa9
FB
381 state = "current";
382 }
85571bc7 383
1d14ffa9
FB
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 (
ca9cc28c 403 "format, %s = %s, (one of: U8 S8 U16 S16 U32 S32)\n",
1d14ffa9
FB
404 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)");
85571bc7 416 }
1d14ffa9
FB
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;
85571bc7 424 }
1d14ffa9 425 printf (" %s\n", opt->descr);
85571bc7 426 }
1d14ffa9
FB
427
428 qemu_free (uprefix);
85571bc7
FB
429}
430
1d14ffa9
FB
431static void audio_process_options (const char *prefix,
432 struct audio_option *opt)
85571bc7 433{
1d14ffa9
FB
434 char *optname;
435 const char qemu_prefix[] = "QEMU_";
363a37d5 436 size_t preflen, optlen;
85571bc7 437
1d14ffa9
FB
438 if (audio_bug (AUDIO_FUNC, !prefix)) {
439 dolog ("prefix = NULL\n");
440 return;
441 }
85571bc7 442
1d14ffa9
FB
443 if (audio_bug (AUDIO_FUNC, !opt)) {
444 dolog ("opt = NULL\n");
445 return;
85571bc7 446 }
85571bc7 447
1d14ffa9 448 preflen = strlen (prefix);
85571bc7 449
1d14ffa9
FB
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);
c0fe3827
FB
461 /* len of opt->name + len of prefix + size of qemu_prefix
462 * (includes trailing zero) + zero + underscore (on behalf of
463 * sizeof) */
363a37d5
BS
464 optlen = len + preflen + sizeof (qemu_prefix) + 1;
465 optname = qemu_malloc (optlen);
1d14ffa9 466
363a37d5 467 pstrcpy (optname, optlen, qemu_prefix);
c0fe3827
FB
468
469 /* copy while upper-casing, including trailing zero */
1d14ffa9 470 for (i = 0; i <= preflen; ++i) {
cd390083 471 optname[i + sizeof (qemu_prefix) - 1] = qemu_toupper(prefix[i]);
1d14ffa9 472 }
363a37d5 473 pstrcat (optname, optlen, "_");
363a37d5 474 pstrcat (optname, optlen, opt->name);
1d14ffa9
FB
475
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);
85571bc7
FB
503 break;
504 }
85571bc7 505
fe8f096b
TS
506 if (!opt->overriddenp) {
507 opt->overriddenp = &opt->overridden;
1d14ffa9 508 }
fe8f096b 509 *opt->overriddenp = !def;
1d14ffa9
FB
510 qemu_free (optname);
511 }
85571bc7
FB
512}
513
1ea879e5 514static void audio_print_settings (struct audsettings *as)
c0fe3827
FB
515{
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;
d50997f9 531 case AUD_FMT_S32:
532 AUD_log (NULL, "S32");
533 break;
534 case AUD_FMT_U32:
535 AUD_log (NULL, "U32");
536 break;
c0fe3827
FB
537 default:
538 AUD_log (NULL, "invalid(%d)", as->fmt);
539 break;
540 }
ec36b695
FB
541
542 AUD_log (NULL, " endianness=");
d929eba5
FB
543 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 }
c0fe3827
FB
554 AUD_log (NULL, "\n");
555}
556
1ea879e5 557static int audio_validate_settings (struct audsettings *as)
c0fe3827
FB
558{
559 int invalid;
560
561 invalid = as->nchannels != 1 && as->nchannels != 2;
d929eba5 562 invalid |= as->endianness != 0 && as->endianness != 1;
c0fe3827
FB
563
564 switch (as->fmt) {
565 case AUD_FMT_S8:
566 case AUD_FMT_U8:
567 case AUD_FMT_S16:
568 case AUD_FMT_U16:
f941aa25
TS
569 case AUD_FMT_S32:
570 case AUD_FMT_U32:
c0fe3827
FB
571 break;
572 default:
573 invalid = 1;
574 break;
575 }
576
577 invalid |= as->freq <= 0;
d929eba5 578 return invalid ? -1 : 0;
c0fe3827
FB
579}
580
1ea879e5 581static int audio_pcm_info_eq (struct audio_pcm_info *info, struct audsettings *as)
85571bc7 582{
1d14ffa9 583 int bits = 8, sign = 0;
85571bc7 584
c0fe3827 585 switch (as->fmt) {
1d14ffa9
FB
586 case AUD_FMT_S8:
587 sign = 1;
588 case AUD_FMT_U8:
589 break;
590
591 case AUD_FMT_S16:
592 sign = 1;
593 case AUD_FMT_U16:
594 bits = 16;
595 break;
f941aa25
TS
596
597 case AUD_FMT_S32:
598 sign = 1;
599 case AUD_FMT_U32:
600 bits = 32;
601 break;
85571bc7 602 }
c0fe3827
FB
603 return info->freq == as->freq
604 && info->nchannels == as->nchannels
1d14ffa9 605 && info->sign == sign
d929eba5
FB
606 && info->bits == bits
607 && info->swap_endianness == (as->endianness != AUDIO_HOST_ENDIANNESS);
1d14ffa9 608}
85571bc7 609
1ea879e5 610void audio_pcm_init_info (struct audio_pcm_info *info, struct audsettings *as)
1d14ffa9 611{
f941aa25 612 int bits = 8, sign = 0, shift = 0;
1d14ffa9 613
c0fe3827 614 switch (as->fmt) {
85571bc7
FB
615 case AUD_FMT_S8:
616 sign = 1;
617 case AUD_FMT_U8:
618 break;
619
620 case AUD_FMT_S16:
621 sign = 1;
622 case AUD_FMT_U16:
623 bits = 16;
f941aa25
TS
624 shift = 1;
625 break;
626
627 case AUD_FMT_S32:
628 sign = 1;
629 case AUD_FMT_U32:
630 bits = 32;
631 shift = 2;
85571bc7
FB
632 break;
633 }
634
c0fe3827 635 info->freq = as->freq;
1d14ffa9
FB
636 info->bits = bits;
637 info->sign = sign;
c0fe3827 638 info->nchannels = as->nchannels;
f941aa25 639 info->shift = (as->nchannels == 2) + shift;
1d14ffa9
FB
640 info->align = (1 << info->shift) - 1;
641 info->bytes_per_second = info->freq << info->shift;
d929eba5 642 info->swap_endianness = (as->endianness != AUDIO_HOST_ENDIANNESS);
85571bc7
FB
643}
644
1d14ffa9 645void audio_pcm_info_clear_buf (struct audio_pcm_info *info, void *buf, int len)
85571bc7 646{
1d14ffa9
FB
647 if (!len) {
648 return;
649 }
650
651 if (info->sign) {
e2f909be 652 memset (buf, 0x00, len << info->shift);
85571bc7
FB
653 }
654 else {
f941aa25
TS
655 switch (info->bits) {
656 case 8:
e2f909be 657 memset (buf, 0x80, len << info->shift);
f941aa25 658 break;
1d14ffa9 659
f941aa25
TS
660 case 16:
661 {
662 int i;
663 uint16_t *p = buf;
664 int shift = info->nchannels - 1;
665 short s = INT16_MAX;
666
667 if (info->swap_endianness) {
668 s = bswap16 (s);
669 }
670
671 for (i = 0; i < len << shift; i++) {
672 p[i] = s;
673 }
1d14ffa9 674 }
f941aa25
TS
675 break;
676
677 case 32:
678 {
679 int i;
680 uint32_t *p = buf;
681 int shift = info->nchannels - 1;
682 int32_t s = INT32_MAX;
683
684 if (info->swap_endianness) {
685 s = bswap32 (s);
686 }
1d14ffa9 687
f941aa25
TS
688 for (i = 0; i < len << shift; i++) {
689 p[i] = s;
690 }
1d14ffa9 691 }
f941aa25
TS
692 break;
693
694 default:
695 AUD_log (NULL, "audio_pcm_info_clear_buf: invalid bits %d\n",
696 info->bits);
697 break;
1d14ffa9 698 }
85571bc7
FB
699 }
700}
701
8ead62cf
FB
702/*
703 * Capture
704 */
1ea879e5 705static void noop_conv (struct st_sample *dst, const void *src,
706 int samples, struct mixeng_volume *vol)
8ead62cf
FB
707{
708 (void) src;
709 (void) dst;
710 (void) samples;
711 (void) vol;
712}
713
714static CaptureVoiceOut *audio_pcm_capture_find_specific (
1ea879e5 715 struct audsettings *as
8ead62cf
FB
716 )
717{
718 CaptureVoiceOut *cap;
1a7dafce 719 AudioState *s = &glob_audio_state;
8ead62cf
FB
720
721 for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) {
d929eba5 722 if (audio_pcm_info_eq (&cap->hw.info, as)) {
8ead62cf
FB
723 return cap;
724 }
725 }
726 return NULL;
727}
728
ec36b695 729static void audio_notify_capture (CaptureVoiceOut *cap, audcnotification_e cmd)
8ead62cf 730{
ec36b695
FB
731 struct capture_callback *cb;
732
733#ifdef DEBUG_CAPTURE
734 dolog ("notification %d sent\n", cmd);
735#endif
736 for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
737 cb->ops.notify (cb->opaque, cmd);
738 }
739}
8ead62cf 740
ec36b695
FB
741static void audio_capture_maybe_changed (CaptureVoiceOut *cap, int enabled)
742{
743 if (cap->hw.enabled != enabled) {
744 audcnotification_e cmd;
8ead62cf 745 cap->hw.enabled = enabled;
ec36b695
FB
746 cmd = enabled ? AUD_CNOTIFY_ENABLE : AUD_CNOTIFY_DISABLE;
747 audio_notify_capture (cap, cmd);
8ead62cf
FB
748 }
749}
750
751static void audio_recalc_and_notify_capture (CaptureVoiceOut *cap)
752{
753 HWVoiceOut *hw = &cap->hw;
754 SWVoiceOut *sw;
755 int enabled = 0;
756
ec36b695 757 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
8ead62cf
FB
758 if (sw->active) {
759 enabled = 1;
760 break;
761 }
762 }
ec36b695 763 audio_capture_maybe_changed (cap, enabled);
8ead62cf
FB
764}
765
766static void audio_detach_capture (HWVoiceOut *hw)
767{
ec36b695
FB
768 SWVoiceCap *sc = hw->cap_head.lh_first;
769
770 while (sc) {
771 SWVoiceCap *sc1 = sc->entries.le_next;
772 SWVoiceOut *sw = &sc->sw;
773 CaptureVoiceOut *cap = sc->cap;
774 int was_active = sw->active;
8ead62cf 775
8ead62cf
FB
776 if (sw->rate) {
777 st_rate_stop (sw->rate);
778 sw->rate = NULL;
779 }
780
72cf2d4f
BS
781 QLIST_REMOVE (sw, entries);
782 QLIST_REMOVE (sc, entries);
ec36b695
FB
783 qemu_free (sc);
784 if (was_active) {
785 /* We have removed soft voice from the capture:
786 this might have changed the overall status of the capture
787 since this might have been the only active voice */
788 audio_recalc_and_notify_capture (cap);
789 }
790 sc = sc1;
8ead62cf
FB
791 }
792}
793
1a7dafce 794static int audio_attach_capture (HWVoiceOut *hw)
8ead62cf 795{
1a7dafce 796 AudioState *s = &glob_audio_state;
8ead62cf
FB
797 CaptureVoiceOut *cap;
798
799 audio_detach_capture (hw);
800 for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) {
ec36b695 801 SWVoiceCap *sc;
8ead62cf 802 SWVoiceOut *sw;
ec36b695 803 HWVoiceOut *hw_cap = &cap->hw;
8ead62cf 804
ec36b695
FB
805 sc = audio_calloc (AUDIO_FUNC, 1, sizeof (*sc));
806 if (!sc) {
8ead62cf 807 dolog ("Could not allocate soft capture voice (%zu bytes)\n",
ec36b695 808 sizeof (*sc));
8ead62cf
FB
809 return -1;
810 }
811
ec36b695
FB
812 sc->cap = cap;
813 sw = &sc->sw;
8ead62cf 814 sw->hw = hw_cap;
ec36b695 815 sw->info = hw->info;
8ead62cf
FB
816 sw->empty = 1;
817 sw->active = hw->enabled;
818 sw->conv = noop_conv;
819 sw->ratio = ((int64_t) hw_cap->info.freq << 32) / sw->info.freq;
820 sw->rate = st_rate_start (sw->info.freq, hw_cap->info.freq);
821 if (!sw->rate) {
822 dolog ("Could not start rate conversion for `%s'\n", SW_NAME (sw));
823 qemu_free (sw);
824 return -1;
825 }
72cf2d4f
BS
826 QLIST_INSERT_HEAD (&hw_cap->sw_head, sw, entries);
827 QLIST_INSERT_HEAD (&hw->cap_head, sc, entries);
ec36b695
FB
828#ifdef DEBUG_CAPTURE
829 asprintf (&sw->name, "for %p %d,%d,%d",
830 hw, sw->info.freq, sw->info.bits, sw->info.nchannels);
831 dolog ("Added %s active = %d\n", sw->name, sw->active);
832#endif
8ead62cf 833 if (sw->active) {
ec36b695 834 audio_capture_maybe_changed (cap, 1);
8ead62cf
FB
835 }
836 }
837 return 0;
838}
839
1d14ffa9
FB
840/*
841 * Hard voice (capture)
842 */
c0fe3827 843static int audio_pcm_hw_find_min_in (HWVoiceIn *hw)
85571bc7 844{
1d14ffa9
FB
845 SWVoiceIn *sw;
846 int m = hw->total_samples_captured;
847
848 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
849 if (sw->active) {
850 m = audio_MIN (m, sw->total_hw_samples_acquired);
851 }
85571bc7 852 }
1d14ffa9 853 return m;
85571bc7
FB
854}
855
1d14ffa9 856int audio_pcm_hw_get_live_in (HWVoiceIn *hw)
85571bc7 857{
1d14ffa9
FB
858 int live = hw->total_samples_captured - audio_pcm_hw_find_min_in (hw);
859 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
860 dolog ("live=%d hw->samples=%d\n", live, hw->samples);
861 return 0;
85571bc7 862 }
1d14ffa9 863 return live;
85571bc7
FB
864}
865
ddabec73 866int audio_pcm_hw_clip_out (HWVoiceOut *hw, void *pcm_buf,
867 int live, int pending)
868{
869 int left = hw->samples - pending;
870 int len = audio_MIN (left, live);
871 int clipped = 0;
872
873 while (len) {
874 struct st_sample *src = hw->mix_buf + hw->rpos;
875 uint8_t *dst = advance (pcm_buf, hw->rpos << hw->info.shift);
876 int samples_till_end_of_buf = hw->samples - hw->rpos;
877 int samples_to_clip = audio_MIN (len, samples_till_end_of_buf);
878
879 hw->clip (dst, src, samples_to_clip);
880
881 hw->rpos = (hw->rpos + samples_to_clip) % hw->samples;
882 len -= samples_to_clip;
883 clipped += samples_to_clip;
884 }
885 return clipped;
886}
887
1d14ffa9
FB
888/*
889 * Soft voice (capture)
890 */
1d14ffa9
FB
891static int audio_pcm_sw_get_rpos_in (SWVoiceIn *sw)
892{
893 HWVoiceIn *hw = sw->hw;
894 int live = hw->total_samples_captured - sw->total_hw_samples_acquired;
895 int rpos;
896
897 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
898 dolog ("live=%d hw->samples=%d\n", live, hw->samples);
899 return 0;
900 }
901
902 rpos = hw->wpos - live;
903 if (rpos >= 0) {
904 return rpos;
85571bc7
FB
905 }
906 else {
1d14ffa9 907 return hw->samples + rpos;
85571bc7 908 }
85571bc7
FB
909}
910
1d14ffa9 911int audio_pcm_sw_read (SWVoiceIn *sw, void *buf, int size)
85571bc7 912{
1d14ffa9
FB
913 HWVoiceIn *hw = sw->hw;
914 int samples, live, ret = 0, swlim, isamp, osamp, rpos, total = 0;
1ea879e5 915 struct st_sample *src, *dst = sw->buf;
1d14ffa9
FB
916
917 rpos = audio_pcm_sw_get_rpos_in (sw) % hw->samples;
918
919 live = hw->total_samples_captured - sw->total_hw_samples_acquired;
920 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
921 dolog ("live_in=%d hw->samples=%d\n", live, hw->samples);
922 return 0;
923 }
924
925 samples = size >> sw->info.shift;
926 if (!live) {
927 return 0;
928 }
85571bc7 929
1d14ffa9
FB
930 swlim = (live * sw->ratio) >> 32;
931 swlim = audio_MIN (swlim, samples);
85571bc7 932
1d14ffa9
FB
933 while (swlim) {
934 src = hw->conv_buf + rpos;
935 isamp = hw->wpos - rpos;
936 /* XXX: <= ? */
937 if (isamp <= 0) {
938 isamp = hw->samples - rpos;
939 }
85571bc7 940
1d14ffa9
FB
941 if (!isamp) {
942 break;
943 }
944 osamp = swlim;
85571bc7 945
1d14ffa9
FB
946 if (audio_bug (AUDIO_FUNC, osamp < 0)) {
947 dolog ("osamp=%d\n", osamp);
c0fe3827 948 return 0;
1d14ffa9 949 }
85571bc7 950
1d14ffa9
FB
951 st_rate_flow (sw->rate, src, dst, &isamp, &osamp);
952 swlim -= osamp;
953 rpos = (rpos + isamp) % hw->samples;
954 dst += osamp;
955 ret += osamp;
956 total += isamp;
957 }
85571bc7 958
571ec3d6 959 sw->clip (buf, sw->buf, ret);
1d14ffa9
FB
960 sw->total_hw_samples_acquired += total;
961 return ret << sw->info.shift;
85571bc7
FB
962}
963
1d14ffa9
FB
964/*
965 * Hard voice (playback)
966 */
c0fe3827 967static int audio_pcm_hw_find_min_out (HWVoiceOut *hw, int *nb_livep)
1d14ffa9 968{
c0fe3827
FB
969 SWVoiceOut *sw;
970 int m = INT_MAX;
971 int nb_live = 0;
85571bc7 972
c0fe3827
FB
973 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
974 if (sw->active || !sw->empty) {
975 m = audio_MIN (m, sw->total_hw_samples_mixed);
976 nb_live += 1;
977 }
85571bc7 978 }
c0fe3827
FB
979
980 *nb_livep = nb_live;
981 return m;
1d14ffa9 982}
85571bc7 983
bdff253c 984static int audio_pcm_hw_get_live_out (HWVoiceOut *hw, int *nb_live)
1d14ffa9
FB
985{
986 int smin;
bdff253c 987 int nb_live1;
85571bc7 988
bdff253c 989 smin = audio_pcm_hw_find_min_out (hw, &nb_live1);
990 if (nb_live) {
991 *nb_live = nb_live1;
85571bc7 992 }
bdff253c 993
994 if (nb_live1) {
1d14ffa9
FB
995 int live = smin;
996
997 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
998 dolog ("live=%d hw->samples=%d\n", live, hw->samples);
999 return 0;
85571bc7 1000 }
1d14ffa9 1001 return live;
85571bc7 1002 }
bdff253c 1003 return 0;
85571bc7
FB
1004}
1005
1d14ffa9
FB
1006/*
1007 * Soft voice (playback)
1008 */
1d14ffa9 1009int audio_pcm_sw_write (SWVoiceOut *sw, void *buf, int size)
85571bc7 1010{
1d14ffa9
FB
1011 int hwsamples, samples, isamp, osamp, wpos, live, dead, left, swlim, blck;
1012 int ret = 0, pos = 0, total = 0;
85571bc7 1013
1d14ffa9
FB
1014 if (!sw) {
1015 return size;
1016 }
85571bc7 1017
1d14ffa9 1018 hwsamples = sw->hw->samples;
85571bc7 1019
1d14ffa9
FB
1020 live = sw->total_hw_samples_mixed;
1021 if (audio_bug (AUDIO_FUNC, live < 0 || live > hwsamples)){
1022 dolog ("live=%d hw->samples=%d\n", live, hwsamples);
1023 return 0;
1024 }
85571bc7 1025
1d14ffa9 1026 if (live == hwsamples) {
ec36b695
FB
1027#ifdef DEBUG_OUT
1028 dolog ("%s is full %d\n", sw->name, live);
1029#endif
1d14ffa9
FB
1030 return 0;
1031 }
85571bc7 1032
1d14ffa9
FB
1033 wpos = (sw->hw->rpos + live) % hwsamples;
1034 samples = size >> sw->info.shift;
85571bc7 1035
1d14ffa9
FB
1036 dead = hwsamples - live;
1037 swlim = ((int64_t) dead << 32) / sw->ratio;
1038 swlim = audio_MIN (swlim, samples);
1039 if (swlim) {
1040 sw->conv (sw->buf, buf, swlim, &sw->vol);
1041 }
1042
1043 while (swlim) {
1044 dead = hwsamples - live;
1045 left = hwsamples - wpos;
1046 blck = audio_MIN (dead, left);
1047 if (!blck) {
1048 break;
1049 }
1050 isamp = swlim;
1051 osamp = blck;
1052 st_rate_flow_mix (
1053 sw->rate,
1054 sw->buf + pos,
1055 sw->hw->mix_buf + wpos,
1056 &isamp,
1057 &osamp
1058 );
1059 ret += isamp;
1060 swlim -= isamp;
1061 pos += isamp;
1062 live += osamp;
1063 wpos = (wpos + osamp) % hwsamples;
1064 total += osamp;
1065 }
1066
1067 sw->total_hw_samples_mixed += total;
1068 sw->empty = sw->total_hw_samples_mixed == 0;
1069
1070#ifdef DEBUG_OUT
1071 dolog (
c0fe3827
FB
1072 "%s: write size %d ret %d total sw %d\n",
1073 SW_NAME (sw),
1d14ffa9
FB
1074 size >> sw->info.shift,
1075 ret,
c0fe3827 1076 sw->total_hw_samples_mixed
1d14ffa9
FB
1077 );
1078#endif
1079
1080 return ret << sw->info.shift;
85571bc7
FB
1081}
1082
1d14ffa9
FB
1083#ifdef DEBUG_AUDIO
1084static void audio_pcm_print_info (const char *cap, struct audio_pcm_info *info)
85571bc7 1085{
1d14ffa9
FB
1086 dolog ("%s: bits %d, sign %d, freq %d, nchan %d\n",
1087 cap, info->bits, info->sign, info->freq, info->nchannels);
85571bc7 1088}
1d14ffa9 1089#endif
85571bc7 1090
1d14ffa9
FB
1091#define DAC
1092#include "audio_template.h"
1093#undef DAC
1094#include "audio_template.h"
1095
713a98f8 1096/*
1097 * Timer
1098 */
713a98f8 1099static int audio_is_timer_needed (void)
1100{
1101 HWVoiceIn *hwi = NULL;
1102 HWVoiceOut *hwo = NULL;
1103
1104 while ((hwo = audio_pcm_hw_find_any_enabled_out (hwo))) {
1105 if (!hwo->poll_mode) return 1;
1106 }
1107 while ((hwi = audio_pcm_hw_find_any_enabled_in (hwi))) {
1108 if (!hwi->poll_mode) return 1;
1109 }
1110 return 0;
1111}
1112
39deb1e4 1113static void audio_reset_timer (AudioState *s)
713a98f8 1114{
713a98f8 1115 if (audio_is_timer_needed ()) {
1116 qemu_mod_timer (s->ts, qemu_get_clock (vm_clock) + 1);
1117 }
1118 else {
1119 qemu_del_timer (s->ts);
1120 }
1121}
1122
39deb1e4 1123static void audio_timer (void *opaque)
1124{
1125 audio_run ("timer");
1126 audio_reset_timer (opaque);
1127}
1128
713a98f8 1129/*
1130 * Public API
1131 */
1d14ffa9 1132int AUD_write (SWVoiceOut *sw, void *buf, int size)
85571bc7 1133{
1d14ffa9 1134 int bytes;
85571bc7 1135
1d14ffa9
FB
1136 if (!sw) {
1137 /* XXX: Consider options */
1138 return size;
1139 }
85571bc7 1140
1d14ffa9 1141 if (!sw->hw->enabled) {
c0fe3827 1142 dolog ("Writing to disabled voice %s\n", SW_NAME (sw));
85571bc7
FB
1143 return 0;
1144 }
1145
1d14ffa9
FB
1146 bytes = sw->hw->pcm_ops->write (sw, buf, size);
1147 return bytes;
1148}
1149
1150int AUD_read (SWVoiceIn *sw, void *buf, int size)
1151{
1152 int bytes;
85571bc7 1153
1d14ffa9
FB
1154 if (!sw) {
1155 /* XXX: Consider options */
1156 return size;
85571bc7 1157 }
1d14ffa9
FB
1158
1159 if (!sw->hw->enabled) {
c0fe3827 1160 dolog ("Reading from disabled voice %s\n", SW_NAME (sw));
1d14ffa9 1161 return 0;
85571bc7 1162 }
1d14ffa9
FB
1163
1164 bytes = sw->hw->pcm_ops->read (sw, buf, size);
1165 return bytes;
85571bc7
FB
1166}
1167
1d14ffa9 1168int AUD_get_buffer_size_out (SWVoiceOut *sw)
85571bc7 1169{
c0fe3827 1170 return sw->hw->samples << sw->hw->info.shift;
1d14ffa9
FB
1171}
1172
1173void AUD_set_active_out (SWVoiceOut *sw, int on)
1174{
1175 HWVoiceOut *hw;
85571bc7 1176
1d14ffa9 1177 if (!sw) {
85571bc7 1178 return;
1d14ffa9 1179 }
85571bc7
FB
1180
1181 hw = sw->hw;
1d14ffa9 1182 if (sw->active != on) {
978dd635 1183 AudioState *s = &glob_audio_state;
1d14ffa9 1184 SWVoiceOut *temp_sw;
ec36b695 1185 SWVoiceCap *sc;
1d14ffa9
FB
1186
1187 if (on) {
1d14ffa9
FB
1188 hw->pending_disable = 0;
1189 if (!hw->enabled) {
1190 hw->enabled = 1;
978dd635 1191 if (s->vm_running) {
713a98f8 1192 hw->pcm_ops->ctl_out (hw, VOICE_ENABLE, conf.try_poll_out);
39deb1e4 1193 audio_reset_timer (s);
978dd635 1194 }
1d14ffa9 1195 }
1d14ffa9
FB
1196 }
1197 else {
1198 if (hw->enabled) {
1199 int nb_active = 0;
1200
1201 for (temp_sw = hw->sw_head.lh_first; temp_sw;
1202 temp_sw = temp_sw->entries.le_next) {
1203 nb_active += temp_sw->active != 0;
1204 }
1205
1206 hw->pending_disable = nb_active == 1;
1207 }
85571bc7 1208 }
ec36b695
FB
1209
1210 for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) {
1211 sc->sw.active = hw->enabled;
8ead62cf 1212 if (hw->enabled) {
ec36b695 1213 audio_capture_maybe_changed (sc->cap, 1);
8ead62cf
FB
1214 }
1215 }
1d14ffa9
FB
1216 sw->active = on;
1217 }
1218}
1219
1220void AUD_set_active_in (SWVoiceIn *sw, int on)
1221{
1222 HWVoiceIn *hw;
1223
1224 if (!sw) {
1225 return;
85571bc7
FB
1226 }
1227
1d14ffa9 1228 hw = sw->hw;
85571bc7 1229 if (sw->active != on) {
978dd635 1230 AudioState *s = &glob_audio_state;
1d14ffa9
FB
1231 SWVoiceIn *temp_sw;
1232
85571bc7 1233 if (on) {
85571bc7
FB
1234 if (!hw->enabled) {
1235 hw->enabled = 1;
978dd635 1236 if (s->vm_running) {
713a98f8 1237 hw->pcm_ops->ctl_in (hw, VOICE_ENABLE, conf.try_poll_in);
39deb1e4 1238 audio_reset_timer (s);
978dd635 1239 }
85571bc7 1240 }
1d14ffa9 1241 sw->total_hw_samples_acquired = hw->total_samples_captured;
85571bc7
FB
1242 }
1243 else {
1d14ffa9 1244 if (hw->enabled) {
85571bc7 1245 int nb_active = 0;
1d14ffa9
FB
1246
1247 for (temp_sw = hw->sw_head.lh_first; temp_sw;
1248 temp_sw = temp_sw->entries.le_next) {
1249 nb_active += temp_sw->active != 0;
85571bc7
FB
1250 }
1251
1252 if (nb_active == 1) {
1d14ffa9
FB
1253 hw->enabled = 0;
1254 hw->pcm_ops->ctl_in (hw, VOICE_DISABLE);
85571bc7
FB
1255 }
1256 }
1257 }
1258 sw->active = on;
1259 }
1260}
1261
1d14ffa9
FB
1262static int audio_get_avail (SWVoiceIn *sw)
1263{
1264 int live;
1265
1266 if (!sw) {
1267 return 0;
1268 }
1269
1270 live = sw->hw->total_samples_captured - sw->total_hw_samples_acquired;
1271 if (audio_bug (AUDIO_FUNC, live < 0 || live > sw->hw->samples)) {
1272 dolog ("live=%d sw->hw->samples=%d\n", live, sw->hw->samples);
1273 return 0;
1274 }
1275
1276 ldebug (
26a76461 1277 "%s: get_avail live %d ret %" PRId64 "\n",
c0fe3827 1278 SW_NAME (sw),
1d14ffa9
FB
1279 live, (((int64_t) live << 32) / sw->ratio) << sw->info.shift
1280 );
1281
1282 return (((int64_t) live << 32) / sw->ratio) << sw->info.shift;
1283}
1284
1285static int audio_get_free (SWVoiceOut *sw)
1286{
1287 int live, dead;
1288
1289 if (!sw) {
1290 return 0;
1291 }
1292
1293 live = sw->total_hw_samples_mixed;
1294
1295 if (audio_bug (AUDIO_FUNC, live < 0 || live > sw->hw->samples)) {
1296 dolog ("live=%d sw->hw->samples=%d\n", live, sw->hw->samples);
c0fe3827 1297 return 0;
1d14ffa9
FB
1298 }
1299
1300 dead = sw->hw->samples - live;
1301
1302#ifdef DEBUG_OUT
26a76461 1303 dolog ("%s: get_free live %d dead %d ret %" PRId64 "\n",
c0fe3827 1304 SW_NAME (sw),
1d14ffa9 1305 live, dead, (((int64_t) dead << 32) / sw->ratio) << sw->info.shift);
85571bc7 1306#endif
1d14ffa9
FB
1307
1308 return (((int64_t) dead << 32) / sw->ratio) << sw->info.shift;
1309}
1310
8ead62cf
FB
1311static void audio_capture_mix_and_clear (HWVoiceOut *hw, int rpos, int samples)
1312{
1313 int n;
1314
1315 if (hw->enabled) {
ec36b695 1316 SWVoiceCap *sc;
8ead62cf 1317
ec36b695
FB
1318 for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) {
1319 SWVoiceOut *sw = &sc->sw;
8ead62cf
FB
1320 int rpos2 = rpos;
1321
1322 n = samples;
1323 while (n) {
1324 int till_end_of_hw = hw->samples - rpos2;
1325 int to_write = audio_MIN (till_end_of_hw, n);
1326 int bytes = to_write << hw->info.shift;
1327 int written;
1328
1329 sw->buf = hw->mix_buf + rpos2;
1330 written = audio_pcm_sw_write (sw, NULL, bytes);
1331 if (written - bytes) {
ec36b695
FB
1332 dolog ("Could not mix %d bytes into a capture "
1333 "buffer, mixed %d\n",
1334 bytes, written);
8ead62cf
FB
1335 break;
1336 }
1337 n -= to_write;
1338 rpos2 = (rpos2 + to_write) % hw->samples;
1339 }
1340 }
1341 }
1342
1343 n = audio_MIN (samples, hw->samples - rpos);
1344 mixeng_clear (hw->mix_buf + rpos, n);
1345 mixeng_clear (hw->mix_buf, samples - n);
1346}
1347
c0fe3827 1348static void audio_run_out (AudioState *s)
1d14ffa9
FB
1349{
1350 HWVoiceOut *hw = NULL;
1351 SWVoiceOut *sw;
1352
1a7dafce 1353 while ((hw = audio_pcm_hw_find_any_enabled_out (hw))) {
1d14ffa9 1354 int played;
8ead62cf 1355 int live, free, nb_live, cleanup_required, prev_rpos;
1d14ffa9 1356
bdff253c 1357 live = audio_pcm_hw_get_live_out (hw, &nb_live);
1d14ffa9
FB
1358 if (!nb_live) {
1359 live = 0;
1360 }
c0fe3827 1361
1d14ffa9
FB
1362 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
1363 dolog ("live=%d hw->samples=%d\n", live, hw->samples);
c0fe3827 1364 continue;
1d14ffa9
FB
1365 }
1366
1367 if (hw->pending_disable && !nb_live) {
ec36b695 1368 SWVoiceCap *sc;
1d14ffa9
FB
1369#ifdef DEBUG_OUT
1370 dolog ("Disabling voice\n");
85571bc7 1371#endif
1d14ffa9
FB
1372 hw->enabled = 0;
1373 hw->pending_disable = 0;
1374 hw->pcm_ops->ctl_out (hw, VOICE_DISABLE);
ec36b695
FB
1375 for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) {
1376 sc->sw.active = 0;
1377 audio_recalc_and_notify_capture (sc->cap);
8ead62cf 1378 }
1d14ffa9
FB
1379 continue;
1380 }
1381
1382 if (!live) {
1383 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1384 if (sw->active) {
1385 free = audio_get_free (sw);
1386 if (free > 0) {
1387 sw->callback.fn (sw->callback.opaque, free);
1388 }
1389 }
1390 }
1391 continue;
1392 }
1393
8ead62cf 1394 prev_rpos = hw->rpos;
bdff253c 1395 played = hw->pcm_ops->run_out (hw, live);
1d14ffa9
FB
1396 if (audio_bug (AUDIO_FUNC, hw->rpos >= hw->samples)) {
1397 dolog ("hw->rpos=%d hw->samples=%d played=%d\n",
1398 hw->rpos, hw->samples, played);
1399 hw->rpos = 0;
1400 }
1401
1402#ifdef DEBUG_OUT
c0fe3827 1403 dolog ("played=%d\n", played);
85571bc7 1404#endif
1d14ffa9
FB
1405
1406 if (played) {
1407 hw->ts_helper += played;
8ead62cf 1408 audio_capture_mix_and_clear (hw, prev_rpos, played);
1d14ffa9
FB
1409 }
1410
c0fe3827 1411 cleanup_required = 0;
1d14ffa9 1412 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1d14ffa9
FB
1413 if (!sw->active && sw->empty) {
1414 continue;
1415 }
1416
1417 if (audio_bug (AUDIO_FUNC, played > sw->total_hw_samples_mixed)) {
1418 dolog ("played=%d sw->total_hw_samples_mixed=%d\n",
1419 played, sw->total_hw_samples_mixed);
1420 played = sw->total_hw_samples_mixed;
1421 }
1422
1423 sw->total_hw_samples_mixed -= played;
1424
1425 if (!sw->total_hw_samples_mixed) {
1426 sw->empty = 1;
c0fe3827 1427 cleanup_required |= !sw->active && !sw->callback.fn;
1d14ffa9
FB
1428 }
1429
1430 if (sw->active) {
1431 free = audio_get_free (sw);
1432 if (free > 0) {
1433 sw->callback.fn (sw->callback.opaque, free);
1434 }
1435 }
1436 }
c0fe3827
FB
1437
1438 if (cleanup_required) {
ec36b695
FB
1439 SWVoiceOut *sw1;
1440
1441 sw = hw->sw_head.lh_first;
1442 while (sw) {
1443 sw1 = sw->entries.le_next;
c0fe3827
FB
1444 if (!sw->active && !sw->callback.fn) {
1445#ifdef DEBUG_PLIVE
1446 dolog ("Finishing with old voice\n");
1447#endif
1a7dafce 1448 audio_close_out (sw);
c0fe3827 1449 }
ec36b695 1450 sw = sw1;
c0fe3827
FB
1451 }
1452 }
1d14ffa9
FB
1453 }
1454}
1455
c0fe3827 1456static void audio_run_in (AudioState *s)
1d14ffa9
FB
1457{
1458 HWVoiceIn *hw = NULL;
1459
1a7dafce 1460 while ((hw = audio_pcm_hw_find_any_enabled_in (hw))) {
1d14ffa9
FB
1461 SWVoiceIn *sw;
1462 int captured, min;
1463
1464 captured = hw->pcm_ops->run_in (hw);
1465
1466 min = audio_pcm_hw_find_min_in (hw);
1467 hw->total_samples_captured += captured - min;
1468 hw->ts_helper += captured;
1469
1470 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1471 sw->total_hw_samples_acquired -= min;
1472
1473 if (sw->active) {
1474 int avail;
1475
1476 avail = audio_get_avail (sw);
1477 if (avail > 0) {
1478 sw->callback.fn (sw->callback.opaque, avail);
1479 }
1480 }
1481 }
1482 }
1483}
1484
8ead62cf
FB
1485static void audio_run_capture (AudioState *s)
1486{
1487 CaptureVoiceOut *cap;
1488
1489 for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) {
1490 int live, rpos, captured;
1491 HWVoiceOut *hw = &cap->hw;
1492 SWVoiceOut *sw;
1493
bdff253c 1494 captured = live = audio_pcm_hw_get_live_out (hw, NULL);
8ead62cf
FB
1495 rpos = hw->rpos;
1496 while (live) {
1497 int left = hw->samples - rpos;
1498 int to_capture = audio_MIN (live, left);
1ea879e5 1499 struct st_sample *src;
8ead62cf
FB
1500 struct capture_callback *cb;
1501
1502 src = hw->mix_buf + rpos;
1503 hw->clip (cap->buf, src, to_capture);
1504 mixeng_clear (src, to_capture);
1505
1506 for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
1507 cb->ops.capture (cb->opaque, cap->buf,
1508 to_capture << hw->info.shift);
1509 }
1510 rpos = (rpos + to_capture) % hw->samples;
1511 live -= to_capture;
1512 }
1513 hw->rpos = rpos;
1514
1515 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1516 if (!sw->active && sw->empty) {
1517 continue;
1518 }
1519
1520 if (audio_bug (AUDIO_FUNC, captured > sw->total_hw_samples_mixed)) {
1521 dolog ("captured=%d sw->total_hw_samples_mixed=%d\n",
1522 captured, sw->total_hw_samples_mixed);
1523 captured = sw->total_hw_samples_mixed;
1524 }
1525
1526 sw->total_hw_samples_mixed -= captured;
1527 sw->empty = sw->total_hw_samples_mixed == 0;
1528 }
1529 }
1530}
1531
713a98f8 1532void audio_run (const char *msg)
571ec3d6 1533{
713a98f8 1534 AudioState *s = &glob_audio_state;
571ec3d6
FB
1535
1536 audio_run_out (s);
1537 audio_run_in (s);
8ead62cf 1538 audio_run_capture (s);
713a98f8 1539#ifdef DEBUG_POLL
1540 {
1541 static double prevtime;
1542 double currtime;
1543 struct timeval tv;
571ec3d6 1544
713a98f8 1545 if (gettimeofday (&tv, NULL)) {
1546 perror ("audio_run: gettimeofday");
1547 return;
1548 }
1549
1550 currtime = tv.tv_sec + tv.tv_usec * 1e-6;
1551 dolog ("Elapsed since last %s: %f\n", msg, currtime - prevtime);
1552 prevtime = currtime;
1553 }
1554#endif
571ec3d6
FB
1555}
1556
1d14ffa9
FB
1557static struct audio_option audio_options[] = {
1558 /* DAC */
98f9f48c 1559 {
1560 .name = "DAC_FIXED_SETTINGS",
1561 .tag = AUD_OPT_BOOL,
1562 .valp = &conf.fixed_out.enabled,
1563 .descr = "Use fixed settings for host DAC"
1564 },
1565 {
1566 .name = "DAC_FIXED_FREQ",
1567 .tag = AUD_OPT_INT,
1568 .valp = &conf.fixed_out.settings.freq,
1569 .descr = "Frequency for fixed host DAC"
1570 },
1571 {
1572 .name = "DAC_FIXED_FMT",
1573 .tag = AUD_OPT_FMT,
1574 .valp = &conf.fixed_out.settings.fmt,
1575 .descr = "Format for fixed host DAC"
1576 },
1577 {
1578 .name = "DAC_FIXED_CHANNELS",
1579 .tag = AUD_OPT_INT,
1580 .valp = &conf.fixed_out.settings.nchannels,
1581 .descr = "Number of channels for fixed DAC (1 - mono, 2 - stereo)"
1582 },
1583 {
1584 .name = "DAC_VOICES",
1585 .tag = AUD_OPT_INT,
1586 .valp = &conf.fixed_out.nb_voices,
1587 .descr = "Number of voices for DAC"
1588 },
713a98f8 1589 {
1590 .name = "DAC_TRY_POLL",
1591 .tag = AUD_OPT_BOOL,
1592 .valp = &conf.try_poll_out,
1593 .descr = "Attempt using poll mode for DAC"
1594 },
1d14ffa9 1595 /* ADC */
98f9f48c 1596 {
1597 .name = "ADC_FIXED_SETTINGS",
1598 .tag = AUD_OPT_BOOL,
1599 .valp = &conf.fixed_in.enabled,
1600 .descr = "Use fixed settings for host ADC"
1601 },
1602 {
1603 .name = "ADC_FIXED_FREQ",
1604 .tag = AUD_OPT_INT,
1605 .valp = &conf.fixed_in.settings.freq,
1606 .descr = "Frequency for fixed host ADC"
1607 },
1608 {
1609 .name = "ADC_FIXED_FMT",
1610 .tag = AUD_OPT_FMT,
1611 .valp = &conf.fixed_in.settings.fmt,
1612 .descr = "Format for fixed host ADC"
1613 },
1614 {
1615 .name = "ADC_FIXED_CHANNELS",
1616 .tag = AUD_OPT_INT,
1617 .valp = &conf.fixed_in.settings.nchannels,
1618 .descr = "Number of channels for fixed ADC (1 - mono, 2 - stereo)"
1619 },
1620 {
1621 .name = "ADC_VOICES",
1622 .tag = AUD_OPT_INT,
1623 .valp = &conf.fixed_in.nb_voices,
1624 .descr = "Number of voices for ADC"
1625 },
713a98f8 1626 {
1627 .name = "ADC_TRY_POLL",
1628 .tag = AUD_OPT_BOOL,
0a90e344 1629 .valp = &conf.try_poll_in,
713a98f8 1630 .descr = "Attempt using poll mode for ADC"
1631 },
1d14ffa9 1632 /* Misc */
98f9f48c 1633 {
1634 .name = "TIMER_PERIOD",
1635 .tag = AUD_OPT_INT,
1636 .valp = &conf.period.hertz,
1637 .descr = "Timer period in HZ (0 - use lowest possible)"
1638 },
1639 {
1640 .name = "PLIVE",
1641 .tag = AUD_OPT_BOOL,
1642 .valp = &conf.plive,
1643 .descr = "(undocumented)"
1644 },
1645 {
1646 .name = "LOG_TO_MONITOR",
1647 .tag = AUD_OPT_BOOL,
1648 .valp = &conf.log_to_monitor,
713a98f8 1649 .descr = "Print logging messages to monitor instead of stderr"
98f9f48c 1650 },
2700efa3 1651 { /* End of list */ }
85571bc7
FB
1652};
1653
571ec3d6
FB
1654static void audio_pp_nb_voices (const char *typ, int nb)
1655{
1656 switch (nb) {
1657 case 0:
1658 printf ("Does not support %s\n", typ);
1659 break;
1660 case 1:
1661 printf ("One %s voice\n", typ);
1662 break;
1663 case INT_MAX:
1664 printf ("Theoretically supports many %s voices\n", typ);
1665 break;
1666 default:
1667 printf ("Theoretically supports upto %d %s voices\n", nb, typ);
1668 break;
1669 }
1670
1671}
1672
1d14ffa9
FB
1673void AUD_help (void)
1674{
1675 size_t i;
1676
1677 audio_process_options ("AUDIO", audio_options);
b1503cda 1678 for (i = 0; i < ARRAY_SIZE (drvtab); i++) {
1d14ffa9
FB
1679 struct audio_driver *d = drvtab[i];
1680 if (d->options) {
1681 audio_process_options (d->name, d->options);
1682 }
1683 }
1684
1685 printf ("Audio options:\n");
1686 audio_print_options ("AUDIO", audio_options);
1687 printf ("\n");
1688
1689 printf ("Available drivers:\n");
1690
b1503cda 1691 for (i = 0; i < ARRAY_SIZE (drvtab); i++) {
1d14ffa9
FB
1692 struct audio_driver *d = drvtab[i];
1693
1694 printf ("Name: %s\n", d->name);
1695 printf ("Description: %s\n", d->descr);
1696
571ec3d6
FB
1697 audio_pp_nb_voices ("playback", d->max_voices_out);
1698 audio_pp_nb_voices ("capture", d->max_voices_in);
1d14ffa9
FB
1699
1700 if (d->options) {
1701 printf ("Options:\n");
1702 audio_print_options (d->name, d->options);
1703 }
1704 else {
1705 printf ("No options\n");
1706 }
1707 printf ("\n");
1708 }
1709
1710 printf (
1711 "Options are settable through environment variables.\n"
1712 "Example:\n"
1713#ifdef _WIN32
1714 " set QEMU_AUDIO_DRV=wav\n"
571ec3d6 1715 " set QEMU_WAV_PATH=c:\\tune.wav\n"
1d14ffa9
FB
1716#else
1717 " export QEMU_AUDIO_DRV=wav\n"
1718 " export QEMU_WAV_PATH=$HOME/tune.wav\n"
1719 "(for csh replace export with setenv in the above)\n"
1720#endif
1721 " qemu ...\n\n"
1722 );
1723}
1724
c0fe3827 1725static int audio_driver_init (AudioState *s, struct audio_driver *drv)
85571bc7 1726{
1d14ffa9
FB
1727 if (drv->options) {
1728 audio_process_options (drv->name, drv->options);
1729 }
c0fe3827 1730 s->drv_opaque = drv->init ();
1d14ffa9 1731
c0fe3827 1732 if (s->drv_opaque) {
1a7dafce 1733 audio_init_nb_voices_out (drv);
1734 audio_init_nb_voices_in (drv);
c0fe3827 1735 s->drv = drv;
1d14ffa9 1736 return 0;
85571bc7
FB
1737 }
1738 else {
1d14ffa9
FB
1739 dolog ("Could not init `%s' audio driver\n", drv->name);
1740 return -1;
85571bc7
FB
1741 }
1742}
1743
9781e040
AL
1744static void audio_vm_change_state_handler (void *opaque, int running,
1745 int reason)
85571bc7 1746{
c0fe3827 1747 AudioState *s = opaque;
1d14ffa9
FB
1748 HWVoiceOut *hwo = NULL;
1749 HWVoiceIn *hwi = NULL;
541e0844 1750 int op = running ? VOICE_ENABLE : VOICE_DISABLE;
1d14ffa9 1751
978dd635 1752 s->vm_running = running;
1a7dafce 1753 while ((hwo = audio_pcm_hw_find_any_enabled_out (hwo))) {
713a98f8 1754 hwo->pcm_ops->ctl_out (hwo, op, conf.try_poll_out);
1d14ffa9 1755 }
85571bc7 1756
1a7dafce 1757 while ((hwi = audio_pcm_hw_find_any_enabled_in (hwi))) {
713a98f8 1758 hwi->pcm_ops->ctl_in (hwi, op, conf.try_poll_in);
85571bc7 1759 }
39deb1e4 1760 audio_reset_timer (s);
85571bc7
FB
1761}
1762
1763static void audio_atexit (void)
1764{
c0fe3827 1765 AudioState *s = &glob_audio_state;
1d14ffa9
FB
1766 HWVoiceOut *hwo = NULL;
1767 HWVoiceIn *hwi = NULL;
1768
1a7dafce 1769 while ((hwo = audio_pcm_hw_find_any_enabled_out (hwo))) {
ec36b695 1770 SWVoiceCap *sc;
8ead62cf 1771
571ec3d6 1772 hwo->pcm_ops->ctl_out (hwo, VOICE_DISABLE);
1d14ffa9 1773 hwo->pcm_ops->fini_out (hwo);
8ead62cf 1774
ec36b695
FB
1775 for (sc = hwo->cap_head.lh_first; sc; sc = sc->entries.le_next) {
1776 CaptureVoiceOut *cap = sc->cap;
1777 struct capture_callback *cb;
1778
1779 for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
1780 cb->ops.destroy (cb->opaque);
1781 }
8ead62cf 1782 }
1d14ffa9 1783 }
85571bc7 1784
1a7dafce 1785 while ((hwi = audio_pcm_hw_find_any_enabled_in (hwi))) {
571ec3d6 1786 hwi->pcm_ops->ctl_in (hwi, VOICE_DISABLE);
1d14ffa9 1787 hwi->pcm_ops->fini_in (hwi);
85571bc7 1788 }
c0fe3827
FB
1789
1790 if (s->drv) {
1791 s->drv->fini (s->drv_opaque);
1792 }
85571bc7
FB
1793}
1794
d959fce9
JQ
1795static const VMStateDescription vmstate_audio = {
1796 .name = "audio",
1797 .version_id = 1,
1798 .minimum_version_id = 1,
1799 .minimum_version_id_old = 1,
1800 .fields = (VMStateField []) {
1801 VMSTATE_END_OF_LIST()
1d14ffa9 1802 }
d959fce9 1803};
85571bc7 1804
1a7dafce 1805static void audio_init (void)
85571bc7 1806{
1d14ffa9 1807 size_t i;
85571bc7
FB
1808 int done = 0;
1809 const char *drvname;
713a98f8 1810 VMChangeStateEntry *e;
c0fe3827 1811 AudioState *s = &glob_audio_state;
1d14ffa9 1812
0d9acba8 1813 if (s->drv) {
1a7dafce 1814 return;
0d9acba8
PB
1815 }
1816
72cf2d4f
BS
1817 QLIST_INIT (&s->hw_head_out);
1818 QLIST_INIT (&s->hw_head_in);
1819 QLIST_INIT (&s->cap_head);
571ec3d6
FB
1820 atexit (audio_atexit);
1821
1822 s->ts = qemu_new_timer (vm_clock, audio_timer, s);
1823 if (!s->ts) {
0d9acba8 1824 hw_error("Could not create audio timer\n");
571ec3d6
FB
1825 }
1826
1d14ffa9
FB
1827 audio_process_options ("AUDIO", audio_options);
1828
c0fe3827
FB
1829 s->nb_hw_voices_out = conf.fixed_out.nb_voices;
1830 s->nb_hw_voices_in = conf.fixed_in.nb_voices;
1831
1d14ffa9 1832 if (s->nb_hw_voices_out <= 0) {
571ec3d6 1833 dolog ("Bogus number of playback voices %d, setting to 1\n",
1d14ffa9
FB
1834 s->nb_hw_voices_out);
1835 s->nb_hw_voices_out = 1;
1836 }
1837
1838 if (s->nb_hw_voices_in <= 0) {
571ec3d6 1839 dolog ("Bogus number of capture voices %d, setting to 0\n",
1d14ffa9 1840 s->nb_hw_voices_in);
571ec3d6 1841 s->nb_hw_voices_in = 0;
1d14ffa9 1842 }
85571bc7 1843
1d14ffa9
FB
1844 {
1845 int def;
1846 drvname = audio_get_conf_str ("QEMU_AUDIO_DRV", NULL, &def);
1847 }
85571bc7 1848
85571bc7
FB
1849 if (drvname) {
1850 int found = 0;
1d14ffa9 1851
b1503cda 1852 for (i = 0; i < ARRAY_SIZE (drvtab); i++) {
85571bc7 1853 if (!strcmp (drvname, drvtab[i]->name)) {
c0fe3827 1854 done = !audio_driver_init (s, drvtab[i]);
85571bc7
FB
1855 found = 1;
1856 break;
1857 }
1858 }
1d14ffa9 1859
85571bc7
FB
1860 if (!found) {
1861 dolog ("Unknown audio driver `%s'\n", drvname);
1d14ffa9 1862 dolog ("Run with -audio-help to list available drivers\n");
85571bc7
FB
1863 }
1864 }
1865
85571bc7 1866 if (!done) {
b1503cda 1867 for (i = 0; !done && i < ARRAY_SIZE (drvtab); i++) {
1d14ffa9 1868 if (drvtab[i]->can_be_default) {
c0fe3827 1869 done = !audio_driver_init (s, drvtab[i]);
1d14ffa9 1870 }
85571bc7
FB
1871 }
1872 }
1873
85571bc7 1874 if (!done) {
c0fe3827
FB
1875 done = !audio_driver_init (s, &no_audio_driver);
1876 if (!done) {
0d9acba8 1877 hw_error("Could not initialize audio subsystem\n");
1d14ffa9
FB
1878 }
1879 else {
c0fe3827 1880 dolog ("warning: Using timer based audio emulation\n");
1d14ffa9 1881 }
85571bc7 1882 }
1d14ffa9 1883
0d9acba8
PB
1884 if (conf.period.hertz <= 0) {
1885 if (conf.period.hertz < 0) {
1886 dolog ("warning: Timer period is negative - %d "
1887 "treating as zero\n",
1888 conf.period.hertz);
571ec3d6 1889 }
0d9acba8
PB
1890 conf.period.ticks = 1;
1891 } else {
4f4cc0ef 1892 conf.period.ticks =
1893 muldiv64 (1, get_ticks_per_sec (), conf.period.hertz);
1d14ffa9 1894 }
0d9acba8
PB
1895
1896 e = qemu_add_vm_change_state_handler (audio_vm_change_state_handler, s);
1897 if (!e) {
1898 dolog ("warning: Could not register change state handler\n"
1899 "(Audio can continue looping even after stopping the VM)\n");
1d14ffa9
FB
1900 }
1901
72cf2d4f 1902 QLIST_INIT (&s->card_head);
0be71e32 1903 vmstate_register (NULL, 0, &vmstate_audio, s);
85571bc7 1904}
8ead62cf 1905
1a7dafce 1906void AUD_register_card (const char *name, QEMUSoundCard *card)
1907{
1908 audio_init ();
1909 card->name = qemu_strdup (name);
1910 memset (&card->entries, 0, sizeof (card->entries));
72cf2d4f 1911 QLIST_INSERT_HEAD (&glob_audio_state.card_head, card, entries);
1a7dafce 1912}
1913
1914void AUD_remove_card (QEMUSoundCard *card)
1915{
72cf2d4f 1916 QLIST_REMOVE (card, entries);
1a7dafce 1917 qemu_free (card->name);
1918}
1919
1920
ec36b695 1921CaptureVoiceOut *AUD_add_capture (
1ea879e5 1922 struct audsettings *as,
8ead62cf
FB
1923 struct audio_capture_ops *ops,
1924 void *cb_opaque
1925 )
1926{
1a7dafce 1927 AudioState *s = &glob_audio_state;
8ead62cf
FB
1928 CaptureVoiceOut *cap;
1929 struct capture_callback *cb;
1930
ec36b695 1931 if (audio_validate_settings (as)) {
8ead62cf
FB
1932 dolog ("Invalid settings were passed when trying to add capture\n");
1933 audio_print_settings (as);
ec36b695 1934 goto err0;
8ead62cf
FB
1935 }
1936
1937 cb = audio_calloc (AUDIO_FUNC, 1, sizeof (*cb));
1938 if (!cb) {
1939 dolog ("Could not allocate capture callback information, size %zu\n",
1940 sizeof (*cb));
1941 goto err0;
1942 }
1943 cb->ops = *ops;
1944 cb->opaque = cb_opaque;
1945
1a7dafce 1946 cap = audio_pcm_capture_find_specific (as);
8ead62cf 1947 if (cap) {
72cf2d4f 1948 QLIST_INSERT_HEAD (&cap->cb_head, cb, entries);
ec36b695 1949 return cap;
8ead62cf
FB
1950 }
1951 else {
1952 HWVoiceOut *hw;
1953 CaptureVoiceOut *cap;
1954
1955 cap = audio_calloc (AUDIO_FUNC, 1, sizeof (*cap));
1956 if (!cap) {
1957 dolog ("Could not allocate capture voice, size %zu\n",
1958 sizeof (*cap));
1959 goto err1;
1960 }
1961
1962 hw = &cap->hw;
72cf2d4f
BS
1963 QLIST_INIT (&hw->sw_head);
1964 QLIST_INIT (&cap->cb_head);
8ead62cf
FB
1965
1966 /* XXX find a more elegant way */
1967 hw->samples = 4096 * 4;
1968 hw->mix_buf = audio_calloc (AUDIO_FUNC, hw->samples,
1ea879e5 1969 sizeof (struct st_sample));
8ead62cf
FB
1970 if (!hw->mix_buf) {
1971 dolog ("Could not allocate capture mix buffer (%d samples)\n",
1972 hw->samples);
1973 goto err2;
1974 }
1975
d929eba5 1976 audio_pcm_init_info (&hw->info, as);
8ead62cf
FB
1977
1978 cap->buf = audio_calloc (AUDIO_FUNC, hw->samples, 1 << hw->info.shift);
1979 if (!cap->buf) {
1980 dolog ("Could not allocate capture buffer "
1981 "(%d samples, each %d bytes)\n",
1982 hw->samples, 1 << hw->info.shift);
1983 goto err3;
1984 }
1985
1986 hw->clip = mixeng_clip
1987 [hw->info.nchannels == 2]
1988 [hw->info.sign]
d929eba5 1989 [hw->info.swap_endianness]
f941aa25 1990 [audio_bits_to_index (hw->info.bits)];
8ead62cf 1991
72cf2d4f
BS
1992 QLIST_INSERT_HEAD (&s->cap_head, cap, entries);
1993 QLIST_INSERT_HEAD (&cap->cb_head, cb, entries);
8ead62cf
FB
1994
1995 hw = NULL;
1a7dafce 1996 while ((hw = audio_pcm_hw_find_any_out (hw))) {
1997 audio_attach_capture (hw);
8ead62cf 1998 }
ec36b695 1999 return cap;
8ead62cf
FB
2000
2001 err3:
2002 qemu_free (cap->hw.mix_buf);
2003 err2:
2004 qemu_free (cap);
2005 err1:
2006 qemu_free (cb);
2007 err0:
ec36b695
FB
2008 return NULL;
2009 }
2010}
2011
2012void AUD_del_capture (CaptureVoiceOut *cap, void *cb_opaque)
2013{
2014 struct capture_callback *cb;
2015
2016 for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
2017 if (cb->opaque == cb_opaque) {
2018 cb->ops.destroy (cb_opaque);
72cf2d4f 2019 QLIST_REMOVE (cb, entries);
ec36b695
FB
2020 qemu_free (cb);
2021
2022 if (!cap->cb_head.lh_first) {
2023 SWVoiceOut *sw = cap->hw.sw_head.lh_first, *sw1;
a3c25997 2024
ec36b695 2025 while (sw) {
a3c25997 2026 SWVoiceCap *sc = (SWVoiceCap *) sw;
ec36b695
FB
2027#ifdef DEBUG_CAPTURE
2028 dolog ("freeing %s\n", sw->name);
2029#endif
a3c25997 2030
ec36b695
FB
2031 sw1 = sw->entries.le_next;
2032 if (sw->rate) {
2033 st_rate_stop (sw->rate);
2034 sw->rate = NULL;
2035 }
72cf2d4f
BS
2036 QLIST_REMOVE (sw, entries);
2037 QLIST_REMOVE (sc, entries);
a3c25997 2038 qemu_free (sc);
ec36b695
FB
2039 sw = sw1;
2040 }
72cf2d4f 2041 QLIST_REMOVE (cap, entries);
ec36b695
FB
2042 qemu_free (cap);
2043 }
2044 return;
2045 }
8ead62cf
FB
2046 }
2047}
683efdcb
AZ
2048
2049void AUD_set_volume_out (SWVoiceOut *sw, int mute, uint8_t lvol, uint8_t rvol)
2050{
2051 if (sw) {
2052 sw->vol.mute = mute;
2053 sw->vol.l = nominal_volume.l * lvol / 255;
2054 sw->vol.r = nominal_volume.r * rvol / 255;
2055 }
2056}
2057
2058void AUD_set_volume_in (SWVoiceIn *sw, int mute, uint8_t lvol, uint8_t rvol)
2059{
2060 if (sw) {
2061 sw->vol.mute = mute;
2062 sw->vol.l = nominal_volume.l * lvol / 255;
2063 sw->vol.r = nominal_volume.r * rvol / 255;
2064 }
2065}