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