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