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