]> git.proxmox.com Git - mirror_qemu.git/blame - audio/audio_template.h
audio: return Error ** from audio_state_by_name
[mirror_qemu.git] / audio / audio_template.h
CommitLineData
1d14ffa9
FB
1/*
2 * QEMU Audio subsystem header
3 *
4 * Copyright (c) 2005 Vassili Karpov (malc)
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24
25#ifdef DAC
571ec3d6
FB
26#define NAME "playback"
27#define HWBUF hw->mix_buf
1d14ffa9 28#define TYPE out
571ec3d6
FB
29#define HW HWVoiceOut
30#define SW SWVoiceOut
1d14ffa9 31#else
571ec3d6 32#define NAME "capture"
1d14ffa9 33#define TYPE in
571ec3d6
FB
34#define HW HWVoiceIn
35#define SW SWVoiceIn
36#define HWBUF hw->conv_buf
1d14ffa9
FB
37#endif
38
526fb058
KZ
39static void glue(audio_init_nb_voices_, TYPE)(AudioState *s,
40 struct audio_driver *drv)
c0fe3827 41{
571ec3d6 42 int max_voices = glue (drv->max_voices_, TYPE);
3724ab3b 43 size_t voice_size = glue(drv->voice_size_, TYPE);
c0fe3827 44
571ec3d6
FB
45 if (glue (s->nb_hw_voices_, TYPE) > max_voices) {
46 if (!max_voices) {
47#ifdef DAC
48 dolog ("Driver `%s' does not support " NAME "\n", drv->name);
49#endif
6c6886bd 50 } else {
571ec3d6
FB
51 dolog ("Driver `%s' does not support %d " NAME " voices, max %d\n",
52 drv->name,
53 glue (s->nb_hw_voices_, TYPE),
54 max_voices);
55 }
56 glue (s->nb_hw_voices_, TYPE) = max_voices;
c0fe3827
FB
57 }
58
470bcabd 59 if (audio_bug(__func__, !voice_size && max_voices)) {
571ec3d6
FB
60 dolog ("drv=`%s' voice_size=0 max_voices=%d\n",
61 drv->name, max_voices);
12f4abf6 62 glue (s->nb_hw_voices_, TYPE) = 0;
571ec3d6
FB
63 }
64
470bcabd 65 if (audio_bug(__func__, voice_size && !max_voices)) {
3724ab3b
VR
66 dolog("drv=`%s' voice_size=%zu max_voices=0\n",
67 drv->name, voice_size);
571ec3d6
FB
68 }
69}
70
71static void glue (audio_pcm_hw_free_resources_, TYPE) (HW *hw)
72{
ff095e52 73 g_free(hw->buf_emul);
8dbd3d17
VR
74 g_free(HWBUF.buffer);
75 HWBUF.buffer = NULL;
76 HWBUF.size = 0;
571ec3d6
FB
77}
78
dc88e38f 79static void glue(audio_pcm_hw_alloc_resources_, TYPE)(HW *hw)
571ec3d6 80{
1930616b
KZ
81 if (glue(audio_get_pdo_, TYPE)(hw->s->dev)->mixing_engine) {
82 size_t samples = hw->samples;
83 if (audio_bug(__func__, samples == 0)) {
84 dolog("Attempted to allocate empty buffer\n");
85 }
c0fe3827 86
8dbd3d17
VR
87 HWBUF.buffer = g_new0(st_sample, samples);
88 HWBUF.size = samples;
89 HWBUF.pos = 0;
1930616b 90 } else {
8dbd3d17
VR
91 HWBUF.buffer = NULL;
92 HWBUF.size = 0;
1930616b 93 }
571ec3d6
FB
94}
95
96static void glue (audio_pcm_sw_free_resources_, TYPE) (SW *sw)
97{
2c3f9a0a
VR
98 g_free(sw->resample_buf.buffer);
99 sw->resample_buf.buffer = NULL;
100 sw->resample_buf.size = 0;
571ec3d6
FB
101
102 if (sw->rate) {
103 st_rate_stop (sw->rate);
104 }
571ec3d6
FB
105 sw->rate = NULL;
106}
107
108static int glue (audio_pcm_sw_alloc_resources_, TYPE) (SW *sw)
109{
148392ab 110 HW *hw = sw->hw;
2f886a34 111 uint64_t samples;
571ec3d6 112
1930616b
KZ
113 if (!glue(audio_get_pdo_, TYPE)(sw->s->dev)->mixing_engine) {
114 return 0;
115 }
116
2f886a34 117 samples = muldiv64(HWBUF.size, sw->info.freq, hw->info.freq);
b9ae74e2 118 if (samples == 0) {
2f886a34
VR
119 uint64_t f_fe_min;
120 uint64_t f_be = (uint32_t)hw->info.freq;
b9ae74e2
VR
121
122 /* f_fe_min = ceil(1 [frames] * f_be [Hz] / size_be [frames]) */
2f886a34 123 f_fe_min = (f_be + HWBUF.size - 1) / HWBUF.size;
b9ae74e2
VR
124 qemu_log_mask(LOG_UNIMP,
125 AUDIO_CAP ": The guest selected a " NAME " sample rate"
2f886a34
VR
126 " of %d Hz for %s. Only sample rates >= %" PRIu64 " Hz"
127 " are supported.\n",
b9ae74e2
VR
128 sw->info.freq, sw->name, f_fe_min);
129 return -1;
130 }
c0fe3827 131
e1e6a6fc
VR
132 /*
133 * Allocate one additional audio frame that is needed for upsampling
134 * if the resample buffer size is small. For large buffer sizes take
2f886a34 135 * care of overflows and truncation.
e1e6a6fc 136 */
2f886a34 137 samples = samples < SIZE_MAX ? samples + 1 : SIZE_MAX;
2c3f9a0a
VR
138 sw->resample_buf.buffer = g_new0(st_sample, samples);
139 sw->resample_buf.size = samples;
140 sw->resample_buf.pos = 0;
c0fe3827 141
571ec3d6 142#ifdef DAC
148392ab 143 sw->rate = st_rate_start(sw->info.freq, hw->info.freq);
571ec3d6 144#else
148392ab 145 sw->rate = st_rate_start(hw->info.freq, sw->info.freq);
571ec3d6 146#endif
25bf0c2d 147
c0fe3827
FB
148 return 0;
149}
150
571ec3d6
FB
151static int glue (audio_pcm_sw_init_, TYPE) (
152 SW *sw,
153 HW *hw,
154 const char *name,
1ea879e5 155 struct audsettings *as
571ec3d6
FB
156 )
157{
158 int err;
159
d929eba5 160 audio_pcm_init_info (&sw->info, as);
571ec3d6
FB
161 sw->hw = hw;
162 sw->active = 0;
163#ifdef DAC
571ec3d6
FB
164 sw->total_hw_samples_mixed = 0;
165 sw->empty = 1;
571ec3d6
FB
166#endif
167
ed2a4a79 168 if (sw->info.is_float) {
571ec3d6 169#ifdef DAC
ed2a4a79 170 sw->conv = mixeng_conv_float[sw->info.nchannels == 2];
571ec3d6 171#else
ed2a4a79 172 sw->clip = mixeng_clip_float[sw->info.nchannels == 2];
571ec3d6 173#endif
ed2a4a79
KZ
174 } else {
175#ifdef DAC
176 sw->conv = mixeng_conv
177#else
178 sw->clip = mixeng_clip
179#endif
180 [sw->info.nchannels == 2]
181 [sw->info.is_signed]
182 [sw->info.swap_endianness]
183 [audio_bits_to_index(sw->info.bits)];
184 }
571ec3d6 185
7267c094 186 sw->name = g_strdup (name);
571ec3d6
FB
187 err = glue (audio_pcm_sw_alloc_resources_, TYPE) (sw);
188 if (err) {
7267c094 189 g_free (sw->name);
571ec3d6
FB
190 sw->name = NULL;
191 }
192 return err;
193}
194
1d14ffa9
FB
195static void glue (audio_pcm_sw_fini_, TYPE) (SW *sw)
196{
197 glue (audio_pcm_sw_free_resources_, TYPE) (sw);
fb7da626
MA
198 g_free (sw->name);
199 sw->name = NULL;
1d14ffa9
FB
200}
201
202static void glue (audio_pcm_hw_add_sw_, TYPE) (HW *hw, SW *sw)
203{
72cf2d4f 204 QLIST_INSERT_HEAD (&hw->sw_head, sw, entries);
1d14ffa9
FB
205}
206
207static void glue (audio_pcm_hw_del_sw_, TYPE) (SW *sw)
208{
72cf2d4f 209 QLIST_REMOVE (sw, entries);
1d14ffa9
FB
210}
211
1a7dafce 212static void glue (audio_pcm_hw_gc_, TYPE) (HW **hwp)
1d14ffa9 213{
c0fe3827 214 HW *hw = *hwp;
526fb058 215 AudioState *s = hw->s;
1d14ffa9 216
1d14ffa9 217 if (!hw->sw_head.lh_first) {
8ead62cf 218#ifdef DAC
8abf3feb 219 audio_detach_capture(hw);
8ead62cf 220#endif
8abf3feb
ZH
221 QLIST_REMOVE(hw, entries);
222 glue(hw->pcm_ops->fini_, TYPE) (hw);
223 glue(s->nb_hw_voices_, TYPE) += 1;
224 glue(audio_pcm_hw_free_resources_ , TYPE) (hw);
225 g_free(hw);
c0fe3827 226 *hwp = NULL;
1d14ffa9
FB
227 }
228}
229
526fb058 230static HW *glue(audio_pcm_hw_find_any_, TYPE)(AudioState *s, HW *hw)
1d14ffa9 231{
1a7dafce 232 return hw ? hw->entries.le_next : glue (s->hw_head_, TYPE).lh_first;
1d14ffa9
FB
233}
234
526fb058 235static HW *glue(audio_pcm_hw_find_any_enabled_, TYPE)(AudioState *s, HW *hw)
1d14ffa9 236{
526fb058 237 while ((hw = glue(audio_pcm_hw_find_any_, TYPE)(s, hw))) {
c0fe3827 238 if (hw->enabled) {
1d14ffa9
FB
239 return hw;
240 }
241 }
242 return NULL;
243}
244
526fb058
KZ
245static HW *glue(audio_pcm_hw_find_specific_, TYPE)(AudioState *s, HW *hw,
246 struct audsettings *as)
1d14ffa9 247{
526fb058 248 while ((hw = glue(audio_pcm_hw_find_any_, TYPE)(s, hw))) {
c0fe3827 249 if (audio_pcm_info_eq (&hw->info, as)) {
1d14ffa9
FB
250 return hw;
251 }
252 }
253 return NULL;
254}
255
526fb058
KZ
256static HW *glue(audio_pcm_hw_add_new_, TYPE)(AudioState *s,
257 struct audsettings *as)
1d14ffa9
FB
258{
259 HW *hw;
571ec3d6 260 struct audio_driver *drv = s->drv;
1d14ffa9 261
571ec3d6
FB
262 if (!glue (s->nb_hw_voices_, TYPE)) {
263 return NULL;
264 }
1d14ffa9 265
470bcabd 266 if (audio_bug(__func__, !drv)) {
571ec3d6 267 dolog ("No host audio driver\n");
12f4abf6 268 return NULL;
1d14ffa9
FB
269 }
270
470bcabd 271 if (audio_bug(__func__, !drv->pcm_ops)) {
571ec3d6 272 dolog ("Host audio driver without pcm_ops\n");
12f4abf6 273 return NULL;
571ec3d6
FB
274 }
275
3724ab3b
VR
276 /*
277 * Since glue(s->nb_hw_voices_, TYPE) is != 0, glue(drv->voice_size_, TYPE)
278 * is guaranteed to be != 0. See the audio_init_nb_voices_* functions.
279 */
280 hw = g_malloc0(glue(drv->voice_size_, TYPE));
526fb058 281 hw->s = s;
571ec3d6 282 hw->pcm_ops = drv->pcm_ops;
c01b2456 283
72cf2d4f 284 QLIST_INIT (&hw->sw_head);
8ead62cf 285#ifdef DAC
72cf2d4f 286 QLIST_INIT (&hw->cap_head);
8ead62cf 287#endif
5706db1d 288 if (glue (hw->pcm_ops->init_, TYPE) (hw, as, s->drv_opaque)) {
12f4abf6 289 goto err0;
571ec3d6
FB
290 }
291
470bcabd 292 if (audio_bug(__func__, hw->samples <= 0)) {
7520462b 293 dolog("hw->samples=%zd\n", hw->samples);
12f4abf6 294 goto err1;
571ec3d6
FB
295 }
296
ed2a4a79 297 if (hw->info.is_float) {
180b044f 298#ifdef DAC
ed2a4a79 299 hw->clip = mixeng_clip_float[hw->info.nchannels == 2];
180b044f 300#else
ed2a4a79 301 hw->conv = mixeng_conv_float[hw->info.nchannels == 2];
180b044f 302#endif
ed2a4a79 303 } else {
571ec3d6 304#ifdef DAC
ed2a4a79 305 hw->clip = mixeng_clip
571ec3d6 306#else
ed2a4a79 307 hw->conv = mixeng_conv
571ec3d6 308#endif
ed2a4a79
KZ
309 [hw->info.nchannels == 2]
310 [hw->info.is_signed]
311 [hw->info.swap_endianness]
312 [audio_bits_to_index(hw->info.bits)];
313 }
571ec3d6 314
dc88e38f 315 glue(audio_pcm_hw_alloc_resources_, TYPE)(hw);
571ec3d6 316
72cf2d4f 317 QLIST_INSERT_HEAD (&s->glue (hw_head_, TYPE), hw, entries);
571ec3d6 318 glue (s->nb_hw_voices_, TYPE) -= 1;
8ead62cf 319#ifdef DAC
1a7dafce 320 audio_attach_capture (hw);
8ead62cf 321#endif
571ec3d6 322 return hw;
12f4abf6
VR
323
324 err1:
325 glue (hw->pcm_ops->fini_, TYPE) (hw);
326 err0:
327 g_free (hw);
328 return NULL;
1d14ffa9
FB
329}
330
71830221
KZ
331AudiodevPerDirectionOptions *glue(audio_get_pdo_, TYPE)(Audiodev *dev)
332{
333 switch (dev->driver) {
334 case AUDIODEV_DRIVER_NONE:
335 return dev->u.none.TYPE;
7a92a857 336#ifdef CONFIG_AUDIO_ALSA
71830221
KZ
337 case AUDIODEV_DRIVER_ALSA:
338 return qapi_AudiodevAlsaPerDirectionOptions_base(dev->u.alsa.TYPE);
7a92a857
DB
339#endif
340#ifdef CONFIG_AUDIO_COREAUDIO
71830221
KZ
341 case AUDIODEV_DRIVER_COREAUDIO:
342 return qapi_AudiodevCoreaudioPerDirectionOptions_base(
343 dev->u.coreaudio.TYPE);
7a92a857
DB
344#endif
345#ifdef CONFIG_DBUS_DISPLAY
739362d4
MAL
346 case AUDIODEV_DRIVER_DBUS:
347 return dev->u.dbus.TYPE;
7a92a857
DB
348#endif
349#ifdef CONFIG_AUDIO_DSOUND
71830221
KZ
350 case AUDIODEV_DRIVER_DSOUND:
351 return dev->u.dsound.TYPE;
7a92a857
DB
352#endif
353#ifdef CONFIG_AUDIO_JACK
2e445703
GM
354 case AUDIODEV_DRIVER_JACK:
355 return qapi_AudiodevJackPerDirectionOptions_base(dev->u.jack.TYPE);
7a92a857
DB
356#endif
357#ifdef CONFIG_AUDIO_OSS
71830221
KZ
358 case AUDIODEV_DRIVER_OSS:
359 return qapi_AudiodevOssPerDirectionOptions_base(dev->u.oss.TYPE);
7a92a857
DB
360#endif
361#ifdef CONFIG_AUDIO_PA
71830221
KZ
362 case AUDIODEV_DRIVER_PA:
363 return qapi_AudiodevPaPerDirectionOptions_base(dev->u.pa.TYPE);
7a92a857 364#endif
c2d3d1c2
DB
365#ifdef CONFIG_AUDIO_PIPEWIRE
366 case AUDIODEV_DRIVER_PIPEWIRE:
367 return qapi_AudiodevPipewirePerDirectionOptions_base(dev->u.pipewire.TYPE);
368#endif
7a92a857 369#ifdef CONFIG_AUDIO_SDL
71830221 370 case AUDIODEV_DRIVER_SDL:
5a0926c2 371 return qapi_AudiodevSdlPerDirectionOptions_base(dev->u.sdl.TYPE);
7a92a857
DB
372#endif
373#ifdef CONFIG_AUDIO_SNDIO
663df1cc
AR
374 case AUDIODEV_DRIVER_SNDIO:
375 return dev->u.sndio.TYPE;
7a92a857
DB
376#endif
377#ifdef CONFIG_SPICE
71830221
KZ
378 case AUDIODEV_DRIVER_SPICE:
379 return dev->u.spice.TYPE;
7a92a857 380#endif
71830221
KZ
381 case AUDIODEV_DRIVER_WAV:
382 return dev->u.wav.TYPE;
383
384 case AUDIODEV_DRIVER__MAX:
385 break;
386 }
387 abort();
388}
389
526fb058 390static HW *glue(audio_pcm_hw_add_, TYPE)(AudioState *s, struct audsettings *as)
1d14ffa9
FB
391{
392 HW *hw;
71830221 393 AudiodevPerDirectionOptions *pdo = glue(audio_get_pdo_, TYPE)(s->dev);
1d14ffa9 394
1930616b 395 if (!pdo->mixing_engine || pdo->fixed_settings) {
526fb058 396 hw = glue(audio_pcm_hw_add_new_, TYPE)(s, as);
1930616b 397 if (!pdo->mixing_engine || hw) {
1d14ffa9
FB
398 return hw;
399 }
400 }
401
526fb058 402 hw = glue(audio_pcm_hw_find_specific_, TYPE)(s, NULL, as);
1d14ffa9
FB
403 if (hw) {
404 return hw;
405 }
406
526fb058 407 hw = glue(audio_pcm_hw_add_new_, TYPE)(s, as);
1d14ffa9
FB
408 if (hw) {
409 return hw;
410 }
411
526fb058 412 return glue(audio_pcm_hw_find_any_, TYPE)(s, NULL);
1d14ffa9
FB
413}
414
526fb058
KZ
415static SW *glue(audio_pcm_create_voice_pair_, TYPE)(
416 AudioState *s,
c0fe3827 417 const char *sw_name,
1ea879e5 418 struct audsettings *as
1d14ffa9
FB
419 )
420{
421 SW *sw;
422 HW *hw;
1ea879e5 423 struct audsettings hw_as;
71830221 424 AudiodevPerDirectionOptions *pdo = glue(audio_get_pdo_, TYPE)(s->dev);
1d14ffa9 425
71830221
KZ
426 if (pdo->fixed_settings) {
427 hw_as = audiodev_to_audsettings(pdo);
6c6886bd 428 } else {
c0fe3827 429 hw_as = *as;
1d14ffa9
FB
430 }
431
c6b69a81 432 sw = g_new0(SW, 1);
526fb058 433 sw->s = s;
1d14ffa9 434
526fb058 435 hw = glue(audio_pcm_hw_add_, TYPE)(s, &hw_as);
1d14ffa9 436 if (!hw) {
90394fe1 437 dolog("Could not create a backend for voice `%s'\n", sw_name);
c6b69a81 438 goto err1;
1d14ffa9
FB
439 }
440
441 glue (audio_pcm_hw_add_sw_, TYPE) (hw, sw);
442
d929eba5 443 if (glue (audio_pcm_sw_init_, TYPE) (sw, hw, sw_name, as)) {
c6b69a81 444 goto err2;
1d14ffa9
FB
445 }
446
447 return sw;
448
c6b69a81 449err2:
1d14ffa9 450 glue (audio_pcm_hw_del_sw_, TYPE) (sw);
1a7dafce 451 glue (audio_pcm_hw_gc_, TYPE) (&hw);
1d14ffa9 452err1:
c6b69a81 453 g_free(sw);
1d14ffa9
FB
454 return NULL;
455}
456
1a7dafce 457static void glue (audio_close_, TYPE) (SW *sw)
c0fe3827
FB
458{
459 glue (audio_pcm_sw_fini_, TYPE) (sw);
460 glue (audio_pcm_hw_del_sw_, TYPE) (sw);
1a7dafce 461 glue (audio_pcm_hw_gc_, TYPE) (&sw->hw);
7267c094 462 g_free (sw);
c0fe3827 463}
571ec3d6 464
c0fe3827 465void glue (AUD_close_, TYPE) (QEMUSoundCard *card, SW *sw)
1d14ffa9
FB
466{
467 if (sw) {
470bcabd 468 if (audio_bug(__func__, !card)) {
1a7dafce 469 dolog ("card=%p\n", card);
12f4abf6 470 return;
c0fe3827
FB
471 }
472
1a7dafce 473 glue (audio_close_, TYPE) (sw);
1d14ffa9
FB
474 }
475}
476
477SW *glue (AUD_open_, TYPE) (
c0fe3827 478 QEMUSoundCard *card,
1d14ffa9
FB
479 SW *sw,
480 const char *name,
481 void *callback_opaque ,
cb4f03e8 482 audio_callback_fn callback_fn,
1ea879e5 483 struct audsettings *as
1d14ffa9
FB
484 )
485{
d1670b20
KZ
486 AudioState *s;
487 AudiodevPerDirectionOptions *pdo;
1d14ffa9 488
470bcabd 489 if (audio_bug(__func__, !card || !name || !callback_fn || !as)) {
1a7dafce 490 dolog ("card=%p name=%p callback_fn=%p as=%p\n",
491 card, name, callback_fn, as);
12f4abf6 492 goto fail;
1d14ffa9
FB
493 }
494
d1670b20
KZ
495 s = card->state;
496 pdo = glue(audio_get_pdo_, TYPE)(s->dev);
497
93b65997
SW
498 ldebug ("open %s, freq %d, nchannels %d, fmt %d\n",
499 name, as->freq, as->nchannels, as->fmt);
500
470bcabd 501 if (audio_bug(__func__, audio_validate_settings(as))) {
c0fe3827 502 audio_print_settings (as);
12f4abf6 503 goto fail;
1d14ffa9
FB
504 }
505
470bcabd 506 if (audio_bug(__func__, !s->drv)) {
c0fe3827 507 dolog ("Can not open `%s' (no host audio driver)\n", name);
12f4abf6 508 goto fail;
1d14ffa9
FB
509 }
510
c0fe3827 511 if (sw && audio_pcm_info_eq (&sw->info, as)) {
1d14ffa9
FB
512 return sw;
513 }
514
71830221 515 if (!pdo->fixed_settings && sw) {
c0fe3827 516 glue (AUD_close_, TYPE) (card, sw);
1d14ffa9
FB
517 sw = NULL;
518 }
519
520 if (sw) {
521 HW *hw = sw->hw;
522
523 if (!hw) {
b637a61c
VR
524 dolog("Internal logic error: voice `%s' has no backend\n",
525 SW_NAME(sw));
1d14ffa9
FB
526 goto fail;
527 }
528
571ec3d6 529 glue (audio_pcm_sw_fini_, TYPE) (sw);
d929eba5 530 if (glue (audio_pcm_sw_init_, TYPE) (sw, hw, name, as)) {
1d14ffa9
FB
531 goto fail;
532 }
6c6886bd 533 } else {
526fb058 534 sw = glue(audio_pcm_create_voice_pair_, TYPE)(s, name, as);
1d14ffa9 535 if (!sw) {
571ec3d6 536 return NULL;
1d14ffa9
FB
537 }
538 }
539
7cbb28ed 540 sw->card = card;
541 sw->vol = nominal_volume;
542 sw->callback.fn = callback_fn;
543 sw->callback.opaque = callback_opaque;
1d14ffa9 544
1d14ffa9 545#ifdef DEBUG_AUDIO
7cbb28ed 546 dolog ("%s\n", name);
547 audio_pcm_print_info ("hw", &sw->hw->info);
548 audio_pcm_print_info ("sw", &sw->info);
1d14ffa9 549#endif
1d14ffa9
FB
550
551 return sw;
552
553 fail:
c0fe3827 554 glue (AUD_close_, TYPE) (card, sw);
1d14ffa9
FB
555 return NULL;
556}
557
558int glue (AUD_is_active_, TYPE) (SW *sw)
559{
560 return sw ? sw->active : 0;
561}
562
563void glue (AUD_init_time_stamp_, TYPE) (SW *sw, QEMUAudioTimeStamp *ts)
564{
565 if (!sw) {
566 return;
567 }
568
569 ts->old_ts = sw->hw->ts_helper;
570}
571
c0fe3827 572uint64_t glue (AUD_get_elapsed_usec_, TYPE) (SW *sw, QEMUAudioTimeStamp *ts)
1d14ffa9
FB
573{
574 uint64_t delta, cur_ts, old_ts;
575
576 if (!sw) {
577 return 0;
578 }
579
580 cur_ts = sw->hw->ts_helper;
581 old_ts = ts->old_ts;
0bfcd599 582 /* dolog ("cur %" PRId64 " old %" PRId64 "\n", cur_ts, old_ts); */
1d14ffa9
FB
583
584 if (cur_ts >= old_ts) {
585 delta = cur_ts - old_ts;
6c6886bd 586 } else {
1d14ffa9
FB
587 delta = UINT64_MAX - old_ts + cur_ts;
588 }
589
590 if (!delta) {
591 return 0;
592 }
593
4f4cc0ef 594 return muldiv64 (delta, sw->hw->info.freq, 1000000);
1d14ffa9
FB
595}
596
597#undef TYPE
598#undef HW
599#undef SW
571ec3d6
FB
600#undef HWBUF
601#undef NAME