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