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