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