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