]>
Commit | Line | Data |
---|---|---|
85571bc7 FB |
1 | /* |
2 | * QEMU Audio subsystem | |
1d14ffa9 FB |
3 | * |
4 | * Copyright (c) 2003-2005 Vassili Karpov (malc) | |
5 | * | |
85571bc7 FB |
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
7 | * of this software and associated documentation files (the "Software"), to deal | |
8 | * in the Software without restriction, including without limitation the rights | |
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
10 | * copies of the Software, and to permit persons to whom the Software is | |
11 | * furnished to do so, subject to the following conditions: | |
12 | * | |
13 | * The above copyright notice and this permission notice shall be included in | |
14 | * all copies or substantial portions of the Software. | |
15 | * | |
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
22 | * THE SOFTWARE. | |
23 | */ | |
6086a565 | 24 | #include "qemu/osdep.h" |
87ecb68b PB |
25 | #include "hw/hw.h" |
26 | #include "audio.h" | |
83c9089e | 27 | #include "monitor/monitor.h" |
1de7afc9 | 28 | #include "qemu/timer.h" |
71830221 KZ |
29 | #include "qapi/error.h" |
30 | #include "qapi/qobject-input-visitor.h" | |
31 | #include "qapi/qapi-visit-audio.h" | |
9c17d615 | 32 | #include "sysemu/sysemu.h" |
f348b6d1 | 33 | #include "qemu/cutils.h" |
3d4d16f4 | 34 | #include "sysemu/replay.h" |
1a961e78 | 35 | #include "trace.h" |
85571bc7 | 36 | |
1d14ffa9 FB |
37 | #define AUDIO_CAP "audio" |
38 | #include "audio_int.h" | |
85571bc7 | 39 | |
1d14ffa9 FB |
40 | /* #define DEBUG_LIVE */ |
41 | /* #define DEBUG_OUT */ | |
8ead62cf | 42 | /* #define DEBUG_CAPTURE */ |
713a98f8 | 43 | /* #define DEBUG_POLL */ |
85571bc7 | 44 | |
c0fe3827 FB |
45 | #define SW_NAME(sw) (sw)->name ? (sw)->name : "unknown" |
46 | ||
2358a494 JQ |
47 | |
48 | /* Order of CONFIG_AUDIO_DRIVERS is import. | |
49 | The 1st one is the one used by default, that is the reason | |
50 | that we generate the list. | |
51 | */ | |
71830221 | 52 | const char *audio_prio_list[] = { |
d3893a39 | 53 | "spice", |
2358a494 | 54 | CONFIG_AUDIO_DRIVERS |
d3893a39 GH |
55 | "none", |
56 | "wav", | |
71830221 | 57 | NULL |
1d14ffa9 | 58 | }; |
85571bc7 | 59 | |
d3893a39 | 60 | static QLIST_HEAD(, audio_driver) audio_drivers; |
71830221 | 61 | static AudiodevListHead audiodevs = QSIMPLEQ_HEAD_INITIALIZER(audiodevs); |
d3893a39 GH |
62 | |
63 | void audio_driver_register(audio_driver *drv) | |
64 | { | |
65 | QLIST_INSERT_HEAD(&audio_drivers, drv, next); | |
66 | } | |
67 | ||
68 | audio_driver *audio_driver_lookup(const char *name) | |
69 | { | |
70 | struct audio_driver *d; | |
71 | ||
72 | QLIST_FOREACH(d, &audio_drivers, next) { | |
73 | if (strcmp(name, d->name) == 0) { | |
74 | return d; | |
75 | } | |
76 | } | |
65ba8696 GH |
77 | |
78 | audio_module_load_one(name); | |
79 | QLIST_FOREACH(d, &audio_drivers, next) { | |
80 | if (strcmp(name, d->name) == 0) { | |
81 | return d; | |
82 | } | |
83 | } | |
84 | ||
d3893a39 GH |
85 | return NULL; |
86 | } | |
87 | ||
c0fe3827 FB |
88 | static AudioState glob_audio_state; |
89 | ||
00e07679 | 90 | const struct mixeng_volume nominal_volume = { |
14658cd1 | 91 | .mute = 0, |
1d14ffa9 | 92 | #ifdef FLOAT_MIXENG |
14658cd1 JQ |
93 | .r = 1.0, |
94 | .l = 1.0, | |
1d14ffa9 | 95 | #else |
14658cd1 JQ |
96 | .r = 1ULL << 32, |
97 | .l = 1ULL << 32, | |
1d14ffa9 | 98 | #endif |
85571bc7 FB |
99 | }; |
100 | ||
1d14ffa9 FB |
101 | #ifdef AUDIO_IS_FLAWLESS_AND_NO_CHECKS_ARE_REQURIED |
102 | #error No its not | |
103 | #else | |
104 | int audio_bug (const char *funcname, int cond) | |
85571bc7 | 105 | { |
1d14ffa9 FB |
106 | if (cond) { |
107 | static int shown; | |
108 | ||
8ead62cf | 109 | AUD_log (NULL, "A bug was just triggered in %s\n", funcname); |
1d14ffa9 FB |
110 | if (!shown) { |
111 | shown = 1; | |
112 | AUD_log (NULL, "Save all your work and restart without audio\n"); | |
1d14ffa9 FB |
113 | AUD_log (NULL, "I am sorry\n"); |
114 | } | |
115 | AUD_log (NULL, "Context:\n"); | |
116 | ||
117 | #if defined AUDIO_BREAKPOINT_ON_BUG | |
118 | # if defined HOST_I386 | |
119 | # if defined __GNUC__ | |
120 | __asm__ ("int3"); | |
121 | # elif defined _MSC_VER | |
122 | _asm _emit 0xcc; | |
123 | # else | |
124 | abort (); | |
125 | # endif | |
126 | # else | |
127 | abort (); | |
128 | # endif | |
129 | #endif | |
85571bc7 FB |
130 | } |
131 | ||
1d14ffa9 | 132 | return cond; |
85571bc7 | 133 | } |
1d14ffa9 | 134 | #endif |
85571bc7 | 135 | |
f941aa25 TS |
136 | static inline int audio_bits_to_index (int bits) |
137 | { | |
138 | switch (bits) { | |
139 | case 8: | |
140 | return 0; | |
141 | ||
142 | case 16: | |
143 | return 1; | |
144 | ||
145 | case 32: | |
146 | return 2; | |
147 | ||
148 | default: | |
149 | audio_bug ("bits_to_index", 1); | |
150 | AUD_log (NULL, "invalid bits %d\n", bits); | |
151 | return 0; | |
152 | } | |
153 | } | |
154 | ||
c0fe3827 FB |
155 | void *audio_calloc (const char *funcname, int nmemb, size_t size) |
156 | { | |
157 | int cond; | |
158 | size_t len; | |
159 | ||
160 | len = nmemb * size; | |
161 | cond = !nmemb || !size; | |
162 | cond |= nmemb < 0; | |
163 | cond |= len < size; | |
164 | ||
165 | if (audio_bug ("audio_calloc", cond)) { | |
166 | AUD_log (NULL, "%s passed invalid arguments to audio_calloc\n", | |
167 | funcname); | |
541e0844 | 168 | AUD_log (NULL, "nmemb=%d size=%zu (len=%zu)\n", nmemb, size, len); |
c0fe3827 FB |
169 | return NULL; |
170 | } | |
171 | ||
7267c094 | 172 | return g_malloc0 (len); |
c0fe3827 FB |
173 | } |
174 | ||
541e0844 | 175 | void AUD_vlog (const char *cap, const char *fmt, va_list ap) |
85571bc7 | 176 | { |
06ac27f6 KZ |
177 | if (cap) { |
178 | fprintf(stderr, "%s: ", cap); | |
541e0844 | 179 | } |
541e0844 | 180 | |
06ac27f6 | 181 | vfprintf(stderr, fmt, ap); |
85571bc7 FB |
182 | } |
183 | ||
541e0844 | 184 | void AUD_log (const char *cap, const char *fmt, ...) |
85571bc7 | 185 | { |
541e0844 FB |
186 | va_list ap; |
187 | ||
188 | va_start (ap, fmt); | |
189 | AUD_vlog (cap, fmt, ap); | |
190 | va_end (ap); | |
85571bc7 FB |
191 | } |
192 | ||
1ea879e5 | 193 | static void audio_print_settings (struct audsettings *as) |
c0fe3827 FB |
194 | { |
195 | dolog ("frequency=%d nchannels=%d fmt=", as->freq, as->nchannels); | |
196 | ||
197 | switch (as->fmt) { | |
85bc5852 | 198 | case AUDIO_FORMAT_S8: |
c0fe3827 FB |
199 | AUD_log (NULL, "S8"); |
200 | break; | |
85bc5852 | 201 | case AUDIO_FORMAT_U8: |
c0fe3827 FB |
202 | AUD_log (NULL, "U8"); |
203 | break; | |
85bc5852 | 204 | case AUDIO_FORMAT_S16: |
c0fe3827 FB |
205 | AUD_log (NULL, "S16"); |
206 | break; | |
85bc5852 | 207 | case AUDIO_FORMAT_U16: |
c0fe3827 FB |
208 | AUD_log (NULL, "U16"); |
209 | break; | |
85bc5852 | 210 | case AUDIO_FORMAT_S32: |
d50997f9 | 211 | AUD_log (NULL, "S32"); |
212 | break; | |
85bc5852 | 213 | case AUDIO_FORMAT_U32: |
d50997f9 | 214 | AUD_log (NULL, "U32"); |
215 | break; | |
c0fe3827 FB |
216 | default: |
217 | AUD_log (NULL, "invalid(%d)", as->fmt); | |
218 | break; | |
219 | } | |
ec36b695 FB |
220 | |
221 | AUD_log (NULL, " endianness="); | |
d929eba5 FB |
222 | switch (as->endianness) { |
223 | case 0: | |
224 | AUD_log (NULL, "little"); | |
225 | break; | |
226 | case 1: | |
227 | AUD_log (NULL, "big"); | |
228 | break; | |
229 | default: | |
230 | AUD_log (NULL, "invalid"); | |
231 | break; | |
232 | } | |
c0fe3827 FB |
233 | AUD_log (NULL, "\n"); |
234 | } | |
235 | ||
1ea879e5 | 236 | static int audio_validate_settings (struct audsettings *as) |
c0fe3827 FB |
237 | { |
238 | int invalid; | |
239 | ||
240 | invalid = as->nchannels != 1 && as->nchannels != 2; | |
d929eba5 | 241 | invalid |= as->endianness != 0 && as->endianness != 1; |
c0fe3827 FB |
242 | |
243 | switch (as->fmt) { | |
85bc5852 KZ |
244 | case AUDIO_FORMAT_S8: |
245 | case AUDIO_FORMAT_U8: | |
246 | case AUDIO_FORMAT_S16: | |
247 | case AUDIO_FORMAT_U16: | |
248 | case AUDIO_FORMAT_S32: | |
249 | case AUDIO_FORMAT_U32: | |
c0fe3827 FB |
250 | break; |
251 | default: | |
252 | invalid = 1; | |
253 | break; | |
254 | } | |
255 | ||
256 | invalid |= as->freq <= 0; | |
d929eba5 | 257 | return invalid ? -1 : 0; |
c0fe3827 FB |
258 | } |
259 | ||
1ea879e5 | 260 | static int audio_pcm_info_eq (struct audio_pcm_info *info, struct audsettings *as) |
85571bc7 | 261 | { |
1d14ffa9 | 262 | int bits = 8, sign = 0; |
85571bc7 | 263 | |
c0fe3827 | 264 | switch (as->fmt) { |
85bc5852 | 265 | case AUDIO_FORMAT_S8: |
1d14ffa9 | 266 | sign = 1; |
b4bd0b16 | 267 | /* fall through */ |
85bc5852 | 268 | case AUDIO_FORMAT_U8: |
1d14ffa9 FB |
269 | break; |
270 | ||
85bc5852 | 271 | case AUDIO_FORMAT_S16: |
1d14ffa9 | 272 | sign = 1; |
b4bd0b16 | 273 | /* fall through */ |
85bc5852 | 274 | case AUDIO_FORMAT_U16: |
1d14ffa9 FB |
275 | bits = 16; |
276 | break; | |
f941aa25 | 277 | |
85bc5852 | 278 | case AUDIO_FORMAT_S32: |
f941aa25 | 279 | sign = 1; |
b4bd0b16 | 280 | /* fall through */ |
85bc5852 | 281 | case AUDIO_FORMAT_U32: |
f941aa25 TS |
282 | bits = 32; |
283 | break; | |
85bc5852 KZ |
284 | |
285 | default: | |
286 | abort(); | |
85571bc7 | 287 | } |
c0fe3827 FB |
288 | return info->freq == as->freq |
289 | && info->nchannels == as->nchannels | |
1d14ffa9 | 290 | && info->sign == sign |
d929eba5 FB |
291 | && info->bits == bits |
292 | && info->swap_endianness == (as->endianness != AUDIO_HOST_ENDIANNESS); | |
1d14ffa9 | 293 | } |
85571bc7 | 294 | |
1ea879e5 | 295 | void audio_pcm_init_info (struct audio_pcm_info *info, struct audsettings *as) |
1d14ffa9 | 296 | { |
f941aa25 | 297 | int bits = 8, sign = 0, shift = 0; |
1d14ffa9 | 298 | |
c0fe3827 | 299 | switch (as->fmt) { |
85bc5852 | 300 | case AUDIO_FORMAT_S8: |
85571bc7 | 301 | sign = 1; |
85bc5852 | 302 | case AUDIO_FORMAT_U8: |
85571bc7 FB |
303 | break; |
304 | ||
85bc5852 | 305 | case AUDIO_FORMAT_S16: |
85571bc7 | 306 | sign = 1; |
85bc5852 | 307 | case AUDIO_FORMAT_U16: |
85571bc7 | 308 | bits = 16; |
f941aa25 TS |
309 | shift = 1; |
310 | break; | |
311 | ||
85bc5852 | 312 | case AUDIO_FORMAT_S32: |
f941aa25 | 313 | sign = 1; |
85bc5852 | 314 | case AUDIO_FORMAT_U32: |
f941aa25 TS |
315 | bits = 32; |
316 | shift = 2; | |
85571bc7 | 317 | break; |
85bc5852 KZ |
318 | |
319 | default: | |
320 | abort(); | |
85571bc7 FB |
321 | } |
322 | ||
c0fe3827 | 323 | info->freq = as->freq; |
1d14ffa9 FB |
324 | info->bits = bits; |
325 | info->sign = sign; | |
c0fe3827 | 326 | info->nchannels = as->nchannels; |
f941aa25 | 327 | info->shift = (as->nchannels == 2) + shift; |
1d14ffa9 FB |
328 | info->align = (1 << info->shift) - 1; |
329 | info->bytes_per_second = info->freq << info->shift; | |
d929eba5 | 330 | info->swap_endianness = (as->endianness != AUDIO_HOST_ENDIANNESS); |
85571bc7 FB |
331 | } |
332 | ||
1d14ffa9 | 333 | void audio_pcm_info_clear_buf (struct audio_pcm_info *info, void *buf, int len) |
85571bc7 | 334 | { |
1d14ffa9 FB |
335 | if (!len) { |
336 | return; | |
337 | } | |
338 | ||
339 | if (info->sign) { | |
e2f909be | 340 | memset (buf, 0x00, len << info->shift); |
85571bc7 FB |
341 | } |
342 | else { | |
f941aa25 TS |
343 | switch (info->bits) { |
344 | case 8: | |
e2f909be | 345 | memset (buf, 0x80, len << info->shift); |
f941aa25 | 346 | break; |
1d14ffa9 | 347 | |
f941aa25 TS |
348 | case 16: |
349 | { | |
350 | int i; | |
351 | uint16_t *p = buf; | |
352 | int shift = info->nchannels - 1; | |
353 | short s = INT16_MAX; | |
354 | ||
355 | if (info->swap_endianness) { | |
356 | s = bswap16 (s); | |
357 | } | |
358 | ||
359 | for (i = 0; i < len << shift; i++) { | |
360 | p[i] = s; | |
361 | } | |
1d14ffa9 | 362 | } |
f941aa25 TS |
363 | break; |
364 | ||
365 | case 32: | |
366 | { | |
367 | int i; | |
368 | uint32_t *p = buf; | |
369 | int shift = info->nchannels - 1; | |
370 | int32_t s = INT32_MAX; | |
371 | ||
372 | if (info->swap_endianness) { | |
373 | s = bswap32 (s); | |
374 | } | |
1d14ffa9 | 375 | |
f941aa25 TS |
376 | for (i = 0; i < len << shift; i++) { |
377 | p[i] = s; | |
378 | } | |
1d14ffa9 | 379 | } |
f941aa25 TS |
380 | break; |
381 | ||
382 | default: | |
383 | AUD_log (NULL, "audio_pcm_info_clear_buf: invalid bits %d\n", | |
384 | info->bits); | |
385 | break; | |
1d14ffa9 | 386 | } |
85571bc7 FB |
387 | } |
388 | } | |
389 | ||
8ead62cf FB |
390 | /* |
391 | * Capture | |
392 | */ | |
00e07679 | 393 | static void noop_conv (struct st_sample *dst, const void *src, int samples) |
8ead62cf FB |
394 | { |
395 | (void) src; | |
396 | (void) dst; | |
397 | (void) samples; | |
8ead62cf FB |
398 | } |
399 | ||
400 | static CaptureVoiceOut *audio_pcm_capture_find_specific ( | |
1ea879e5 | 401 | struct audsettings *as |
8ead62cf FB |
402 | ) |
403 | { | |
404 | CaptureVoiceOut *cap; | |
1a7dafce | 405 | AudioState *s = &glob_audio_state; |
8ead62cf FB |
406 | |
407 | for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) { | |
d929eba5 | 408 | if (audio_pcm_info_eq (&cap->hw.info, as)) { |
8ead62cf FB |
409 | return cap; |
410 | } | |
411 | } | |
412 | return NULL; | |
413 | } | |
414 | ||
ec36b695 | 415 | static void audio_notify_capture (CaptureVoiceOut *cap, audcnotification_e cmd) |
8ead62cf | 416 | { |
ec36b695 FB |
417 | struct capture_callback *cb; |
418 | ||
419 | #ifdef DEBUG_CAPTURE | |
420 | dolog ("notification %d sent\n", cmd); | |
421 | #endif | |
422 | for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) { | |
423 | cb->ops.notify (cb->opaque, cmd); | |
424 | } | |
425 | } | |
8ead62cf | 426 | |
ec36b695 FB |
427 | static void audio_capture_maybe_changed (CaptureVoiceOut *cap, int enabled) |
428 | { | |
429 | if (cap->hw.enabled != enabled) { | |
430 | audcnotification_e cmd; | |
8ead62cf | 431 | cap->hw.enabled = enabled; |
ec36b695 FB |
432 | cmd = enabled ? AUD_CNOTIFY_ENABLE : AUD_CNOTIFY_DISABLE; |
433 | audio_notify_capture (cap, cmd); | |
8ead62cf FB |
434 | } |
435 | } | |
436 | ||
437 | static void audio_recalc_and_notify_capture (CaptureVoiceOut *cap) | |
438 | { | |
439 | HWVoiceOut *hw = &cap->hw; | |
440 | SWVoiceOut *sw; | |
441 | int enabled = 0; | |
442 | ||
ec36b695 | 443 | for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) { |
8ead62cf FB |
444 | if (sw->active) { |
445 | enabled = 1; | |
446 | break; | |
447 | } | |
448 | } | |
ec36b695 | 449 | audio_capture_maybe_changed (cap, enabled); |
8ead62cf FB |
450 | } |
451 | ||
452 | static void audio_detach_capture (HWVoiceOut *hw) | |
453 | { | |
ec36b695 FB |
454 | SWVoiceCap *sc = hw->cap_head.lh_first; |
455 | ||
456 | while (sc) { | |
457 | SWVoiceCap *sc1 = sc->entries.le_next; | |
458 | SWVoiceOut *sw = &sc->sw; | |
459 | CaptureVoiceOut *cap = sc->cap; | |
460 | int was_active = sw->active; | |
8ead62cf | 461 | |
8ead62cf FB |
462 | if (sw->rate) { |
463 | st_rate_stop (sw->rate); | |
464 | sw->rate = NULL; | |
465 | } | |
466 | ||
72cf2d4f BS |
467 | QLIST_REMOVE (sw, entries); |
468 | QLIST_REMOVE (sc, entries); | |
7267c094 | 469 | g_free (sc); |
ec36b695 FB |
470 | if (was_active) { |
471 | /* We have removed soft voice from the capture: | |
472 | this might have changed the overall status of the capture | |
473 | since this might have been the only active voice */ | |
474 | audio_recalc_and_notify_capture (cap); | |
475 | } | |
476 | sc = sc1; | |
8ead62cf FB |
477 | } |
478 | } | |
479 | ||
1a7dafce | 480 | static int audio_attach_capture (HWVoiceOut *hw) |
8ead62cf | 481 | { |
1a7dafce | 482 | AudioState *s = &glob_audio_state; |
8ead62cf FB |
483 | CaptureVoiceOut *cap; |
484 | ||
485 | audio_detach_capture (hw); | |
486 | for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) { | |
ec36b695 | 487 | SWVoiceCap *sc; |
8ead62cf | 488 | SWVoiceOut *sw; |
ec36b695 | 489 | HWVoiceOut *hw_cap = &cap->hw; |
8ead62cf | 490 | |
e8d85444 | 491 | sc = g_malloc0(sizeof(*sc)); |
8ead62cf | 492 | |
ec36b695 FB |
493 | sc->cap = cap; |
494 | sw = &sc->sw; | |
8ead62cf | 495 | sw->hw = hw_cap; |
ec36b695 | 496 | sw->info = hw->info; |
8ead62cf FB |
497 | sw->empty = 1; |
498 | sw->active = hw->enabled; | |
499 | sw->conv = noop_conv; | |
500 | sw->ratio = ((int64_t) hw_cap->info.freq << 32) / sw->info.freq; | |
83617103 | 501 | sw->vol = nominal_volume; |
8ead62cf FB |
502 | sw->rate = st_rate_start (sw->info.freq, hw_cap->info.freq); |
503 | if (!sw->rate) { | |
504 | dolog ("Could not start rate conversion for `%s'\n", SW_NAME (sw)); | |
7267c094 | 505 | g_free (sw); |
8ead62cf FB |
506 | return -1; |
507 | } | |
72cf2d4f BS |
508 | QLIST_INSERT_HEAD (&hw_cap->sw_head, sw, entries); |
509 | QLIST_INSERT_HEAD (&hw->cap_head, sc, entries); | |
ec36b695 | 510 | #ifdef DEBUG_CAPTURE |
457b6543 SW |
511 | sw->name = g_strdup_printf ("for %p %d,%d,%d", |
512 | hw, sw->info.freq, sw->info.bits, | |
513 | sw->info.nchannels); | |
ec36b695 FB |
514 | dolog ("Added %s active = %d\n", sw->name, sw->active); |
515 | #endif | |
8ead62cf | 516 | if (sw->active) { |
ec36b695 | 517 | audio_capture_maybe_changed (cap, 1); |
8ead62cf FB |
518 | } |
519 | } | |
520 | return 0; | |
521 | } | |
522 | ||
1d14ffa9 FB |
523 | /* |
524 | * Hard voice (capture) | |
525 | */ | |
c0fe3827 | 526 | static int audio_pcm_hw_find_min_in (HWVoiceIn *hw) |
85571bc7 | 527 | { |
1d14ffa9 FB |
528 | SWVoiceIn *sw; |
529 | int m = hw->total_samples_captured; | |
530 | ||
531 | for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) { | |
532 | if (sw->active) { | |
533 | m = audio_MIN (m, sw->total_hw_samples_acquired); | |
534 | } | |
85571bc7 | 535 | } |
1d14ffa9 | 536 | return m; |
85571bc7 FB |
537 | } |
538 | ||
1d14ffa9 | 539 | int audio_pcm_hw_get_live_in (HWVoiceIn *hw) |
85571bc7 | 540 | { |
1d14ffa9 | 541 | int live = hw->total_samples_captured - audio_pcm_hw_find_min_in (hw); |
470bcabd | 542 | if (audio_bug(__func__, live < 0 || live > hw->samples)) { |
1d14ffa9 FB |
543 | dolog ("live=%d hw->samples=%d\n", live, hw->samples); |
544 | return 0; | |
85571bc7 | 545 | } |
1d14ffa9 | 546 | return live; |
85571bc7 FB |
547 | } |
548 | ||
ddabec73 | 549 | int audio_pcm_hw_clip_out (HWVoiceOut *hw, void *pcm_buf, |
550 | int live, int pending) | |
551 | { | |
552 | int left = hw->samples - pending; | |
553 | int len = audio_MIN (left, live); | |
554 | int clipped = 0; | |
555 | ||
556 | while (len) { | |
557 | struct st_sample *src = hw->mix_buf + hw->rpos; | |
558 | uint8_t *dst = advance (pcm_buf, hw->rpos << hw->info.shift); | |
559 | int samples_till_end_of_buf = hw->samples - hw->rpos; | |
560 | int samples_to_clip = audio_MIN (len, samples_till_end_of_buf); | |
561 | ||
562 | hw->clip (dst, src, samples_to_clip); | |
563 | ||
564 | hw->rpos = (hw->rpos + samples_to_clip) % hw->samples; | |
565 | len -= samples_to_clip; | |
566 | clipped += samples_to_clip; | |
567 | } | |
568 | return clipped; | |
569 | } | |
570 | ||
1d14ffa9 FB |
571 | /* |
572 | * Soft voice (capture) | |
573 | */ | |
1d14ffa9 FB |
574 | static int audio_pcm_sw_get_rpos_in (SWVoiceIn *sw) |
575 | { | |
576 | HWVoiceIn *hw = sw->hw; | |
577 | int live = hw->total_samples_captured - sw->total_hw_samples_acquired; | |
578 | int rpos; | |
579 | ||
470bcabd | 580 | if (audio_bug(__func__, live < 0 || live > hw->samples)) { |
1d14ffa9 FB |
581 | dolog ("live=%d hw->samples=%d\n", live, hw->samples); |
582 | return 0; | |
583 | } | |
584 | ||
585 | rpos = hw->wpos - live; | |
586 | if (rpos >= 0) { | |
587 | return rpos; | |
85571bc7 FB |
588 | } |
589 | else { | |
1d14ffa9 | 590 | return hw->samples + rpos; |
85571bc7 | 591 | } |
85571bc7 FB |
592 | } |
593 | ||
1d14ffa9 | 594 | int audio_pcm_sw_read (SWVoiceIn *sw, void *buf, int size) |
85571bc7 | 595 | { |
1d14ffa9 FB |
596 | HWVoiceIn *hw = sw->hw; |
597 | int samples, live, ret = 0, swlim, isamp, osamp, rpos, total = 0; | |
1ea879e5 | 598 | struct st_sample *src, *dst = sw->buf; |
1d14ffa9 FB |
599 | |
600 | rpos = audio_pcm_sw_get_rpos_in (sw) % hw->samples; | |
601 | ||
602 | live = hw->total_samples_captured - sw->total_hw_samples_acquired; | |
470bcabd | 603 | if (audio_bug(__func__, live < 0 || live > hw->samples)) { |
1d14ffa9 FB |
604 | dolog ("live_in=%d hw->samples=%d\n", live, hw->samples); |
605 | return 0; | |
606 | } | |
607 | ||
608 | samples = size >> sw->info.shift; | |
609 | if (!live) { | |
610 | return 0; | |
611 | } | |
85571bc7 | 612 | |
1d14ffa9 FB |
613 | swlim = (live * sw->ratio) >> 32; |
614 | swlim = audio_MIN (swlim, samples); | |
85571bc7 | 615 | |
1d14ffa9 FB |
616 | while (swlim) { |
617 | src = hw->conv_buf + rpos; | |
618 | isamp = hw->wpos - rpos; | |
619 | /* XXX: <= ? */ | |
620 | if (isamp <= 0) { | |
621 | isamp = hw->samples - rpos; | |
622 | } | |
85571bc7 | 623 | |
1d14ffa9 FB |
624 | if (!isamp) { |
625 | break; | |
626 | } | |
627 | osamp = swlim; | |
85571bc7 | 628 | |
470bcabd | 629 | if (audio_bug(__func__, osamp < 0)) { |
1d14ffa9 | 630 | dolog ("osamp=%d\n", osamp); |
c0fe3827 | 631 | return 0; |
1d14ffa9 | 632 | } |
85571bc7 | 633 | |
1d14ffa9 FB |
634 | st_rate_flow (sw->rate, src, dst, &isamp, &osamp); |
635 | swlim -= osamp; | |
636 | rpos = (rpos + isamp) % hw->samples; | |
637 | dst += osamp; | |
638 | ret += osamp; | |
639 | total += isamp; | |
640 | } | |
85571bc7 | 641 | |
c01b2456 MAL |
642 | if (!(hw->ctl_caps & VOICE_VOLUME_CAP)) { |
643 | mixeng_volume (sw->buf, ret, &sw->vol); | |
644 | } | |
00e07679 | 645 | |
571ec3d6 | 646 | sw->clip (buf, sw->buf, ret); |
1d14ffa9 FB |
647 | sw->total_hw_samples_acquired += total; |
648 | return ret << sw->info.shift; | |
85571bc7 FB |
649 | } |
650 | ||
1d14ffa9 FB |
651 | /* |
652 | * Hard voice (playback) | |
653 | */ | |
c0fe3827 | 654 | static int audio_pcm_hw_find_min_out (HWVoiceOut *hw, int *nb_livep) |
1d14ffa9 | 655 | { |
c0fe3827 FB |
656 | SWVoiceOut *sw; |
657 | int m = INT_MAX; | |
658 | int nb_live = 0; | |
85571bc7 | 659 | |
c0fe3827 FB |
660 | for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) { |
661 | if (sw->active || !sw->empty) { | |
662 | m = audio_MIN (m, sw->total_hw_samples_mixed); | |
663 | nb_live += 1; | |
664 | } | |
85571bc7 | 665 | } |
c0fe3827 FB |
666 | |
667 | *nb_livep = nb_live; | |
668 | return m; | |
1d14ffa9 | 669 | } |
85571bc7 | 670 | |
bdff253c | 671 | static int audio_pcm_hw_get_live_out (HWVoiceOut *hw, int *nb_live) |
1d14ffa9 FB |
672 | { |
673 | int smin; | |
bdff253c | 674 | int nb_live1; |
85571bc7 | 675 | |
bdff253c | 676 | smin = audio_pcm_hw_find_min_out (hw, &nb_live1); |
677 | if (nb_live) { | |
678 | *nb_live = nb_live1; | |
85571bc7 | 679 | } |
bdff253c | 680 | |
681 | if (nb_live1) { | |
1d14ffa9 FB |
682 | int live = smin; |
683 | ||
470bcabd | 684 | if (audio_bug(__func__, live < 0 || live > hw->samples)) { |
1d14ffa9 FB |
685 | dolog ("live=%d hw->samples=%d\n", live, hw->samples); |
686 | return 0; | |
85571bc7 | 687 | } |
1d14ffa9 | 688 | return live; |
85571bc7 | 689 | } |
bdff253c | 690 | return 0; |
85571bc7 FB |
691 | } |
692 | ||
1d14ffa9 FB |
693 | /* |
694 | * Soft voice (playback) | |
695 | */ | |
1d14ffa9 | 696 | int audio_pcm_sw_write (SWVoiceOut *sw, void *buf, int size) |
85571bc7 | 697 | { |
1d14ffa9 FB |
698 | int hwsamples, samples, isamp, osamp, wpos, live, dead, left, swlim, blck; |
699 | int ret = 0, pos = 0, total = 0; | |
85571bc7 | 700 | |
1d14ffa9 FB |
701 | if (!sw) { |
702 | return size; | |
703 | } | |
85571bc7 | 704 | |
1d14ffa9 | 705 | hwsamples = sw->hw->samples; |
85571bc7 | 706 | |
1d14ffa9 | 707 | live = sw->total_hw_samples_mixed; |
470bcabd | 708 | if (audio_bug(__func__, live < 0 || live > hwsamples)) { |
1d14ffa9 FB |
709 | dolog ("live=%d hw->samples=%d\n", live, hwsamples); |
710 | return 0; | |
711 | } | |
85571bc7 | 712 | |
1d14ffa9 | 713 | if (live == hwsamples) { |
ec36b695 FB |
714 | #ifdef DEBUG_OUT |
715 | dolog ("%s is full %d\n", sw->name, live); | |
716 | #endif | |
1d14ffa9 FB |
717 | return 0; |
718 | } | |
85571bc7 | 719 | |
1d14ffa9 FB |
720 | wpos = (sw->hw->rpos + live) % hwsamples; |
721 | samples = size >> sw->info.shift; | |
85571bc7 | 722 | |
1d14ffa9 FB |
723 | dead = hwsamples - live; |
724 | swlim = ((int64_t) dead << 32) / sw->ratio; | |
725 | swlim = audio_MIN (swlim, samples); | |
726 | if (swlim) { | |
00e07679 | 727 | sw->conv (sw->buf, buf, swlim); |
c01b2456 MAL |
728 | |
729 | if (!(sw->hw->ctl_caps & VOICE_VOLUME_CAP)) { | |
730 | mixeng_volume (sw->buf, swlim, &sw->vol); | |
731 | } | |
1d14ffa9 FB |
732 | } |
733 | ||
734 | while (swlim) { | |
735 | dead = hwsamples - live; | |
736 | left = hwsamples - wpos; | |
737 | blck = audio_MIN (dead, left); | |
738 | if (!blck) { | |
739 | break; | |
740 | } | |
741 | isamp = swlim; | |
742 | osamp = blck; | |
743 | st_rate_flow_mix ( | |
744 | sw->rate, | |
745 | sw->buf + pos, | |
746 | sw->hw->mix_buf + wpos, | |
747 | &isamp, | |
748 | &osamp | |
749 | ); | |
750 | ret += isamp; | |
751 | swlim -= isamp; | |
752 | pos += isamp; | |
753 | live += osamp; | |
754 | wpos = (wpos + osamp) % hwsamples; | |
755 | total += osamp; | |
756 | } | |
757 | ||
758 | sw->total_hw_samples_mixed += total; | |
759 | sw->empty = sw->total_hw_samples_mixed == 0; | |
760 | ||
761 | #ifdef DEBUG_OUT | |
762 | dolog ( | |
c0fe3827 FB |
763 | "%s: write size %d ret %d total sw %d\n", |
764 | SW_NAME (sw), | |
1d14ffa9 FB |
765 | size >> sw->info.shift, |
766 | ret, | |
c0fe3827 | 767 | sw->total_hw_samples_mixed |
1d14ffa9 FB |
768 | ); |
769 | #endif | |
770 | ||
771 | return ret << sw->info.shift; | |
85571bc7 FB |
772 | } |
773 | ||
1d14ffa9 FB |
774 | #ifdef DEBUG_AUDIO |
775 | static void audio_pcm_print_info (const char *cap, struct audio_pcm_info *info) | |
85571bc7 | 776 | { |
1d14ffa9 FB |
777 | dolog ("%s: bits %d, sign %d, freq %d, nchan %d\n", |
778 | cap, info->bits, info->sign, info->freq, info->nchannels); | |
85571bc7 | 779 | } |
1d14ffa9 | 780 | #endif |
85571bc7 | 781 | |
1d14ffa9 FB |
782 | #define DAC |
783 | #include "audio_template.h" | |
784 | #undef DAC | |
785 | #include "audio_template.h" | |
786 | ||
713a98f8 | 787 | /* |
788 | * Timer | |
789 | */ | |
1a961e78 GH |
790 | |
791 | static bool audio_timer_running; | |
792 | static uint64_t audio_timer_last; | |
793 | ||
713a98f8 | 794 | static int audio_is_timer_needed (void) |
795 | { | |
796 | HWVoiceIn *hwi = NULL; | |
797 | HWVoiceOut *hwo = NULL; | |
798 | ||
799 | while ((hwo = audio_pcm_hw_find_any_enabled_out (hwo))) { | |
800 | if (!hwo->poll_mode) return 1; | |
801 | } | |
802 | while ((hwi = audio_pcm_hw_find_any_enabled_in (hwi))) { | |
803 | if (!hwi->poll_mode) return 1; | |
804 | } | |
805 | return 0; | |
806 | } | |
807 | ||
39deb1e4 | 808 | static void audio_reset_timer (AudioState *s) |
713a98f8 | 809 | { |
713a98f8 | 810 | if (audio_is_timer_needed ()) { |
1ffc2665 | 811 | timer_mod_anticipate_ns(s->ts, |
71830221 | 812 | qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + s->period_ticks); |
1a961e78 GH |
813 | if (!audio_timer_running) { |
814 | audio_timer_running = true; | |
815 | audio_timer_last = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); | |
71830221 | 816 | trace_audio_timer_start(s->period_ticks / SCALE_MS); |
1a961e78 GH |
817 | } |
818 | } else { | |
819 | timer_del(s->ts); | |
820 | if (audio_timer_running) { | |
821 | audio_timer_running = false; | |
822 | trace_audio_timer_stop(); | |
823 | } | |
713a98f8 | 824 | } |
825 | } | |
826 | ||
39deb1e4 | 827 | static void audio_timer (void *opaque) |
828 | { | |
1a961e78 | 829 | int64_t now, diff; |
71830221 | 830 | AudioState *s = opaque; |
1a961e78 GH |
831 | |
832 | now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); | |
833 | diff = now - audio_timer_last; | |
71830221 | 834 | if (diff > s->period_ticks * 3 / 2) { |
1a961e78 GH |
835 | trace_audio_timer_delayed(diff / SCALE_MS); |
836 | } | |
837 | audio_timer_last = now; | |
838 | ||
71830221 KZ |
839 | audio_run("timer"); |
840 | audio_reset_timer(s); | |
39deb1e4 | 841 | } |
842 | ||
713a98f8 | 843 | /* |
844 | * Public API | |
845 | */ | |
1d14ffa9 | 846 | int AUD_write (SWVoiceOut *sw, void *buf, int size) |
85571bc7 | 847 | { |
1d14ffa9 FB |
848 | if (!sw) { |
849 | /* XXX: Consider options */ | |
850 | return size; | |
851 | } | |
85571bc7 | 852 | |
1d14ffa9 | 853 | if (!sw->hw->enabled) { |
c0fe3827 | 854 | dolog ("Writing to disabled voice %s\n", SW_NAME (sw)); |
85571bc7 FB |
855 | return 0; |
856 | } | |
857 | ||
9be38598 | 858 | return sw->hw->pcm_ops->write(sw, buf, size); |
1d14ffa9 FB |
859 | } |
860 | ||
861 | int AUD_read (SWVoiceIn *sw, void *buf, int size) | |
862 | { | |
1d14ffa9 FB |
863 | if (!sw) { |
864 | /* XXX: Consider options */ | |
865 | return size; | |
85571bc7 | 866 | } |
1d14ffa9 FB |
867 | |
868 | if (!sw->hw->enabled) { | |
c0fe3827 | 869 | dolog ("Reading from disabled voice %s\n", SW_NAME (sw)); |
1d14ffa9 | 870 | return 0; |
85571bc7 | 871 | } |
1d14ffa9 | 872 | |
9be38598 | 873 | return sw->hw->pcm_ops->read(sw, buf, size); |
85571bc7 FB |
874 | } |
875 | ||
1d14ffa9 | 876 | int AUD_get_buffer_size_out (SWVoiceOut *sw) |
85571bc7 | 877 | { |
c0fe3827 | 878 | return sw->hw->samples << sw->hw->info.shift; |
1d14ffa9 FB |
879 | } |
880 | ||
881 | void AUD_set_active_out (SWVoiceOut *sw, int on) | |
882 | { | |
883 | HWVoiceOut *hw; | |
85571bc7 | 884 | |
1d14ffa9 | 885 | if (!sw) { |
85571bc7 | 886 | return; |
1d14ffa9 | 887 | } |
85571bc7 FB |
888 | |
889 | hw = sw->hw; | |
1d14ffa9 | 890 | if (sw->active != on) { |
978dd635 | 891 | AudioState *s = &glob_audio_state; |
1d14ffa9 | 892 | SWVoiceOut *temp_sw; |
ec36b695 | 893 | SWVoiceCap *sc; |
1d14ffa9 FB |
894 | |
895 | if (on) { | |
1d14ffa9 FB |
896 | hw->pending_disable = 0; |
897 | if (!hw->enabled) { | |
898 | hw->enabled = 1; | |
978dd635 | 899 | if (s->vm_running) { |
05d2f2a6 | 900 | hw->pcm_ops->ctl_out(hw, VOICE_ENABLE); |
39deb1e4 | 901 | audio_reset_timer (s); |
978dd635 | 902 | } |
1d14ffa9 | 903 | } |
1d14ffa9 FB |
904 | } |
905 | else { | |
906 | if (hw->enabled) { | |
907 | int nb_active = 0; | |
908 | ||
909 | for (temp_sw = hw->sw_head.lh_first; temp_sw; | |
910 | temp_sw = temp_sw->entries.le_next) { | |
911 | nb_active += temp_sw->active != 0; | |
912 | } | |
913 | ||
914 | hw->pending_disable = nb_active == 1; | |
915 | } | |
85571bc7 | 916 | } |
ec36b695 FB |
917 | |
918 | for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) { | |
919 | sc->sw.active = hw->enabled; | |
8ead62cf | 920 | if (hw->enabled) { |
ec36b695 | 921 | audio_capture_maybe_changed (sc->cap, 1); |
8ead62cf FB |
922 | } |
923 | } | |
1d14ffa9 FB |
924 | sw->active = on; |
925 | } | |
926 | } | |
927 | ||
928 | void AUD_set_active_in (SWVoiceIn *sw, int on) | |
929 | { | |
930 | HWVoiceIn *hw; | |
931 | ||
932 | if (!sw) { | |
933 | return; | |
85571bc7 FB |
934 | } |
935 | ||
1d14ffa9 | 936 | hw = sw->hw; |
85571bc7 | 937 | if (sw->active != on) { |
978dd635 | 938 | AudioState *s = &glob_audio_state; |
1d14ffa9 FB |
939 | SWVoiceIn *temp_sw; |
940 | ||
85571bc7 | 941 | if (on) { |
85571bc7 FB |
942 | if (!hw->enabled) { |
943 | hw->enabled = 1; | |
978dd635 | 944 | if (s->vm_running) { |
05d2f2a6 | 945 | hw->pcm_ops->ctl_in(hw, VOICE_ENABLE); |
39deb1e4 | 946 | audio_reset_timer (s); |
978dd635 | 947 | } |
85571bc7 | 948 | } |
1d14ffa9 | 949 | sw->total_hw_samples_acquired = hw->total_samples_captured; |
85571bc7 FB |
950 | } |
951 | else { | |
1d14ffa9 | 952 | if (hw->enabled) { |
85571bc7 | 953 | int nb_active = 0; |
1d14ffa9 FB |
954 | |
955 | for (temp_sw = hw->sw_head.lh_first; temp_sw; | |
956 | temp_sw = temp_sw->entries.le_next) { | |
957 | nb_active += temp_sw->active != 0; | |
85571bc7 FB |
958 | } |
959 | ||
960 | if (nb_active == 1) { | |
1d14ffa9 FB |
961 | hw->enabled = 0; |
962 | hw->pcm_ops->ctl_in (hw, VOICE_DISABLE); | |
85571bc7 FB |
963 | } |
964 | } | |
965 | } | |
966 | sw->active = on; | |
967 | } | |
968 | } | |
969 | ||
1d14ffa9 FB |
970 | static int audio_get_avail (SWVoiceIn *sw) |
971 | { | |
972 | int live; | |
973 | ||
974 | if (!sw) { | |
975 | return 0; | |
976 | } | |
977 | ||
978 | live = sw->hw->total_samples_captured - sw->total_hw_samples_acquired; | |
470bcabd | 979 | if (audio_bug(__func__, live < 0 || live > sw->hw->samples)) { |
1d14ffa9 FB |
980 | dolog ("live=%d sw->hw->samples=%d\n", live, sw->hw->samples); |
981 | return 0; | |
982 | } | |
983 | ||
984 | ldebug ( | |
26a76461 | 985 | "%s: get_avail live %d ret %" PRId64 "\n", |
c0fe3827 | 986 | SW_NAME (sw), |
1d14ffa9 FB |
987 | live, (((int64_t) live << 32) / sw->ratio) << sw->info.shift |
988 | ); | |
989 | ||
990 | return (((int64_t) live << 32) / sw->ratio) << sw->info.shift; | |
991 | } | |
992 | ||
993 | static int audio_get_free (SWVoiceOut *sw) | |
994 | { | |
995 | int live, dead; | |
996 | ||
997 | if (!sw) { | |
998 | return 0; | |
999 | } | |
1000 | ||
1001 | live = sw->total_hw_samples_mixed; | |
1002 | ||
470bcabd | 1003 | if (audio_bug(__func__, live < 0 || live > sw->hw->samples)) { |
1d14ffa9 | 1004 | dolog ("live=%d sw->hw->samples=%d\n", live, sw->hw->samples); |
c0fe3827 | 1005 | return 0; |
1d14ffa9 FB |
1006 | } |
1007 | ||
1008 | dead = sw->hw->samples - live; | |
1009 | ||
1010 | #ifdef DEBUG_OUT | |
26a76461 | 1011 | dolog ("%s: get_free live %d dead %d ret %" PRId64 "\n", |
c0fe3827 | 1012 | SW_NAME (sw), |
1d14ffa9 | 1013 | live, dead, (((int64_t) dead << 32) / sw->ratio) << sw->info.shift); |
85571bc7 | 1014 | #endif |
1d14ffa9 FB |
1015 | |
1016 | return (((int64_t) dead << 32) / sw->ratio) << sw->info.shift; | |
1017 | } | |
1018 | ||
8ead62cf FB |
1019 | static void audio_capture_mix_and_clear (HWVoiceOut *hw, int rpos, int samples) |
1020 | { | |
1021 | int n; | |
1022 | ||
1023 | if (hw->enabled) { | |
ec36b695 | 1024 | SWVoiceCap *sc; |
8ead62cf | 1025 | |
ec36b695 FB |
1026 | for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) { |
1027 | SWVoiceOut *sw = &sc->sw; | |
8ead62cf FB |
1028 | int rpos2 = rpos; |
1029 | ||
1030 | n = samples; | |
1031 | while (n) { | |
1032 | int till_end_of_hw = hw->samples - rpos2; | |
1033 | int to_write = audio_MIN (till_end_of_hw, n); | |
1034 | int bytes = to_write << hw->info.shift; | |
1035 | int written; | |
1036 | ||
1037 | sw->buf = hw->mix_buf + rpos2; | |
1038 | written = audio_pcm_sw_write (sw, NULL, bytes); | |
1039 | if (written - bytes) { | |
ec36b695 FB |
1040 | dolog ("Could not mix %d bytes into a capture " |
1041 | "buffer, mixed %d\n", | |
1042 | bytes, written); | |
8ead62cf FB |
1043 | break; |
1044 | } | |
1045 | n -= to_write; | |
1046 | rpos2 = (rpos2 + to_write) % hw->samples; | |
1047 | } | |
1048 | } | |
1049 | } | |
1050 | ||
1051 | n = audio_MIN (samples, hw->samples - rpos); | |
1052 | mixeng_clear (hw->mix_buf + rpos, n); | |
1053 | mixeng_clear (hw->mix_buf, samples - n); | |
1054 | } | |
1055 | ||
c0fe3827 | 1056 | static void audio_run_out (AudioState *s) |
1d14ffa9 FB |
1057 | { |
1058 | HWVoiceOut *hw = NULL; | |
1059 | SWVoiceOut *sw; | |
1060 | ||
1a7dafce | 1061 | while ((hw = audio_pcm_hw_find_any_enabled_out (hw))) { |
1d14ffa9 | 1062 | int played; |
8ead62cf | 1063 | int live, free, nb_live, cleanup_required, prev_rpos; |
1d14ffa9 | 1064 | |
bdff253c | 1065 | live = audio_pcm_hw_get_live_out (hw, &nb_live); |
1d14ffa9 FB |
1066 | if (!nb_live) { |
1067 | live = 0; | |
1068 | } | |
c0fe3827 | 1069 | |
470bcabd | 1070 | if (audio_bug(__func__, live < 0 || live > hw->samples)) { |
1d14ffa9 | 1071 | dolog ("live=%d hw->samples=%d\n", live, hw->samples); |
c0fe3827 | 1072 | continue; |
1d14ffa9 FB |
1073 | } |
1074 | ||
1075 | if (hw->pending_disable && !nb_live) { | |
ec36b695 | 1076 | SWVoiceCap *sc; |
1d14ffa9 FB |
1077 | #ifdef DEBUG_OUT |
1078 | dolog ("Disabling voice\n"); | |
85571bc7 | 1079 | #endif |
1d14ffa9 FB |
1080 | hw->enabled = 0; |
1081 | hw->pending_disable = 0; | |
1082 | hw->pcm_ops->ctl_out (hw, VOICE_DISABLE); | |
ec36b695 FB |
1083 | for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) { |
1084 | sc->sw.active = 0; | |
1085 | audio_recalc_and_notify_capture (sc->cap); | |
8ead62cf | 1086 | } |
1d14ffa9 FB |
1087 | continue; |
1088 | } | |
1089 | ||
1090 | if (!live) { | |
1091 | for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) { | |
1092 | if (sw->active) { | |
1093 | free = audio_get_free (sw); | |
1094 | if (free > 0) { | |
1095 | sw->callback.fn (sw->callback.opaque, free); | |
1096 | } | |
1097 | } | |
1098 | } | |
1099 | continue; | |
1100 | } | |
1101 | ||
8ead62cf | 1102 | prev_rpos = hw->rpos; |
bdff253c | 1103 | played = hw->pcm_ops->run_out (hw, live); |
3d4d16f4 | 1104 | replay_audio_out(&played); |
470bcabd | 1105 | if (audio_bug(__func__, hw->rpos >= hw->samples)) { |
1d14ffa9 FB |
1106 | dolog ("hw->rpos=%d hw->samples=%d played=%d\n", |
1107 | hw->rpos, hw->samples, played); | |
1108 | hw->rpos = 0; | |
1109 | } | |
1110 | ||
1111 | #ifdef DEBUG_OUT | |
c0fe3827 | 1112 | dolog ("played=%d\n", played); |
85571bc7 | 1113 | #endif |
1d14ffa9 FB |
1114 | |
1115 | if (played) { | |
1116 | hw->ts_helper += played; | |
8ead62cf | 1117 | audio_capture_mix_and_clear (hw, prev_rpos, played); |
1d14ffa9 FB |
1118 | } |
1119 | ||
c0fe3827 | 1120 | cleanup_required = 0; |
1d14ffa9 | 1121 | for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) { |
1d14ffa9 FB |
1122 | if (!sw->active && sw->empty) { |
1123 | continue; | |
1124 | } | |
1125 | ||
470bcabd | 1126 | if (audio_bug(__func__, played > sw->total_hw_samples_mixed)) { |
1d14ffa9 FB |
1127 | dolog ("played=%d sw->total_hw_samples_mixed=%d\n", |
1128 | played, sw->total_hw_samples_mixed); | |
1129 | played = sw->total_hw_samples_mixed; | |
1130 | } | |
1131 | ||
1132 | sw->total_hw_samples_mixed -= played; | |
1133 | ||
1134 | if (!sw->total_hw_samples_mixed) { | |
1135 | sw->empty = 1; | |
c0fe3827 | 1136 | cleanup_required |= !sw->active && !sw->callback.fn; |
1d14ffa9 FB |
1137 | } |
1138 | ||
1139 | if (sw->active) { | |
1140 | free = audio_get_free (sw); | |
1141 | if (free > 0) { | |
1142 | sw->callback.fn (sw->callback.opaque, free); | |
1143 | } | |
1144 | } | |
1145 | } | |
c0fe3827 FB |
1146 | |
1147 | if (cleanup_required) { | |
ec36b695 FB |
1148 | SWVoiceOut *sw1; |
1149 | ||
1150 | sw = hw->sw_head.lh_first; | |
1151 | while (sw) { | |
1152 | sw1 = sw->entries.le_next; | |
c0fe3827 | 1153 | if (!sw->active && !sw->callback.fn) { |
1a7dafce | 1154 | audio_close_out (sw); |
c0fe3827 | 1155 | } |
ec36b695 | 1156 | sw = sw1; |
c0fe3827 FB |
1157 | } |
1158 | } | |
1d14ffa9 FB |
1159 | } |
1160 | } | |
1161 | ||
c0fe3827 | 1162 | static void audio_run_in (AudioState *s) |
1d14ffa9 FB |
1163 | { |
1164 | HWVoiceIn *hw = NULL; | |
1165 | ||
1a7dafce | 1166 | while ((hw = audio_pcm_hw_find_any_enabled_in (hw))) { |
1d14ffa9 | 1167 | SWVoiceIn *sw; |
3d4d16f4 | 1168 | int captured = 0, min; |
1d14ffa9 | 1169 | |
3d4d16f4 PD |
1170 | if (replay_mode != REPLAY_MODE_PLAY) { |
1171 | captured = hw->pcm_ops->run_in(hw); | |
1172 | } | |
1173 | replay_audio_in(&captured, hw->conv_buf, &hw->wpos, hw->samples); | |
1d14ffa9 FB |
1174 | |
1175 | min = audio_pcm_hw_find_min_in (hw); | |
1176 | hw->total_samples_captured += captured - min; | |
1177 | hw->ts_helper += captured; | |
1178 | ||
1179 | for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) { | |
1180 | sw->total_hw_samples_acquired -= min; | |
1181 | ||
1182 | if (sw->active) { | |
1183 | int avail; | |
1184 | ||
1185 | avail = audio_get_avail (sw); | |
1186 | if (avail > 0) { | |
1187 | sw->callback.fn (sw->callback.opaque, avail); | |
1188 | } | |
1189 | } | |
1190 | } | |
1191 | } | |
1192 | } | |
1193 | ||
8ead62cf FB |
1194 | static void audio_run_capture (AudioState *s) |
1195 | { | |
1196 | CaptureVoiceOut *cap; | |
1197 | ||
1198 | for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) { | |
1199 | int live, rpos, captured; | |
1200 | HWVoiceOut *hw = &cap->hw; | |
1201 | SWVoiceOut *sw; | |
1202 | ||
bdff253c | 1203 | captured = live = audio_pcm_hw_get_live_out (hw, NULL); |
8ead62cf FB |
1204 | rpos = hw->rpos; |
1205 | while (live) { | |
1206 | int left = hw->samples - rpos; | |
1207 | int to_capture = audio_MIN (live, left); | |
1ea879e5 | 1208 | struct st_sample *src; |
8ead62cf FB |
1209 | struct capture_callback *cb; |
1210 | ||
1211 | src = hw->mix_buf + rpos; | |
1212 | hw->clip (cap->buf, src, to_capture); | |
1213 | mixeng_clear (src, to_capture); | |
1214 | ||
1215 | for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) { | |
1216 | cb->ops.capture (cb->opaque, cap->buf, | |
1217 | to_capture << hw->info.shift); | |
1218 | } | |
1219 | rpos = (rpos + to_capture) % hw->samples; | |
1220 | live -= to_capture; | |
1221 | } | |
1222 | hw->rpos = rpos; | |
1223 | ||
1224 | for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) { | |
1225 | if (!sw->active && sw->empty) { | |
1226 | continue; | |
1227 | } | |
1228 | ||
470bcabd | 1229 | if (audio_bug(__func__, captured > sw->total_hw_samples_mixed)) { |
8ead62cf FB |
1230 | dolog ("captured=%d sw->total_hw_samples_mixed=%d\n", |
1231 | captured, sw->total_hw_samples_mixed); | |
1232 | captured = sw->total_hw_samples_mixed; | |
1233 | } | |
1234 | ||
1235 | sw->total_hw_samples_mixed -= captured; | |
1236 | sw->empty = sw->total_hw_samples_mixed == 0; | |
1237 | } | |
1238 | } | |
1239 | } | |
1240 | ||
713a98f8 | 1241 | void audio_run (const char *msg) |
571ec3d6 | 1242 | { |
713a98f8 | 1243 | AudioState *s = &glob_audio_state; |
571ec3d6 FB |
1244 | |
1245 | audio_run_out (s); | |
1246 | audio_run_in (s); | |
8ead62cf | 1247 | audio_run_capture (s); |
713a98f8 | 1248 | #ifdef DEBUG_POLL |
1249 | { | |
1250 | static double prevtime; | |
1251 | double currtime; | |
1252 | struct timeval tv; | |
571ec3d6 | 1253 | |
713a98f8 | 1254 | if (gettimeofday (&tv, NULL)) { |
1255 | perror ("audio_run: gettimeofday"); | |
1256 | return; | |
1257 | } | |
1258 | ||
1259 | currtime = tv.tv_sec + tv.tv_usec * 1e-6; | |
1260 | dolog ("Elapsed since last %s: %f\n", msg, currtime - prevtime); | |
1261 | prevtime = currtime; | |
1262 | } | |
1263 | #endif | |
571ec3d6 FB |
1264 | } |
1265 | ||
71830221 KZ |
1266 | static int audio_driver_init(AudioState *s, struct audio_driver *drv, |
1267 | bool msg, Audiodev *dev) | |
85571bc7 | 1268 | { |
71830221 | 1269 | s->drv_opaque = drv->init(dev); |
1d14ffa9 | 1270 | |
c0fe3827 | 1271 | if (s->drv_opaque) { |
1a7dafce | 1272 | audio_init_nb_voices_out (drv); |
1273 | audio_init_nb_voices_in (drv); | |
c0fe3827 | 1274 | s->drv = drv; |
1d14ffa9 | 1275 | return 0; |
85571bc7 FB |
1276 | } |
1277 | else { | |
fb37ce92 GH |
1278 | if (msg) { |
1279 | dolog("Could not init `%s' audio driver\n", drv->name); | |
1280 | } | |
1d14ffa9 | 1281 | return -1; |
85571bc7 FB |
1282 | } |
1283 | } | |
1284 | ||
9781e040 | 1285 | static void audio_vm_change_state_handler (void *opaque, int running, |
1dfb4dd9 | 1286 | RunState state) |
85571bc7 | 1287 | { |
c0fe3827 | 1288 | AudioState *s = opaque; |
1d14ffa9 FB |
1289 | HWVoiceOut *hwo = NULL; |
1290 | HWVoiceIn *hwi = NULL; | |
541e0844 | 1291 | int op = running ? VOICE_ENABLE : VOICE_DISABLE; |
1d14ffa9 | 1292 | |
978dd635 | 1293 | s->vm_running = running; |
1a7dafce | 1294 | while ((hwo = audio_pcm_hw_find_any_enabled_out (hwo))) { |
05d2f2a6 | 1295 | hwo->pcm_ops->ctl_out(hwo, op); |
1d14ffa9 | 1296 | } |
85571bc7 | 1297 | |
1a7dafce | 1298 | while ((hwi = audio_pcm_hw_find_any_enabled_in (hwi))) { |
05d2f2a6 | 1299 | hwi->pcm_ops->ctl_in(hwi, op); |
85571bc7 | 1300 | } |
39deb1e4 | 1301 | audio_reset_timer (s); |
85571bc7 FB |
1302 | } |
1303 | ||
a384c205 MAL |
1304 | static bool is_cleaning_up; |
1305 | ||
1306 | bool audio_is_cleaning_up(void) | |
1307 | { | |
1308 | return is_cleaning_up; | |
1309 | } | |
1310 | ||
1311 | void audio_cleanup(void) | |
85571bc7 | 1312 | { |
c0fe3827 | 1313 | AudioState *s = &glob_audio_state; |
a384c205 MAL |
1314 | HWVoiceOut *hwo, *hwon; |
1315 | HWVoiceIn *hwi, *hwin; | |
1d14ffa9 | 1316 | |
a384c205 MAL |
1317 | is_cleaning_up = true; |
1318 | QLIST_FOREACH_SAFE(hwo, &glob_audio_state.hw_head_out, entries, hwon) { | |
ec36b695 | 1319 | SWVoiceCap *sc; |
8ead62cf | 1320 | |
aeb29b64 JK |
1321 | if (hwo->enabled) { |
1322 | hwo->pcm_ops->ctl_out (hwo, VOICE_DISABLE); | |
1323 | } | |
1d14ffa9 | 1324 | hwo->pcm_ops->fini_out (hwo); |
8ead62cf | 1325 | |
ec36b695 FB |
1326 | for (sc = hwo->cap_head.lh_first; sc; sc = sc->entries.le_next) { |
1327 | CaptureVoiceOut *cap = sc->cap; | |
1328 | struct capture_callback *cb; | |
1329 | ||
1330 | for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) { | |
1331 | cb->ops.destroy (cb->opaque); | |
1332 | } | |
8ead62cf | 1333 | } |
a384c205 | 1334 | QLIST_REMOVE(hwo, entries); |
1d14ffa9 | 1335 | } |
85571bc7 | 1336 | |
a384c205 | 1337 | QLIST_FOREACH_SAFE(hwi, &glob_audio_state.hw_head_in, entries, hwin) { |
aeb29b64 JK |
1338 | if (hwi->enabled) { |
1339 | hwi->pcm_ops->ctl_in (hwi, VOICE_DISABLE); | |
1340 | } | |
1d14ffa9 | 1341 | hwi->pcm_ops->fini_in (hwi); |
a384c205 | 1342 | QLIST_REMOVE(hwi, entries); |
85571bc7 | 1343 | } |
c0fe3827 FB |
1344 | |
1345 | if (s->drv) { | |
1346 | s->drv->fini (s->drv_opaque); | |
a384c205 | 1347 | s->drv = NULL; |
c0fe3827 | 1348 | } |
71830221 KZ |
1349 | |
1350 | if (s->dev) { | |
1351 | qapi_free_Audiodev(s->dev); | |
1352 | s->dev = NULL; | |
1353 | } | |
85571bc7 FB |
1354 | } |
1355 | ||
d959fce9 JQ |
1356 | static const VMStateDescription vmstate_audio = { |
1357 | .name = "audio", | |
1358 | .version_id = 1, | |
1359 | .minimum_version_id = 1, | |
35d08458 | 1360 | .fields = (VMStateField[]) { |
d959fce9 | 1361 | VMSTATE_END_OF_LIST() |
1d14ffa9 | 1362 | } |
d959fce9 | 1363 | }; |
85571bc7 | 1364 | |
71830221 KZ |
1365 | static void audio_validate_opts(Audiodev *dev, Error **errp); |
1366 | ||
1367 | static AudiodevListEntry *audiodev_find( | |
1368 | AudiodevListHead *head, const char *drvname) | |
1369 | { | |
1370 | AudiodevListEntry *e; | |
1371 | QSIMPLEQ_FOREACH(e, head, next) { | |
1372 | if (strcmp(AudiodevDriver_str(e->dev->driver), drvname) == 0) { | |
1373 | return e; | |
1374 | } | |
1375 | } | |
1376 | ||
1377 | return NULL; | |
1378 | } | |
1379 | ||
1380 | static int audio_init(Audiodev *dev) | |
85571bc7 | 1381 | { |
1d14ffa9 | 1382 | size_t i; |
85571bc7 | 1383 | int done = 0; |
71830221 | 1384 | const char *drvname = NULL; |
713a98f8 | 1385 | VMChangeStateEntry *e; |
c0fe3827 | 1386 | AudioState *s = &glob_audio_state; |
d3893a39 | 1387 | struct audio_driver *driver; |
71830221 KZ |
1388 | /* silence gcc warning about uninitialized variable */ |
1389 | AudiodevListHead head = QSIMPLEQ_HEAD_INITIALIZER(head); | |
1d14ffa9 | 1390 | |
0d9acba8 | 1391 | if (s->drv) { |
71830221 KZ |
1392 | if (dev) { |
1393 | dolog("Cannot create more than one audio backend, sorry\n"); | |
1394 | qapi_free_Audiodev(dev); | |
1395 | } | |
1396 | return -1; | |
0d9acba8 PB |
1397 | } |
1398 | ||
71830221 KZ |
1399 | if (dev) { |
1400 | /* -audiodev option */ | |
1401 | drvname = AudiodevDriver_str(dev->driver); | |
1402 | } else { | |
1403 | /* legacy implicit initialization */ | |
1404 | head = audio_handle_legacy_opts(); | |
1405 | /* | |
1406 | * In case of legacy initialization, all Audiodevs in the list will have | |
1407 | * the same configuration (except the driver), so it does't matter which | |
1408 | * one we chose. We need an Audiodev to set up AudioState before we can | |
1409 | * init a driver. Also note that dev at this point is still in the | |
1410 | * list. | |
1411 | */ | |
1412 | dev = QSIMPLEQ_FIRST(&head)->dev; | |
1413 | audio_validate_opts(dev, &error_abort); | |
1414 | } | |
1415 | s->dev = dev; | |
1416 | ||
72cf2d4f BS |
1417 | QLIST_INIT (&s->hw_head_out); |
1418 | QLIST_INIT (&s->hw_head_in); | |
1419 | QLIST_INIT (&s->cap_head); | |
a384c205 | 1420 | atexit(audio_cleanup); |
571ec3d6 | 1421 | |
bc72ad67 | 1422 | s->ts = timer_new_ns(QEMU_CLOCK_VIRTUAL, audio_timer, s); |
571ec3d6 | 1423 | |
71830221 KZ |
1424 | s->nb_hw_voices_out = audio_get_pdo_out(dev)->voices; |
1425 | s->nb_hw_voices_in = audio_get_pdo_in(dev)->voices; | |
c0fe3827 | 1426 | |
1d14ffa9 | 1427 | if (s->nb_hw_voices_out <= 0) { |
571ec3d6 | 1428 | dolog ("Bogus number of playback voices %d, setting to 1\n", |
1d14ffa9 FB |
1429 | s->nb_hw_voices_out); |
1430 | s->nb_hw_voices_out = 1; | |
1431 | } | |
1432 | ||
1433 | if (s->nb_hw_voices_in <= 0) { | |
571ec3d6 | 1434 | dolog ("Bogus number of capture voices %d, setting to 0\n", |
1d14ffa9 | 1435 | s->nb_hw_voices_in); |
571ec3d6 | 1436 | s->nb_hw_voices_in = 0; |
1d14ffa9 | 1437 | } |
85571bc7 | 1438 | |
85571bc7 | 1439 | if (drvname) { |
d3893a39 GH |
1440 | driver = audio_driver_lookup(drvname); |
1441 | if (driver) { | |
71830221 | 1442 | done = !audio_driver_init(s, driver, true, dev); |
d3893a39 | 1443 | } else { |
85571bc7 FB |
1444 | dolog ("Unknown audio driver `%s'\n", drvname); |
1445 | } | |
71830221 KZ |
1446 | } else { |
1447 | for (i = 0; audio_prio_list[i]; i++) { | |
1448 | AudiodevListEntry *e = audiodev_find(&head, audio_prio_list[i]); | |
d3893a39 | 1449 | driver = audio_driver_lookup(audio_prio_list[i]); |
71830221 KZ |
1450 | |
1451 | if (e && driver) { | |
1452 | s->dev = dev = e->dev; | |
1453 | audio_validate_opts(dev, &error_abort); | |
1454 | done = !audio_driver_init(s, driver, false, dev); | |
1455 | if (done) { | |
1456 | e->dev = NULL; | |
1457 | break; | |
1458 | } | |
1d14ffa9 | 1459 | } |
85571bc7 FB |
1460 | } |
1461 | } | |
71830221 | 1462 | audio_free_audiodev_list(&head); |
85571bc7 | 1463 | |
85571bc7 | 1464 | if (!done) { |
d3893a39 | 1465 | driver = audio_driver_lookup("none"); |
71830221 | 1466 | done = !audio_driver_init(s, driver, false, dev); |
7e274652 MA |
1467 | assert(done); |
1468 | dolog("warning: Using timer based audio emulation\n"); | |
85571bc7 | 1469 | } |
1d14ffa9 | 1470 | |
71830221 KZ |
1471 | if (dev->timer_period <= 0) { |
1472 | s->period_ticks = 1; | |
0d9acba8 | 1473 | } else { |
be1092af | 1474 | s->period_ticks = dev->timer_period * SCALE_US; |
1d14ffa9 | 1475 | } |
0d9acba8 PB |
1476 | |
1477 | e = qemu_add_vm_change_state_handler (audio_vm_change_state_handler, s); | |
1478 | if (!e) { | |
1479 | dolog ("warning: Could not register change state handler\n" | |
1480 | "(Audio can continue looping even after stopping the VM)\n"); | |
1d14ffa9 FB |
1481 | } |
1482 | ||
72cf2d4f | 1483 | QLIST_INIT (&s->card_head); |
0be71e32 | 1484 | vmstate_register (NULL, 0, &vmstate_audio, s); |
71830221 KZ |
1485 | return 0; |
1486 | } | |
1487 | ||
1488 | void audio_free_audiodev_list(AudiodevListHead *head) | |
1489 | { | |
1490 | AudiodevListEntry *e; | |
1491 | while ((e = QSIMPLEQ_FIRST(head))) { | |
1492 | QSIMPLEQ_REMOVE_HEAD(head, next); | |
1493 | qapi_free_Audiodev(e->dev); | |
1494 | g_free(e); | |
1495 | } | |
85571bc7 | 1496 | } |
8ead62cf | 1497 | |
1a7dafce | 1498 | void AUD_register_card (const char *name, QEMUSoundCard *card) |
1499 | { | |
71830221 | 1500 | audio_init(NULL); |
7267c094 | 1501 | card->name = g_strdup (name); |
1a7dafce | 1502 | memset (&card->entries, 0, sizeof (card->entries)); |
72cf2d4f | 1503 | QLIST_INSERT_HEAD (&glob_audio_state.card_head, card, entries); |
1a7dafce | 1504 | } |
1505 | ||
1506 | void AUD_remove_card (QEMUSoundCard *card) | |
1507 | { | |
72cf2d4f | 1508 | QLIST_REMOVE (card, entries); |
7267c094 | 1509 | g_free (card->name); |
1a7dafce | 1510 | } |
1511 | ||
1512 | ||
ec36b695 | 1513 | CaptureVoiceOut *AUD_add_capture ( |
1ea879e5 | 1514 | struct audsettings *as, |
8ead62cf FB |
1515 | struct audio_capture_ops *ops, |
1516 | void *cb_opaque | |
1517 | ) | |
1518 | { | |
1a7dafce | 1519 | AudioState *s = &glob_audio_state; |
8ead62cf FB |
1520 | CaptureVoiceOut *cap; |
1521 | struct capture_callback *cb; | |
1522 | ||
ec36b695 | 1523 | if (audio_validate_settings (as)) { |
8ead62cf FB |
1524 | dolog ("Invalid settings were passed when trying to add capture\n"); |
1525 | audio_print_settings (as); | |
e8d85444 | 1526 | return NULL; |
8ead62cf FB |
1527 | } |
1528 | ||
e8d85444 | 1529 | cb = g_malloc0(sizeof(*cb)); |
8ead62cf FB |
1530 | cb->ops = *ops; |
1531 | cb->opaque = cb_opaque; | |
1532 | ||
1a7dafce | 1533 | cap = audio_pcm_capture_find_specific (as); |
8ead62cf | 1534 | if (cap) { |
72cf2d4f | 1535 | QLIST_INSERT_HEAD (&cap->cb_head, cb, entries); |
ec36b695 | 1536 | return cap; |
8ead62cf FB |
1537 | } |
1538 | else { | |
1539 | HWVoiceOut *hw; | |
1540 | CaptureVoiceOut *cap; | |
1541 | ||
e8d85444 | 1542 | cap = g_malloc0(sizeof(*cap)); |
8ead62cf FB |
1543 | |
1544 | hw = &cap->hw; | |
72cf2d4f BS |
1545 | QLIST_INIT (&hw->sw_head); |
1546 | QLIST_INIT (&cap->cb_head); | |
8ead62cf FB |
1547 | |
1548 | /* XXX find a more elegant way */ | |
1549 | hw->samples = 4096 * 4; | |
e8d85444 | 1550 | hw->mix_buf = g_new0(struct st_sample, hw->samples); |
8ead62cf | 1551 | |
d929eba5 | 1552 | audio_pcm_init_info (&hw->info, as); |
8ead62cf | 1553 | |
e8d85444 | 1554 | cap->buf = g_malloc0_n(hw->samples, 1 << hw->info.shift); |
8ead62cf FB |
1555 | |
1556 | hw->clip = mixeng_clip | |
1557 | [hw->info.nchannels == 2] | |
1558 | [hw->info.sign] | |
d929eba5 | 1559 | [hw->info.swap_endianness] |
f941aa25 | 1560 | [audio_bits_to_index (hw->info.bits)]; |
8ead62cf | 1561 | |
72cf2d4f BS |
1562 | QLIST_INSERT_HEAD (&s->cap_head, cap, entries); |
1563 | QLIST_INSERT_HEAD (&cap->cb_head, cb, entries); | |
8ead62cf | 1564 | |
a384c205 | 1565 | QLIST_FOREACH(hw, &glob_audio_state.hw_head_out, entries) { |
1a7dafce | 1566 | audio_attach_capture (hw); |
8ead62cf | 1567 | } |
ec36b695 | 1568 | return cap; |
ec36b695 FB |
1569 | } |
1570 | } | |
1571 | ||
1572 | void AUD_del_capture (CaptureVoiceOut *cap, void *cb_opaque) | |
1573 | { | |
1574 | struct capture_callback *cb; | |
1575 | ||
1576 | for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) { | |
1577 | if (cb->opaque == cb_opaque) { | |
1578 | cb->ops.destroy (cb_opaque); | |
72cf2d4f | 1579 | QLIST_REMOVE (cb, entries); |
7267c094 | 1580 | g_free (cb); |
ec36b695 FB |
1581 | |
1582 | if (!cap->cb_head.lh_first) { | |
1583 | SWVoiceOut *sw = cap->hw.sw_head.lh_first, *sw1; | |
a3c25997 | 1584 | |
ec36b695 | 1585 | while (sw) { |
a3c25997 | 1586 | SWVoiceCap *sc = (SWVoiceCap *) sw; |
ec36b695 FB |
1587 | #ifdef DEBUG_CAPTURE |
1588 | dolog ("freeing %s\n", sw->name); | |
1589 | #endif | |
a3c25997 | 1590 | |
ec36b695 FB |
1591 | sw1 = sw->entries.le_next; |
1592 | if (sw->rate) { | |
1593 | st_rate_stop (sw->rate); | |
1594 | sw->rate = NULL; | |
1595 | } | |
72cf2d4f BS |
1596 | QLIST_REMOVE (sw, entries); |
1597 | QLIST_REMOVE (sc, entries); | |
7267c094 | 1598 | g_free (sc); |
ec36b695 FB |
1599 | sw = sw1; |
1600 | } | |
72cf2d4f | 1601 | QLIST_REMOVE (cap, entries); |
3268a845 GH |
1602 | g_free (cap->hw.mix_buf); |
1603 | g_free (cap->buf); | |
7267c094 | 1604 | g_free (cap); |
ec36b695 FB |
1605 | } |
1606 | return; | |
1607 | } | |
8ead62cf FB |
1608 | } |
1609 | } | |
683efdcb AZ |
1610 | |
1611 | void AUD_set_volume_out (SWVoiceOut *sw, int mute, uint8_t lvol, uint8_t rvol) | |
1612 | { | |
1613 | if (sw) { | |
6c95ab94 MAL |
1614 | HWVoiceOut *hw = sw->hw; |
1615 | ||
683efdcb AZ |
1616 | sw->vol.mute = mute; |
1617 | sw->vol.l = nominal_volume.l * lvol / 255; | |
1618 | sw->vol.r = nominal_volume.r * rvol / 255; | |
6c95ab94 MAL |
1619 | |
1620 | if (hw->pcm_ops->ctl_out) { | |
1621 | hw->pcm_ops->ctl_out (hw, VOICE_VOLUME, sw); | |
1622 | } | |
683efdcb AZ |
1623 | } |
1624 | } | |
1625 | ||
1626 | void AUD_set_volume_in (SWVoiceIn *sw, int mute, uint8_t lvol, uint8_t rvol) | |
1627 | { | |
1628 | if (sw) { | |
6c95ab94 MAL |
1629 | HWVoiceIn *hw = sw->hw; |
1630 | ||
683efdcb AZ |
1631 | sw->vol.mute = mute; |
1632 | sw->vol.l = nominal_volume.l * lvol / 255; | |
1633 | sw->vol.r = nominal_volume.r * rvol / 255; | |
6c95ab94 MAL |
1634 | |
1635 | if (hw->pcm_ops->ctl_in) { | |
1636 | hw->pcm_ops->ctl_in (hw, VOICE_VOLUME, sw); | |
1637 | } | |
683efdcb AZ |
1638 | } |
1639 | } | |
71830221 KZ |
1640 | |
1641 | void audio_create_pdos(Audiodev *dev) | |
1642 | { | |
1643 | switch (dev->driver) { | |
1644 | #define CASE(DRIVER, driver, pdo_name) \ | |
1645 | case AUDIODEV_DRIVER_##DRIVER: \ | |
1646 | if (!dev->u.driver.has_in) { \ | |
1647 | dev->u.driver.in = g_malloc0( \ | |
1648 | sizeof(Audiodev##pdo_name##PerDirectionOptions)); \ | |
1649 | dev->u.driver.has_in = true; \ | |
1650 | } \ | |
1651 | if (!dev->u.driver.has_out) { \ | |
1652 | dev->u.driver.out = g_malloc0( \ | |
1653 | sizeof(AudiodevAlsaPerDirectionOptions)); \ | |
1654 | dev->u.driver.has_out = true; \ | |
1655 | } \ | |
1656 | break | |
1657 | ||
1658 | CASE(NONE, none, ); | |
1659 | CASE(ALSA, alsa, Alsa); | |
1660 | CASE(COREAUDIO, coreaudio, Coreaudio); | |
1661 | CASE(DSOUND, dsound, ); | |
1662 | CASE(OSS, oss, Oss); | |
1663 | CASE(PA, pa, Pa); | |
1664 | CASE(SDL, sdl, ); | |
1665 | CASE(SPICE, spice, ); | |
1666 | CASE(WAV, wav, ); | |
1667 | ||
1668 | case AUDIODEV_DRIVER__MAX: | |
1669 | abort(); | |
1670 | }; | |
1671 | } | |
1672 | ||
1673 | static void audio_validate_per_direction_opts( | |
1674 | AudiodevPerDirectionOptions *pdo, Error **errp) | |
1675 | { | |
1676 | if (!pdo->has_fixed_settings) { | |
1677 | pdo->has_fixed_settings = true; | |
1678 | pdo->fixed_settings = true; | |
1679 | } | |
1680 | if (!pdo->fixed_settings && | |
1681 | (pdo->has_frequency || pdo->has_channels || pdo->has_format)) { | |
1682 | error_setg(errp, | |
1683 | "You can't use frequency, channels or format with fixed-settings=off"); | |
1684 | return; | |
1685 | } | |
1686 | ||
1687 | if (!pdo->has_frequency) { | |
1688 | pdo->has_frequency = true; | |
1689 | pdo->frequency = 44100; | |
1690 | } | |
1691 | if (!pdo->has_channels) { | |
1692 | pdo->has_channels = true; | |
1693 | pdo->channels = 2; | |
1694 | } | |
1695 | if (!pdo->has_voices) { | |
1696 | pdo->has_voices = true; | |
1697 | pdo->voices = 1; | |
1698 | } | |
1699 | if (!pdo->has_format) { | |
1700 | pdo->has_format = true; | |
1701 | pdo->format = AUDIO_FORMAT_S16; | |
1702 | } | |
1703 | } | |
1704 | ||
1705 | static void audio_validate_opts(Audiodev *dev, Error **errp) | |
1706 | { | |
1707 | Error *err = NULL; | |
1708 | ||
1709 | audio_create_pdos(dev); | |
1710 | ||
1711 | audio_validate_per_direction_opts(audio_get_pdo_in(dev), &err); | |
1712 | if (err) { | |
1713 | error_propagate(errp, err); | |
1714 | return; | |
1715 | } | |
1716 | ||
1717 | audio_validate_per_direction_opts(audio_get_pdo_out(dev), &err); | |
1718 | if (err) { | |
1719 | error_propagate(errp, err); | |
1720 | return; | |
1721 | } | |
1722 | ||
1723 | if (!dev->has_timer_period) { | |
1724 | dev->has_timer_period = true; | |
1725 | dev->timer_period = 10000; /* 100Hz -> 10ms */ | |
1726 | } | |
1727 | } | |
1728 | ||
1729 | void audio_parse_option(const char *opt) | |
1730 | { | |
1731 | AudiodevListEntry *e; | |
1732 | Audiodev *dev = NULL; | |
1733 | ||
1734 | Visitor *v = qobject_input_visitor_new_str(opt, "driver", &error_fatal); | |
1735 | visit_type_Audiodev(v, NULL, &dev, &error_fatal); | |
1736 | visit_free(v); | |
1737 | ||
1738 | audio_validate_opts(dev, &error_fatal); | |
1739 | ||
1740 | e = g_malloc0(sizeof(AudiodevListEntry)); | |
1741 | e->dev = dev; | |
1742 | QSIMPLEQ_INSERT_TAIL(&audiodevs, e, next); | |
1743 | } | |
1744 | ||
1745 | void audio_init_audiodevs(void) | |
1746 | { | |
1747 | AudiodevListEntry *e; | |
1748 | ||
1749 | QSIMPLEQ_FOREACH(e, &audiodevs, next) { | |
1750 | audio_init(e->dev); | |
1751 | } | |
1752 | } | |
1753 | ||
1754 | audsettings audiodev_to_audsettings(AudiodevPerDirectionOptions *pdo) | |
1755 | { | |
1756 | return (audsettings) { | |
1757 | .freq = pdo->frequency, | |
1758 | .nchannels = pdo->channels, | |
1759 | .fmt = pdo->format, | |
1760 | .endianness = AUDIO_HOST_ENDIANNESS, | |
1761 | }; | |
1762 | } | |
1763 | ||
1764 | int audioformat_bytes_per_sample(AudioFormat fmt) | |
1765 | { | |
1766 | switch (fmt) { | |
1767 | case AUDIO_FORMAT_U8: | |
1768 | case AUDIO_FORMAT_S8: | |
1769 | return 1; | |
1770 | ||
1771 | case AUDIO_FORMAT_U16: | |
1772 | case AUDIO_FORMAT_S16: | |
1773 | return 2; | |
1774 | ||
1775 | case AUDIO_FORMAT_U32: | |
1776 | case AUDIO_FORMAT_S32: | |
1777 | return 4; | |
1778 | ||
1779 | case AUDIO_FORMAT__MAX: | |
1780 | ; | |
1781 | } | |
1782 | abort(); | |
1783 | } | |
1784 | ||
1785 | ||
1786 | /* frames = freq * usec / 1e6 */ | |
1787 | int audio_buffer_frames(AudiodevPerDirectionOptions *pdo, | |
1788 | audsettings *as, int def_usecs) | |
1789 | { | |
1790 | uint64_t usecs = pdo->has_buffer_length ? pdo->buffer_length : def_usecs; | |
1791 | return (as->freq * usecs + 500000) / 1000000; | |
1792 | } | |
1793 | ||
1794 | /* samples = channels * frames = channels * freq * usec / 1e6 */ | |
1795 | int audio_buffer_samples(AudiodevPerDirectionOptions *pdo, | |
1796 | audsettings *as, int def_usecs) | |
1797 | { | |
1798 | return as->nchannels * audio_buffer_frames(pdo, as, def_usecs); | |
1799 | } | |
1800 | ||
1801 | /* | |
1802 | * bytes = bytes_per_sample * samples = | |
1803 | * bytes_per_sample * channels * freq * usec / 1e6 | |
1804 | */ | |
1805 | int audio_buffer_bytes(AudiodevPerDirectionOptions *pdo, | |
1806 | audsettings *as, int def_usecs) | |
1807 | { | |
1808 | return audio_buffer_samples(pdo, as, def_usecs) * | |
1809 | audioformat_bytes_per_sample(as->fmt); | |
1810 | } |