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