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