kvm/powerpc: extern one function for MPC85xx code use
[qemu/mini2440.git] / audio / audio_template.h
blob369ed3b9e5d5b456817127851ffb3d00ee77c534
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) (
40 AudioState *s,
41 struct audio_driver *drv
44 int max_voices = glue (drv->max_voices_, TYPE);
45 int voice_size = glue (drv->voice_size_, TYPE);
47 if (glue (s->nb_hw_voices_, TYPE) > max_voices) {
48 if (!max_voices) {
49 #ifdef DAC
50 dolog ("Driver `%s' does not support " NAME "\n", drv->name);
51 #endif
53 else {
54 dolog ("Driver `%s' does not support %d " NAME " voices, max %d\n",
55 drv->name,
56 glue (s->nb_hw_voices_, TYPE),
57 max_voices);
59 glue (s->nb_hw_voices_, TYPE) = max_voices;
62 if (audio_bug (AUDIO_FUNC, !voice_size && max_voices)) {
63 dolog ("drv=`%s' voice_size=0 max_voices=%d\n",
64 drv->name, max_voices);
65 glue (s->nb_hw_voices_, TYPE) = 0;
68 if (audio_bug (AUDIO_FUNC, voice_size && !max_voices)) {
69 dolog ("drv=`%s' voice_size=%d max_voices=0\n",
70 drv->name, voice_size);
74 static void glue (audio_pcm_hw_free_resources_, TYPE) (HW *hw)
76 if (HWBUF) {
77 qemu_free (HWBUF);
80 HWBUF = NULL;
83 static int glue (audio_pcm_hw_alloc_resources_, TYPE) (HW *hw)
85 HWBUF = audio_calloc (AUDIO_FUNC, hw->samples, sizeof (struct st_sample));
86 if (!HWBUF) {
87 dolog ("Could not allocate " NAME " buffer (%d samples)\n",
88 hw->samples);
89 return -1;
92 return 0;
95 static void glue (audio_pcm_sw_free_resources_, TYPE) (SW *sw)
97 if (sw->buf) {
98 qemu_free (sw->buf);
101 if (sw->rate) {
102 st_rate_stop (sw->rate);
105 sw->buf = NULL;
106 sw->rate = NULL;
109 static int glue (audio_pcm_sw_alloc_resources_, TYPE) (SW *sw)
111 int samples;
113 #ifdef DAC
114 samples = sw->hw->samples;
115 #else
116 samples = ((int64_t) sw->hw->samples << 32) / sw->ratio;
117 #endif
119 sw->buf = audio_calloc (AUDIO_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 qemu_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 #ifdef DAC
160 sw->conv = mixeng_conv
161 #else
162 sw->clip = mixeng_clip
163 #endif
164 [sw->info.nchannels == 2]
165 [sw->info.sign]
166 [sw->info.swap_endianness]
167 [audio_bits_to_index (sw->info.bits)];
169 sw->name = qemu_strdup (name);
170 err = glue (audio_pcm_sw_alloc_resources_, TYPE) (sw);
171 if (err) {
172 qemu_free (sw->name);
173 sw->name = NULL;
175 return err;
178 static void glue (audio_pcm_sw_fini_, TYPE) (SW *sw)
180 glue (audio_pcm_sw_free_resources_, TYPE) (sw);
181 if (sw->name) {
182 qemu_free (sw->name);
183 sw->name = NULL;
187 static void glue (audio_pcm_hw_add_sw_, TYPE) (HW *hw, SW *sw)
189 LIST_INSERT_HEAD (&hw->sw_head, sw, entries);
192 static void glue (audio_pcm_hw_del_sw_, TYPE) (SW *sw)
194 LIST_REMOVE (sw, entries);
197 static void glue (audio_pcm_hw_gc_, TYPE) (AudioState *s, HW **hwp)
199 HW *hw = *hwp;
201 if (!hw->sw_head.lh_first) {
202 #ifdef DAC
203 audio_detach_capture (hw);
204 #endif
205 LIST_REMOVE (hw, entries);
206 glue (s->nb_hw_voices_, TYPE) += 1;
207 glue (audio_pcm_hw_free_resources_ ,TYPE) (hw);
208 glue (hw->pcm_ops->fini_, TYPE) (hw);
209 qemu_free (hw);
210 *hwp = NULL;
214 static HW *glue (audio_pcm_hw_find_any_, TYPE) (AudioState *s, HW *hw)
216 return hw ? hw->entries.le_next : s->glue (hw_head_, TYPE).lh_first;
219 static HW *glue (audio_pcm_hw_find_any_enabled_, TYPE) (AudioState *s, HW *hw)
221 while ((hw = glue (audio_pcm_hw_find_any_, TYPE) (s, hw))) {
222 if (hw->enabled) {
223 return hw;
226 return NULL;
229 static HW *glue (audio_pcm_hw_find_specific_, TYPE) (
230 AudioState *s,
231 HW *hw,
232 struct audsettings *as
235 while ((hw = glue (audio_pcm_hw_find_any_, TYPE) (s, hw))) {
236 if (audio_pcm_info_eq (&hw->info, as)) {
237 return hw;
240 return NULL;
243 static HW *glue (audio_pcm_hw_add_new_, TYPE) (AudioState *s,
244 struct audsettings *as)
246 HW *hw;
247 struct audio_driver *drv = s->drv;
249 if (!glue (s->nb_hw_voices_, TYPE)) {
250 return NULL;
253 if (audio_bug (AUDIO_FUNC, !drv)) {
254 dolog ("No host audio driver\n");
255 return NULL;
258 if (audio_bug (AUDIO_FUNC, !drv->pcm_ops)) {
259 dolog ("Host audio driver without pcm_ops\n");
260 return NULL;
263 hw = audio_calloc (AUDIO_FUNC, 1, glue (drv->voice_size_, TYPE));
264 if (!hw) {
265 dolog ("Can not allocate voice `%s' size %d\n",
266 drv->name, glue (drv->voice_size_, TYPE));
267 return NULL;
270 hw->pcm_ops = drv->pcm_ops;
271 LIST_INIT (&hw->sw_head);
272 #ifdef DAC
273 LIST_INIT (&hw->cap_head);
274 #endif
275 if (glue (hw->pcm_ops->init_, TYPE) (hw, as)) {
276 goto err0;
279 if (audio_bug (AUDIO_FUNC, hw->samples <= 0)) {
280 dolog ("hw->samples=%d\n", hw->samples);
281 goto err1;
284 #ifdef DAC
285 hw->clip = mixeng_clip
286 #else
287 hw->conv = mixeng_conv
288 #endif
289 [hw->info.nchannels == 2]
290 [hw->info.sign]
291 [hw->info.swap_endianness]
292 [audio_bits_to_index (hw->info.bits)];
294 if (glue (audio_pcm_hw_alloc_resources_, TYPE) (hw)) {
295 goto err1;
298 LIST_INSERT_HEAD (&s->glue (hw_head_, TYPE), hw, entries);
299 glue (s->nb_hw_voices_, TYPE) -= 1;
300 #ifdef DAC
301 audio_attach_capture (s, hw);
302 #endif
303 return hw;
305 err1:
306 glue (hw->pcm_ops->fini_, TYPE) (hw);
307 err0:
308 qemu_free (hw);
309 return NULL;
312 static HW *glue (audio_pcm_hw_add_, TYPE) (AudioState *s,
313 struct audsettings *as)
315 HW *hw;
317 if (glue (conf.fixed_, TYPE).enabled && glue (conf.fixed_, TYPE).greedy) {
318 hw = glue (audio_pcm_hw_add_new_, TYPE) (s, as);
319 if (hw) {
320 return hw;
324 hw = glue (audio_pcm_hw_find_specific_, TYPE) (s, NULL, as);
325 if (hw) {
326 return hw;
329 hw = glue (audio_pcm_hw_add_new_, TYPE) (s, as);
330 if (hw) {
331 return hw;
334 return glue (audio_pcm_hw_find_any_, TYPE) (s, NULL);
337 static SW *glue (audio_pcm_create_voice_pair_, TYPE) (
338 AudioState *s,
339 const char *sw_name,
340 struct audsettings *as
343 SW *sw;
344 HW *hw;
345 struct audsettings hw_as;
347 if (glue (conf.fixed_, TYPE).enabled) {
348 hw_as = glue (conf.fixed_, TYPE).settings;
350 else {
351 hw_as = *as;
354 sw = audio_calloc (AUDIO_FUNC, 1, sizeof (*sw));
355 if (!sw) {
356 dolog ("Could not allocate soft voice `%s' (%zu bytes)\n",
357 sw_name ? sw_name : "unknown", sizeof (*sw));
358 goto err1;
361 hw = glue (audio_pcm_hw_add_, TYPE) (s, &hw_as);
362 if (!hw) {
363 goto err2;
366 glue (audio_pcm_hw_add_sw_, TYPE) (hw, sw);
368 if (glue (audio_pcm_sw_init_, TYPE) (sw, hw, sw_name, as)) {
369 goto err3;
372 return sw;
374 err3:
375 glue (audio_pcm_hw_del_sw_, TYPE) (sw);
376 glue (audio_pcm_hw_gc_, TYPE) (s, &hw);
377 err2:
378 qemu_free (sw);
379 err1:
380 return NULL;
383 static void glue (audio_close_, TYPE) (AudioState *s, SW *sw)
385 glue (audio_pcm_sw_fini_, TYPE) (sw);
386 glue (audio_pcm_hw_del_sw_, TYPE) (sw);
387 glue (audio_pcm_hw_gc_, TYPE) (s, &sw->hw);
388 qemu_free (sw);
391 void glue (AUD_close_, TYPE) (QEMUSoundCard *card, SW *sw)
393 if (sw) {
394 if (audio_bug (AUDIO_FUNC, !card || !card->audio)) {
395 dolog ("card=%p card->audio=%p\n",
396 card, card ? card->audio : NULL);
397 return;
400 glue (audio_close_, TYPE) (card->audio, sw);
404 SW *glue (AUD_open_, TYPE) (
405 QEMUSoundCard *card,
406 SW *sw,
407 const char *name,
408 void *callback_opaque ,
409 audio_callback_fn_t callback_fn,
410 struct audsettings *as
413 AudioState *s;
414 #ifdef DAC
415 int live = 0;
416 SW *old_sw = NULL;
417 #endif
419 ldebug ("open %s, freq %d, nchannels %d, fmt %d\n",
420 name, as->freq, as->nchannels, as->fmt);
422 if (audio_bug (AUDIO_FUNC,
423 !card || !card->audio || !name || !callback_fn || !as)) {
424 dolog ("card=%p card->audio=%p name=%p callback_fn=%p as=%p\n",
425 card, card ? card->audio : NULL, name, callback_fn, as);
426 goto fail;
429 s = card->audio;
431 if (audio_bug (AUDIO_FUNC, audio_validate_settings (as))) {
432 audio_print_settings (as);
433 goto fail;
436 if (audio_bug (AUDIO_FUNC, !s->drv)) {
437 dolog ("Can not open `%s' (no host audio driver)\n", name);
438 goto fail;
441 if (sw && audio_pcm_info_eq (&sw->info, as)) {
442 return sw;
445 #ifdef DAC
446 if (conf.plive && sw && (!sw->active && !sw->empty)) {
447 live = sw->total_hw_samples_mixed;
449 #ifdef DEBUG_PLIVE
450 dolog ("Replacing voice %s with %d live samples\n", SW_NAME (sw), live);
451 dolog ("Old %s freq %d, bits %d, channels %d\n",
452 SW_NAME (sw), sw->info.freq, sw->info.bits, sw->info.nchannels);
453 dolog ("New %s freq %d, bits %d, channels %d\n",
454 name,
455 freq,
456 (fmt == AUD_FMT_S16 || fmt == AUD_FMT_U16) ? 16 : 8,
457 nchannels);
458 #endif
460 if (live) {
461 old_sw = sw;
462 old_sw->callback.fn = NULL;
463 sw = NULL;
466 #endif
468 if (!glue (conf.fixed_, TYPE).enabled && sw) {
469 glue (AUD_close_, TYPE) (card, sw);
470 sw = NULL;
473 if (sw) {
474 HW *hw = sw->hw;
476 if (!hw) {
477 dolog ("Internal logic error voice `%s' has no hardware store\n",
478 SW_NAME (sw));
479 goto fail;
482 glue (audio_pcm_sw_fini_, TYPE) (sw);
483 if (glue (audio_pcm_sw_init_, TYPE) (sw, hw, name, as)) {
484 goto fail;
487 else {
488 sw = glue (audio_pcm_create_voice_pair_, TYPE) (s, name, as);
489 if (!sw) {
490 dolog ("Failed to create voice `%s'\n", name);
491 return NULL;
495 if (sw) {
496 sw->vol = nominal_volume;
497 sw->callback.fn = callback_fn;
498 sw->callback.opaque = callback_opaque;
500 #ifdef DAC
501 if (live) {
502 int mixed =
503 (live << old_sw->info.shift)
504 * old_sw->info.bytes_per_second
505 / sw->info.bytes_per_second;
507 #ifdef DEBUG_PLIVE
508 dolog ("Silence will be mixed %d\n", mixed);
509 #endif
510 sw->total_hw_samples_mixed += mixed;
512 #endif
514 #ifdef DEBUG_AUDIO
515 dolog ("%s\n", name);
516 audio_pcm_print_info ("hw", &sw->hw->info);
517 audio_pcm_print_info ("sw", &sw->info);
518 #endif
521 return sw;
523 fail:
524 glue (AUD_close_, TYPE) (card, sw);
525 return NULL;
528 int glue (AUD_is_active_, TYPE) (SW *sw)
530 return sw ? sw->active : 0;
533 void glue (AUD_init_time_stamp_, TYPE) (SW *sw, QEMUAudioTimeStamp *ts)
535 if (!sw) {
536 return;
539 ts->old_ts = sw->hw->ts_helper;
542 uint64_t glue (AUD_get_elapsed_usec_, TYPE) (SW *sw, QEMUAudioTimeStamp *ts)
544 uint64_t delta, cur_ts, old_ts;
546 if (!sw) {
547 return 0;
550 cur_ts = sw->hw->ts_helper;
551 old_ts = ts->old_ts;
552 /* dolog ("cur %lld old %lld\n", cur_ts, old_ts); */
554 if (cur_ts >= old_ts) {
555 delta = cur_ts - old_ts;
557 else {
558 delta = UINT64_MAX - old_ts + cur_ts;
561 if (!delta) {
562 return 0;
565 return (delta * sw->hw->info.freq) / 1000000;
568 #undef TYPE
569 #undef HW
570 #undef SW
571 #undef HWBUF
572 #undef NAME