VM state change support (malc)
[qemu] / audio / audio_template.h
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
26 #define TYPE out
27 #define HW glue (HWVoice, Out)
28 #define SW glue (SWVoice, Out)
29 #else
30 #define TYPE in
31 #define HW glue (HWVoice, In)
32 #define SW glue (SWVoice, In)
33 #endif
34
35 static int glue (audio_pcm_hw_init_, TYPE) (
36     HW *hw,
37     audsettings_t *as
38     )
39 {
40     glue (audio_pcm_hw_free_resources_, TYPE) (hw);
41
42     if (glue (hw->pcm_ops->init_, TYPE) (hw, as)) {
43         return -1;
44     }
45
46     if (audio_bug (AUDIO_FUNC, hw->samples <= 0)) {
47         dolog ("hw->samples=%d\n", hw->samples);
48         return -1;
49     }
50
51     LIST_INIT (&hw->sw_head);
52 #ifdef DAC
53     hw->clip =
54         mixeng_clip
55 #else
56     hw->conv =
57         mixeng_conv
58 #endif
59         [hw->info.nchannels == 2]
60         [hw->info.sign]
61         [hw->info.swap_endian]
62         [hw->info.bits == 16];
63
64     if (glue (audio_pcm_hw_alloc_resources_, TYPE) (hw)) {
65         glue (hw->pcm_ops->fini_, TYPE) (hw);
66         return -1;
67     }
68
69     return 0;
70 }
71
72 static void glue (audio_pcm_sw_fini_, TYPE) (SW *sw)
73 {
74     glue (audio_pcm_sw_free_resources_, TYPE) (sw);
75     if (sw->name) {
76         qemu_free (sw->name);
77         sw->name = NULL;
78     }
79 }
80
81 static void glue (audio_pcm_hw_add_sw_, TYPE) (HW *hw, SW *sw)
82 {
83     LIST_INSERT_HEAD (&hw->sw_head, sw, entries);
84 }
85
86 static void glue (audio_pcm_hw_del_sw_, TYPE) (SW *sw)
87 {
88     LIST_REMOVE (sw, entries);
89 }
90
91 static void glue (audio_pcm_hw_gc_, TYPE) (AudioState *s, HW **hwp)
92 {
93     HW *hw = *hwp;
94
95     if (!hw->sw_head.lh_first) {
96         LIST_REMOVE (hw, entries);
97         glue (s->nb_hw_voices_, TYPE) += 1;
98         glue (audio_pcm_hw_free_resources_ ,TYPE) (hw);
99         glue (hw->pcm_ops->fini_, TYPE) (hw);
100         qemu_free (hw);
101         *hwp = NULL;
102     }
103 }
104
105 static HW *glue (audio_pcm_hw_find_any_, TYPE) (AudioState *s, HW *hw)
106 {
107     return hw ? hw->entries.le_next : s->glue (hw_head_, TYPE).lh_first;
108 }
109
110 static HW *glue (audio_pcm_hw_find_any_enabled_, TYPE) (AudioState *s, HW *hw)
111 {
112     while ((hw = glue (audio_pcm_hw_find_any_, TYPE) (s, hw))) {
113         if (hw->enabled) {
114             return hw;
115         }
116     }
117     return NULL;
118 }
119
120 static HW *glue (audio_pcm_hw_find_any_passive_, TYPE) (AudioState *s)
121 {
122     if (glue (s->nb_hw_voices_, TYPE)) {
123         struct audio_driver *drv = s->drv;
124
125         if (audio_bug (AUDIO_FUNC, !drv)) {
126             dolog ("No host audio driver\n");
127             return NULL;
128         }
129
130         HW *hw = audio_calloc (AUDIO_FUNC, 1, glue (drv->voice_size_, TYPE));
131         if (!hw) {
132             dolog ("Can not allocate voice `%s' size %d\n",
133                    drv->name, glue (drv->voice_size_, TYPE));
134             return NULL;
135         }
136
137         LIST_INSERT_HEAD (&s->glue (hw_head_, TYPE), hw, entries);
138         glue (s->nb_hw_voices_, TYPE) -= 1;
139         return hw;
140     }
141
142     return NULL;
143 }
144
145 static HW *glue (audio_pcm_hw_find_specific_, TYPE) (
146     AudioState *s,
147     HW *hw,
148     audsettings_t *as
149     )
150 {
151     while ((hw = glue (audio_pcm_hw_find_any_, TYPE) (s, hw))) {
152         if (audio_pcm_info_eq (&hw->info, as)) {
153             return hw;
154         }
155     }
156     return NULL;
157 }
158
159 static HW *glue (audio_pcm_hw_add_new_, TYPE) (AudioState *s, audsettings_t *as)
160 {
161     HW *hw;
162
163     hw = glue (audio_pcm_hw_find_any_passive_, TYPE) (s);
164     if (hw) {
165         hw->pcm_ops = s->drv->pcm_ops;
166         if (!hw->pcm_ops) {
167             return NULL;
168         }
169
170         if (glue (audio_pcm_hw_init_, TYPE) (hw, as)) {
171             glue (audio_pcm_hw_gc_, TYPE) (s, &hw);
172             return NULL;
173         }
174         else {
175             return hw;
176         }
177     }
178
179     return NULL;
180 }
181
182 static HW *glue (audio_pcm_hw_add_, TYPE) (AudioState *s, audsettings_t *as)
183 {
184     HW *hw;
185
186     if (glue (conf.fixed_, TYPE).enabled && glue (conf.fixed_, TYPE).greedy) {
187         hw = glue (audio_pcm_hw_add_new_, TYPE) (s, as);
188         if (hw) {
189             return hw;
190         }
191     }
192
193     hw = glue (audio_pcm_hw_find_specific_, TYPE) (s, NULL, as);
194     if (hw) {
195         return hw;
196     }
197
198     hw = glue (audio_pcm_hw_add_new_, TYPE) (s, as);
199     if (hw) {
200         return hw;
201     }
202
203     return glue (audio_pcm_hw_find_any_, TYPE) (s, NULL);
204 }
205
206 static SW *glue (audio_pcm_create_voice_pair_, TYPE) (
207     AudioState *s,
208     const char *sw_name,
209     audsettings_t *as
210     )
211 {
212     SW *sw;
213     HW *hw;
214     audsettings_t hw_as;
215
216     if (glue (conf.fixed_, TYPE).enabled) {
217         hw_as = glue (conf.fixed_, TYPE).settings;
218     }
219     else {
220         hw_as = *as;
221     }
222
223     sw = audio_calloc (AUDIO_FUNC, 1, sizeof (*sw));
224     if (!sw) {
225         dolog ("Could not allocate soft voice `%s' (%zu bytes)\n",
226                sw_name ? sw_name : "unknown", sizeof (*sw));
227         goto err1;
228     }
229
230     hw = glue (audio_pcm_hw_add_, TYPE) (s, &hw_as);
231     if (!hw) {
232         goto err2;
233     }
234
235     glue (audio_pcm_hw_add_sw_, TYPE) (hw, sw);
236
237     if (glue (audio_pcm_sw_init_, TYPE) (sw, hw, sw_name, as)) {
238         goto err3;
239     }
240
241     return sw;
242
243 err3:
244     glue (audio_pcm_hw_del_sw_, TYPE) (sw);
245     glue (audio_pcm_hw_gc_, TYPE) (s, &hw);
246 err2:
247     qemu_free (sw);
248 err1:
249     return NULL;
250 }
251
252 static void glue (audio_close_, TYPE) (AudioState *s, SW *sw)
253 {
254     glue (audio_pcm_sw_fini_, TYPE) (sw);
255     glue (audio_pcm_hw_del_sw_, TYPE) (sw);
256     glue (audio_pcm_hw_gc_, TYPE) (s, &sw->hw);
257     qemu_free (sw);
258 }
259 void glue (AUD_close_, TYPE) (QEMUSoundCard *card, SW *sw)
260 {
261     if (sw) {
262         if (audio_bug (AUDIO_FUNC, !card || !card->audio)) {
263             dolog ("card=%p card->audio=%p\n",
264                    card, card ? card->audio : NULL);
265             return;
266         }
267
268         glue (audio_close_, TYPE) (card->audio, sw);
269     }
270 }
271
272 SW *glue (AUD_open_, TYPE) (
273     QEMUSoundCard *card,
274     SW *sw,
275     const char *name,
276     void *callback_opaque ,
277     audio_callback_fn_t callback_fn,
278     audsettings_t *as
279     )
280 {
281     AudioState *s;
282 #ifdef DAC
283     int live = 0;
284     SW *old_sw = NULL;
285 #endif
286
287     ldebug ("open %s, freq %d, nchannels %d, fmt %d\n",
288             name, as->freq, as->nchannels, as->fmt);
289
290     if (audio_bug (AUDIO_FUNC,
291                    !card || !card->audio || !name || !callback_fn || !as)) {
292         dolog ("card=%p card->audio=%p name=%p callback_fn=%p as=%p\n",
293                card, card ? card->audio : NULL, name, callback_fn, as);
294         goto fail;
295     }
296
297     s = card->audio;
298
299     if (audio_bug (AUDIO_FUNC, audio_validate_settigs (as))) {
300         audio_print_settings (as);
301         goto fail;
302     }
303
304     if (audio_bug (AUDIO_FUNC, !s->drv)) {
305         dolog ("Can not open `%s' (no host audio driver)\n", name);
306         goto fail;
307     }
308
309     if (sw && audio_pcm_info_eq (&sw->info, as)) {
310         return sw;
311     }
312
313 #ifdef DAC
314     if (conf.plive && sw && (!sw->active && !sw->empty)) {
315         live = sw->total_hw_samples_mixed;
316
317 #ifdef DEBUG_PLIVE
318         dolog ("Replacing voice %s with %d live samples\n", SW_NAME (sw), live);
319         dolog ("Old %s freq %d, bits %d, channels %d\n",
320                SW_NAME (sw), sw->info.freq, sw->info.bits, sw->info.nchannels);
321         dolog ("New %s freq %d, bits %d, channels %d\n",
322                name,
323                freq,
324                (fmt == AUD_FMT_S16 || fmt == AUD_FMT_U16) ? 16 : 8,
325                nchannels);
326 #endif
327
328         if (live) {
329             old_sw = sw;
330             old_sw->callback.fn = NULL;
331             sw = NULL;
332         }
333     }
334 #endif
335
336     if (!glue (conf.fixed_, TYPE).enabled && sw) {
337         glue (AUD_close_, TYPE) (card, sw);
338         sw = NULL;
339     }
340
341     if (sw) {
342         HW *hw = sw->hw;
343
344         if (!hw) {
345             dolog ("Internal logic error voice `%s' has no hardware store\n",
346                    SW_NAME (sw));
347             goto fail;
348         }
349
350         if (glue (audio_pcm_sw_init_, TYPE) (sw, hw, name, as)) {
351             goto fail;
352         }
353     }
354     else {
355         sw = glue (audio_pcm_create_voice_pair_, TYPE) (s, name, as);
356         if (!sw) {
357             dolog ("Failed to create voice `%s'\n", name);
358             goto fail;
359         }
360     }
361
362     if (sw) {
363         sw->vol = nominal_volume;
364         sw->callback.fn = callback_fn;
365         sw->callback.opaque = callback_opaque;
366
367 #ifdef DAC
368         if (live) {
369             int mixed =
370                 (live << old_sw->info.shift)
371                 * old_sw->info.bytes_per_second
372                 / sw->info.bytes_per_second;
373
374 #ifdef DEBUG_PLIVE
375             dolog ("Silence will be mixed %d\n", mixed);
376 #endif
377             sw->total_hw_samples_mixed += mixed;
378         }
379 #endif
380
381 #ifdef DEBUG_AUDIO
382         dolog ("%s\n", name);
383         audio_pcm_print_info ("hw", &sw->hw->info);
384         audio_pcm_print_info ("sw", &sw->info);
385 #endif
386     }
387
388     return sw;
389
390  fail:
391     glue (AUD_close_, TYPE) (card, sw);
392     return NULL;
393 }
394
395 int glue (AUD_is_active_, TYPE) (SW *sw)
396 {
397     return sw ? sw->active : 0;
398 }
399
400 void glue (AUD_init_time_stamp_, TYPE) (SW *sw, QEMUAudioTimeStamp *ts)
401 {
402     if (!sw) {
403         return;
404     }
405
406     ts->old_ts = sw->hw->ts_helper;
407 }
408
409 uint64_t glue (AUD_get_elapsed_usec_, TYPE) (SW *sw, QEMUAudioTimeStamp *ts)
410 {
411     uint64_t delta, cur_ts, old_ts;
412
413     if (!sw) {
414         return 0;
415     }
416
417     cur_ts = sw->hw->ts_helper;
418     old_ts = ts->old_ts;
419     /* dolog ("cur %lld old %lld\n", cur_ts, old_ts); */
420
421     if (cur_ts >= old_ts) {
422         delta = cur_ts - old_ts;
423     }
424     else {
425         delta = UINT64_MAX - old_ts + cur_ts;
426     }
427
428     if (!delta) {
429         return 0;
430     }
431
432     return (delta * sw->hw->info.freq) / 1000000;
433 }
434
435 #undef TYPE
436 #undef HW
437 #undef SW