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