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