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