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