target/i386: reimplement 0x0f 0x50-0x5f, add AVX
[qemu.git] / audio / audio_template.h
blob720a32e57e7dd98e29d6f3d0cb529464f6a27a8f
1 /*
2 * QEMU Audio subsystem header
4 * Copyright (c) 2005 Vassili Karpov (malc)
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
25 #ifdef DAC
26 #define NAME "playback"
27 #define HWBUF hw->mix_buf
28 #define TYPE out
29 #define HW HWVoiceOut
30 #define SW SWVoiceOut
31 #else
32 #define NAME "capture"
33 #define TYPE in
34 #define HW HWVoiceIn
35 #define SW SWVoiceIn
36 #define HWBUF hw->conv_buf
37 #endif
39 static void glue(audio_init_nb_voices_, TYPE)(AudioState *s,
40 struct audio_driver *drv)
42 int max_voices = glue (drv->max_voices_, TYPE);
43 int voice_size = glue (drv->voice_size_, TYPE);
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
50 } else {
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);
56 glue (s->nb_hw_voices_, TYPE) = max_voices;
59 if (audio_bug(__func__, !voice_size && max_voices)) {
60 dolog ("drv=`%s' voice_size=0 max_voices=%d\n",
61 drv->name, max_voices);
62 glue (s->nb_hw_voices_, TYPE) = 0;
65 if (audio_bug(__func__, voice_size && !max_voices)) {
66 dolog ("drv=`%s' voice_size=%d max_voices=0\n",
67 drv->name, voice_size);
71 static void glue (audio_pcm_hw_free_resources_, TYPE) (HW *hw)
73 g_free(hw->buf_emul);
74 g_free (HWBUF);
75 HWBUF = NULL;
78 static void glue(audio_pcm_hw_alloc_resources_, TYPE)(HW *hw)
80 if (glue(audio_get_pdo_, TYPE)(hw->s->dev)->mixing_engine) {
81 size_t samples = hw->samples;
82 if (audio_bug(__func__, samples == 0)) {
83 dolog("Attempted to allocate empty buffer\n");
86 HWBUF = g_malloc0(sizeof(STSampleBuffer) + sizeof(st_sample) * samples);
87 HWBUF->size = samples;
88 } else {
89 HWBUF = NULL;
93 static void glue (audio_pcm_sw_free_resources_, TYPE) (SW *sw)
95 g_free (sw->buf);
97 if (sw->rate) {
98 st_rate_stop (sw->rate);
101 sw->buf = NULL;
102 sw->rate = NULL;
105 static int glue (audio_pcm_sw_alloc_resources_, TYPE) (SW *sw)
107 int samples;
109 if (!glue(audio_get_pdo_, TYPE)(sw->s->dev)->mixing_engine) {
110 return 0;
113 #ifdef DAC
114 samples = ((int64_t) sw->HWBUF->size << 32) / sw->ratio;
115 #else
116 samples = (int64_t)sw->HWBUF->size * sw->ratio >> 32;
117 #endif
119 sw->buf = audio_calloc(__func__, samples, sizeof(struct st_sample));
120 if (!sw->buf) {
121 dolog ("Could not allocate buffer for `%s' (%d samples)\n",
122 SW_NAME (sw), samples);
123 return -1;
126 #ifdef DAC
127 sw->rate = st_rate_start (sw->info.freq, sw->hw->info.freq);
128 #else
129 sw->rate = st_rate_start (sw->hw->info.freq, sw->info.freq);
130 #endif
131 if (!sw->rate) {
132 g_free (sw->buf);
133 sw->buf = NULL;
134 return -1;
136 return 0;
139 static int glue (audio_pcm_sw_init_, TYPE) (
140 SW *sw,
141 HW *hw,
142 const char *name,
143 struct audsettings *as
146 int err;
148 audio_pcm_init_info (&sw->info, as);
149 sw->hw = hw;
150 sw->active = 0;
151 #ifdef DAC
152 sw->ratio = ((int64_t) sw->hw->info.freq << 32) / sw->info.freq;
153 sw->total_hw_samples_mixed = 0;
154 sw->empty = 1;
155 #else
156 sw->ratio = ((int64_t) sw->info.freq << 32) / sw->hw->info.freq;
157 #endif
159 if (sw->info.is_float) {
160 #ifdef DAC
161 sw->conv = mixeng_conv_float[sw->info.nchannels == 2];
162 #else
163 sw->clip = mixeng_clip_float[sw->info.nchannels == 2];
164 #endif
165 } else {
166 #ifdef DAC
167 sw->conv = mixeng_conv
168 #else
169 sw->clip = mixeng_clip
170 #endif
171 [sw->info.nchannels == 2]
172 [sw->info.is_signed]
173 [sw->info.swap_endianness]
174 [audio_bits_to_index(sw->info.bits)];
177 sw->name = g_strdup (name);
178 err = glue (audio_pcm_sw_alloc_resources_, TYPE) (sw);
179 if (err) {
180 g_free (sw->name);
181 sw->name = NULL;
183 return err;
186 static void glue (audio_pcm_sw_fini_, TYPE) (SW *sw)
188 glue (audio_pcm_sw_free_resources_, TYPE) (sw);
189 g_free (sw->name);
190 sw->name = NULL;
193 static void glue (audio_pcm_hw_add_sw_, TYPE) (HW *hw, SW *sw)
195 QLIST_INSERT_HEAD (&hw->sw_head, sw, entries);
198 static void glue (audio_pcm_hw_del_sw_, TYPE) (SW *sw)
200 QLIST_REMOVE (sw, entries);
203 static void glue (audio_pcm_hw_gc_, TYPE) (HW **hwp)
205 HW *hw = *hwp;
206 AudioState *s = hw->s;
208 if (!hw->sw_head.lh_first) {
209 #ifdef DAC
210 audio_detach_capture(hw);
211 #endif
212 QLIST_REMOVE(hw, entries);
213 glue(hw->pcm_ops->fini_, TYPE) (hw);
214 glue(s->nb_hw_voices_, TYPE) += 1;
215 glue(audio_pcm_hw_free_resources_ , TYPE) (hw);
216 g_free(hw);
217 *hwp = NULL;
221 static HW *glue(audio_pcm_hw_find_any_, TYPE)(AudioState *s, HW *hw)
223 return hw ? hw->entries.le_next : glue (s->hw_head_, TYPE).lh_first;
226 static HW *glue(audio_pcm_hw_find_any_enabled_, TYPE)(AudioState *s, HW *hw)
228 while ((hw = glue(audio_pcm_hw_find_any_, TYPE)(s, hw))) {
229 if (hw->enabled) {
230 return hw;
233 return NULL;
236 static HW *glue(audio_pcm_hw_find_specific_, TYPE)(AudioState *s, HW *hw,
237 struct audsettings *as)
239 while ((hw = glue(audio_pcm_hw_find_any_, TYPE)(s, hw))) {
240 if (audio_pcm_info_eq (&hw->info, as)) {
241 return hw;
244 return NULL;
247 static HW *glue(audio_pcm_hw_add_new_, TYPE)(AudioState *s,
248 struct audsettings *as)
250 HW *hw;
251 struct audio_driver *drv = s->drv;
253 if (!glue (s->nb_hw_voices_, TYPE)) {
254 return NULL;
257 if (audio_bug(__func__, !drv)) {
258 dolog ("No host audio driver\n");
259 return NULL;
262 if (audio_bug(__func__, !drv->pcm_ops)) {
263 dolog ("Host audio driver without pcm_ops\n");
264 return NULL;
267 hw = audio_calloc(__func__, 1, glue(drv->voice_size_, TYPE));
268 if (!hw) {
269 dolog ("Can not allocate voice `%s' size %d\n",
270 drv->name, glue (drv->voice_size_, TYPE));
271 return NULL;
274 hw->s = s;
275 hw->pcm_ops = drv->pcm_ops;
277 QLIST_INIT (&hw->sw_head);
278 #ifdef DAC
279 QLIST_INIT (&hw->cap_head);
280 #endif
281 if (glue (hw->pcm_ops->init_, TYPE) (hw, as, s->drv_opaque)) {
282 goto err0;
285 if (audio_bug(__func__, hw->samples <= 0)) {
286 dolog("hw->samples=%zd\n", hw->samples);
287 goto err1;
290 if (hw->info.is_float) {
291 #ifdef DAC
292 hw->clip = mixeng_clip_float[hw->info.nchannels == 2];
293 #else
294 hw->conv = mixeng_conv_float[hw->info.nchannels == 2];
295 #endif
296 } else {
297 #ifdef DAC
298 hw->clip = mixeng_clip
299 #else
300 hw->conv = mixeng_conv
301 #endif
302 [hw->info.nchannels == 2]
303 [hw->info.is_signed]
304 [hw->info.swap_endianness]
305 [audio_bits_to_index(hw->info.bits)];
308 glue(audio_pcm_hw_alloc_resources_, TYPE)(hw);
310 QLIST_INSERT_HEAD (&s->glue (hw_head_, TYPE), hw, entries);
311 glue (s->nb_hw_voices_, TYPE) -= 1;
312 #ifdef DAC
313 audio_attach_capture (hw);
314 #endif
315 return hw;
317 err1:
318 glue (hw->pcm_ops->fini_, TYPE) (hw);
319 err0:
320 g_free (hw);
321 return NULL;
324 AudiodevPerDirectionOptions *glue(audio_get_pdo_, TYPE)(Audiodev *dev)
326 switch (dev->driver) {
327 case AUDIODEV_DRIVER_NONE:
328 return dev->u.none.TYPE;
329 case AUDIODEV_DRIVER_ALSA:
330 return qapi_AudiodevAlsaPerDirectionOptions_base(dev->u.alsa.TYPE);
331 case AUDIODEV_DRIVER_COREAUDIO:
332 return qapi_AudiodevCoreaudioPerDirectionOptions_base(
333 dev->u.coreaudio.TYPE);
334 case AUDIODEV_DRIVER_DBUS:
335 return dev->u.dbus.TYPE;
336 case AUDIODEV_DRIVER_DSOUND:
337 return dev->u.dsound.TYPE;
338 case AUDIODEV_DRIVER_JACK:
339 return qapi_AudiodevJackPerDirectionOptions_base(dev->u.jack.TYPE);
340 case AUDIODEV_DRIVER_OSS:
341 return qapi_AudiodevOssPerDirectionOptions_base(dev->u.oss.TYPE);
342 case AUDIODEV_DRIVER_PA:
343 return qapi_AudiodevPaPerDirectionOptions_base(dev->u.pa.TYPE);
344 case AUDIODEV_DRIVER_SDL:
345 return qapi_AudiodevSdlPerDirectionOptions_base(dev->u.sdl.TYPE);
346 case AUDIODEV_DRIVER_SNDIO:
347 return dev->u.sndio.TYPE;
348 case AUDIODEV_DRIVER_SPICE:
349 return dev->u.spice.TYPE;
350 case AUDIODEV_DRIVER_WAV:
351 return dev->u.wav.TYPE;
353 case AUDIODEV_DRIVER__MAX:
354 break;
356 abort();
359 static HW *glue(audio_pcm_hw_add_, TYPE)(AudioState *s, struct audsettings *as)
361 HW *hw;
362 AudiodevPerDirectionOptions *pdo = glue(audio_get_pdo_, TYPE)(s->dev);
364 if (!pdo->mixing_engine || pdo->fixed_settings) {
365 hw = glue(audio_pcm_hw_add_new_, TYPE)(s, as);
366 if (!pdo->mixing_engine || hw) {
367 return hw;
371 hw = glue(audio_pcm_hw_find_specific_, TYPE)(s, NULL, as);
372 if (hw) {
373 return hw;
376 hw = glue(audio_pcm_hw_add_new_, TYPE)(s, as);
377 if (hw) {
378 return hw;
381 return glue(audio_pcm_hw_find_any_, TYPE)(s, NULL);
384 static SW *glue(audio_pcm_create_voice_pair_, TYPE)(
385 AudioState *s,
386 const char *sw_name,
387 struct audsettings *as
390 SW *sw;
391 HW *hw;
392 struct audsettings hw_as;
393 AudiodevPerDirectionOptions *pdo = glue(audio_get_pdo_, TYPE)(s->dev);
395 if (pdo->fixed_settings) {
396 hw_as = audiodev_to_audsettings(pdo);
397 } else {
398 hw_as = *as;
401 sw = audio_calloc(__func__, 1, sizeof(*sw));
402 if (!sw) {
403 dolog ("Could not allocate soft voice `%s' (%zu bytes)\n",
404 sw_name ? sw_name : "unknown", sizeof (*sw));
405 goto err1;
407 sw->s = s;
409 hw = glue(audio_pcm_hw_add_, TYPE)(s, &hw_as);
410 if (!hw) {
411 goto err2;
414 glue (audio_pcm_hw_add_sw_, TYPE) (hw, sw);
416 if (glue (audio_pcm_sw_init_, TYPE) (sw, hw, sw_name, as)) {
417 goto err3;
420 return sw;
422 err3:
423 glue (audio_pcm_hw_del_sw_, TYPE) (sw);
424 glue (audio_pcm_hw_gc_, TYPE) (&hw);
425 err2:
426 g_free (sw);
427 err1:
428 return NULL;
431 static void glue (audio_close_, TYPE) (SW *sw)
433 glue (audio_pcm_sw_fini_, TYPE) (sw);
434 glue (audio_pcm_hw_del_sw_, TYPE) (sw);
435 glue (audio_pcm_hw_gc_, TYPE) (&sw->hw);
436 g_free (sw);
439 void glue (AUD_close_, TYPE) (QEMUSoundCard *card, SW *sw)
441 if (sw) {
442 if (audio_bug(__func__, !card)) {
443 dolog ("card=%p\n", card);
444 return;
447 glue (audio_close_, TYPE) (sw);
451 SW *glue (AUD_open_, TYPE) (
452 QEMUSoundCard *card,
453 SW *sw,
454 const char *name,
455 void *callback_opaque ,
456 audio_callback_fn callback_fn,
457 struct audsettings *as
460 AudioState *s;
461 AudiodevPerDirectionOptions *pdo;
463 if (audio_bug(__func__, !card || !name || !callback_fn || !as)) {
464 dolog ("card=%p name=%p callback_fn=%p as=%p\n",
465 card, name, callback_fn, as);
466 goto fail;
469 s = card->state;
470 pdo = glue(audio_get_pdo_, TYPE)(s->dev);
472 ldebug ("open %s, freq %d, nchannels %d, fmt %d\n",
473 name, as->freq, as->nchannels, as->fmt);
475 if (audio_bug(__func__, audio_validate_settings(as))) {
476 audio_print_settings (as);
477 goto fail;
480 if (audio_bug(__func__, !s->drv)) {
481 dolog ("Can not open `%s' (no host audio driver)\n", name);
482 goto fail;
485 if (sw && audio_pcm_info_eq (&sw->info, as)) {
486 return sw;
489 if (!pdo->fixed_settings && sw) {
490 glue (AUD_close_, TYPE) (card, sw);
491 sw = NULL;
494 if (sw) {
495 HW *hw = sw->hw;
497 if (!hw) {
498 dolog ("Internal logic error voice `%s' has no hardware store\n",
499 SW_NAME (sw));
500 goto fail;
503 glue (audio_pcm_sw_fini_, TYPE) (sw);
504 if (glue (audio_pcm_sw_init_, TYPE) (sw, hw, name, as)) {
505 goto fail;
507 } else {
508 sw = glue(audio_pcm_create_voice_pair_, TYPE)(s, name, as);
509 if (!sw) {
510 dolog ("Failed to create voice `%s'\n", name);
511 return NULL;
515 sw->card = card;
516 sw->vol = nominal_volume;
517 sw->callback.fn = callback_fn;
518 sw->callback.opaque = callback_opaque;
520 #ifdef DEBUG_AUDIO
521 dolog ("%s\n", name);
522 audio_pcm_print_info ("hw", &sw->hw->info);
523 audio_pcm_print_info ("sw", &sw->info);
524 #endif
526 return sw;
528 fail:
529 glue (AUD_close_, TYPE) (card, sw);
530 return NULL;
533 int glue (AUD_is_active_, TYPE) (SW *sw)
535 return sw ? sw->active : 0;
538 void glue (AUD_init_time_stamp_, TYPE) (SW *sw, QEMUAudioTimeStamp *ts)
540 if (!sw) {
541 return;
544 ts->old_ts = sw->hw->ts_helper;
547 uint64_t glue (AUD_get_elapsed_usec_, TYPE) (SW *sw, QEMUAudioTimeStamp *ts)
549 uint64_t delta, cur_ts, old_ts;
551 if (!sw) {
552 return 0;
555 cur_ts = sw->hw->ts_helper;
556 old_ts = ts->old_ts;
557 /* dolog ("cur %" PRId64 " old %" PRId64 "\n", cur_ts, old_ts); */
559 if (cur_ts >= old_ts) {
560 delta = cur_ts - old_ts;
561 } else {
562 delta = UINT64_MAX - old_ts + cur_ts;
565 if (!delta) {
566 return 0;
569 return muldiv64 (delta, sw->hw->info.freq, 1000000);
572 #undef TYPE
573 #undef HW
574 #undef SW
575 #undef HWBUF
576 #undef NAME