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