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