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