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