]>
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 | */ | |
0b8fa32f | 24 | |
6086a565 | 25 | #include "qemu/osdep.h" |
87ecb68b | 26 | #include "audio.h" |
d6454270 | 27 | #include "migration/vmstate.h" |
83c9089e | 28 | #include "monitor/monitor.h" |
1de7afc9 | 29 | #include "qemu/timer.h" |
71830221 | 30 | #include "qapi/error.h" |
637d1809 | 31 | #include "qapi/clone-visitor.h" |
71830221 KZ |
32 | #include "qapi/qobject-input-visitor.h" |
33 | #include "qapi/qapi-visit-audio.h" | |
637d1809 | 34 | #include "qapi/qapi-commands-audio.h" |
f348b6d1 | 35 | #include "qemu/cutils.h" |
b9ae74e2 | 36 | #include "qemu/log.h" |
0b8fa32f | 37 | #include "qemu/module.h" |
5e03b6da | 38 | #include "qemu/help_option.h" |
89fc45d5 | 39 | #include "sysemu/sysemu.h" |
3d4d16f4 | 40 | #include "sysemu/replay.h" |
54d31236 | 41 | #include "sysemu/runstate.h" |
f0c4555e | 42 | #include "ui/qemu-spice.h" |
1a961e78 | 43 | #include "trace.h" |
85571bc7 | 44 | |
1d14ffa9 FB |
45 | #define AUDIO_CAP "audio" |
46 | #include "audio_int.h" | |
85571bc7 | 47 | |
1d14ffa9 FB |
48 | /* #define DEBUG_LIVE */ |
49 | /* #define DEBUG_OUT */ | |
8ead62cf | 50 | /* #define DEBUG_CAPTURE */ |
713a98f8 | 51 | /* #define DEBUG_POLL */ |
85571bc7 | 52 | |
c0fe3827 FB |
53 | #define SW_NAME(sw) (sw)->name ? (sw)->name : "unknown" |
54 | ||
2358a494 JQ |
55 | |
56 | /* Order of CONFIG_AUDIO_DRIVERS is import. | |
57 | The 1st one is the one used by default, that is the reason | |
58 | that we generate the list. | |
59 | */ | |
71830221 | 60 | const char *audio_prio_list[] = { |
d3893a39 | 61 | "spice", |
2358a494 | 62 | CONFIG_AUDIO_DRIVERS |
d3893a39 GH |
63 | "none", |
64 | "wav", | |
71830221 | 65 | NULL |
1d14ffa9 | 66 | }; |
85571bc7 | 67 | |
d3893a39 | 68 | static QLIST_HEAD(, audio_driver) audio_drivers; |
71830221 | 69 | static AudiodevListHead audiodevs = QSIMPLEQ_HEAD_INITIALIZER(audiodevs); |
d3893a39 GH |
70 | |
71 | void audio_driver_register(audio_driver *drv) | |
72 | { | |
73 | QLIST_INSERT_HEAD(&audio_drivers, drv, next); | |
74 | } | |
75 | ||
76 | audio_driver *audio_driver_lookup(const char *name) | |
77 | { | |
78 | struct audio_driver *d; | |
c551fb0b CF |
79 | Error *local_err = NULL; |
80 | int rv; | |
d3893a39 GH |
81 | |
82 | QLIST_FOREACH(d, &audio_drivers, next) { | |
83 | if (strcmp(name, d->name) == 0) { | |
84 | return d; | |
85 | } | |
86 | } | |
c551fb0b CF |
87 | rv = audio_module_load(name, &local_err); |
88 | if (rv > 0) { | |
89 | QLIST_FOREACH(d, &audio_drivers, next) { | |
90 | if (strcmp(name, d->name) == 0) { | |
91 | return d; | |
92 | } | |
65ba8696 | 93 | } |
c551fb0b CF |
94 | } else if (rv < 0) { |
95 | error_report_err(local_err); | |
65ba8696 | 96 | } |
d3893a39 GH |
97 | return NULL; |
98 | } | |
99 | ||
ecd97e95 KZ |
100 | static QTAILQ_HEAD(AudioStateHead, AudioState) audio_states = |
101 | QTAILQ_HEAD_INITIALIZER(audio_states); | |
c0fe3827 | 102 | |
00e07679 | 103 | const struct mixeng_volume nominal_volume = { |
14658cd1 | 104 | .mute = 0, |
1d14ffa9 | 105 | #ifdef FLOAT_MIXENG |
14658cd1 JQ |
106 | .r = 1.0, |
107 | .l = 1.0, | |
1d14ffa9 | 108 | #else |
14658cd1 JQ |
109 | .r = 1ULL << 32, |
110 | .l = 1ULL << 32, | |
1d14ffa9 | 111 | #endif |
85571bc7 FB |
112 | }; |
113 | ||
af2041ed KZ |
114 | static bool legacy_config = true; |
115 | ||
1d14ffa9 | 116 | int audio_bug (const char *funcname, int cond) |
85571bc7 | 117 | { |
1d14ffa9 FB |
118 | if (cond) { |
119 | static int shown; | |
120 | ||
8ead62cf | 121 | AUD_log (NULL, "A bug was just triggered in %s\n", funcname); |
1d14ffa9 FB |
122 | if (!shown) { |
123 | shown = 1; | |
124 | AUD_log (NULL, "Save all your work and restart without audio\n"); | |
1d14ffa9 FB |
125 | AUD_log (NULL, "I am sorry\n"); |
126 | } | |
127 | AUD_log (NULL, "Context:\n"); | |
85571bc7 FB |
128 | } |
129 | ||
1d14ffa9 | 130 | return cond; |
85571bc7 FB |
131 | } |
132 | ||
f941aa25 TS |
133 | static inline int audio_bits_to_index (int bits) |
134 | { | |
135 | switch (bits) { | |
136 | case 8: | |
137 | return 0; | |
138 | ||
139 | case 16: | |
140 | return 1; | |
141 | ||
142 | case 32: | |
143 | return 2; | |
144 | ||
145 | default: | |
146 | audio_bug ("bits_to_index", 1); | |
147 | AUD_log (NULL, "invalid bits %d\n", bits); | |
12f4abf6 | 148 | return 0; |
f941aa25 TS |
149 | } |
150 | } | |
151 | ||
541e0844 | 152 | void AUD_vlog (const char *cap, const char *fmt, va_list ap) |
85571bc7 | 153 | { |
06ac27f6 KZ |
154 | if (cap) { |
155 | fprintf(stderr, "%s: ", cap); | |
541e0844 | 156 | } |
541e0844 | 157 | |
06ac27f6 | 158 | vfprintf(stderr, fmt, ap); |
85571bc7 FB |
159 | } |
160 | ||
541e0844 | 161 | void AUD_log (const char *cap, const char *fmt, ...) |
85571bc7 | 162 | { |
541e0844 FB |
163 | va_list ap; |
164 | ||
165 | va_start (ap, fmt); | |
166 | AUD_vlog (cap, fmt, ap); | |
167 | va_end (ap); | |
85571bc7 FB |
168 | } |
169 | ||
1ea879e5 | 170 | static void audio_print_settings (struct audsettings *as) |
c0fe3827 FB |
171 | { |
172 | dolog ("frequency=%d nchannels=%d fmt=", as->freq, as->nchannels); | |
173 | ||
174 | switch (as->fmt) { | |
85bc5852 | 175 | case AUDIO_FORMAT_S8: |
c0fe3827 FB |
176 | AUD_log (NULL, "S8"); |
177 | break; | |
85bc5852 | 178 | case AUDIO_FORMAT_U8: |
c0fe3827 FB |
179 | AUD_log (NULL, "U8"); |
180 | break; | |
85bc5852 | 181 | case AUDIO_FORMAT_S16: |
c0fe3827 FB |
182 | AUD_log (NULL, "S16"); |
183 | break; | |
85bc5852 | 184 | case AUDIO_FORMAT_U16: |
c0fe3827 FB |
185 | AUD_log (NULL, "U16"); |
186 | break; | |
85bc5852 | 187 | case AUDIO_FORMAT_S32: |
d50997f9 | 188 | AUD_log (NULL, "S32"); |
189 | break; | |
85bc5852 | 190 | case AUDIO_FORMAT_U32: |
d50997f9 | 191 | AUD_log (NULL, "U32"); |
192 | break; | |
ed2a4a79 KZ |
193 | case AUDIO_FORMAT_F32: |
194 | AUD_log (NULL, "F32"); | |
195 | break; | |
c0fe3827 FB |
196 | default: |
197 | AUD_log (NULL, "invalid(%d)", as->fmt); | |
198 | break; | |
199 | } | |
ec36b695 FB |
200 | |
201 | AUD_log (NULL, " endianness="); | |
d929eba5 FB |
202 | switch (as->endianness) { |
203 | case 0: | |
204 | AUD_log (NULL, "little"); | |
205 | break; | |
206 | case 1: | |
207 | AUD_log (NULL, "big"); | |
208 | break; | |
209 | default: | |
210 | AUD_log (NULL, "invalid"); | |
211 | break; | |
212 | } | |
c0fe3827 FB |
213 | AUD_log (NULL, "\n"); |
214 | } | |
215 | ||
1ea879e5 | 216 | static int audio_validate_settings (struct audsettings *as) |
c0fe3827 FB |
217 | { |
218 | int invalid; | |
219 | ||
b5c7db3e | 220 | invalid = as->nchannels < 1; |
d929eba5 | 221 | invalid |= as->endianness != 0 && as->endianness != 1; |
c0fe3827 FB |
222 | |
223 | switch (as->fmt) { | |
85bc5852 KZ |
224 | case AUDIO_FORMAT_S8: |
225 | case AUDIO_FORMAT_U8: | |
226 | case AUDIO_FORMAT_S16: | |
227 | case AUDIO_FORMAT_U16: | |
228 | case AUDIO_FORMAT_S32: | |
229 | case AUDIO_FORMAT_U32: | |
ed2a4a79 | 230 | case AUDIO_FORMAT_F32: |
c0fe3827 FB |
231 | break; |
232 | default: | |
233 | invalid = 1; | |
234 | break; | |
235 | } | |
236 | ||
237 | invalid |= as->freq <= 0; | |
d929eba5 | 238 | return invalid ? -1 : 0; |
c0fe3827 FB |
239 | } |
240 | ||
1ea879e5 | 241 | static int audio_pcm_info_eq (struct audio_pcm_info *info, struct audsettings *as) |
85571bc7 | 242 | { |
ed2a4a79 KZ |
243 | int bits = 8; |
244 | bool is_signed = false, is_float = false; | |
85571bc7 | 245 | |
c0fe3827 | 246 | switch (as->fmt) { |
85bc5852 | 247 | case AUDIO_FORMAT_S8: |
ed2a4a79 | 248 | is_signed = true; |
b4bd0b16 | 249 | /* fall through */ |
85bc5852 | 250 | case AUDIO_FORMAT_U8: |
1d14ffa9 FB |
251 | break; |
252 | ||
85bc5852 | 253 | case AUDIO_FORMAT_S16: |
ed2a4a79 | 254 | is_signed = true; |
b4bd0b16 | 255 | /* fall through */ |
85bc5852 | 256 | case AUDIO_FORMAT_U16: |
1d14ffa9 FB |
257 | bits = 16; |
258 | break; | |
f941aa25 | 259 | |
ed2a4a79 KZ |
260 | case AUDIO_FORMAT_F32: |
261 | is_float = true; | |
262 | /* fall through */ | |
85bc5852 | 263 | case AUDIO_FORMAT_S32: |
ed2a4a79 | 264 | is_signed = true; |
b4bd0b16 | 265 | /* fall through */ |
85bc5852 | 266 | case AUDIO_FORMAT_U32: |
f941aa25 TS |
267 | bits = 32; |
268 | break; | |
85bc5852 KZ |
269 | |
270 | default: | |
271 | abort(); | |
85571bc7 | 272 | } |
c0fe3827 FB |
273 | return info->freq == as->freq |
274 | && info->nchannels == as->nchannels | |
ed2a4a79 KZ |
275 | && info->is_signed == is_signed |
276 | && info->is_float == is_float | |
d929eba5 FB |
277 | && info->bits == bits |
278 | && info->swap_endianness == (as->endianness != AUDIO_HOST_ENDIANNESS); | |
1d14ffa9 | 279 | } |
85571bc7 | 280 | |
1ea879e5 | 281 | void audio_pcm_init_info (struct audio_pcm_info *info, struct audsettings *as) |
1d14ffa9 | 282 | { |
ed2a4a79 KZ |
283 | int bits = 8, mul; |
284 | bool is_signed = false, is_float = false; | |
1d14ffa9 | 285 | |
c0fe3827 | 286 | switch (as->fmt) { |
85bc5852 | 287 | case AUDIO_FORMAT_S8: |
ed2a4a79 | 288 | is_signed = true; |
f7621fd1 | 289 | /* fall through */ |
85bc5852 | 290 | case AUDIO_FORMAT_U8: |
2b9cce8c | 291 | mul = 1; |
85571bc7 FB |
292 | break; |
293 | ||
85bc5852 | 294 | case AUDIO_FORMAT_S16: |
ed2a4a79 | 295 | is_signed = true; |
e4634941 | 296 | /* fall through */ |
85bc5852 | 297 | case AUDIO_FORMAT_U16: |
85571bc7 | 298 | bits = 16; |
2b9cce8c | 299 | mul = 2; |
f941aa25 TS |
300 | break; |
301 | ||
ed2a4a79 KZ |
302 | case AUDIO_FORMAT_F32: |
303 | is_float = true; | |
304 | /* fall through */ | |
85bc5852 | 305 | case AUDIO_FORMAT_S32: |
ed2a4a79 | 306 | is_signed = true; |
e4634941 | 307 | /* fall through */ |
85bc5852 | 308 | case AUDIO_FORMAT_U32: |
f941aa25 | 309 | bits = 32; |
2b9cce8c | 310 | mul = 4; |
85571bc7 | 311 | break; |
85bc5852 KZ |
312 | |
313 | default: | |
314 | abort(); | |
85571bc7 FB |
315 | } |
316 | ||
c0fe3827 | 317 | info->freq = as->freq; |
1d14ffa9 | 318 | info->bits = bits; |
ed2a4a79 KZ |
319 | info->is_signed = is_signed; |
320 | info->is_float = is_float; | |
c0fe3827 | 321 | info->nchannels = as->nchannels; |
2b9cce8c KZ |
322 | info->bytes_per_frame = as->nchannels * mul; |
323 | info->bytes_per_second = info->freq * info->bytes_per_frame; | |
d929eba5 | 324 | info->swap_endianness = (as->endianness != AUDIO_HOST_ENDIANNESS); |
85571bc7 FB |
325 | } |
326 | ||
1d14ffa9 | 327 | void audio_pcm_info_clear_buf (struct audio_pcm_info *info, void *buf, int len) |
85571bc7 | 328 | { |
1d14ffa9 FB |
329 | if (!len) { |
330 | return; | |
331 | } | |
332 | ||
ed2a4a79 | 333 | if (info->is_signed || info->is_float) { |
2b9cce8c | 334 | memset(buf, 0x00, len * info->bytes_per_frame); |
6c6886bd | 335 | } else { |
f941aa25 TS |
336 | switch (info->bits) { |
337 | case 8: | |
2b9cce8c | 338 | memset(buf, 0x80, len * info->bytes_per_frame); |
f941aa25 | 339 | break; |
1d14ffa9 | 340 | |
f941aa25 TS |
341 | case 16: |
342 | { | |
343 | int i; | |
344 | uint16_t *p = buf; | |
f941aa25 TS |
345 | short s = INT16_MAX; |
346 | ||
347 | if (info->swap_endianness) { | |
348 | s = bswap16 (s); | |
349 | } | |
350 | ||
2b9cce8c | 351 | for (i = 0; i < len * info->nchannels; i++) { |
f941aa25 TS |
352 | p[i] = s; |
353 | } | |
1d14ffa9 | 354 | } |
f941aa25 TS |
355 | break; |
356 | ||
357 | case 32: | |
358 | { | |
359 | int i; | |
360 | uint32_t *p = buf; | |
f941aa25 TS |
361 | int32_t s = INT32_MAX; |
362 | ||
363 | if (info->swap_endianness) { | |
364 | s = bswap32 (s); | |
365 | } | |
1d14ffa9 | 366 | |
2b9cce8c | 367 | for (i = 0; i < len * info->nchannels; i++) { |
f941aa25 TS |
368 | p[i] = s; |
369 | } | |
1d14ffa9 | 370 | } |
f941aa25 TS |
371 | break; |
372 | ||
373 | default: | |
374 | AUD_log (NULL, "audio_pcm_info_clear_buf: invalid bits %d\n", | |
375 | info->bits); | |
376 | break; | |
1d14ffa9 | 377 | } |
85571bc7 FB |
378 | } |
379 | } | |
380 | ||
8ead62cf FB |
381 | /* |
382 | * Capture | |
383 | */ | |
526fb058 KZ |
384 | static CaptureVoiceOut *audio_pcm_capture_find_specific(AudioState *s, |
385 | struct audsettings *as) | |
8ead62cf FB |
386 | { |
387 | CaptureVoiceOut *cap; | |
8ead62cf FB |
388 | |
389 | for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) { | |
d929eba5 | 390 | if (audio_pcm_info_eq (&cap->hw.info, as)) { |
8ead62cf FB |
391 | return cap; |
392 | } | |
393 | } | |
394 | return NULL; | |
395 | } | |
396 | ||
ec36b695 | 397 | static void audio_notify_capture (CaptureVoiceOut *cap, audcnotification_e cmd) |
8ead62cf | 398 | { |
ec36b695 FB |
399 | struct capture_callback *cb; |
400 | ||
401 | #ifdef DEBUG_CAPTURE | |
402 | dolog ("notification %d sent\n", cmd); | |
403 | #endif | |
404 | for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) { | |
405 | cb->ops.notify (cb->opaque, cmd); | |
406 | } | |
407 | } | |
8ead62cf | 408 | |
ec36b695 FB |
409 | static void audio_capture_maybe_changed (CaptureVoiceOut *cap, int enabled) |
410 | { | |
411 | if (cap->hw.enabled != enabled) { | |
412 | audcnotification_e cmd; | |
8ead62cf | 413 | cap->hw.enabled = enabled; |
ec36b695 FB |
414 | cmd = enabled ? AUD_CNOTIFY_ENABLE : AUD_CNOTIFY_DISABLE; |
415 | audio_notify_capture (cap, cmd); | |
8ead62cf FB |
416 | } |
417 | } | |
418 | ||
419 | static void audio_recalc_and_notify_capture (CaptureVoiceOut *cap) | |
420 | { | |
421 | HWVoiceOut *hw = &cap->hw; | |
422 | SWVoiceOut *sw; | |
423 | int enabled = 0; | |
424 | ||
ec36b695 | 425 | for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) { |
8ead62cf FB |
426 | if (sw->active) { |
427 | enabled = 1; | |
428 | break; | |
429 | } | |
430 | } | |
ec36b695 | 431 | audio_capture_maybe_changed (cap, enabled); |
8ead62cf FB |
432 | } |
433 | ||
434 | static void audio_detach_capture (HWVoiceOut *hw) | |
435 | { | |
ec36b695 FB |
436 | SWVoiceCap *sc = hw->cap_head.lh_first; |
437 | ||
438 | while (sc) { | |
439 | SWVoiceCap *sc1 = sc->entries.le_next; | |
440 | SWVoiceOut *sw = &sc->sw; | |
441 | CaptureVoiceOut *cap = sc->cap; | |
442 | int was_active = sw->active; | |
8ead62cf | 443 | |
8ead62cf FB |
444 | if (sw->rate) { |
445 | st_rate_stop (sw->rate); | |
446 | sw->rate = NULL; | |
447 | } | |
448 | ||
72cf2d4f BS |
449 | QLIST_REMOVE (sw, entries); |
450 | QLIST_REMOVE (sc, entries); | |
7267c094 | 451 | g_free (sc); |
ec36b695 FB |
452 | if (was_active) { |
453 | /* We have removed soft voice from the capture: | |
454 | this might have changed the overall status of the capture | |
455 | since this might have been the only active voice */ | |
456 | audio_recalc_and_notify_capture (cap); | |
457 | } | |
458 | sc = sc1; | |
8ead62cf FB |
459 | } |
460 | } | |
461 | ||
1a7dafce | 462 | static int audio_attach_capture (HWVoiceOut *hw) |
8ead62cf | 463 | { |
526fb058 | 464 | AudioState *s = hw->s; |
8ead62cf FB |
465 | CaptureVoiceOut *cap; |
466 | ||
467 | audio_detach_capture (hw); | |
468 | for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) { | |
ec36b695 | 469 | SWVoiceCap *sc; |
8ead62cf | 470 | SWVoiceOut *sw; |
ec36b695 | 471 | HWVoiceOut *hw_cap = &cap->hw; |
8ead62cf | 472 | |
e8d85444 | 473 | sc = g_malloc0(sizeof(*sc)); |
8ead62cf | 474 | |
ec36b695 FB |
475 | sc->cap = cap; |
476 | sw = &sc->sw; | |
8ead62cf | 477 | sw->hw = hw_cap; |
ec36b695 | 478 | sw->info = hw->info; |
8ead62cf FB |
479 | sw->empty = 1; |
480 | sw->active = hw->enabled; | |
8ead62cf | 481 | sw->ratio = ((int64_t) hw_cap->info.freq << 32) / sw->info.freq; |
83617103 | 482 | sw->vol = nominal_volume; |
8ead62cf | 483 | sw->rate = st_rate_start (sw->info.freq, hw_cap->info.freq); |
72cf2d4f BS |
484 | QLIST_INSERT_HEAD (&hw_cap->sw_head, sw, entries); |
485 | QLIST_INSERT_HEAD (&hw->cap_head, sc, entries); | |
ec36b695 | 486 | #ifdef DEBUG_CAPTURE |
457b6543 SW |
487 | sw->name = g_strdup_printf ("for %p %d,%d,%d", |
488 | hw, sw->info.freq, sw->info.bits, | |
489 | sw->info.nchannels); | |
ec36b695 FB |
490 | dolog ("Added %s active = %d\n", sw->name, sw->active); |
491 | #endif | |
8ead62cf | 492 | if (sw->active) { |
ec36b695 | 493 | audio_capture_maybe_changed (cap, 1); |
8ead62cf FB |
494 | } |
495 | } | |
496 | return 0; | |
497 | } | |
498 | ||
1d14ffa9 FB |
499 | /* |
500 | * Hard voice (capture) | |
501 | */ | |
7520462b | 502 | static size_t audio_pcm_hw_find_min_in (HWVoiceIn *hw) |
85571bc7 | 503 | { |
1d14ffa9 | 504 | SWVoiceIn *sw; |
7520462b | 505 | size_t m = hw->total_samples_captured; |
1d14ffa9 FB |
506 | |
507 | for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) { | |
508 | if (sw->active) { | |
58935915 | 509 | m = MIN (m, sw->total_hw_samples_acquired); |
1d14ffa9 | 510 | } |
85571bc7 | 511 | } |
1d14ffa9 | 512 | return m; |
85571bc7 FB |
513 | } |
514 | ||
3f5bbfc2 | 515 | static size_t audio_pcm_hw_get_live_in(HWVoiceIn *hw) |
85571bc7 | 516 | { |
7520462b | 517 | size_t live = hw->total_samples_captured - audio_pcm_hw_find_min_in (hw); |
8dbd3d17 VR |
518 | if (audio_bug(__func__, live > hw->conv_buf.size)) { |
519 | dolog("live=%zu hw->conv_buf.size=%zu\n", live, hw->conv_buf.size); | |
12f4abf6 | 520 | return 0; |
85571bc7 | 521 | } |
1d14ffa9 | 522 | return live; |
85571bc7 FB |
523 | } |
524 | ||
251f1549 VR |
525 | static size_t audio_pcm_hw_conv_in(HWVoiceIn *hw, void *pcm_buf, size_t samples) |
526 | { | |
527 | size_t conv = 0; | |
8dbd3d17 | 528 | STSampleBuffer *conv_buf = &hw->conv_buf; |
251f1549 VR |
529 | |
530 | while (samples) { | |
531 | uint8_t *src = advance(pcm_buf, conv * hw->info.bytes_per_frame); | |
532 | size_t proc = MIN(samples, conv_buf->size - conv_buf->pos); | |
533 | ||
8dbd3d17 | 534 | hw->conv(conv_buf->buffer + conv_buf->pos, src, proc); |
251f1549 VR |
535 | conv_buf->pos = (conv_buf->pos + proc) % conv_buf->size; |
536 | samples -= proc; | |
537 | conv += proc; | |
538 | } | |
539 | ||
540 | return conv; | |
541 | } | |
542 | ||
1d14ffa9 FB |
543 | /* |
544 | * Soft voice (capture) | |
545 | */ | |
7520462b | 546 | static size_t audio_pcm_sw_read(SWVoiceIn *sw, void *buf, size_t size) |
85571bc7 | 547 | { |
1d14ffa9 | 548 | HWVoiceIn *hw = sw->hw; |
7520462b | 549 | size_t samples, live, ret = 0, swlim, isamp, osamp, rpos, total = 0; |
2c3f9a0a | 550 | struct st_sample *src, *dst = sw->resample_buf.buffer; |
1d14ffa9 | 551 | |
1d14ffa9 | 552 | live = hw->total_samples_captured - sw->total_hw_samples_acquired; |
0ceb26af VR |
553 | if (!live) { |
554 | return 0; | |
555 | } | |
8dbd3d17 VR |
556 | if (audio_bug(__func__, live > hw->conv_buf.size)) { |
557 | dolog("live_in=%zu hw->conv_buf.size=%zu\n", live, hw->conv_buf.size); | |
12f4abf6 | 558 | return 0; |
1d14ffa9 FB |
559 | } |
560 | ||
8dbd3d17 | 561 | rpos = audio_ring_posb(hw->conv_buf.pos, live, hw->conv_buf.size); |
0ceb26af | 562 | |
2b9cce8c | 563 | samples = size / sw->info.bytes_per_frame; |
85571bc7 | 564 | |
1d14ffa9 | 565 | swlim = (live * sw->ratio) >> 32; |
58935915 | 566 | swlim = MIN (swlim, samples); |
85571bc7 | 567 | |
1d14ffa9 | 568 | while (swlim) { |
8dbd3d17 VR |
569 | src = hw->conv_buf.buffer + rpos; |
570 | if (hw->conv_buf.pos > rpos) { | |
571 | isamp = hw->conv_buf.pos - rpos; | |
7520462b | 572 | } else { |
8dbd3d17 | 573 | isamp = hw->conv_buf.size - rpos; |
1d14ffa9 | 574 | } |
85571bc7 | 575 | |
1d14ffa9 FB |
576 | if (!isamp) { |
577 | break; | |
578 | } | |
579 | osamp = swlim; | |
85571bc7 | 580 | |
1d14ffa9 FB |
581 | st_rate_flow (sw->rate, src, dst, &isamp, &osamp); |
582 | swlim -= osamp; | |
8dbd3d17 | 583 | rpos = (rpos + isamp) % hw->conv_buf.size; |
1d14ffa9 FB |
584 | dst += osamp; |
585 | ret += osamp; | |
586 | total += isamp; | |
587 | } | |
85571bc7 | 588 | |
669b9522 | 589 | if (!hw->pcm_ops->volume_in) { |
2c3f9a0a | 590 | mixeng_volume(sw->resample_buf.buffer, ret, &sw->vol); |
c01b2456 | 591 | } |
00e07679 | 592 | |
2c3f9a0a | 593 | sw->clip(buf, sw->resample_buf.buffer, ret); |
1d14ffa9 | 594 | sw->total_hw_samples_acquired += total; |
2b9cce8c | 595 | return ret * sw->info.bytes_per_frame; |
85571bc7 FB |
596 | } |
597 | ||
1d14ffa9 FB |
598 | /* |
599 | * Hard voice (playback) | |
600 | */ | |
7520462b | 601 | static size_t audio_pcm_hw_find_min_out (HWVoiceOut *hw, int *nb_livep) |
1d14ffa9 | 602 | { |
c0fe3827 | 603 | SWVoiceOut *sw; |
7520462b | 604 | size_t m = SIZE_MAX; |
c0fe3827 | 605 | int nb_live = 0; |
85571bc7 | 606 | |
c0fe3827 FB |
607 | for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) { |
608 | if (sw->active || !sw->empty) { | |
58935915 | 609 | m = MIN (m, sw->total_hw_samples_mixed); |
c0fe3827 FB |
610 | nb_live += 1; |
611 | } | |
85571bc7 | 612 | } |
c0fe3827 FB |
613 | |
614 | *nb_livep = nb_live; | |
615 | return m; | |
1d14ffa9 | 616 | } |
85571bc7 | 617 | |
7520462b | 618 | static size_t audio_pcm_hw_get_live_out (HWVoiceOut *hw, int *nb_live) |
1d14ffa9 | 619 | { |
7520462b | 620 | size_t smin; |
bdff253c | 621 | int nb_live1; |
85571bc7 | 622 | |
bdff253c | 623 | smin = audio_pcm_hw_find_min_out (hw, &nb_live1); |
624 | if (nb_live) { | |
625 | *nb_live = nb_live1; | |
85571bc7 | 626 | } |
bdff253c | 627 | |
628 | if (nb_live1) { | |
7520462b | 629 | size_t live = smin; |
1d14ffa9 | 630 | |
8dbd3d17 VR |
631 | if (audio_bug(__func__, live > hw->mix_buf.size)) { |
632 | dolog("live=%zu hw->mix_buf.size=%zu\n", live, hw->mix_buf.size); | |
12f4abf6 | 633 | return 0; |
85571bc7 | 634 | } |
1d14ffa9 | 635 | return live; |
85571bc7 | 636 | } |
bdff253c | 637 | return 0; |
85571bc7 FB |
638 | } |
639 | ||
9833438e VR |
640 | static size_t audio_pcm_hw_get_free(HWVoiceOut *hw) |
641 | { | |
642 | return (hw->pcm_ops->buffer_get_free ? hw->pcm_ops->buffer_get_free(hw) : | |
643 | INT_MAX) / hw->info.bytes_per_frame; | |
644 | } | |
645 | ||
8e56a172 VR |
646 | static void audio_pcm_hw_clip_out(HWVoiceOut *hw, void *pcm_buf, size_t len) |
647 | { | |
648 | size_t clipped = 0; | |
8dbd3d17 | 649 | size_t pos = hw->mix_buf.pos; |
8e56a172 VR |
650 | |
651 | while (len) { | |
8dbd3d17 | 652 | st_sample *src = hw->mix_buf.buffer + pos; |
8e56a172 | 653 | uint8_t *dst = advance(pcm_buf, clipped * hw->info.bytes_per_frame); |
8dbd3d17 | 654 | size_t samples_till_end_of_buf = hw->mix_buf.size - pos; |
8e56a172 VR |
655 | size_t samples_to_clip = MIN(len, samples_till_end_of_buf); |
656 | ||
657 | hw->clip(dst, src, samples_to_clip); | |
658 | ||
8dbd3d17 | 659 | pos = (pos + samples_to_clip) % hw->mix_buf.size; |
8e56a172 VR |
660 | len -= samples_to_clip; |
661 | clipped += samples_to_clip; | |
662 | } | |
663 | } | |
664 | ||
1d14ffa9 FB |
665 | /* |
666 | * Soft voice (playback) | |
667 | */ | |
8a81abee VR |
668 | static void audio_pcm_sw_resample_out(SWVoiceOut *sw, |
669 | size_t frames_in_max, size_t frames_out_max, | |
670 | size_t *total_in, size_t *total_out) | |
671 | { | |
672 | HWVoiceOut *hw = sw->hw; | |
673 | struct st_sample *src, *dst; | |
674 | size_t live, wpos, frames_in, frames_out; | |
675 | ||
676 | live = sw->total_hw_samples_mixed; | |
677 | wpos = (hw->mix_buf.pos + live) % hw->mix_buf.size; | |
678 | ||
679 | /* write to mix_buf from wpos to end of buffer */ | |
680 | src = sw->resample_buf.buffer; | |
681 | frames_in = frames_in_max; | |
682 | dst = hw->mix_buf.buffer + wpos; | |
683 | frames_out = MIN(frames_out_max, hw->mix_buf.size - wpos); | |
684 | st_rate_flow_mix(sw->rate, src, dst, &frames_in, &frames_out); | |
685 | wpos += frames_out; | |
686 | *total_in = frames_in; | |
687 | *total_out = frames_out; | |
688 | ||
689 | /* write to mix_buf from start of buffer if there are input frames left */ | |
690 | if (frames_in_max - frames_in > 0 && wpos == hw->mix_buf.size) { | |
691 | src += frames_in; | |
692 | frames_in = frames_in_max - frames_in; | |
693 | dst = hw->mix_buf.buffer; | |
694 | frames_out = frames_out_max - frames_out; | |
695 | st_rate_flow_mix(sw->rate, src, dst, &frames_in, &frames_out); | |
696 | *total_in += frames_in; | |
697 | *total_out += frames_out; | |
698 | } | |
699 | } | |
700 | ||
d5647bd9 | 701 | static size_t audio_pcm_sw_write(SWVoiceOut *sw, void *buf, size_t buf_len) |
85571bc7 | 702 | { |
d5647bd9 | 703 | HWVoiceOut *hw = sw->hw; |
1a01df3d VR |
704 | size_t live, dead, hw_free, sw_max, fe_max; |
705 | size_t frames_in_max, frames_out_max, total_in, total_out; | |
85571bc7 | 706 | |
1d14ffa9 | 707 | live = sw->total_hw_samples_mixed; |
d5647bd9 VR |
708 | if (audio_bug(__func__, live > hw->mix_buf.size)) { |
709 | dolog("live=%zu hw->mix_buf.size=%zu\n", live, hw->mix_buf.size); | |
12f4abf6 | 710 | return 0; |
1d14ffa9 | 711 | } |
85571bc7 | 712 | |
d5647bd9 | 713 | if (live == hw->mix_buf.size) { |
ec36b695 | 714 | #ifdef DEBUG_OUT |
0c29b786 | 715 | dolog ("%s is full %zu\n", sw->name, live); |
ec36b695 | 716 | #endif |
1d14ffa9 FB |
717 | return 0; |
718 | } | |
85571bc7 | 719 | |
d5647bd9 VR |
720 | dead = hw->mix_buf.size - live; |
721 | hw_free = audio_pcm_hw_get_free(hw); | |
9833438e | 722 | hw_free = hw_free > live ? hw_free - live : 0; |
1a01df3d VR |
723 | frames_out_max = MIN(dead, hw_free); |
724 | sw_max = st_rate_frames_in(sw->rate, frames_out_max); | |
725 | fe_max = MIN(buf_len / sw->info.bytes_per_frame, sw->resample_buf.size); | |
726 | frames_in_max = MIN(sw_max, fe_max); | |
c01b2456 | 727 | |
1a01df3d VR |
728 | if (!frames_in_max) { |
729 | return 0; | |
1d14ffa9 FB |
730 | } |
731 | ||
1a01df3d VR |
732 | sw->conv(sw->resample_buf.buffer, buf, frames_in_max); |
733 | if (!sw->hw->pcm_ops->volume_out) { | |
734 | mixeng_volume(sw->resample_buf.buffer, frames_in_max, &sw->vol); | |
735 | } | |
736 | ||
737 | audio_pcm_sw_resample_out(sw, frames_in_max, frames_out_max, | |
d5647bd9 | 738 | &total_in, &total_out); |
1d14ffa9 | 739 | |
d5647bd9 | 740 | sw->total_hw_samples_mixed += total_out; |
1d14ffa9 FB |
741 | sw->empty = sw->total_hw_samples_mixed == 0; |
742 | ||
743 | #ifdef DEBUG_OUT | |
744 | dolog ( | |
d5647bd9 VR |
745 | "%s: write size %zu written %zu total mixed %zu\n", |
746 | SW_NAME(sw), | |
747 | buf_len / sw->info.bytes_per_frame, | |
748 | total_in, | |
c0fe3827 | 749 | sw->total_hw_samples_mixed |
1d14ffa9 FB |
750 | ); |
751 | #endif | |
752 | ||
d5647bd9 | 753 | return total_in * sw->info.bytes_per_frame; |
85571bc7 FB |
754 | } |
755 | ||
1d14ffa9 FB |
756 | #ifdef DEBUG_AUDIO |
757 | static void audio_pcm_print_info (const char *cap, struct audio_pcm_info *info) | |
85571bc7 | 758 | { |
ed2a4a79 KZ |
759 | dolog("%s: bits %d, sign %d, float %d, freq %d, nchan %d\n", |
760 | cap, info->bits, info->is_signed, info->is_float, info->freq, | |
761 | info->nchannels); | |
85571bc7 | 762 | } |
1d14ffa9 | 763 | #endif |
85571bc7 | 764 | |
1d14ffa9 FB |
765 | #define DAC |
766 | #include "audio_template.h" | |
767 | #undef DAC | |
768 | #include "audio_template.h" | |
769 | ||
713a98f8 | 770 | /* |
771 | * Timer | |
772 | */ | |
526fb058 | 773 | static int audio_is_timer_needed(AudioState *s) |
713a98f8 | 774 | { |
775 | HWVoiceIn *hwi = NULL; | |
776 | HWVoiceOut *hwo = NULL; | |
777 | ||
526fb058 | 778 | while ((hwo = audio_pcm_hw_find_any_enabled_out(s, hwo))) { |
6c6886bd ZH |
779 | if (!hwo->poll_mode) { |
780 | return 1; | |
781 | } | |
713a98f8 | 782 | } |
526fb058 | 783 | while ((hwi = audio_pcm_hw_find_any_enabled_in(s, hwi))) { |
6c6886bd ZH |
784 | if (!hwi->poll_mode) { |
785 | return 1; | |
786 | } | |
713a98f8 | 787 | } |
788 | return 0; | |
789 | } | |
790 | ||
39deb1e4 | 791 | static void audio_reset_timer (AudioState *s) |
713a98f8 | 792 | { |
526fb058 | 793 | if (audio_is_timer_needed(s)) { |
1ffc2665 | 794 | timer_mod_anticipate_ns(s->ts, |
71830221 | 795 | qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + s->period_ticks); |
526fb058 KZ |
796 | if (!s->timer_running) { |
797 | s->timer_running = true; | |
798 | s->timer_last = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); | |
71830221 | 799 | trace_audio_timer_start(s->period_ticks / SCALE_MS); |
1a961e78 GH |
800 | } |
801 | } else { | |
802 | timer_del(s->ts); | |
526fb058 KZ |
803 | if (s->timer_running) { |
804 | s->timer_running = false; | |
1a961e78 GH |
805 | trace_audio_timer_stop(); |
806 | } | |
713a98f8 | 807 | } |
808 | } | |
809 | ||
39deb1e4 | 810 | static void audio_timer (void *opaque) |
811 | { | |
1a961e78 | 812 | int64_t now, diff; |
71830221 | 813 | AudioState *s = opaque; |
1a961e78 GH |
814 | |
815 | now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); | |
526fb058 | 816 | diff = now - s->timer_last; |
71830221 | 817 | if (diff > s->period_ticks * 3 / 2) { |
1a961e78 GH |
818 | trace_audio_timer_delayed(diff / SCALE_MS); |
819 | } | |
526fb058 | 820 | s->timer_last = now; |
1a961e78 | 821 | |
18e2c177 | 822 | audio_run(s, "timer"); |
71830221 | 823 | audio_reset_timer(s); |
39deb1e4 | 824 | } |
825 | ||
713a98f8 | 826 | /* |
827 | * Public API | |
828 | */ | |
7520462b | 829 | size_t AUD_write(SWVoiceOut *sw, void *buf, size_t size) |
85571bc7 | 830 | { |
1930616b KZ |
831 | HWVoiceOut *hw; |
832 | ||
1d14ffa9 FB |
833 | if (!sw) { |
834 | /* XXX: Consider options */ | |
835 | return size; | |
836 | } | |
1930616b | 837 | hw = sw->hw; |
85571bc7 | 838 | |
1930616b | 839 | if (!hw->enabled) { |
c0fe3827 | 840 | dolog ("Writing to disabled voice %s\n", SW_NAME (sw)); |
85571bc7 FB |
841 | return 0; |
842 | } | |
843 | ||
1930616b KZ |
844 | if (audio_get_pdo_out(hw->s->dev)->mixing_engine) { |
845 | return audio_pcm_sw_write(sw, buf, size); | |
846 | } else { | |
847 | return hw->pcm_ops->write(hw, buf, size); | |
848 | } | |
1d14ffa9 FB |
849 | } |
850 | ||
7520462b | 851 | size_t AUD_read(SWVoiceIn *sw, void *buf, size_t size) |
1d14ffa9 | 852 | { |
1930616b KZ |
853 | HWVoiceIn *hw; |
854 | ||
1d14ffa9 FB |
855 | if (!sw) { |
856 | /* XXX: Consider options */ | |
857 | return size; | |
85571bc7 | 858 | } |
1930616b | 859 | hw = sw->hw; |
1d14ffa9 | 860 | |
1930616b | 861 | if (!hw->enabled) { |
c0fe3827 | 862 | dolog ("Reading from disabled voice %s\n", SW_NAME (sw)); |
1d14ffa9 | 863 | return 0; |
85571bc7 | 864 | } |
1d14ffa9 | 865 | |
1930616b KZ |
866 | if (audio_get_pdo_in(hw->s->dev)->mixing_engine) { |
867 | return audio_pcm_sw_read(sw, buf, size); | |
868 | } else { | |
869 | return hw->pcm_ops->read(hw, buf, size); | |
870 | } | |
85571bc7 FB |
871 | } |
872 | ||
69ac0786 | 873 | int AUD_get_buffer_size_out(SWVoiceOut *sw) |
85571bc7 | 874 | { |
69ac0786 | 875 | return sw->hw->samples * sw->hw->info.bytes_per_frame; |
1d14ffa9 FB |
876 | } |
877 | ||
878 | void AUD_set_active_out (SWVoiceOut *sw, int on) | |
879 | { | |
880 | HWVoiceOut *hw; | |
85571bc7 | 881 | |
1d14ffa9 | 882 | if (!sw) { |
85571bc7 | 883 | return; |
1d14ffa9 | 884 | } |
85571bc7 FB |
885 | |
886 | hw = sw->hw; | |
1d14ffa9 | 887 | if (sw->active != on) { |
526fb058 | 888 | AudioState *s = sw->s; |
1d14ffa9 | 889 | SWVoiceOut *temp_sw; |
ec36b695 | 890 | SWVoiceCap *sc; |
1d14ffa9 FB |
891 | |
892 | if (on) { | |
1d14ffa9 FB |
893 | hw->pending_disable = 0; |
894 | if (!hw->enabled) { | |
895 | hw->enabled = 1; | |
978dd635 | 896 | if (s->vm_running) { |
571a8c52 KZ |
897 | if (hw->pcm_ops->enable_out) { |
898 | hw->pcm_ops->enable_out(hw, true); | |
899 | } | |
39deb1e4 | 900 | audio_reset_timer (s); |
978dd635 | 901 | } |
1d14ffa9 | 902 | } |
6c6886bd | 903 | } else { |
1d14ffa9 FB |
904 | if (hw->enabled) { |
905 | int nb_active = 0; | |
906 | ||
907 | for (temp_sw = hw->sw_head.lh_first; temp_sw; | |
908 | temp_sw = temp_sw->entries.le_next) { | |
909 | nb_active += temp_sw->active != 0; | |
910 | } | |
911 | ||
912 | hw->pending_disable = nb_active == 1; | |
913 | } | |
85571bc7 | 914 | } |
ec36b695 FB |
915 | |
916 | for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) { | |
917 | sc->sw.active = hw->enabled; | |
8ead62cf | 918 | if (hw->enabled) { |
ec36b695 | 919 | audio_capture_maybe_changed (sc->cap, 1); |
8ead62cf FB |
920 | } |
921 | } | |
1d14ffa9 FB |
922 | sw->active = on; |
923 | } | |
924 | } | |
925 | ||
926 | void AUD_set_active_in (SWVoiceIn *sw, int on) | |
927 | { | |
928 | HWVoiceIn *hw; | |
929 | ||
930 | if (!sw) { | |
931 | return; | |
85571bc7 FB |
932 | } |
933 | ||
1d14ffa9 | 934 | hw = sw->hw; |
85571bc7 | 935 | if (sw->active != on) { |
526fb058 | 936 | AudioState *s = sw->s; |
1d14ffa9 FB |
937 | SWVoiceIn *temp_sw; |
938 | ||
85571bc7 | 939 | if (on) { |
85571bc7 FB |
940 | if (!hw->enabled) { |
941 | hw->enabled = 1; | |
978dd635 | 942 | if (s->vm_running) { |
571a8c52 KZ |
943 | if (hw->pcm_ops->enable_in) { |
944 | hw->pcm_ops->enable_in(hw, true); | |
945 | } | |
39deb1e4 | 946 | audio_reset_timer (s); |
978dd635 | 947 | } |
85571bc7 | 948 | } |
1d14ffa9 | 949 | sw->total_hw_samples_acquired = hw->total_samples_captured; |
6c6886bd | 950 | } else { |
1d14ffa9 | 951 | if (hw->enabled) { |
85571bc7 | 952 | int nb_active = 0; |
1d14ffa9 FB |
953 | |
954 | for (temp_sw = hw->sw_head.lh_first; temp_sw; | |
955 | temp_sw = temp_sw->entries.le_next) { | |
956 | nb_active += temp_sw->active != 0; | |
85571bc7 FB |
957 | } |
958 | ||
959 | if (nb_active == 1) { | |
1d14ffa9 | 960 | hw->enabled = 0; |
571a8c52 KZ |
961 | if (hw->pcm_ops->enable_in) { |
962 | hw->pcm_ops->enable_in(hw, false); | |
963 | } | |
85571bc7 FB |
964 | } |
965 | } | |
966 | } | |
967 | sw->active = on; | |
968 | } | |
969 | } | |
970 | ||
0724c579 VR |
971 | /** |
972 | * audio_frontend_frames_in() - returns the number of frames the resampling | |
973 | * code generates from frames_in frames | |
974 | * | |
975 | * @sw: audio recording frontend | |
976 | * @frames_in: number of frames | |
977 | */ | |
978 | static size_t audio_frontend_frames_in(SWVoiceIn *sw, size_t frames_in) | |
979 | { | |
b73ef11f | 980 | return (int64_t)frames_in * sw->ratio >> 32; |
0724c579 VR |
981 | } |
982 | ||
7520462b | 983 | static size_t audio_get_avail (SWVoiceIn *sw) |
1d14ffa9 | 984 | { |
7520462b | 985 | size_t live; |
1d14ffa9 FB |
986 | |
987 | if (!sw) { | |
988 | return 0; | |
989 | } | |
990 | ||
991 | live = sw->hw->total_samples_captured - sw->total_hw_samples_acquired; | |
8dbd3d17 VR |
992 | if (audio_bug(__func__, live > sw->hw->conv_buf.size)) { |
993 | dolog("live=%zu sw->hw->conv_buf.size=%zu\n", live, | |
994 | sw->hw->conv_buf.size); | |
12f4abf6 | 995 | return 0; |
1d14ffa9 FB |
996 | } |
997 | ||
998 | ldebug ( | |
0724c579 | 999 | "%s: get_avail live %zu frontend frames %zu\n", |
c0fe3827 | 1000 | SW_NAME (sw), |
0724c579 | 1001 | live, audio_frontend_frames_in(sw, live) |
1d14ffa9 FB |
1002 | ); |
1003 | ||
0724c579 | 1004 | return live; |
1d14ffa9 FB |
1005 | } |
1006 | ||
7520462b | 1007 | static size_t audio_get_free(SWVoiceOut *sw) |
1d14ffa9 | 1008 | { |
7520462b | 1009 | size_t live, dead; |
1d14ffa9 FB |
1010 | |
1011 | if (!sw) { | |
1012 | return 0; | |
1013 | } | |
1014 | ||
1015 | live = sw->total_hw_samples_mixed; | |
1016 | ||
8dbd3d17 VR |
1017 | if (audio_bug(__func__, live > sw->hw->mix_buf.size)) { |
1018 | dolog("live=%zu sw->hw->mix_buf.size=%zu\n", live, | |
1019 | sw->hw->mix_buf.size); | |
12f4abf6 | 1020 | return 0; |
1d14ffa9 FB |
1021 | } |
1022 | ||
8dbd3d17 | 1023 | dead = sw->hw->mix_buf.size - live; |
1d14ffa9 FB |
1024 | |
1025 | #ifdef DEBUG_OUT | |
1a01df3d VR |
1026 | dolog("%s: get_free live %zu dead %zu frontend frames %u\n", |
1027 | SW_NAME(sw), live, dead, st_rate_frames_in(sw->rate, dead)); | |
85571bc7 | 1028 | #endif |
1d14ffa9 | 1029 | |
9833438e | 1030 | return dead; |
1d14ffa9 FB |
1031 | } |
1032 | ||
7520462b KZ |
1033 | static void audio_capture_mix_and_clear(HWVoiceOut *hw, size_t rpos, |
1034 | size_t samples) | |
8ead62cf | 1035 | { |
7520462b | 1036 | size_t n; |
8ead62cf FB |
1037 | |
1038 | if (hw->enabled) { | |
ec36b695 | 1039 | SWVoiceCap *sc; |
8ead62cf | 1040 | |
ec36b695 FB |
1041 | for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) { |
1042 | SWVoiceOut *sw = &sc->sw; | |
671cca35 | 1043 | size_t rpos2 = rpos; |
8ead62cf FB |
1044 | |
1045 | n = samples; | |
1046 | while (n) { | |
8dbd3d17 | 1047 | size_t till_end_of_hw = hw->mix_buf.size - rpos2; |
671cca35 VR |
1048 | size_t to_read = MIN(till_end_of_hw, n); |
1049 | size_t live, frames_in, frames_out; | |
8ead62cf | 1050 | |
2c3f9a0a | 1051 | sw->resample_buf.buffer = hw->mix_buf.buffer + rpos2; |
671cca35 VR |
1052 | sw->resample_buf.size = to_read; |
1053 | live = sw->total_hw_samples_mixed; | |
1054 | ||
1055 | audio_pcm_sw_resample_out(sw, | |
1056 | to_read, sw->hw->mix_buf.size - live, | |
1057 | &frames_in, &frames_out); | |
1058 | ||
1059 | sw->total_hw_samples_mixed += frames_out; | |
1060 | sw->empty = sw->total_hw_samples_mixed == 0; | |
1061 | ||
1062 | if (to_read - frames_in) { | |
1063 | dolog("Could not mix %zu frames into a capture " | |
7520462b | 1064 | "buffer, mixed %zu\n", |
671cca35 | 1065 | to_read, frames_in); |
8ead62cf FB |
1066 | break; |
1067 | } | |
671cca35 VR |
1068 | n -= to_read; |
1069 | rpos2 = (rpos2 + to_read) % hw->mix_buf.size; | |
8ead62cf FB |
1070 | } |
1071 | } | |
1072 | } | |
1073 | ||
8dbd3d17 VR |
1074 | n = MIN(samples, hw->mix_buf.size - rpos); |
1075 | mixeng_clear(hw->mix_buf.buffer + rpos, n); | |
1076 | mixeng_clear(hw->mix_buf.buffer, samples - n); | |
8ead62cf FB |
1077 | } |
1078 | ||
ff095e52 KZ |
1079 | static size_t audio_pcm_hw_run_out(HWVoiceOut *hw, size_t live) |
1080 | { | |
1081 | size_t clipped = 0; | |
1082 | ||
1083 | while (live) { | |
aec6d0dc VR |
1084 | size_t size = live * hw->info.bytes_per_frame; |
1085 | size_t decr, proc; | |
ff095e52 | 1086 | void *buf = hw->pcm_ops->get_buffer_out(hw, &size); |
4c3356f9 VR |
1087 | |
1088 | if (size == 0) { | |
fb35c2ce | 1089 | break; |
ff095e52 KZ |
1090 | } |
1091 | ||
2b9cce8c | 1092 | decr = MIN(size / hw->info.bytes_per_frame, live); |
4c3356f9 VR |
1093 | if (buf) { |
1094 | audio_pcm_hw_clip_out(hw, buf, decr); | |
1095 | } | |
2b9cce8c KZ |
1096 | proc = hw->pcm_ops->put_buffer_out(hw, buf, |
1097 | decr * hw->info.bytes_per_frame) / | |
1098 | hw->info.bytes_per_frame; | |
ff095e52 KZ |
1099 | |
1100 | live -= proc; | |
1101 | clipped += proc; | |
8dbd3d17 | 1102 | hw->mix_buf.pos = (hw->mix_buf.pos + proc) % hw->mix_buf.size; |
ff095e52 KZ |
1103 | |
1104 | if (proc == 0 || proc < decr) { | |
1105 | break; | |
1106 | } | |
1107 | } | |
1108 | ||
fdc8c5f4 VR |
1109 | if (hw->pcm_ops->run_buffer_out) { |
1110 | hw->pcm_ops->run_buffer_out(hw); | |
1111 | } | |
1112 | ||
ff095e52 KZ |
1113 | return clipped; |
1114 | } | |
1115 | ||
c0fe3827 | 1116 | static void audio_run_out (AudioState *s) |
1d14ffa9 FB |
1117 | { |
1118 | HWVoiceOut *hw = NULL; | |
1119 | SWVoiceOut *sw; | |
1120 | ||
4d31ff32 VR |
1121 | while ((hw = audio_pcm_hw_find_any_enabled_out(s, hw))) { |
1122 | size_t played, live, prev_rpos; | |
1123 | size_t hw_free = audio_pcm_hw_get_free(hw); | |
1124 | int nb_live; | |
1125 | ||
1126 | if (!audio_get_pdo_out(s->dev)->mixing_engine) { | |
1930616b KZ |
1127 | /* there is exactly 1 sw for each hw with no mixeng */ |
1128 | sw = hw->sw_head.lh_first; | |
1129 | ||
1130 | if (hw->pending_disable) { | |
1131 | hw->enabled = 0; | |
1132 | hw->pending_disable = 0; | |
1133 | if (hw->pcm_ops->enable_out) { | |
1134 | hw->pcm_ops->enable_out(hw, false); | |
1135 | } | |
1136 | } | |
1137 | ||
1138 | if (sw->active) { | |
7099a6a2 VR |
1139 | sw->callback.fn(sw->callback.opaque, |
1140 | hw_free * sw->info.bytes_per_frame); | |
1930616b | 1141 | } |
1930616b | 1142 | |
dd052dbf VR |
1143 | if (hw->pcm_ops->run_buffer_out) { |
1144 | hw->pcm_ops->run_buffer_out(hw); | |
1145 | } | |
1146 | ||
4d31ff32 VR |
1147 | continue; |
1148 | } | |
1d14ffa9 | 1149 | |
a806f959 VR |
1150 | for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) { |
1151 | if (sw->active) { | |
9833438e VR |
1152 | size_t sw_free = audio_get_free(sw); |
1153 | size_t free; | |
1154 | ||
1155 | if (hw_free > sw->total_hw_samples_mixed) { | |
1a01df3d | 1156 | free = st_rate_frames_in(sw->rate, |
9833438e VR |
1157 | MIN(sw_free, hw_free - sw->total_hw_samples_mixed)); |
1158 | } else { | |
1159 | free = 0; | |
1160 | } | |
a806f959 | 1161 | if (free > 0) { |
1a01df3d | 1162 | free = MIN(free, sw->resample_buf.size); |
c4e59264 VR |
1163 | sw->callback.fn(sw->callback.opaque, |
1164 | free * sw->info.bytes_per_frame); | |
a806f959 VR |
1165 | } |
1166 | } | |
1167 | } | |
1168 | ||
bdff253c | 1169 | live = audio_pcm_hw_get_live_out (hw, &nb_live); |
1d14ffa9 FB |
1170 | if (!nb_live) { |
1171 | live = 0; | |
1172 | } | |
c0fe3827 | 1173 | |
8dbd3d17 VR |
1174 | if (audio_bug(__func__, live > hw->mix_buf.size)) { |
1175 | dolog("live=%zu hw->mix_buf.size=%zu\n", live, hw->mix_buf.size); | |
12f4abf6 | 1176 | continue; |
1d14ffa9 FB |
1177 | } |
1178 | ||
1179 | if (hw->pending_disable && !nb_live) { | |
ec36b695 | 1180 | SWVoiceCap *sc; |
1d14ffa9 FB |
1181 | #ifdef DEBUG_OUT |
1182 | dolog ("Disabling voice\n"); | |
85571bc7 | 1183 | #endif |
1d14ffa9 FB |
1184 | hw->enabled = 0; |
1185 | hw->pending_disable = 0; | |
571a8c52 KZ |
1186 | if (hw->pcm_ops->enable_out) { |
1187 | hw->pcm_ops->enable_out(hw, false); | |
1188 | } | |
ec36b695 FB |
1189 | for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) { |
1190 | sc->sw.active = 0; | |
1191 | audio_recalc_and_notify_capture (sc->cap); | |
8ead62cf | 1192 | } |
1d14ffa9 FB |
1193 | continue; |
1194 | } | |
1195 | ||
1196 | if (!live) { | |
a8a98cfd VR |
1197 | if (hw->pcm_ops->run_buffer_out) { |
1198 | hw->pcm_ops->run_buffer_out(hw); | |
1199 | } | |
1d14ffa9 FB |
1200 | continue; |
1201 | } | |
1202 | ||
8dbd3d17 | 1203 | prev_rpos = hw->mix_buf.pos; |
3f5bbfc2 | 1204 | played = audio_pcm_hw_run_out(hw, live); |
3d4d16f4 | 1205 | replay_audio_out(&played); |
8dbd3d17 VR |
1206 | if (audio_bug(__func__, hw->mix_buf.pos >= hw->mix_buf.size)) { |
1207 | dolog("hw->mix_buf.pos=%zu hw->mix_buf.size=%zu played=%zu\n", | |
1208 | hw->mix_buf.pos, hw->mix_buf.size, played); | |
1209 | hw->mix_buf.pos = 0; | |
1d14ffa9 FB |
1210 | } |
1211 | ||
1212 | #ifdef DEBUG_OUT | |
7520462b | 1213 | dolog("played=%zu\n", played); |
85571bc7 | 1214 | #endif |
1d14ffa9 FB |
1215 | |
1216 | if (played) { | |
1217 | hw->ts_helper += played; | |
8ead62cf | 1218 | audio_capture_mix_and_clear (hw, prev_rpos, played); |
1d14ffa9 FB |
1219 | } |
1220 | ||
1221 | for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) { | |
1d14ffa9 FB |
1222 | if (!sw->active && sw->empty) { |
1223 | continue; | |
1224 | } | |
1225 | ||
470bcabd | 1226 | if (audio_bug(__func__, played > sw->total_hw_samples_mixed)) { |
7520462b KZ |
1227 | dolog("played=%zu sw->total_hw_samples_mixed=%zu\n", |
1228 | played, sw->total_hw_samples_mixed); | |
12f4abf6 | 1229 | played = sw->total_hw_samples_mixed; |
1d14ffa9 FB |
1230 | } |
1231 | ||
1232 | sw->total_hw_samples_mixed -= played; | |
1233 | ||
1234 | if (!sw->total_hw_samples_mixed) { | |
1235 | sw->empty = 1; | |
1d14ffa9 | 1236 | } |
1d14ffa9 FB |
1237 | } |
1238 | } | |
1239 | } | |
1240 | ||
ff095e52 KZ |
1241 | static size_t audio_pcm_hw_run_in(HWVoiceIn *hw, size_t samples) |
1242 | { | |
1243 | size_t conv = 0; | |
1244 | ||
a2893c83 VR |
1245 | if (hw->pcm_ops->run_buffer_in) { |
1246 | hw->pcm_ops->run_buffer_in(hw); | |
1247 | } | |
1248 | ||
ff095e52 KZ |
1249 | while (samples) { |
1250 | size_t proc; | |
2b9cce8c | 1251 | size_t size = samples * hw->info.bytes_per_frame; |
ff095e52 KZ |
1252 | void *buf = hw->pcm_ops->get_buffer_in(hw, &size); |
1253 | ||
2b9cce8c | 1254 | assert(size % hw->info.bytes_per_frame == 0); |
ff095e52 | 1255 | if (size == 0) { |
ff095e52 KZ |
1256 | break; |
1257 | } | |
1258 | ||
251f1549 | 1259 | proc = audio_pcm_hw_conv_in(hw, buf, size / hw->info.bytes_per_frame); |
ff095e52 KZ |
1260 | |
1261 | samples -= proc; | |
1262 | conv += proc; | |
2b9cce8c | 1263 | hw->pcm_ops->put_buffer_in(hw, buf, proc * hw->info.bytes_per_frame); |
ff095e52 KZ |
1264 | } |
1265 | ||
1266 | return conv; | |
1267 | } | |
1268 | ||
c0fe3827 | 1269 | static void audio_run_in (AudioState *s) |
1d14ffa9 FB |
1270 | { |
1271 | HWVoiceIn *hw = NULL; | |
1272 | ||
1930616b KZ |
1273 | if (!audio_get_pdo_in(s->dev)->mixing_engine) { |
1274 | while ((hw = audio_pcm_hw_find_any_enabled_in(s, hw))) { | |
1275 | /* there is exactly 1 sw for each hw with no mixeng */ | |
1276 | SWVoiceIn *sw = hw->sw_head.lh_first; | |
1277 | if (sw->active) { | |
1278 | sw->callback.fn(sw->callback.opaque, INT_MAX); | |
1279 | } | |
1280 | } | |
1281 | return; | |
1282 | } | |
1283 | ||
526fb058 | 1284 | while ((hw = audio_pcm_hw_find_any_enabled_in(s, hw))) { |
1d14ffa9 | 1285 | SWVoiceIn *sw; |
7520462b | 1286 | size_t captured = 0, min; |
1d14ffa9 | 1287 | |
3d4d16f4 | 1288 | if (replay_mode != REPLAY_MODE_PLAY) { |
3f5bbfc2 | 1289 | captured = audio_pcm_hw_run_in( |
8dbd3d17 | 1290 | hw, hw->conv_buf.size - audio_pcm_hw_get_live_in(hw)); |
3d4d16f4 | 1291 | } |
8dbd3d17 VR |
1292 | replay_audio_in(&captured, hw->conv_buf.buffer, &hw->conv_buf.pos, |
1293 | hw->conv_buf.size); | |
1d14ffa9 FB |
1294 | |
1295 | min = audio_pcm_hw_find_min_in (hw); | |
1296 | hw->total_samples_captured += captured - min; | |
1297 | hw->ts_helper += captured; | |
1298 | ||
1299 | for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) { | |
1300 | sw->total_hw_samples_acquired -= min; | |
1301 | ||
1302 | if (sw->active) { | |
0724c579 | 1303 | size_t sw_avail = audio_get_avail(sw); |
7520462b | 1304 | size_t avail; |
1d14ffa9 | 1305 | |
0724c579 | 1306 | avail = audio_frontend_frames_in(sw, sw_avail); |
1d14ffa9 | 1307 | if (avail > 0) { |
0724c579 VR |
1308 | sw->callback.fn(sw->callback.opaque, |
1309 | avail * sw->info.bytes_per_frame); | |
1d14ffa9 FB |
1310 | } |
1311 | } | |
1312 | } | |
1313 | } | |
1314 | } | |
1315 | ||
8ead62cf FB |
1316 | static void audio_run_capture (AudioState *s) |
1317 | { | |
1318 | CaptureVoiceOut *cap; | |
1319 | ||
1320 | for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) { | |
7520462b | 1321 | size_t live, rpos, captured; |
8ead62cf FB |
1322 | HWVoiceOut *hw = &cap->hw; |
1323 | SWVoiceOut *sw; | |
1324 | ||
bdff253c | 1325 | captured = live = audio_pcm_hw_get_live_out (hw, NULL); |
8dbd3d17 | 1326 | rpos = hw->mix_buf.pos; |
8ead62cf | 1327 | while (live) { |
8dbd3d17 | 1328 | size_t left = hw->mix_buf.size - rpos; |
7520462b | 1329 | size_t to_capture = MIN(live, left); |
1ea879e5 | 1330 | struct st_sample *src; |
8ead62cf FB |
1331 | struct capture_callback *cb; |
1332 | ||
8dbd3d17 | 1333 | src = hw->mix_buf.buffer + rpos; |
8ead62cf FB |
1334 | hw->clip (cap->buf, src, to_capture); |
1335 | mixeng_clear (src, to_capture); | |
1336 | ||
1337 | for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) { | |
1338 | cb->ops.capture (cb->opaque, cap->buf, | |
2b9cce8c | 1339 | to_capture * hw->info.bytes_per_frame); |
8ead62cf | 1340 | } |
8dbd3d17 | 1341 | rpos = (rpos + to_capture) % hw->mix_buf.size; |
8ead62cf FB |
1342 | live -= to_capture; |
1343 | } | |
8dbd3d17 | 1344 | hw->mix_buf.pos = rpos; |
8ead62cf FB |
1345 | |
1346 | for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) { | |
1347 | if (!sw->active && sw->empty) { | |
1348 | continue; | |
1349 | } | |
1350 | ||
470bcabd | 1351 | if (audio_bug(__func__, captured > sw->total_hw_samples_mixed)) { |
7520462b KZ |
1352 | dolog("captured=%zu sw->total_hw_samples_mixed=%zu\n", |
1353 | captured, sw->total_hw_samples_mixed); | |
12f4abf6 | 1354 | captured = sw->total_hw_samples_mixed; |
8ead62cf FB |
1355 | } |
1356 | ||
1357 | sw->total_hw_samples_mixed -= captured; | |
1358 | sw->empty = sw->total_hw_samples_mixed == 0; | |
1359 | } | |
1360 | } | |
1361 | } | |
1362 | ||
18e2c177 | 1363 | void audio_run(AudioState *s, const char *msg) |
571ec3d6 | 1364 | { |
18e2c177 KZ |
1365 | audio_run_out(s); |
1366 | audio_run_in(s); | |
1367 | audio_run_capture(s); | |
571ec3d6 | 1368 | |
713a98f8 | 1369 | #ifdef DEBUG_POLL |
1370 | { | |
1371 | static double prevtime; | |
1372 | double currtime; | |
1373 | struct timeval tv; | |
571ec3d6 | 1374 | |
713a98f8 | 1375 | if (gettimeofday (&tv, NULL)) { |
1376 | perror ("audio_run: gettimeofday"); | |
1377 | return; | |
1378 | } | |
1379 | ||
1380 | currtime = tv.tv_sec + tv.tv_usec * 1e-6; | |
1381 | dolog ("Elapsed since last %s: %f\n", msg, currtime - prevtime); | |
1382 | prevtime = currtime; | |
1383 | } | |
1384 | #endif | |
571ec3d6 FB |
1385 | } |
1386 | ||
a2893c83 | 1387 | void audio_generic_run_buffer_in(HWVoiceIn *hw) |
ff095e52 | 1388 | { |
ff095e52 | 1389 | if (unlikely(!hw->buf_emul)) { |
1d8549ad VR |
1390 | hw->size_emul = hw->samples * hw->info.bytes_per_frame; |
1391 | hw->buf_emul = g_malloc(hw->size_emul); | |
ff095e52 KZ |
1392 | hw->pos_emul = hw->pending_emul = 0; |
1393 | } | |
1394 | ||
1395 | while (hw->pending_emul < hw->size_emul) { | |
1396 | size_t read_len = MIN(hw->size_emul - hw->pos_emul, | |
1397 | hw->size_emul - hw->pending_emul); | |
1398 | size_t read = hw->pcm_ops->read(hw, hw->buf_emul + hw->pos_emul, | |
1399 | read_len); | |
1400 | hw->pending_emul += read; | |
7ffc90f3 | 1401 | hw->pos_emul = (hw->pos_emul + read) % hw->size_emul; |
ff095e52 KZ |
1402 | if (read < read_len) { |
1403 | break; | |
1404 | } | |
1405 | } | |
a2893c83 VR |
1406 | } |
1407 | ||
1408 | void *audio_generic_get_buffer_in(HWVoiceIn *hw, size_t *size) | |
1409 | { | |
18404ff1 | 1410 | size_t start; |
ff095e52 | 1411 | |
18404ff1 VR |
1412 | start = audio_ring_posb(hw->pos_emul, hw->pending_emul, hw->size_emul); |
1413 | assert(start < hw->size_emul); | |
ff095e52 | 1414 | |
599eac4e VR |
1415 | *size = MIN(*size, hw->pending_emul); |
1416 | *size = MIN(*size, hw->size_emul - start); | |
ff095e52 KZ |
1417 | return hw->buf_emul + start; |
1418 | } | |
1419 | ||
1420 | void audio_generic_put_buffer_in(HWVoiceIn *hw, void *buf, size_t size) | |
1421 | { | |
1422 | assert(size <= hw->pending_emul); | |
1423 | hw->pending_emul -= size; | |
1424 | } | |
1425 | ||
9833438e VR |
1426 | size_t audio_generic_buffer_get_free(HWVoiceOut *hw) |
1427 | { | |
1428 | if (hw->buf_emul) { | |
1429 | return hw->size_emul - hw->pending_emul; | |
1430 | } else { | |
1431 | return hw->samples * hw->info.bytes_per_frame; | |
1432 | } | |
1433 | } | |
1434 | ||
fdc8c5f4 VR |
1435 | void audio_generic_run_buffer_out(HWVoiceOut *hw) |
1436 | { | |
1437 | while (hw->pending_emul) { | |
18404ff1 | 1438 | size_t write_len, written, start; |
fdc8c5f4 | 1439 | |
18404ff1 VR |
1440 | start = audio_ring_posb(hw->pos_emul, hw->pending_emul, hw->size_emul); |
1441 | assert(start < hw->size_emul); | |
fdc8c5f4 VR |
1442 | |
1443 | write_len = MIN(hw->pending_emul, hw->size_emul - start); | |
1444 | ||
1445 | written = hw->pcm_ops->write(hw, hw->buf_emul + start, write_len); | |
1446 | hw->pending_emul -= written; | |
1447 | ||
1448 | if (written < write_len) { | |
1449 | break; | |
1450 | } | |
1451 | } | |
1452 | } | |
1453 | ||
ff095e52 KZ |
1454 | void *audio_generic_get_buffer_out(HWVoiceOut *hw, size_t *size) |
1455 | { | |
1456 | if (unlikely(!hw->buf_emul)) { | |
1d8549ad VR |
1457 | hw->size_emul = hw->samples * hw->info.bytes_per_frame; |
1458 | hw->buf_emul = g_malloc(hw->size_emul); | |
ff095e52 KZ |
1459 | hw->pos_emul = hw->pending_emul = 0; |
1460 | } | |
1461 | ||
1462 | *size = MIN(hw->size_emul - hw->pending_emul, | |
1463 | hw->size_emul - hw->pos_emul); | |
1464 | return hw->buf_emul + hw->pos_emul; | |
1465 | } | |
1466 | ||
fdc8c5f4 | 1467 | size_t audio_generic_put_buffer_out(HWVoiceOut *hw, void *buf, size_t size) |
ff095e52 KZ |
1468 | { |
1469 | assert(buf == hw->buf_emul + hw->pos_emul && | |
1470 | size + hw->pending_emul <= hw->size_emul); | |
1471 | ||
1472 | hw->pending_emul += size; | |
1473 | hw->pos_emul = (hw->pos_emul + size) % hw->size_emul; | |
1474 | ||
1475 | return size; | |
1476 | } | |
1477 | ||
ff095e52 KZ |
1478 | size_t audio_generic_write(HWVoiceOut *hw, void *buf, size_t size) |
1479 | { | |
2d882307 VR |
1480 | size_t total = 0; |
1481 | ||
9833438e VR |
1482 | if (hw->pcm_ops->buffer_get_free) { |
1483 | size_t free = hw->pcm_ops->buffer_get_free(hw); | |
1484 | ||
1485 | size = MIN(size, free); | |
1486 | } | |
1487 | ||
2d882307 VR |
1488 | while (total < size) { |
1489 | size_t dst_size = size - total; | |
1490 | size_t copy_size, proc; | |
1491 | void *dst = hw->pcm_ops->get_buffer_out(hw, &dst_size); | |
ff095e52 | 1492 | |
2d882307 VR |
1493 | if (dst_size == 0) { |
1494 | break; | |
1495 | } | |
1496 | ||
1497 | copy_size = MIN(size - total, dst_size); | |
1498 | if (dst) { | |
1499 | memcpy(dst, (char *)buf + total, copy_size); | |
1500 | } | |
1501 | proc = hw->pcm_ops->put_buffer_out(hw, dst, copy_size); | |
1502 | total += proc; | |
1503 | ||
1504 | if (proc == 0 || proc < copy_size) { | |
1505 | break; | |
1506 | } | |
1507 | } | |
1508 | ||
2d882307 | 1509 | return total; |
ff095e52 KZ |
1510 | } |
1511 | ||
1512 | size_t audio_generic_read(HWVoiceIn *hw, void *buf, size_t size) | |
1513 | { | |
b9896dc5 | 1514 | size_t total = 0; |
ff095e52 | 1515 | |
a2893c83 VR |
1516 | if (hw->pcm_ops->run_buffer_in) { |
1517 | hw->pcm_ops->run_buffer_in(hw); | |
1518 | } | |
1519 | ||
b9896dc5 VR |
1520 | while (total < size) { |
1521 | size_t src_size = size - total; | |
1522 | void *src = hw->pcm_ops->get_buffer_in(hw, &src_size); | |
ff095e52 | 1523 | |
b9896dc5 | 1524 | if (src_size == 0) { |
b9896dc5 VR |
1525 | break; |
1526 | } | |
1527 | ||
1528 | memcpy((char *)buf + total, src, src_size); | |
1529 | hw->pcm_ops->put_buffer_in(hw, src, src_size); | |
1530 | total += src_size; | |
1531 | } | |
1532 | ||
1533 | return total; | |
8d1439b6 | 1534 | } |
ff095e52 | 1535 | |
71830221 KZ |
1536 | static int audio_driver_init(AudioState *s, struct audio_driver *drv, |
1537 | bool msg, Audiodev *dev) | |
85571bc7 | 1538 | { |
71830221 | 1539 | s->drv_opaque = drv->init(dev); |
1d14ffa9 | 1540 | |
c0fe3827 | 1541 | if (s->drv_opaque) { |
ff095e52 KZ |
1542 | if (!drv->pcm_ops->get_buffer_in) { |
1543 | drv->pcm_ops->get_buffer_in = audio_generic_get_buffer_in; | |
1544 | drv->pcm_ops->put_buffer_in = audio_generic_put_buffer_in; | |
1545 | } | |
1546 | if (!drv->pcm_ops->get_buffer_out) { | |
1547 | drv->pcm_ops->get_buffer_out = audio_generic_get_buffer_out; | |
1548 | drv->pcm_ops->put_buffer_out = audio_generic_put_buffer_out; | |
1549 | } | |
1550 | ||
526fb058 KZ |
1551 | audio_init_nb_voices_out(s, drv); |
1552 | audio_init_nb_voices_in(s, drv); | |
c0fe3827 | 1553 | s->drv = drv; |
1d14ffa9 | 1554 | return 0; |
6c6886bd | 1555 | } else { |
fb37ce92 GH |
1556 | if (msg) { |
1557 | dolog("Could not init `%s' audio driver\n", drv->name); | |
1558 | } | |
1d14ffa9 | 1559 | return -1; |
85571bc7 FB |
1560 | } |
1561 | } | |
1562 | ||
538f0497 | 1563 | static void audio_vm_change_state_handler (void *opaque, bool running, |
1dfb4dd9 | 1564 | RunState state) |
85571bc7 | 1565 | { |
c0fe3827 | 1566 | AudioState *s = opaque; |
1d14ffa9 FB |
1567 | HWVoiceOut *hwo = NULL; |
1568 | HWVoiceIn *hwi = NULL; | |
1d14ffa9 | 1569 | |
978dd635 | 1570 | s->vm_running = running; |
526fb058 | 1571 | while ((hwo = audio_pcm_hw_find_any_enabled_out(s, hwo))) { |
571a8c52 KZ |
1572 | if (hwo->pcm_ops->enable_out) { |
1573 | hwo->pcm_ops->enable_out(hwo, running); | |
1574 | } | |
1d14ffa9 | 1575 | } |
85571bc7 | 1576 | |
526fb058 | 1577 | while ((hwi = audio_pcm_hw_find_any_enabled_in(s, hwi))) { |
571a8c52 KZ |
1578 | if (hwi->pcm_ops->enable_in) { |
1579 | hwi->pcm_ops->enable_in(hwi, running); | |
1580 | } | |
85571bc7 | 1581 | } |
39deb1e4 | 1582 | audio_reset_timer (s); |
85571bc7 FB |
1583 | } |
1584 | ||
ecd97e95 | 1585 | static void free_audio_state(AudioState *s) |
85571bc7 | 1586 | { |
a384c205 MAL |
1587 | HWVoiceOut *hwo, *hwon; |
1588 | HWVoiceIn *hwi, *hwin; | |
1d14ffa9 | 1589 | |
526fb058 | 1590 | QLIST_FOREACH_SAFE(hwo, &s->hw_head_out, entries, hwon) { |
ec36b695 | 1591 | SWVoiceCap *sc; |
8ead62cf | 1592 | |
571a8c52 KZ |
1593 | if (hwo->enabled && hwo->pcm_ops->enable_out) { |
1594 | hwo->pcm_ops->enable_out(hwo, false); | |
aeb29b64 | 1595 | } |
1d14ffa9 | 1596 | hwo->pcm_ops->fini_out (hwo); |
8ead62cf | 1597 | |
ec36b695 FB |
1598 | for (sc = hwo->cap_head.lh_first; sc; sc = sc->entries.le_next) { |
1599 | CaptureVoiceOut *cap = sc->cap; | |
1600 | struct capture_callback *cb; | |
1601 | ||
1602 | for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) { | |
1603 | cb->ops.destroy (cb->opaque); | |
1604 | } | |
8ead62cf | 1605 | } |
a384c205 | 1606 | QLIST_REMOVE(hwo, entries); |
1d14ffa9 | 1607 | } |
85571bc7 | 1608 | |
526fb058 | 1609 | QLIST_FOREACH_SAFE(hwi, &s->hw_head_in, entries, hwin) { |
571a8c52 KZ |
1610 | if (hwi->enabled && hwi->pcm_ops->enable_in) { |
1611 | hwi->pcm_ops->enable_in(hwi, false); | |
aeb29b64 | 1612 | } |
1d14ffa9 | 1613 | hwi->pcm_ops->fini_in (hwi); |
a384c205 | 1614 | QLIST_REMOVE(hwi, entries); |
85571bc7 | 1615 | } |
c0fe3827 FB |
1616 | |
1617 | if (s->drv) { | |
1618 | s->drv->fini (s->drv_opaque); | |
a384c205 | 1619 | s->drv = NULL; |
c0fe3827 | 1620 | } |
71830221 KZ |
1621 | |
1622 | if (s->dev) { | |
1623 | qapi_free_Audiodev(s->dev); | |
1624 | s->dev = NULL; | |
1625 | } | |
e76ba19a KZ |
1626 | |
1627 | if (s->ts) { | |
1628 | timer_free(s->ts); | |
1629 | s->ts = NULL; | |
1630 | } | |
1631 | ||
ecd97e95 KZ |
1632 | g_free(s); |
1633 | } | |
1634 | ||
1635 | void audio_cleanup(void) | |
1636 | { | |
ecd97e95 KZ |
1637 | while (!QTAILQ_EMPTY(&audio_states)) { |
1638 | AudioState *s = QTAILQ_FIRST(&audio_states); | |
1639 | QTAILQ_REMOVE(&audio_states, s, list); | |
1640 | free_audio_state(s); | |
1641 | } | |
85571bc7 FB |
1642 | } |
1643 | ||
da77adba DDAG |
1644 | static bool vmstate_audio_needed(void *opaque) |
1645 | { | |
1646 | /* | |
1647 | * Never needed, this vmstate only exists in case | |
1648 | * an old qemu sends it to us. | |
1649 | */ | |
1650 | return false; | |
1651 | } | |
1652 | ||
d959fce9 JQ |
1653 | static const VMStateDescription vmstate_audio = { |
1654 | .name = "audio", | |
1655 | .version_id = 1, | |
1656 | .minimum_version_id = 1, | |
da77adba | 1657 | .needed = vmstate_audio_needed, |
35d08458 | 1658 | .fields = (VMStateField[]) { |
d959fce9 | 1659 | VMSTATE_END_OF_LIST() |
1d14ffa9 | 1660 | } |
d959fce9 | 1661 | }; |
85571bc7 | 1662 | |
71830221 KZ |
1663 | static void audio_validate_opts(Audiodev *dev, Error **errp); |
1664 | ||
1665 | static AudiodevListEntry *audiodev_find( | |
1666 | AudiodevListHead *head, const char *drvname) | |
1667 | { | |
1668 | AudiodevListEntry *e; | |
1669 | QSIMPLEQ_FOREACH(e, head, next) { | |
1670 | if (strcmp(AudiodevDriver_str(e->dev->driver), drvname) == 0) { | |
1671 | return e; | |
1672 | } | |
1673 | } | |
1674 | ||
1675 | return NULL; | |
1676 | } | |
1677 | ||
ecd97e95 KZ |
1678 | /* |
1679 | * if we have dev, this function was called because of an -audiodev argument => | |
1680 | * initialize a new state with it | |
1681 | * if dev == NULL => legacy implicit initialization, return the already created | |
1682 | * state or create a new one | |
1683 | */ | |
af2041ed | 1684 | static AudioState *audio_init(Audiodev *dev, const char *name) |
85571bc7 | 1685 | { |
ecd97e95 | 1686 | static bool atexit_registered; |
1d14ffa9 | 1687 | size_t i; |
85571bc7 | 1688 | int done = 0; |
71830221 | 1689 | const char *drvname = NULL; |
713a98f8 | 1690 | VMChangeStateEntry *e; |
ecd97e95 | 1691 | AudioState *s; |
d3893a39 | 1692 | struct audio_driver *driver; |
71830221 KZ |
1693 | /* silence gcc warning about uninitialized variable */ |
1694 | AudiodevListHead head = QSIMPLEQ_HEAD_INITIALIZER(head); | |
1d14ffa9 | 1695 | |
f0c4555e GH |
1696 | if (using_spice) { |
1697 | /* | |
1698 | * When using spice allow the spice audio driver being picked | |
1699 | * as default. | |
1700 | * | |
1701 | * Temporary hack. Using audio devices without explicit | |
1702 | * audiodev= property is already deprecated. Same goes for | |
1703 | * the -soundhw switch. Once this support gets finally | |
1704 | * removed we can also drop the concept of a default audio | |
1705 | * backend and this can go away. | |
1706 | */ | |
1707 | driver = audio_driver_lookup("spice"); | |
06c8c375 GH |
1708 | if (driver) { |
1709 | driver->can_be_default = 1; | |
1710 | } | |
f0c4555e GH |
1711 | } |
1712 | ||
71830221 KZ |
1713 | if (dev) { |
1714 | /* -audiodev option */ | |
af2041ed | 1715 | legacy_config = false; |
71830221 | 1716 | drvname = AudiodevDriver_str(dev->driver); |
ecd97e95 | 1717 | } else if (!QTAILQ_EMPTY(&audio_states)) { |
af2041ed | 1718 | if (!legacy_config) { |
4b3b7793 KZ |
1719 | dolog("Device %s: audiodev default parameter is deprecated, please " |
1720 | "specify audiodev=%s\n", name, | |
1721 | QTAILQ_FIRST(&audio_states)->dev->id); | |
af2041ed | 1722 | } |
ecd97e95 | 1723 | return QTAILQ_FIRST(&audio_states); |
71830221 KZ |
1724 | } else { |
1725 | /* legacy implicit initialization */ | |
1726 | head = audio_handle_legacy_opts(); | |
1727 | /* | |
1728 | * In case of legacy initialization, all Audiodevs in the list will have | |
e3a6e0da | 1729 | * the same configuration (except the driver), so it doesn't matter which |
71830221 KZ |
1730 | * one we chose. We need an Audiodev to set up AudioState before we can |
1731 | * init a driver. Also note that dev at this point is still in the | |
1732 | * list. | |
1733 | */ | |
1734 | dev = QSIMPLEQ_FIRST(&head)->dev; | |
1735 | audio_validate_opts(dev, &error_abort); | |
1736 | } | |
ecd97e95 | 1737 | |
b21e2380 | 1738 | s = g_new0(AudioState, 1); |
71830221 KZ |
1739 | s->dev = dev; |
1740 | ||
72cf2d4f BS |
1741 | QLIST_INIT (&s->hw_head_out); |
1742 | QLIST_INIT (&s->hw_head_in); | |
1743 | QLIST_INIT (&s->cap_head); | |
ecd97e95 KZ |
1744 | if (!atexit_registered) { |
1745 | atexit(audio_cleanup); | |
1746 | atexit_registered = true; | |
1747 | } | |
571ec3d6 | 1748 | |
bc72ad67 | 1749 | s->ts = timer_new_ns(QEMU_CLOCK_VIRTUAL, audio_timer, s); |
571ec3d6 | 1750 | |
71830221 KZ |
1751 | s->nb_hw_voices_out = audio_get_pdo_out(dev)->voices; |
1752 | s->nb_hw_voices_in = audio_get_pdo_in(dev)->voices; | |
c0fe3827 | 1753 | |
61ddafbc | 1754 | if (s->nb_hw_voices_out < 1) { |
571ec3d6 | 1755 | dolog ("Bogus number of playback voices %d, setting to 1\n", |
1d14ffa9 FB |
1756 | s->nb_hw_voices_out); |
1757 | s->nb_hw_voices_out = 1; | |
1758 | } | |
1759 | ||
a7b7802b | 1760 | if (s->nb_hw_voices_in < 0) { |
571ec3d6 | 1761 | dolog ("Bogus number of capture voices %d, setting to 0\n", |
1d14ffa9 | 1762 | s->nb_hw_voices_in); |
571ec3d6 | 1763 | s->nb_hw_voices_in = 0; |
1d14ffa9 | 1764 | } |
85571bc7 | 1765 | |
85571bc7 | 1766 | if (drvname) { |
d3893a39 GH |
1767 | driver = audio_driver_lookup(drvname); |
1768 | if (driver) { | |
71830221 | 1769 | done = !audio_driver_init(s, driver, true, dev); |
d3893a39 | 1770 | } else { |
85571bc7 FB |
1771 | dolog ("Unknown audio driver `%s'\n", drvname); |
1772 | } | |
0f957c53 MAL |
1773 | if (!done) { |
1774 | free_audio_state(s); | |
1775 | return NULL; | |
1776 | } | |
71830221 KZ |
1777 | } else { |
1778 | for (i = 0; audio_prio_list[i]; i++) { | |
1779 | AudiodevListEntry *e = audiodev_find(&head, audio_prio_list[i]); | |
d3893a39 | 1780 | driver = audio_driver_lookup(audio_prio_list[i]); |
71830221 KZ |
1781 | |
1782 | if (e && driver) { | |
1783 | s->dev = dev = e->dev; | |
1784 | audio_validate_opts(dev, &error_abort); | |
1785 | done = !audio_driver_init(s, driver, false, dev); | |
1786 | if (done) { | |
1787 | e->dev = NULL; | |
1788 | break; | |
1789 | } | |
1d14ffa9 | 1790 | } |
85571bc7 FB |
1791 | } |
1792 | } | |
71830221 | 1793 | audio_free_audiodev_list(&head); |
85571bc7 | 1794 | |
85571bc7 | 1795 | if (!done) { |
d3893a39 | 1796 | driver = audio_driver_lookup("none"); |
71830221 | 1797 | done = !audio_driver_init(s, driver, false, dev); |
7e274652 MA |
1798 | assert(done); |
1799 | dolog("warning: Using timer based audio emulation\n"); | |
85571bc7 | 1800 | } |
1d14ffa9 | 1801 | |
71830221 KZ |
1802 | if (dev->timer_period <= 0) { |
1803 | s->period_ticks = 1; | |
0d9acba8 | 1804 | } else { |
40ad46d3 | 1805 | s->period_ticks = dev->timer_period * (int64_t)SCALE_US; |
1d14ffa9 | 1806 | } |
0d9acba8 PB |
1807 | |
1808 | e = qemu_add_vm_change_state_handler (audio_vm_change_state_handler, s); | |
1809 | if (!e) { | |
1810 | dolog ("warning: Could not register change state handler\n" | |
1811 | "(Audio can continue looping even after stopping the VM)\n"); | |
1d14ffa9 FB |
1812 | } |
1813 | ||
0f957c53 | 1814 | QTAILQ_INSERT_TAIL(&audio_states, s, list); |
72cf2d4f | 1815 | QLIST_INIT (&s->card_head); |
0be71e32 | 1816 | vmstate_register (NULL, 0, &vmstate_audio, s); |
ecd97e95 | 1817 | return s; |
71830221 KZ |
1818 | } |
1819 | ||
1820 | void audio_free_audiodev_list(AudiodevListHead *head) | |
1821 | { | |
1822 | AudiodevListEntry *e; | |
1823 | while ((e = QSIMPLEQ_FIRST(head))) { | |
1824 | QSIMPLEQ_REMOVE_HEAD(head, next); | |
1825 | qapi_free_Audiodev(e->dev); | |
1826 | g_free(e); | |
1827 | } | |
85571bc7 | 1828 | } |
8ead62cf | 1829 | |
1a7dafce | 1830 | void AUD_register_card (const char *name, QEMUSoundCard *card) |
1831 | { | |
ecd97e95 | 1832 | if (!card->state) { |
af2041ed | 1833 | card->state = audio_init(NULL, name); |
ecd97e95 KZ |
1834 | } |
1835 | ||
7267c094 | 1836 | card->name = g_strdup (name); |
1a7dafce | 1837 | memset (&card->entries, 0, sizeof (card->entries)); |
ecd97e95 | 1838 | QLIST_INSERT_HEAD(&card->state->card_head, card, entries); |
1a7dafce | 1839 | } |
1840 | ||
1841 | void AUD_remove_card (QEMUSoundCard *card) | |
1842 | { | |
72cf2d4f | 1843 | QLIST_REMOVE (card, entries); |
7267c094 | 1844 | g_free (card->name); |
1a7dafce | 1845 | } |
1846 | ||
33940dd3 | 1847 | static struct audio_pcm_ops capture_pcm_ops; |
1a7dafce | 1848 | |
ecd97e95 KZ |
1849 | CaptureVoiceOut *AUD_add_capture( |
1850 | AudioState *s, | |
1ea879e5 | 1851 | struct audsettings *as, |
8ead62cf FB |
1852 | struct audio_capture_ops *ops, |
1853 | void *cb_opaque | |
1854 | ) | |
1855 | { | |
1856 | CaptureVoiceOut *cap; | |
1857 | struct capture_callback *cb; | |
1858 | ||
ecd97e95 | 1859 | if (!s) { |
af2041ed | 1860 | if (!legacy_config) { |
4b3b7793 | 1861 | dolog("Capturing without setting an audiodev is deprecated\n"); |
af2041ed KZ |
1862 | } |
1863 | s = audio_init(NULL, NULL); | |
ecd97e95 KZ |
1864 | } |
1865 | ||
1930616b KZ |
1866 | if (!audio_get_pdo_out(s->dev)->mixing_engine) { |
1867 | dolog("Can't capture with mixeng disabled\n"); | |
1868 | return NULL; | |
1869 | } | |
1870 | ||
ec36b695 | 1871 | if (audio_validate_settings (as)) { |
8ead62cf FB |
1872 | dolog ("Invalid settings were passed when trying to add capture\n"); |
1873 | audio_print_settings (as); | |
e8d85444 | 1874 | return NULL; |
8ead62cf FB |
1875 | } |
1876 | ||
e8d85444 | 1877 | cb = g_malloc0(sizeof(*cb)); |
8ead62cf FB |
1878 | cb->ops = *ops; |
1879 | cb->opaque = cb_opaque; | |
1880 | ||
526fb058 | 1881 | cap = audio_pcm_capture_find_specific(s, as); |
8ead62cf | 1882 | if (cap) { |
72cf2d4f | 1883 | QLIST_INSERT_HEAD (&cap->cb_head, cb, entries); |
ec36b695 | 1884 | return cap; |
6c6886bd | 1885 | } else { |
8ead62cf FB |
1886 | HWVoiceOut *hw; |
1887 | CaptureVoiceOut *cap; | |
1888 | ||
e8d85444 | 1889 | cap = g_malloc0(sizeof(*cap)); |
8ead62cf FB |
1890 | |
1891 | hw = &cap->hw; | |
526fb058 | 1892 | hw->s = s; |
33940dd3 | 1893 | hw->pcm_ops = &capture_pcm_ops; |
72cf2d4f BS |
1894 | QLIST_INIT (&hw->sw_head); |
1895 | QLIST_INIT (&cap->cb_head); | |
8ead62cf FB |
1896 | |
1897 | /* XXX find a more elegant way */ | |
1898 | hw->samples = 4096 * 4; | |
dc88e38f | 1899 | audio_pcm_hw_alloc_resources_out(hw); |
8ead62cf | 1900 | |
d929eba5 | 1901 | audio_pcm_init_info (&hw->info, as); |
8ead62cf | 1902 | |
8dbd3d17 | 1903 | cap->buf = g_malloc0_n(hw->mix_buf.size, hw->info.bytes_per_frame); |
8ead62cf | 1904 | |
ed2a4a79 KZ |
1905 | if (hw->info.is_float) { |
1906 | hw->clip = mixeng_clip_float[hw->info.nchannels == 2]; | |
1907 | } else { | |
1908 | hw->clip = mixeng_clip | |
1909 | [hw->info.nchannels == 2] | |
1910 | [hw->info.is_signed] | |
1911 | [hw->info.swap_endianness] | |
1912 | [audio_bits_to_index(hw->info.bits)]; | |
1913 | } | |
8ead62cf | 1914 | |
72cf2d4f BS |
1915 | QLIST_INSERT_HEAD (&s->cap_head, cap, entries); |
1916 | QLIST_INSERT_HEAD (&cap->cb_head, cb, entries); | |
8ead62cf | 1917 | |
526fb058 | 1918 | QLIST_FOREACH(hw, &s->hw_head_out, entries) { |
1a7dafce | 1919 | audio_attach_capture (hw); |
8ead62cf | 1920 | } |
ec36b695 | 1921 | return cap; |
ec36b695 FB |
1922 | } |
1923 | } | |
1924 | ||
1925 | void AUD_del_capture (CaptureVoiceOut *cap, void *cb_opaque) | |
1926 | { | |
1927 | struct capture_callback *cb; | |
1928 | ||
1929 | for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) { | |
1930 | if (cb->opaque == cb_opaque) { | |
1931 | cb->ops.destroy (cb_opaque); | |
72cf2d4f | 1932 | QLIST_REMOVE (cb, entries); |
7267c094 | 1933 | g_free (cb); |
ec36b695 FB |
1934 | |
1935 | if (!cap->cb_head.lh_first) { | |
1936 | SWVoiceOut *sw = cap->hw.sw_head.lh_first, *sw1; | |
a3c25997 | 1937 | |
ec36b695 | 1938 | while (sw) { |
a3c25997 | 1939 | SWVoiceCap *sc = (SWVoiceCap *) sw; |
ec36b695 FB |
1940 | #ifdef DEBUG_CAPTURE |
1941 | dolog ("freeing %s\n", sw->name); | |
1942 | #endif | |
a3c25997 | 1943 | |
ec36b695 FB |
1944 | sw1 = sw->entries.le_next; |
1945 | if (sw->rate) { | |
1946 | st_rate_stop (sw->rate); | |
1947 | sw->rate = NULL; | |
1948 | } | |
72cf2d4f BS |
1949 | QLIST_REMOVE (sw, entries); |
1950 | QLIST_REMOVE (sc, entries); | |
7267c094 | 1951 | g_free (sc); |
ec36b695 FB |
1952 | sw = sw1; |
1953 | } | |
72cf2d4f | 1954 | QLIST_REMOVE (cap, entries); |
8dbd3d17 | 1955 | g_free(cap->hw.mix_buf.buffer); |
3268a845 | 1956 | g_free (cap->buf); |
7267c094 | 1957 | g_free (cap); |
ec36b695 FB |
1958 | } |
1959 | return; | |
1960 | } | |
8ead62cf FB |
1961 | } |
1962 | } | |
683efdcb AZ |
1963 | |
1964 | void AUD_set_volume_out (SWVoiceOut *sw, int mute, uint8_t lvol, uint8_t rvol) | |
cecc1e79 KZ |
1965 | { |
1966 | Volume vol = { .mute = mute, .channels = 2, .vol = { lvol, rvol } }; | |
1967 | audio_set_volume_out(sw, &vol); | |
1968 | } | |
1969 | ||
1970 | void audio_set_volume_out(SWVoiceOut *sw, Volume *vol) | |
683efdcb AZ |
1971 | { |
1972 | if (sw) { | |
6c95ab94 MAL |
1973 | HWVoiceOut *hw = sw->hw; |
1974 | ||
cecc1e79 KZ |
1975 | sw->vol.mute = vol->mute; |
1976 | sw->vol.l = nominal_volume.l * vol->vol[0] / 255; | |
1977 | sw->vol.r = nominal_volume.l * vol->vol[vol->channels > 1 ? 1 : 0] / | |
1978 | 255; | |
6c95ab94 | 1979 | |
571a8c52 | 1980 | if (hw->pcm_ops->volume_out) { |
cecc1e79 | 1981 | hw->pcm_ops->volume_out(hw, vol); |
6c95ab94 | 1982 | } |
683efdcb AZ |
1983 | } |
1984 | } | |
1985 | ||
1986 | void AUD_set_volume_in (SWVoiceIn *sw, int mute, uint8_t lvol, uint8_t rvol) | |
cecc1e79 KZ |
1987 | { |
1988 | Volume vol = { .mute = mute, .channels = 2, .vol = { lvol, rvol } }; | |
1989 | audio_set_volume_in(sw, &vol); | |
1990 | } | |
1991 | ||
1992 | void audio_set_volume_in(SWVoiceIn *sw, Volume *vol) | |
683efdcb AZ |
1993 | { |
1994 | if (sw) { | |
6c95ab94 MAL |
1995 | HWVoiceIn *hw = sw->hw; |
1996 | ||
cecc1e79 KZ |
1997 | sw->vol.mute = vol->mute; |
1998 | sw->vol.l = nominal_volume.l * vol->vol[0] / 255; | |
1999 | sw->vol.r = nominal_volume.r * vol->vol[vol->channels > 1 ? 1 : 0] / | |
2000 | 255; | |
6c95ab94 | 2001 | |
571a8c52 | 2002 | if (hw->pcm_ops->volume_in) { |
cecc1e79 | 2003 | hw->pcm_ops->volume_in(hw, vol); |
6c95ab94 | 2004 | } |
683efdcb AZ |
2005 | } |
2006 | } | |
71830221 KZ |
2007 | |
2008 | void audio_create_pdos(Audiodev *dev) | |
2009 | { | |
2010 | switch (dev->driver) { | |
2011 | #define CASE(DRIVER, driver, pdo_name) \ | |
2012 | case AUDIODEV_DRIVER_##DRIVER: \ | |
ceb19c8f | 2013 | if (!dev->u.driver.in) { \ |
71830221 KZ |
2014 | dev->u.driver.in = g_malloc0( \ |
2015 | sizeof(Audiodev##pdo_name##PerDirectionOptions)); \ | |
71830221 | 2016 | } \ |
ceb19c8f | 2017 | if (!dev->u.driver.out) { \ |
71830221 | 2018 | dev->u.driver.out = g_malloc0( \ |
725662d6 | 2019 | sizeof(Audiodev##pdo_name##PerDirectionOptions)); \ |
71830221 KZ |
2020 | } \ |
2021 | break | |
2022 | ||
2023 | CASE(NONE, none, ); | |
7a92a857 | 2024 | #ifdef CONFIG_AUDIO_ALSA |
71830221 | 2025 | CASE(ALSA, alsa, Alsa); |
7a92a857 DB |
2026 | #endif |
2027 | #ifdef CONFIG_AUDIO_COREAUDIO | |
71830221 | 2028 | CASE(COREAUDIO, coreaudio, Coreaudio); |
7a92a857 DB |
2029 | #endif |
2030 | #ifdef CONFIG_DBUS_DISPLAY | |
739362d4 | 2031 | CASE(DBUS, dbus, ); |
7a92a857 DB |
2032 | #endif |
2033 | #ifdef CONFIG_AUDIO_DSOUND | |
71830221 | 2034 | CASE(DSOUND, dsound, ); |
7a92a857 DB |
2035 | #endif |
2036 | #ifdef CONFIG_AUDIO_JACK | |
2e445703 | 2037 | CASE(JACK, jack, Jack); |
7a92a857 DB |
2038 | #endif |
2039 | #ifdef CONFIG_AUDIO_OSS | |
71830221 | 2040 | CASE(OSS, oss, Oss); |
7a92a857 DB |
2041 | #endif |
2042 | #ifdef CONFIG_AUDIO_PA | |
71830221 | 2043 | CASE(PA, pa, Pa); |
7a92a857 DB |
2044 | #endif |
2045 | #ifdef CONFIG_AUDIO_SDL | |
5a0926c2 | 2046 | CASE(SDL, sdl, Sdl); |
7a92a857 DB |
2047 | #endif |
2048 | #ifdef CONFIG_AUDIO_SNDIO | |
663df1cc | 2049 | CASE(SNDIO, sndio, ); |
7a92a857 DB |
2050 | #endif |
2051 | #ifdef CONFIG_SPICE | |
71830221 | 2052 | CASE(SPICE, spice, ); |
7a92a857 | 2053 | #endif |
71830221 KZ |
2054 | CASE(WAV, wav, ); |
2055 | ||
2056 | case AUDIODEV_DRIVER__MAX: | |
2057 | abort(); | |
2058 | }; | |
2059 | } | |
2060 | ||
2061 | static void audio_validate_per_direction_opts( | |
2062 | AudiodevPerDirectionOptions *pdo, Error **errp) | |
2063 | { | |
1930616b KZ |
2064 | if (!pdo->has_mixing_engine) { |
2065 | pdo->has_mixing_engine = true; | |
2066 | pdo->mixing_engine = true; | |
2067 | } | |
71830221 KZ |
2068 | if (!pdo->has_fixed_settings) { |
2069 | pdo->has_fixed_settings = true; | |
1930616b | 2070 | pdo->fixed_settings = pdo->mixing_engine; |
71830221 KZ |
2071 | } |
2072 | if (!pdo->fixed_settings && | |
2073 | (pdo->has_frequency || pdo->has_channels || pdo->has_format)) { | |
2074 | error_setg(errp, | |
2075 | "You can't use frequency, channels or format with fixed-settings=off"); | |
2076 | return; | |
2077 | } | |
1930616b KZ |
2078 | if (!pdo->mixing_engine && pdo->fixed_settings) { |
2079 | error_setg(errp, "You can't use fixed-settings without mixeng"); | |
2080 | return; | |
2081 | } | |
71830221 KZ |
2082 | |
2083 | if (!pdo->has_frequency) { | |
2084 | pdo->has_frequency = true; | |
2085 | pdo->frequency = 44100; | |
2086 | } | |
2087 | if (!pdo->has_channels) { | |
2088 | pdo->has_channels = true; | |
2089 | pdo->channels = 2; | |
2090 | } | |
2091 | if (!pdo->has_voices) { | |
2092 | pdo->has_voices = true; | |
1930616b | 2093 | pdo->voices = pdo->mixing_engine ? 1 : INT_MAX; |
71830221 KZ |
2094 | } |
2095 | if (!pdo->has_format) { | |
2096 | pdo->has_format = true; | |
2097 | pdo->format = AUDIO_FORMAT_S16; | |
2098 | } | |
2099 | } | |
2100 | ||
2101 | static void audio_validate_opts(Audiodev *dev, Error **errp) | |
2102 | { | |
2103 | Error *err = NULL; | |
2104 | ||
2105 | audio_create_pdos(dev); | |
2106 | ||
2107 | audio_validate_per_direction_opts(audio_get_pdo_in(dev), &err); | |
2108 | if (err) { | |
2109 | error_propagate(errp, err); | |
2110 | return; | |
2111 | } | |
2112 | ||
2113 | audio_validate_per_direction_opts(audio_get_pdo_out(dev), &err); | |
2114 | if (err) { | |
2115 | error_propagate(errp, err); | |
2116 | return; | |
2117 | } | |
2118 | ||
2119 | if (!dev->has_timer_period) { | |
2120 | dev->has_timer_period = true; | |
2121 | dev->timer_period = 10000; /* 100Hz -> 10ms */ | |
2122 | } | |
2123 | } | |
2124 | ||
5e03b6da CF |
2125 | void audio_help(void) |
2126 | { | |
2127 | int i; | |
2128 | ||
2129 | printf("Available audio drivers:\n"); | |
2130 | ||
2131 | for (i = 0; i < AUDIODEV_DRIVER__MAX; i++) { | |
2132 | audio_driver *driver = audio_driver_lookup(AudiodevDriver_str(i)); | |
2133 | if (driver) { | |
2134 | printf("%s\n", driver->name); | |
2135 | } | |
2136 | } | |
2137 | } | |
2138 | ||
71830221 KZ |
2139 | void audio_parse_option(const char *opt) |
2140 | { | |
71830221 KZ |
2141 | Audiodev *dev = NULL; |
2142 | ||
5e03b6da CF |
2143 | if (is_help_option(opt)) { |
2144 | audio_help(); | |
2145 | exit(EXIT_SUCCESS); | |
2146 | } | |
71830221 KZ |
2147 | Visitor *v = qobject_input_visitor_new_str(opt, "driver", &error_fatal); |
2148 | visit_type_Audiodev(v, NULL, &dev, &error_fatal); | |
2149 | visit_free(v); | |
2150 | ||
039a6837 PB |
2151 | audio_define(dev); |
2152 | } | |
2153 | ||
2154 | void audio_define(Audiodev *dev) | |
2155 | { | |
2156 | AudiodevListEntry *e; | |
2157 | ||
71830221 KZ |
2158 | audio_validate_opts(dev, &error_fatal); |
2159 | ||
b21e2380 | 2160 | e = g_new0(AudiodevListEntry, 1); |
71830221 KZ |
2161 | e->dev = dev; |
2162 | QSIMPLEQ_INSERT_TAIL(&audiodevs, e, next); | |
2163 | } | |
2164 | ||
0f957c53 | 2165 | bool audio_init_audiodevs(void) |
71830221 KZ |
2166 | { |
2167 | AudiodevListEntry *e; | |
2168 | ||
2169 | QSIMPLEQ_FOREACH(e, &audiodevs, next) { | |
0f957c53 MAL |
2170 | if (!audio_init(e->dev, NULL)) { |
2171 | return false; | |
2172 | } | |
71830221 | 2173 | } |
0f957c53 MAL |
2174 | |
2175 | return true; | |
71830221 KZ |
2176 | } |
2177 | ||
2178 | audsettings audiodev_to_audsettings(AudiodevPerDirectionOptions *pdo) | |
2179 | { | |
2180 | return (audsettings) { | |
2181 | .freq = pdo->frequency, | |
2182 | .nchannels = pdo->channels, | |
2183 | .fmt = pdo->format, | |
2184 | .endianness = AUDIO_HOST_ENDIANNESS, | |
2185 | }; | |
2186 | } | |
2187 | ||
2188 | int audioformat_bytes_per_sample(AudioFormat fmt) | |
2189 | { | |
2190 | switch (fmt) { | |
2191 | case AUDIO_FORMAT_U8: | |
2192 | case AUDIO_FORMAT_S8: | |
2193 | return 1; | |
2194 | ||
2195 | case AUDIO_FORMAT_U16: | |
2196 | case AUDIO_FORMAT_S16: | |
2197 | return 2; | |
2198 | ||
2199 | case AUDIO_FORMAT_U32: | |
2200 | case AUDIO_FORMAT_S32: | |
ed2a4a79 | 2201 | case AUDIO_FORMAT_F32: |
71830221 KZ |
2202 | return 4; |
2203 | ||
2204 | case AUDIO_FORMAT__MAX: | |
2205 | ; | |
2206 | } | |
2207 | abort(); | |
2208 | } | |
2209 | ||
2210 | ||
2211 | /* frames = freq * usec / 1e6 */ | |
2212 | int audio_buffer_frames(AudiodevPerDirectionOptions *pdo, | |
2213 | audsettings *as, int def_usecs) | |
2214 | { | |
2215 | uint64_t usecs = pdo->has_buffer_length ? pdo->buffer_length : def_usecs; | |
2216 | return (as->freq * usecs + 500000) / 1000000; | |
2217 | } | |
2218 | ||
2219 | /* samples = channels * frames = channels * freq * usec / 1e6 */ | |
2220 | int audio_buffer_samples(AudiodevPerDirectionOptions *pdo, | |
2221 | audsettings *as, int def_usecs) | |
2222 | { | |
2223 | return as->nchannels * audio_buffer_frames(pdo, as, def_usecs); | |
2224 | } | |
2225 | ||
2226 | /* | |
2227 | * bytes = bytes_per_sample * samples = | |
2228 | * bytes_per_sample * channels * freq * usec / 1e6 | |
2229 | */ | |
2230 | int audio_buffer_bytes(AudiodevPerDirectionOptions *pdo, | |
2231 | audsettings *as, int def_usecs) | |
2232 | { | |
2233 | return audio_buffer_samples(pdo, as, def_usecs) * | |
2234 | audioformat_bytes_per_sample(as->fmt); | |
2235 | } | |
ecd97e95 KZ |
2236 | |
2237 | AudioState *audio_state_by_name(const char *name) | |
2238 | { | |
2239 | AudioState *s; | |
2240 | QTAILQ_FOREACH(s, &audio_states, list) { | |
2241 | assert(s->dev); | |
2242 | if (strcmp(name, s->dev->id) == 0) { | |
2243 | return s; | |
2244 | } | |
2245 | } | |
2246 | return NULL; | |
2247 | } | |
2248 | ||
2249 | const char *audio_get_id(QEMUSoundCard *card) | |
2250 | { | |
2251 | if (card->state) { | |
2252 | assert(card->state->dev); | |
2253 | return card->state->dev->id; | |
2254 | } else { | |
2255 | return ""; | |
2256 | } | |
2257 | } | |
857271a2 | 2258 | |
37a54d05 VR |
2259 | const char *audio_application_name(void) |
2260 | { | |
2261 | const char *vm_name; | |
2262 | ||
2263 | vm_name = qemu_get_vm_name(); | |
2264 | return vm_name ? vm_name : "qemu"; | |
2265 | } | |
2266 | ||
857271a2 KZ |
2267 | void audio_rate_start(RateCtl *rate) |
2268 | { | |
2269 | memset(rate, 0, sizeof(RateCtl)); | |
2270 | rate->start_ticks = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); | |
2271 | } | |
2272 | ||
02732641 | 2273 | size_t audio_rate_peek_bytes(RateCtl *rate, struct audio_pcm_info *info) |
857271a2 KZ |
2274 | { |
2275 | int64_t now; | |
2276 | int64_t ticks; | |
2277 | int64_t bytes; | |
02732641 | 2278 | int64_t frames; |
857271a2 KZ |
2279 | |
2280 | now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); | |
2281 | ticks = now - rate->start_ticks; | |
2282 | bytes = muldiv64(ticks, info->bytes_per_second, NANOSECONDS_PER_SECOND); | |
02732641 VR |
2283 | frames = (bytes - rate->bytes_sent) / info->bytes_per_frame; |
2284 | if (frames < 0 || frames > 65536) { | |
2285 | AUD_log(NULL, "Resetting rate control (%" PRId64 " frames)\n", frames); | |
857271a2 | 2286 | audio_rate_start(rate); |
02732641 | 2287 | frames = 0; |
857271a2 KZ |
2288 | } |
2289 | ||
02732641 VR |
2290 | return frames * info->bytes_per_frame; |
2291 | } | |
2292 | ||
2293 | void audio_rate_add_bytes(RateCtl *rate, size_t bytes_used) | |
2294 | { | |
2295 | rate->bytes_sent += bytes_used; | |
2296 | } | |
2297 | ||
613fe02b | 2298 | size_t audio_rate_get_bytes(RateCtl *rate, struct audio_pcm_info *info, |
02732641 VR |
2299 | size_t bytes_avail) |
2300 | { | |
2301 | size_t bytes; | |
2302 | ||
2303 | bytes = audio_rate_peek_bytes(rate, info); | |
2304 | bytes = MIN(bytes, bytes_avail); | |
2305 | audio_rate_add_bytes(rate, bytes); | |
2306 | ||
2307 | return bytes; | |
857271a2 | 2308 | } |
637d1809 DB |
2309 | |
2310 | AudiodevList *qmp_query_audiodevs(Error **errp) | |
2311 | { | |
2312 | AudiodevList *ret = NULL; | |
2313 | AudiodevListEntry *e; | |
2314 | QSIMPLEQ_FOREACH(e, &audiodevs, next) { | |
2315 | QAPI_LIST_PREPEND(ret, QAPI_CLONE(Audiodev, e->dev)); | |
2316 | } | |
2317 | return ret; | |
2318 | } |