Statistics
| Branch: | Revision:

root / audio / audio.c @ 4f690853

History | View | Annotate | Download (50.7 kB)

1 85571bc7 bellard
/*
2 85571bc7 bellard
 * QEMU Audio subsystem
3 1d14ffa9 bellard
 *
4 1d14ffa9 bellard
 * Copyright (c) 2003-2005 Vassili Karpov (malc)
5 1d14ffa9 bellard
 *
6 85571bc7 bellard
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 85571bc7 bellard
 * of this software and associated documentation files (the "Software"), to deal
8 85571bc7 bellard
 * in the Software without restriction, including without limitation the rights
9 85571bc7 bellard
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 85571bc7 bellard
 * copies of the Software, and to permit persons to whom the Software is
11 85571bc7 bellard
 * furnished to do so, subject to the following conditions:
12 85571bc7 bellard
 *
13 85571bc7 bellard
 * The above copyright notice and this permission notice shall be included in
14 85571bc7 bellard
 * all copies or substantial portions of the Software.
15 85571bc7 bellard
 *
16 85571bc7 bellard
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 85571bc7 bellard
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 85571bc7 bellard
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 85571bc7 bellard
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 85571bc7 bellard
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 85571bc7 bellard
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 85571bc7 bellard
 * THE SOFTWARE.
23 85571bc7 bellard
 */
24 87ecb68b pbrook
#include "hw/hw.h"
25 87ecb68b pbrook
#include "audio.h"
26 376253ec aliguori
#include "monitor.h"
27 87ecb68b pbrook
#include "qemu-timer.h"
28 87ecb68b pbrook
#include "sysemu.h"
29 85571bc7 bellard
30 1d14ffa9 bellard
#define AUDIO_CAP "audio"
31 1d14ffa9 bellard
#include "audio_int.h"
32 85571bc7 bellard
33 1d14ffa9 bellard
/* #define DEBUG_PLIVE */
34 1d14ffa9 bellard
/* #define DEBUG_LIVE */
35 1d14ffa9 bellard
/* #define DEBUG_OUT */
36 8ead62cf bellard
/* #define DEBUG_CAPTURE */
37 713a98f8 malc
/* #define DEBUG_POLL */
38 85571bc7 bellard
39 c0fe3827 bellard
#define SW_NAME(sw) (sw)->name ? (sw)->name : "unknown"
40 c0fe3827 bellard
41 2358a494 Juan Quintela
42 2358a494 Juan Quintela
/* Order of CONFIG_AUDIO_DRIVERS is import.
43 2358a494 Juan Quintela
   The 1st one is the one used by default, that is the reason
44 2358a494 Juan Quintela
    that we generate the list.
45 2358a494 Juan Quintela
*/
46 1d14ffa9 bellard
static struct audio_driver *drvtab[] = {
47 2358a494 Juan Quintela
    CONFIG_AUDIO_DRIVERS
48 1d14ffa9 bellard
    &no_audio_driver,
49 1d14ffa9 bellard
    &wav_audio_driver
50 1d14ffa9 bellard
};
51 85571bc7 bellard
52 c0fe3827 bellard
struct fixed_settings {
53 c0fe3827 bellard
    int enabled;
54 c0fe3827 bellard
    int nb_voices;
55 c0fe3827 bellard
    int greedy;
56 1ea879e5 malc
    struct audsettings settings;
57 c0fe3827 bellard
};
58 c0fe3827 bellard
59 c0fe3827 bellard
static struct {
60 c0fe3827 bellard
    struct fixed_settings fixed_out;
61 c0fe3827 bellard
    struct fixed_settings fixed_in;
62 c0fe3827 bellard
    union {
63 c310de86 malc
        int hertz;
64 c0fe3827 bellard
        int64_t ticks;
65 c0fe3827 bellard
    } period;
66 c0fe3827 bellard
    int plive;
67 541e0844 bellard
    int log_to_monitor;
68 713a98f8 malc
    int try_poll_in;
69 713a98f8 malc
    int try_poll_out;
70 c0fe3827 bellard
} conf = {
71 14658cd1 Juan Quintela
    .fixed_out = { /* DAC fixed settings */
72 14658cd1 Juan Quintela
        .enabled = 1,
73 14658cd1 Juan Quintela
        .nb_voices = 1,
74 14658cd1 Juan Quintela
        .greedy = 1,
75 14658cd1 Juan Quintela
        .settings = {
76 14658cd1 Juan Quintela
            .freq = 44100,
77 14658cd1 Juan Quintela
            .nchannels = 2,
78 14658cd1 Juan Quintela
            .fmt = AUD_FMT_S16,
79 14658cd1 Juan Quintela
            .endianness =  AUDIO_HOST_ENDIANNESS,
80 c0fe3827 bellard
        }
81 c0fe3827 bellard
    },
82 c0fe3827 bellard
83 14658cd1 Juan Quintela
    .fixed_in = { /* ADC fixed settings */
84 14658cd1 Juan Quintela
        .enabled = 1,
85 14658cd1 Juan Quintela
        .nb_voices = 1,
86 14658cd1 Juan Quintela
        .greedy = 1,
87 14658cd1 Juan Quintela
        .settings = {
88 14658cd1 Juan Quintela
            .freq = 44100,
89 14658cd1 Juan Quintela
            .nchannels = 2,
90 14658cd1 Juan Quintela
            .fmt = AUD_FMT_S16,
91 14658cd1 Juan Quintela
            .endianness = AUDIO_HOST_ENDIANNESS,
92 c0fe3827 bellard
        }
93 c0fe3827 bellard
    },
94 c0fe3827 bellard
95 aea86747 malc
    .period = { .hertz = 250 },
96 14658cd1 Juan Quintela
    .plive = 0,
97 14658cd1 Juan Quintela
    .log_to_monitor = 0,
98 713a98f8 malc
    .try_poll_in = 1,
99 713a98f8 malc
    .try_poll_out = 1,
100 1d14ffa9 bellard
};
101 1d14ffa9 bellard
102 c0fe3827 bellard
static AudioState glob_audio_state;
103 c0fe3827 bellard
104 1ea879e5 malc
struct mixeng_volume nominal_volume = {
105 14658cd1 Juan Quintela
    .mute = 0,
106 1d14ffa9 bellard
#ifdef FLOAT_MIXENG
107 14658cd1 Juan Quintela
    .r = 1.0,
108 14658cd1 Juan Quintela
    .l = 1.0,
109 1d14ffa9 bellard
#else
110 14658cd1 Juan Quintela
    .r = 1ULL << 32,
111 14658cd1 Juan Quintela
    .l = 1ULL << 32,
112 1d14ffa9 bellard
#endif
113 85571bc7 bellard
};
114 85571bc7 bellard
115 1d14ffa9 bellard
#ifdef AUDIO_IS_FLAWLESS_AND_NO_CHECKS_ARE_REQURIED
116 1d14ffa9 bellard
#error No its not
117 1d14ffa9 bellard
#else
118 82584e21 malc
static void audio_print_options (const char *prefix,
119 82584e21 malc
                                 struct audio_option *opt);
120 82584e21 malc
121 1d14ffa9 bellard
int audio_bug (const char *funcname, int cond)
122 85571bc7 bellard
{
123 1d14ffa9 bellard
    if (cond) {
124 1d14ffa9 bellard
        static int shown;
125 1d14ffa9 bellard
126 8ead62cf bellard
        AUD_log (NULL, "A bug was just triggered in %s\n", funcname);
127 1d14ffa9 bellard
        if (!shown) {
128 82584e21 malc
            struct audio_driver *d;
129 82584e21 malc
130 1d14ffa9 bellard
            shown = 1;
131 1d14ffa9 bellard
            AUD_log (NULL, "Save all your work and restart without audio\n");
132 155a8ad3 malc
            AUD_log (NULL, "Please send bug report to av1474@comtv.ru\n");
133 1d14ffa9 bellard
            AUD_log (NULL, "I am sorry\n");
134 82584e21 malc
            d = glob_audio_state.drv;
135 82584e21 malc
            if (d) {
136 82584e21 malc
                audio_print_options (d->name, d->options);
137 82584e21 malc
            }
138 1d14ffa9 bellard
        }
139 1d14ffa9 bellard
        AUD_log (NULL, "Context:\n");
140 1d14ffa9 bellard
141 1d14ffa9 bellard
#if defined AUDIO_BREAKPOINT_ON_BUG
142 1d14ffa9 bellard
#  if defined HOST_I386
143 1d14ffa9 bellard
#    if defined __GNUC__
144 1d14ffa9 bellard
        __asm__ ("int3");
145 1d14ffa9 bellard
#    elif defined _MSC_VER
146 1d14ffa9 bellard
        _asm _emit 0xcc;
147 1d14ffa9 bellard
#    else
148 1d14ffa9 bellard
        abort ();
149 1d14ffa9 bellard
#    endif
150 1d14ffa9 bellard
#  else
151 1d14ffa9 bellard
        abort ();
152 1d14ffa9 bellard
#  endif
153 1d14ffa9 bellard
#endif
154 85571bc7 bellard
    }
155 85571bc7 bellard
156 1d14ffa9 bellard
    return cond;
157 85571bc7 bellard
}
158 1d14ffa9 bellard
#endif
159 85571bc7 bellard
160 f941aa25 ths
static inline int audio_bits_to_index (int bits)
161 f941aa25 ths
{
162 f941aa25 ths
    switch (bits) {
163 f941aa25 ths
    case 8:
164 f941aa25 ths
        return 0;
165 f941aa25 ths
166 f941aa25 ths
    case 16:
167 f941aa25 ths
        return 1;
168 f941aa25 ths
169 f941aa25 ths
    case 32:
170 f941aa25 ths
        return 2;
171 f941aa25 ths
172 f941aa25 ths
    default:
173 f941aa25 ths
        audio_bug ("bits_to_index", 1);
174 f941aa25 ths
        AUD_log (NULL, "invalid bits %d\n", bits);
175 f941aa25 ths
        return 0;
176 f941aa25 ths
    }
177 f941aa25 ths
}
178 f941aa25 ths
179 c0fe3827 bellard
void *audio_calloc (const char *funcname, int nmemb, size_t size)
180 c0fe3827 bellard
{
181 c0fe3827 bellard
    int cond;
182 c0fe3827 bellard
    size_t len;
183 c0fe3827 bellard
184 c0fe3827 bellard
    len = nmemb * size;
185 c0fe3827 bellard
    cond = !nmemb || !size;
186 c0fe3827 bellard
    cond |= nmemb < 0;
187 c0fe3827 bellard
    cond |= len < size;
188 c0fe3827 bellard
189 c0fe3827 bellard
    if (audio_bug ("audio_calloc", cond)) {
190 c0fe3827 bellard
        AUD_log (NULL, "%s passed invalid arguments to audio_calloc\n",
191 c0fe3827 bellard
                 funcname);
192 541e0844 bellard
        AUD_log (NULL, "nmemb=%d size=%zu (len=%zu)\n", nmemb, size, len);
193 c0fe3827 bellard
        return NULL;
194 c0fe3827 bellard
    }
195 c0fe3827 bellard
196 c0fe3827 bellard
    return qemu_mallocz (len);
197 c0fe3827 bellard
}
198 c0fe3827 bellard
199 1d14ffa9 bellard
static char *audio_alloc_prefix (const char *s)
200 85571bc7 bellard
{
201 1d14ffa9 bellard
    const char qemu_prefix[] = "QEMU_";
202 090f1fa3 aliguori
    size_t len, i;
203 090f1fa3 aliguori
    char *r, *u;
204 85571bc7 bellard
205 1d14ffa9 bellard
    if (!s) {
206 1d14ffa9 bellard
        return NULL;
207 1d14ffa9 bellard
    }
208 85571bc7 bellard
209 1d14ffa9 bellard
    len = strlen (s);
210 a3772d4d blueswir1
    r = qemu_malloc (len + sizeof (qemu_prefix));
211 85571bc7 bellard
212 090f1fa3 aliguori
    u = r + sizeof (qemu_prefix) - 1;
213 85571bc7 bellard
214 090f1fa3 aliguori
    pstrcpy (r, len + sizeof (qemu_prefix), qemu_prefix);
215 090f1fa3 aliguori
    pstrcat (r, len + sizeof (qemu_prefix), s);
216 1d14ffa9 bellard
217 090f1fa3 aliguori
    for (i = 0; i < len; ++i) {
218 090f1fa3 aliguori
        u[i] = qemu_toupper(u[i]);
219 85571bc7 bellard
    }
220 090f1fa3 aliguori
221 1d14ffa9 bellard
    return r;
222 85571bc7 bellard
}
223 85571bc7 bellard
224 9596ebb7 pbrook
static const char *audio_audfmt_to_string (audfmt_e fmt)
225 85571bc7 bellard
{
226 1d14ffa9 bellard
    switch (fmt) {
227 1d14ffa9 bellard
    case AUD_FMT_U8:
228 1d14ffa9 bellard
        return "U8";
229 85571bc7 bellard
230 1d14ffa9 bellard
    case AUD_FMT_U16:
231 1d14ffa9 bellard
        return "U16";
232 85571bc7 bellard
233 85571bc7 bellard
    case AUD_FMT_S8:
234 1d14ffa9 bellard
        return "S8";
235 85571bc7 bellard
236 85571bc7 bellard
    case AUD_FMT_S16:
237 1d14ffa9 bellard
        return "S16";
238 f941aa25 ths
239 f941aa25 ths
    case AUD_FMT_U32:
240 f941aa25 ths
        return "U32";
241 f941aa25 ths
242 f941aa25 ths
    case AUD_FMT_S32:
243 f941aa25 ths
        return "S32";
244 85571bc7 bellard
    }
245 85571bc7 bellard
246 1d14ffa9 bellard
    dolog ("Bogus audfmt %d returning S16\n", fmt);
247 1d14ffa9 bellard
    return "S16";
248 85571bc7 bellard
}
249 85571bc7 bellard
250 9596ebb7 pbrook
static audfmt_e audio_string_to_audfmt (const char *s, audfmt_e defval,
251 9596ebb7 pbrook
                                        int *defaultp)
252 85571bc7 bellard
{
253 1d14ffa9 bellard
    if (!strcasecmp (s, "u8")) {
254 1d14ffa9 bellard
        *defaultp = 0;
255 1d14ffa9 bellard
        return AUD_FMT_U8;
256 1d14ffa9 bellard
    }
257 1d14ffa9 bellard
    else if (!strcasecmp (s, "u16")) {
258 1d14ffa9 bellard
        *defaultp = 0;
259 1d14ffa9 bellard
        return AUD_FMT_U16;
260 1d14ffa9 bellard
    }
261 f941aa25 ths
    else if (!strcasecmp (s, "u32")) {
262 f941aa25 ths
        *defaultp = 0;
263 f941aa25 ths
        return AUD_FMT_U32;
264 f941aa25 ths
    }
265 1d14ffa9 bellard
    else if (!strcasecmp (s, "s8")) {
266 1d14ffa9 bellard
        *defaultp = 0;
267 1d14ffa9 bellard
        return AUD_FMT_S8;
268 1d14ffa9 bellard
    }
269 1d14ffa9 bellard
    else if (!strcasecmp (s, "s16")) {
270 1d14ffa9 bellard
        *defaultp = 0;
271 1d14ffa9 bellard
        return AUD_FMT_S16;
272 1d14ffa9 bellard
    }
273 f941aa25 ths
    else if (!strcasecmp (s, "s32")) {
274 f941aa25 ths
        *defaultp = 0;
275 f941aa25 ths
        return AUD_FMT_S32;
276 f941aa25 ths
    }
277 1d14ffa9 bellard
    else {
278 1d14ffa9 bellard
        dolog ("Bogus audio format `%s' using %s\n",
279 1d14ffa9 bellard
               s, audio_audfmt_to_string (defval));
280 1d14ffa9 bellard
        *defaultp = 1;
281 1d14ffa9 bellard
        return defval;
282 1d14ffa9 bellard
    }
283 85571bc7 bellard
}
284 85571bc7 bellard
285 1d14ffa9 bellard
static audfmt_e audio_get_conf_fmt (const char *envname,
286 1d14ffa9 bellard
                                    audfmt_e defval,
287 1d14ffa9 bellard
                                    int *defaultp)
288 85571bc7 bellard
{
289 1d14ffa9 bellard
    const char *var = getenv (envname);
290 1d14ffa9 bellard
    if (!var) {
291 1d14ffa9 bellard
        *defaultp = 1;
292 1d14ffa9 bellard
        return defval;
293 85571bc7 bellard
    }
294 1d14ffa9 bellard
    return audio_string_to_audfmt (var, defval, defaultp);
295 85571bc7 bellard
}
296 85571bc7 bellard
297 1d14ffa9 bellard
static int audio_get_conf_int (const char *key, int defval, int *defaultp)
298 85571bc7 bellard
{
299 1d14ffa9 bellard
    int val;
300 1d14ffa9 bellard
    char *strval;
301 85571bc7 bellard
302 1d14ffa9 bellard
    strval = getenv (key);
303 1d14ffa9 bellard
    if (strval) {
304 1d14ffa9 bellard
        *defaultp = 0;
305 1d14ffa9 bellard
        val = atoi (strval);
306 1d14ffa9 bellard
        return val;
307 1d14ffa9 bellard
    }
308 1d14ffa9 bellard
    else {
309 1d14ffa9 bellard
        *defaultp = 1;
310 1d14ffa9 bellard
        return defval;
311 1d14ffa9 bellard
    }
312 85571bc7 bellard
}
313 85571bc7 bellard
314 1d14ffa9 bellard
static const char *audio_get_conf_str (const char *key,
315 1d14ffa9 bellard
                                       const char *defval,
316 1d14ffa9 bellard
                                       int *defaultp)
317 85571bc7 bellard
{
318 1d14ffa9 bellard
    const char *val = getenv (key);
319 1d14ffa9 bellard
    if (!val) {
320 1d14ffa9 bellard
        *defaultp = 1;
321 1d14ffa9 bellard
        return defval;
322 1d14ffa9 bellard
    }
323 1d14ffa9 bellard
    else {
324 1d14ffa9 bellard
        *defaultp = 0;
325 1d14ffa9 bellard
        return val;
326 85571bc7 bellard
    }
327 85571bc7 bellard
}
328 85571bc7 bellard
329 541e0844 bellard
void AUD_vlog (const char *cap, const char *fmt, va_list ap)
330 85571bc7 bellard
{
331 541e0844 bellard
    if (conf.log_to_monitor) {
332 541e0844 bellard
        if (cap) {
333 376253ec aliguori
            monitor_printf(cur_mon, "%s: ", cap);
334 541e0844 bellard
        }
335 541e0844 bellard
336 376253ec aliguori
        monitor_vprintf(cur_mon, fmt, ap);
337 541e0844 bellard
    }
338 541e0844 bellard
    else {
339 541e0844 bellard
        if (cap) {
340 541e0844 bellard
            fprintf (stderr, "%s: ", cap);
341 541e0844 bellard
        }
342 541e0844 bellard
343 541e0844 bellard
        vfprintf (stderr, fmt, ap);
344 85571bc7 bellard
    }
345 85571bc7 bellard
}
346 85571bc7 bellard
347 541e0844 bellard
void AUD_log (const char *cap, const char *fmt, ...)
348 85571bc7 bellard
{
349 541e0844 bellard
    va_list ap;
350 541e0844 bellard
351 541e0844 bellard
    va_start (ap, fmt);
352 541e0844 bellard
    AUD_vlog (cap, fmt, ap);
353 541e0844 bellard
    va_end (ap);
354 85571bc7 bellard
}
355 85571bc7 bellard
356 1d14ffa9 bellard
static void audio_print_options (const char *prefix,
357 1d14ffa9 bellard
                                 struct audio_option *opt)
358 85571bc7 bellard
{
359 1d14ffa9 bellard
    char *uprefix;
360 1d14ffa9 bellard
361 1d14ffa9 bellard
    if (!prefix) {
362 1d14ffa9 bellard
        dolog ("No prefix specified\n");
363 1d14ffa9 bellard
        return;
364 1d14ffa9 bellard
    }
365 1d14ffa9 bellard
366 1d14ffa9 bellard
    if (!opt) {
367 1d14ffa9 bellard
        dolog ("No options\n");
368 85571bc7 bellard
        return;
369 1d14ffa9 bellard
    }
370 85571bc7 bellard
371 1d14ffa9 bellard
    uprefix = audio_alloc_prefix (prefix);
372 85571bc7 bellard
373 1d14ffa9 bellard
    for (; opt->name; opt++) {
374 1d14ffa9 bellard
        const char *state = "default";
375 1d14ffa9 bellard
        printf ("  %s_%s: ", uprefix, opt->name);
376 85571bc7 bellard
377 fe8f096b ths
        if (opt->overriddenp && *opt->overriddenp) {
378 1d14ffa9 bellard
            state = "current";
379 1d14ffa9 bellard
        }
380 85571bc7 bellard
381 1d14ffa9 bellard
        switch (opt->tag) {
382 1d14ffa9 bellard
        case AUD_OPT_BOOL:
383 1d14ffa9 bellard
            {
384 1d14ffa9 bellard
                int *intp = opt->valp;
385 1d14ffa9 bellard
                printf ("boolean, %s = %d\n", state, *intp ? 1 : 0);
386 1d14ffa9 bellard
            }
387 1d14ffa9 bellard
            break;
388 1d14ffa9 bellard
389 1d14ffa9 bellard
        case AUD_OPT_INT:
390 1d14ffa9 bellard
            {
391 1d14ffa9 bellard
                int *intp = opt->valp;
392 1d14ffa9 bellard
                printf ("integer, %s = %d\n", state, *intp);
393 1d14ffa9 bellard
            }
394 1d14ffa9 bellard
            break;
395 1d14ffa9 bellard
396 1d14ffa9 bellard
        case AUD_OPT_FMT:
397 1d14ffa9 bellard
            {
398 1d14ffa9 bellard
                audfmt_e *fmtp = opt->valp;
399 1d14ffa9 bellard
                printf (
400 ca9cc28c balrog
                    "format, %s = %s, (one of: U8 S8 U16 S16 U32 S32)\n",
401 1d14ffa9 bellard
                    state,
402 1d14ffa9 bellard
                    audio_audfmt_to_string (*fmtp)
403 1d14ffa9 bellard
                    );
404 1d14ffa9 bellard
            }
405 1d14ffa9 bellard
            break;
406 1d14ffa9 bellard
407 1d14ffa9 bellard
        case AUD_OPT_STR:
408 1d14ffa9 bellard
            {
409 1d14ffa9 bellard
                const char **strp = opt->valp;
410 1d14ffa9 bellard
                printf ("string, %s = %s\n",
411 1d14ffa9 bellard
                        state,
412 1d14ffa9 bellard
                        *strp ? *strp : "(not set)");
413 85571bc7 bellard
            }
414 1d14ffa9 bellard
            break;
415 1d14ffa9 bellard
416 1d14ffa9 bellard
        default:
417 1d14ffa9 bellard
            printf ("???\n");
418 1d14ffa9 bellard
            dolog ("Bad value tag for option %s_%s %d\n",
419 1d14ffa9 bellard
                   uprefix, opt->name, opt->tag);
420 1d14ffa9 bellard
            break;
421 85571bc7 bellard
        }
422 1d14ffa9 bellard
        printf ("    %s\n", opt->descr);
423 85571bc7 bellard
    }
424 1d14ffa9 bellard
425 1d14ffa9 bellard
    qemu_free (uprefix);
426 85571bc7 bellard
}
427 85571bc7 bellard
428 1d14ffa9 bellard
static void audio_process_options (const char *prefix,
429 1d14ffa9 bellard
                                   struct audio_option *opt)
430 85571bc7 bellard
{
431 1d14ffa9 bellard
    char *optname;
432 1d14ffa9 bellard
    const char qemu_prefix[] = "QEMU_";
433 363a37d5 blueswir1
    size_t preflen, optlen;
434 85571bc7 bellard
435 1d14ffa9 bellard
    if (audio_bug (AUDIO_FUNC, !prefix)) {
436 1d14ffa9 bellard
        dolog ("prefix = NULL\n");
437 1d14ffa9 bellard
        return;
438 1d14ffa9 bellard
    }
439 85571bc7 bellard
440 1d14ffa9 bellard
    if (audio_bug (AUDIO_FUNC, !opt)) {
441 1d14ffa9 bellard
        dolog ("opt = NULL\n");
442 1d14ffa9 bellard
        return;
443 85571bc7 bellard
    }
444 85571bc7 bellard
445 1d14ffa9 bellard
    preflen = strlen (prefix);
446 85571bc7 bellard
447 1d14ffa9 bellard
    for (; opt->name; opt++) {
448 1d14ffa9 bellard
        size_t len, i;
449 1d14ffa9 bellard
        int def;
450 1d14ffa9 bellard
451 1d14ffa9 bellard
        if (!opt->valp) {
452 1d14ffa9 bellard
            dolog ("Option value pointer for `%s' is not set\n",
453 1d14ffa9 bellard
                   opt->name);
454 1d14ffa9 bellard
            continue;
455 1d14ffa9 bellard
        }
456 1d14ffa9 bellard
457 1d14ffa9 bellard
        len = strlen (opt->name);
458 c0fe3827 bellard
        /* len of opt->name + len of prefix + size of qemu_prefix
459 c0fe3827 bellard
         * (includes trailing zero) + zero + underscore (on behalf of
460 c0fe3827 bellard
         * sizeof) */
461 363a37d5 blueswir1
        optlen = len + preflen + sizeof (qemu_prefix) + 1;
462 363a37d5 blueswir1
        optname = qemu_malloc (optlen);
463 1d14ffa9 bellard
464 363a37d5 blueswir1
        pstrcpy (optname, optlen, qemu_prefix);
465 c0fe3827 bellard
466 c0fe3827 bellard
        /* copy while upper-casing, including trailing zero */
467 1d14ffa9 bellard
        for (i = 0; i <= preflen; ++i) {
468 cd390083 blueswir1
            optname[i + sizeof (qemu_prefix) - 1] = qemu_toupper(prefix[i]);
469 1d14ffa9 bellard
        }
470 363a37d5 blueswir1
        pstrcat (optname, optlen, "_");
471 363a37d5 blueswir1
        pstrcat (optname, optlen, opt->name);
472 1d14ffa9 bellard
473 1d14ffa9 bellard
        def = 1;
474 1d14ffa9 bellard
        switch (opt->tag) {
475 1d14ffa9 bellard
        case AUD_OPT_BOOL:
476 1d14ffa9 bellard
        case AUD_OPT_INT:
477 1d14ffa9 bellard
            {
478 1d14ffa9 bellard
                int *intp = opt->valp;
479 1d14ffa9 bellard
                *intp = audio_get_conf_int (optname, *intp, &def);
480 1d14ffa9 bellard
            }
481 1d14ffa9 bellard
            break;
482 1d14ffa9 bellard
483 1d14ffa9 bellard
        case AUD_OPT_FMT:
484 1d14ffa9 bellard
            {
485 1d14ffa9 bellard
                audfmt_e *fmtp = opt->valp;
486 1d14ffa9 bellard
                *fmtp = audio_get_conf_fmt (optname, *fmtp, &def);
487 1d14ffa9 bellard
            }
488 1d14ffa9 bellard
            break;
489 1d14ffa9 bellard
490 1d14ffa9 bellard
        case AUD_OPT_STR:
491 1d14ffa9 bellard
            {
492 1d14ffa9 bellard
                const char **strp = opt->valp;
493 1d14ffa9 bellard
                *strp = audio_get_conf_str (optname, *strp, &def);
494 1d14ffa9 bellard
            }
495 1d14ffa9 bellard
            break;
496 1d14ffa9 bellard
497 1d14ffa9 bellard
        default:
498 1d14ffa9 bellard
            dolog ("Bad value tag for option `%s' - %d\n",
499 1d14ffa9 bellard
                   optname, opt->tag);
500 85571bc7 bellard
            break;
501 85571bc7 bellard
        }
502 85571bc7 bellard
503 fe8f096b ths
        if (!opt->overriddenp) {
504 fe8f096b ths
            opt->overriddenp = &opt->overridden;
505 1d14ffa9 bellard
        }
506 fe8f096b ths
        *opt->overriddenp = !def;
507 1d14ffa9 bellard
        qemu_free (optname);
508 1d14ffa9 bellard
    }
509 85571bc7 bellard
}
510 85571bc7 bellard
511 1ea879e5 malc
static void audio_print_settings (struct audsettings *as)
512 c0fe3827 bellard
{
513 c0fe3827 bellard
    dolog ("frequency=%d nchannels=%d fmt=", as->freq, as->nchannels);
514 c0fe3827 bellard
515 c0fe3827 bellard
    switch (as->fmt) {
516 c0fe3827 bellard
    case AUD_FMT_S8:
517 c0fe3827 bellard
        AUD_log (NULL, "S8");
518 c0fe3827 bellard
        break;
519 c0fe3827 bellard
    case AUD_FMT_U8:
520 c0fe3827 bellard
        AUD_log (NULL, "U8");
521 c0fe3827 bellard
        break;
522 c0fe3827 bellard
    case AUD_FMT_S16:
523 c0fe3827 bellard
        AUD_log (NULL, "S16");
524 c0fe3827 bellard
        break;
525 c0fe3827 bellard
    case AUD_FMT_U16:
526 c0fe3827 bellard
        AUD_log (NULL, "U16");
527 c0fe3827 bellard
        break;
528 d50997f9 malc
    case AUD_FMT_S32:
529 d50997f9 malc
        AUD_log (NULL, "S32");
530 d50997f9 malc
        break;
531 d50997f9 malc
    case AUD_FMT_U32:
532 d50997f9 malc
        AUD_log (NULL, "U32");
533 d50997f9 malc
        break;
534 c0fe3827 bellard
    default:
535 c0fe3827 bellard
        AUD_log (NULL, "invalid(%d)", as->fmt);
536 c0fe3827 bellard
        break;
537 c0fe3827 bellard
    }
538 ec36b695 bellard
539 ec36b695 bellard
    AUD_log (NULL, " endianness=");
540 d929eba5 bellard
    switch (as->endianness) {
541 d929eba5 bellard
    case 0:
542 d929eba5 bellard
        AUD_log (NULL, "little");
543 d929eba5 bellard
        break;
544 d929eba5 bellard
    case 1:
545 d929eba5 bellard
        AUD_log (NULL, "big");
546 d929eba5 bellard
        break;
547 d929eba5 bellard
    default:
548 d929eba5 bellard
        AUD_log (NULL, "invalid");
549 d929eba5 bellard
        break;
550 d929eba5 bellard
    }
551 c0fe3827 bellard
    AUD_log (NULL, "\n");
552 c0fe3827 bellard
}
553 c0fe3827 bellard
554 1ea879e5 malc
static int audio_validate_settings (struct audsettings *as)
555 c0fe3827 bellard
{
556 c0fe3827 bellard
    int invalid;
557 c0fe3827 bellard
558 c0fe3827 bellard
    invalid = as->nchannels != 1 && as->nchannels != 2;
559 d929eba5 bellard
    invalid |= as->endianness != 0 && as->endianness != 1;
560 c0fe3827 bellard
561 c0fe3827 bellard
    switch (as->fmt) {
562 c0fe3827 bellard
    case AUD_FMT_S8:
563 c0fe3827 bellard
    case AUD_FMT_U8:
564 c0fe3827 bellard
    case AUD_FMT_S16:
565 c0fe3827 bellard
    case AUD_FMT_U16:
566 f941aa25 ths
    case AUD_FMT_S32:
567 f941aa25 ths
    case AUD_FMT_U32:
568 c0fe3827 bellard
        break;
569 c0fe3827 bellard
    default:
570 c0fe3827 bellard
        invalid = 1;
571 c0fe3827 bellard
        break;
572 c0fe3827 bellard
    }
573 c0fe3827 bellard
574 c0fe3827 bellard
    invalid |= as->freq <= 0;
575 d929eba5 bellard
    return invalid ? -1 : 0;
576 c0fe3827 bellard
}
577 c0fe3827 bellard
578 1ea879e5 malc
static int audio_pcm_info_eq (struct audio_pcm_info *info, struct audsettings *as)
579 85571bc7 bellard
{
580 1d14ffa9 bellard
    int bits = 8, sign = 0;
581 85571bc7 bellard
582 c0fe3827 bellard
    switch (as->fmt) {
583 1d14ffa9 bellard
    case AUD_FMT_S8:
584 1d14ffa9 bellard
        sign = 1;
585 1d14ffa9 bellard
    case AUD_FMT_U8:
586 1d14ffa9 bellard
        break;
587 1d14ffa9 bellard
588 1d14ffa9 bellard
    case AUD_FMT_S16:
589 1d14ffa9 bellard
        sign = 1;
590 1d14ffa9 bellard
    case AUD_FMT_U16:
591 1d14ffa9 bellard
        bits = 16;
592 1d14ffa9 bellard
        break;
593 f941aa25 ths
594 f941aa25 ths
    case AUD_FMT_S32:
595 f941aa25 ths
        sign = 1;
596 f941aa25 ths
    case AUD_FMT_U32:
597 f941aa25 ths
        bits = 32;
598 f941aa25 ths
        break;
599 85571bc7 bellard
    }
600 c0fe3827 bellard
    return info->freq == as->freq
601 c0fe3827 bellard
        && info->nchannels == as->nchannels
602 1d14ffa9 bellard
        && info->sign == sign
603 d929eba5 bellard
        && info->bits == bits
604 d929eba5 bellard
        && info->swap_endianness == (as->endianness != AUDIO_HOST_ENDIANNESS);
605 1d14ffa9 bellard
}
606 85571bc7 bellard
607 1ea879e5 malc
void audio_pcm_init_info (struct audio_pcm_info *info, struct audsettings *as)
608 1d14ffa9 bellard
{
609 f941aa25 ths
    int bits = 8, sign = 0, shift = 0;
610 1d14ffa9 bellard
611 c0fe3827 bellard
    switch (as->fmt) {
612 85571bc7 bellard
    case AUD_FMT_S8:
613 85571bc7 bellard
        sign = 1;
614 85571bc7 bellard
    case AUD_FMT_U8:
615 85571bc7 bellard
        break;
616 85571bc7 bellard
617 85571bc7 bellard
    case AUD_FMT_S16:
618 85571bc7 bellard
        sign = 1;
619 85571bc7 bellard
    case AUD_FMT_U16:
620 85571bc7 bellard
        bits = 16;
621 f941aa25 ths
        shift = 1;
622 f941aa25 ths
        break;
623 f941aa25 ths
624 f941aa25 ths
    case AUD_FMT_S32:
625 f941aa25 ths
        sign = 1;
626 f941aa25 ths
    case AUD_FMT_U32:
627 f941aa25 ths
        bits = 32;
628 f941aa25 ths
        shift = 2;
629 85571bc7 bellard
        break;
630 85571bc7 bellard
    }
631 85571bc7 bellard
632 c0fe3827 bellard
    info->freq = as->freq;
633 1d14ffa9 bellard
    info->bits = bits;
634 1d14ffa9 bellard
    info->sign = sign;
635 c0fe3827 bellard
    info->nchannels = as->nchannels;
636 f941aa25 ths
    info->shift = (as->nchannels == 2) + shift;
637 1d14ffa9 bellard
    info->align = (1 << info->shift) - 1;
638 1d14ffa9 bellard
    info->bytes_per_second = info->freq << info->shift;
639 d929eba5 bellard
    info->swap_endianness = (as->endianness != AUDIO_HOST_ENDIANNESS);
640 85571bc7 bellard
}
641 85571bc7 bellard
642 1d14ffa9 bellard
void audio_pcm_info_clear_buf (struct audio_pcm_info *info, void *buf, int len)
643 85571bc7 bellard
{
644 1d14ffa9 bellard
    if (!len) {
645 1d14ffa9 bellard
        return;
646 1d14ffa9 bellard
    }
647 1d14ffa9 bellard
648 1d14ffa9 bellard
    if (info->sign) {
649 e2f909be bellard
        memset (buf, 0x00, len << info->shift);
650 85571bc7 bellard
    }
651 85571bc7 bellard
    else {
652 f941aa25 ths
        switch (info->bits) {
653 f941aa25 ths
        case 8:
654 e2f909be bellard
            memset (buf, 0x80, len << info->shift);
655 f941aa25 ths
            break;
656 1d14ffa9 bellard
657 f941aa25 ths
        case 16:
658 f941aa25 ths
            {
659 f941aa25 ths
                int i;
660 f941aa25 ths
                uint16_t *p = buf;
661 f941aa25 ths
                int shift = info->nchannels - 1;
662 f941aa25 ths
                short s = INT16_MAX;
663 f941aa25 ths
664 f941aa25 ths
                if (info->swap_endianness) {
665 f941aa25 ths
                    s = bswap16 (s);
666 f941aa25 ths
                }
667 f941aa25 ths
668 f941aa25 ths
                for (i = 0; i < len << shift; i++) {
669 f941aa25 ths
                    p[i] = s;
670 f941aa25 ths
                }
671 1d14ffa9 bellard
            }
672 f941aa25 ths
            break;
673 f941aa25 ths
674 f941aa25 ths
        case 32:
675 f941aa25 ths
            {
676 f941aa25 ths
                int i;
677 f941aa25 ths
                uint32_t *p = buf;
678 f941aa25 ths
                int shift = info->nchannels - 1;
679 f941aa25 ths
                int32_t s = INT32_MAX;
680 f941aa25 ths
681 f941aa25 ths
                if (info->swap_endianness) {
682 f941aa25 ths
                    s = bswap32 (s);
683 f941aa25 ths
                }
684 1d14ffa9 bellard
685 f941aa25 ths
                for (i = 0; i < len << shift; i++) {
686 f941aa25 ths
                    p[i] = s;
687 f941aa25 ths
                }
688 1d14ffa9 bellard
            }
689 f941aa25 ths
            break;
690 f941aa25 ths
691 f941aa25 ths
        default:
692 f941aa25 ths
            AUD_log (NULL, "audio_pcm_info_clear_buf: invalid bits %d\n",
693 f941aa25 ths
                     info->bits);
694 f941aa25 ths
            break;
695 1d14ffa9 bellard
        }
696 85571bc7 bellard
    }
697 85571bc7 bellard
}
698 85571bc7 bellard
699 1d14ffa9 bellard
/*
700 8ead62cf bellard
 * Capture
701 8ead62cf bellard
 */
702 1ea879e5 malc
static void noop_conv (struct st_sample *dst, const void *src,
703 1ea879e5 malc
                       int samples, struct mixeng_volume *vol)
704 8ead62cf bellard
{
705 8ead62cf bellard
    (void) src;
706 8ead62cf bellard
    (void) dst;
707 8ead62cf bellard
    (void) samples;
708 8ead62cf bellard
    (void) vol;
709 8ead62cf bellard
}
710 8ead62cf bellard
711 8ead62cf bellard
static CaptureVoiceOut *audio_pcm_capture_find_specific (
712 1ea879e5 malc
    struct audsettings *as
713 8ead62cf bellard
    )
714 8ead62cf bellard
{
715 8ead62cf bellard
    CaptureVoiceOut *cap;
716 1a7dafce malc
    AudioState *s = &glob_audio_state;
717 8ead62cf bellard
718 8ead62cf bellard
    for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) {
719 d929eba5 bellard
        if (audio_pcm_info_eq (&cap->hw.info, as)) {
720 8ead62cf bellard
            return cap;
721 8ead62cf bellard
        }
722 8ead62cf bellard
    }
723 8ead62cf bellard
    return NULL;
724 8ead62cf bellard
}
725 8ead62cf bellard
726 ec36b695 bellard
static void audio_notify_capture (CaptureVoiceOut *cap, audcnotification_e cmd)
727 8ead62cf bellard
{
728 ec36b695 bellard
    struct capture_callback *cb;
729 ec36b695 bellard
730 ec36b695 bellard
#ifdef DEBUG_CAPTURE
731 ec36b695 bellard
    dolog ("notification %d sent\n", cmd);
732 ec36b695 bellard
#endif
733 ec36b695 bellard
    for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
734 ec36b695 bellard
        cb->ops.notify (cb->opaque, cmd);
735 ec36b695 bellard
    }
736 ec36b695 bellard
}
737 8ead62cf bellard
738 ec36b695 bellard
static void audio_capture_maybe_changed (CaptureVoiceOut *cap, int enabled)
739 ec36b695 bellard
{
740 ec36b695 bellard
    if (cap->hw.enabled != enabled) {
741 ec36b695 bellard
        audcnotification_e cmd;
742 8ead62cf bellard
        cap->hw.enabled = enabled;
743 ec36b695 bellard
        cmd = enabled ? AUD_CNOTIFY_ENABLE : AUD_CNOTIFY_DISABLE;
744 ec36b695 bellard
        audio_notify_capture (cap, cmd);
745 8ead62cf bellard
    }
746 8ead62cf bellard
}
747 8ead62cf bellard
748 8ead62cf bellard
static void audio_recalc_and_notify_capture (CaptureVoiceOut *cap)
749 8ead62cf bellard
{
750 8ead62cf bellard
    HWVoiceOut *hw = &cap->hw;
751 8ead62cf bellard
    SWVoiceOut *sw;
752 8ead62cf bellard
    int enabled = 0;
753 8ead62cf bellard
754 ec36b695 bellard
    for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
755 8ead62cf bellard
        if (sw->active) {
756 8ead62cf bellard
            enabled = 1;
757 8ead62cf bellard
            break;
758 8ead62cf bellard
        }
759 8ead62cf bellard
    }
760 ec36b695 bellard
    audio_capture_maybe_changed (cap, enabled);
761 8ead62cf bellard
}
762 8ead62cf bellard
763 8ead62cf bellard
static void audio_detach_capture (HWVoiceOut *hw)
764 8ead62cf bellard
{
765 ec36b695 bellard
    SWVoiceCap *sc = hw->cap_head.lh_first;
766 ec36b695 bellard
767 ec36b695 bellard
    while (sc) {
768 ec36b695 bellard
        SWVoiceCap *sc1 = sc->entries.le_next;
769 ec36b695 bellard
        SWVoiceOut *sw = &sc->sw;
770 ec36b695 bellard
        CaptureVoiceOut *cap = sc->cap;
771 ec36b695 bellard
        int was_active = sw->active;
772 8ead62cf bellard
773 8ead62cf bellard
        if (sw->rate) {
774 8ead62cf bellard
            st_rate_stop (sw->rate);
775 8ead62cf bellard
            sw->rate = NULL;
776 8ead62cf bellard
        }
777 8ead62cf bellard
778 72cf2d4f Blue Swirl
        QLIST_REMOVE (sw, entries);
779 72cf2d4f Blue Swirl
        QLIST_REMOVE (sc, entries);
780 ec36b695 bellard
        qemu_free (sc);
781 ec36b695 bellard
        if (was_active) {
782 ec36b695 bellard
            /* We have removed soft voice from the capture:
783 ec36b695 bellard
               this might have changed the overall status of the capture
784 ec36b695 bellard
               since this might have been the only active voice */
785 ec36b695 bellard
            audio_recalc_and_notify_capture (cap);
786 ec36b695 bellard
        }
787 ec36b695 bellard
        sc = sc1;
788 8ead62cf bellard
    }
789 8ead62cf bellard
}
790 8ead62cf bellard
791 1a7dafce malc
static int audio_attach_capture (HWVoiceOut *hw)
792 8ead62cf bellard
{
793 1a7dafce malc
    AudioState *s = &glob_audio_state;
794 8ead62cf bellard
    CaptureVoiceOut *cap;
795 8ead62cf bellard
796 8ead62cf bellard
    audio_detach_capture (hw);
797 8ead62cf bellard
    for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) {
798 ec36b695 bellard
        SWVoiceCap *sc;
799 8ead62cf bellard
        SWVoiceOut *sw;
800 ec36b695 bellard
        HWVoiceOut *hw_cap = &cap->hw;
801 8ead62cf bellard
802 ec36b695 bellard
        sc = audio_calloc (AUDIO_FUNC, 1, sizeof (*sc));
803 ec36b695 bellard
        if (!sc) {
804 8ead62cf bellard
            dolog ("Could not allocate soft capture voice (%zu bytes)\n",
805 ec36b695 bellard
                   sizeof (*sc));
806 8ead62cf bellard
            return -1;
807 8ead62cf bellard
        }
808 8ead62cf bellard
809 ec36b695 bellard
        sc->cap = cap;
810 ec36b695 bellard
        sw = &sc->sw;
811 8ead62cf bellard
        sw->hw = hw_cap;
812 ec36b695 bellard
        sw->info = hw->info;
813 8ead62cf bellard
        sw->empty = 1;
814 8ead62cf bellard
        sw->active = hw->enabled;
815 8ead62cf bellard
        sw->conv = noop_conv;
816 8ead62cf bellard
        sw->ratio = ((int64_t) hw_cap->info.freq << 32) / sw->info.freq;
817 8ead62cf bellard
        sw->rate = st_rate_start (sw->info.freq, hw_cap->info.freq);
818 8ead62cf bellard
        if (!sw->rate) {
819 8ead62cf bellard
            dolog ("Could not start rate conversion for `%s'\n", SW_NAME (sw));
820 8ead62cf bellard
            qemu_free (sw);
821 8ead62cf bellard
            return -1;
822 8ead62cf bellard
        }
823 72cf2d4f Blue Swirl
        QLIST_INSERT_HEAD (&hw_cap->sw_head, sw, entries);
824 72cf2d4f Blue Swirl
        QLIST_INSERT_HEAD (&hw->cap_head, sc, entries);
825 ec36b695 bellard
#ifdef DEBUG_CAPTURE
826 ec36b695 bellard
        asprintf (&sw->name, "for %p %d,%d,%d",
827 ec36b695 bellard
                  hw, sw->info.freq, sw->info.bits, sw->info.nchannels);
828 ec36b695 bellard
        dolog ("Added %s active = %d\n", sw->name, sw->active);
829 ec36b695 bellard
#endif
830 8ead62cf bellard
        if (sw->active) {
831 ec36b695 bellard
            audio_capture_maybe_changed (cap, 1);
832 8ead62cf bellard
        }
833 8ead62cf bellard
    }
834 8ead62cf bellard
    return 0;
835 8ead62cf bellard
}
836 8ead62cf bellard
837 8ead62cf bellard
/*
838 1d14ffa9 bellard
 * Hard voice (capture)
839 1d14ffa9 bellard
 */
840 c0fe3827 bellard
static int audio_pcm_hw_find_min_in (HWVoiceIn *hw)
841 85571bc7 bellard
{
842 1d14ffa9 bellard
    SWVoiceIn *sw;
843 1d14ffa9 bellard
    int m = hw->total_samples_captured;
844 1d14ffa9 bellard
845 1d14ffa9 bellard
    for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
846 1d14ffa9 bellard
        if (sw->active) {
847 1d14ffa9 bellard
            m = audio_MIN (m, sw->total_hw_samples_acquired);
848 1d14ffa9 bellard
        }
849 85571bc7 bellard
    }
850 1d14ffa9 bellard
    return m;
851 85571bc7 bellard
}
852 85571bc7 bellard
853 1d14ffa9 bellard
int audio_pcm_hw_get_live_in (HWVoiceIn *hw)
854 85571bc7 bellard
{
855 1d14ffa9 bellard
    int live = hw->total_samples_captured - audio_pcm_hw_find_min_in (hw);
856 1d14ffa9 bellard
    if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
857 1d14ffa9 bellard
        dolog ("live=%d hw->samples=%d\n", live, hw->samples);
858 1d14ffa9 bellard
        return 0;
859 85571bc7 bellard
    }
860 1d14ffa9 bellard
    return live;
861 85571bc7 bellard
}
862 85571bc7 bellard
863 ddabec73 malc
int audio_pcm_hw_clip_out (HWVoiceOut *hw, void *pcm_buf,
864 ddabec73 malc
                           int live, int pending)
865 ddabec73 malc
{
866 ddabec73 malc
    int left = hw->samples - pending;
867 ddabec73 malc
    int len = audio_MIN (left, live);
868 ddabec73 malc
    int clipped = 0;
869 ddabec73 malc
870 ddabec73 malc
    while (len) {
871 ddabec73 malc
        struct st_sample *src = hw->mix_buf + hw->rpos;
872 ddabec73 malc
        uint8_t *dst = advance (pcm_buf, hw->rpos << hw->info.shift);
873 ddabec73 malc
        int samples_till_end_of_buf = hw->samples - hw->rpos;
874 ddabec73 malc
        int samples_to_clip = audio_MIN (len, samples_till_end_of_buf);
875 ddabec73 malc
876 ddabec73 malc
        hw->clip (dst, src, samples_to_clip);
877 ddabec73 malc
878 ddabec73 malc
        hw->rpos = (hw->rpos + samples_to_clip) % hw->samples;
879 ddabec73 malc
        len -= samples_to_clip;
880 ddabec73 malc
        clipped += samples_to_clip;
881 ddabec73 malc
    }
882 ddabec73 malc
    return clipped;
883 ddabec73 malc
}
884 ddabec73 malc
885 1d14ffa9 bellard
/*
886 1d14ffa9 bellard
 * Soft voice (capture)
887 1d14ffa9 bellard
 */
888 1d14ffa9 bellard
static int audio_pcm_sw_get_rpos_in (SWVoiceIn *sw)
889 1d14ffa9 bellard
{
890 1d14ffa9 bellard
    HWVoiceIn *hw = sw->hw;
891 1d14ffa9 bellard
    int live = hw->total_samples_captured - sw->total_hw_samples_acquired;
892 1d14ffa9 bellard
    int rpos;
893 1d14ffa9 bellard
894 1d14ffa9 bellard
    if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
895 1d14ffa9 bellard
        dolog ("live=%d hw->samples=%d\n", live, hw->samples);
896 1d14ffa9 bellard
        return 0;
897 1d14ffa9 bellard
    }
898 1d14ffa9 bellard
899 1d14ffa9 bellard
    rpos = hw->wpos - live;
900 1d14ffa9 bellard
    if (rpos >= 0) {
901 1d14ffa9 bellard
        return rpos;
902 85571bc7 bellard
    }
903 85571bc7 bellard
    else {
904 1d14ffa9 bellard
        return hw->samples + rpos;
905 85571bc7 bellard
    }
906 85571bc7 bellard
}
907 85571bc7 bellard
908 1d14ffa9 bellard
int audio_pcm_sw_read (SWVoiceIn *sw, void *buf, int size)
909 85571bc7 bellard
{
910 1d14ffa9 bellard
    HWVoiceIn *hw = sw->hw;
911 1d14ffa9 bellard
    int samples, live, ret = 0, swlim, isamp, osamp, rpos, total = 0;
912 1ea879e5 malc
    struct st_sample *src, *dst = sw->buf;
913 1d14ffa9 bellard
914 1d14ffa9 bellard
    rpos = audio_pcm_sw_get_rpos_in (sw) % hw->samples;
915 1d14ffa9 bellard
916 1d14ffa9 bellard
    live = hw->total_samples_captured - sw->total_hw_samples_acquired;
917 1d14ffa9 bellard
    if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
918 1d14ffa9 bellard
        dolog ("live_in=%d hw->samples=%d\n", live, hw->samples);
919 1d14ffa9 bellard
        return 0;
920 1d14ffa9 bellard
    }
921 1d14ffa9 bellard
922 1d14ffa9 bellard
    samples = size >> sw->info.shift;
923 1d14ffa9 bellard
    if (!live) {
924 1d14ffa9 bellard
        return 0;
925 1d14ffa9 bellard
    }
926 85571bc7 bellard
927 1d14ffa9 bellard
    swlim = (live * sw->ratio) >> 32;
928 1d14ffa9 bellard
    swlim = audio_MIN (swlim, samples);
929 85571bc7 bellard
930 1d14ffa9 bellard
    while (swlim) {
931 1d14ffa9 bellard
        src = hw->conv_buf + rpos;
932 1d14ffa9 bellard
        isamp = hw->wpos - rpos;
933 1d14ffa9 bellard
        /* XXX: <= ? */
934 1d14ffa9 bellard
        if (isamp <= 0) {
935 1d14ffa9 bellard
            isamp = hw->samples - rpos;
936 1d14ffa9 bellard
        }
937 85571bc7 bellard
938 1d14ffa9 bellard
        if (!isamp) {
939 1d14ffa9 bellard
            break;
940 1d14ffa9 bellard
        }
941 1d14ffa9 bellard
        osamp = swlim;
942 85571bc7 bellard
943 1d14ffa9 bellard
        if (audio_bug (AUDIO_FUNC, osamp < 0)) {
944 1d14ffa9 bellard
            dolog ("osamp=%d\n", osamp);
945 c0fe3827 bellard
            return 0;
946 1d14ffa9 bellard
        }
947 85571bc7 bellard
948 1d14ffa9 bellard
        st_rate_flow (sw->rate, src, dst, &isamp, &osamp);
949 1d14ffa9 bellard
        swlim -= osamp;
950 1d14ffa9 bellard
        rpos = (rpos + isamp) % hw->samples;
951 1d14ffa9 bellard
        dst += osamp;
952 1d14ffa9 bellard
        ret += osamp;
953 1d14ffa9 bellard
        total += isamp;
954 1d14ffa9 bellard
    }
955 85571bc7 bellard
956 571ec3d6 bellard
    sw->clip (buf, sw->buf, ret);
957 1d14ffa9 bellard
    sw->total_hw_samples_acquired += total;
958 1d14ffa9 bellard
    return ret << sw->info.shift;
959 85571bc7 bellard
}
960 85571bc7 bellard
961 1d14ffa9 bellard
/*
962 1d14ffa9 bellard
 * Hard voice (playback)
963 1d14ffa9 bellard
 */
964 c0fe3827 bellard
static int audio_pcm_hw_find_min_out (HWVoiceOut *hw, int *nb_livep)
965 1d14ffa9 bellard
{
966 c0fe3827 bellard
    SWVoiceOut *sw;
967 c0fe3827 bellard
    int m = INT_MAX;
968 c0fe3827 bellard
    int nb_live = 0;
969 85571bc7 bellard
970 c0fe3827 bellard
    for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
971 c0fe3827 bellard
        if (sw->active || !sw->empty) {
972 c0fe3827 bellard
            m = audio_MIN (m, sw->total_hw_samples_mixed);
973 c0fe3827 bellard
            nb_live += 1;
974 c0fe3827 bellard
        }
975 85571bc7 bellard
    }
976 c0fe3827 bellard
977 c0fe3827 bellard
    *nb_livep = nb_live;
978 c0fe3827 bellard
    return m;
979 1d14ffa9 bellard
}
980 85571bc7 bellard
981 bdff253c malc
static int audio_pcm_hw_get_live_out (HWVoiceOut *hw, int *nb_live)
982 1d14ffa9 bellard
{
983 1d14ffa9 bellard
    int smin;
984 bdff253c malc
    int nb_live1;
985 85571bc7 bellard
986 bdff253c malc
    smin = audio_pcm_hw_find_min_out (hw, &nb_live1);
987 bdff253c malc
    if (nb_live) {
988 bdff253c malc
        *nb_live = nb_live1;
989 85571bc7 bellard
    }
990 bdff253c malc
991 bdff253c malc
    if (nb_live1) {
992 1d14ffa9 bellard
        int live = smin;
993 1d14ffa9 bellard
994 1d14ffa9 bellard
        if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
995 1d14ffa9 bellard
            dolog ("live=%d hw->samples=%d\n", live, hw->samples);
996 1d14ffa9 bellard
            return 0;
997 85571bc7 bellard
        }
998 1d14ffa9 bellard
        return live;
999 85571bc7 bellard
    }
1000 bdff253c malc
    return 0;
1001 85571bc7 bellard
}
1002 85571bc7 bellard
1003 1d14ffa9 bellard
/*
1004 1d14ffa9 bellard
 * Soft voice (playback)
1005 1d14ffa9 bellard
 */
1006 1d14ffa9 bellard
int audio_pcm_sw_write (SWVoiceOut *sw, void *buf, int size)
1007 85571bc7 bellard
{
1008 1d14ffa9 bellard
    int hwsamples, samples, isamp, osamp, wpos, live, dead, left, swlim, blck;
1009 1d14ffa9 bellard
    int ret = 0, pos = 0, total = 0;
1010 85571bc7 bellard
1011 1d14ffa9 bellard
    if (!sw) {
1012 1d14ffa9 bellard
        return size;
1013 1d14ffa9 bellard
    }
1014 85571bc7 bellard
1015 1d14ffa9 bellard
    hwsamples = sw->hw->samples;
1016 85571bc7 bellard
1017 1d14ffa9 bellard
    live = sw->total_hw_samples_mixed;
1018 1d14ffa9 bellard
    if (audio_bug (AUDIO_FUNC, live < 0 || live > hwsamples)){
1019 1d14ffa9 bellard
        dolog ("live=%d hw->samples=%d\n", live, hwsamples);
1020 1d14ffa9 bellard
        return 0;
1021 1d14ffa9 bellard
    }
1022 85571bc7 bellard
1023 1d14ffa9 bellard
    if (live == hwsamples) {
1024 ec36b695 bellard
#ifdef DEBUG_OUT
1025 ec36b695 bellard
        dolog ("%s is full %d\n", sw->name, live);
1026 ec36b695 bellard
#endif
1027 1d14ffa9 bellard
        return 0;
1028 1d14ffa9 bellard
    }
1029 85571bc7 bellard
1030 1d14ffa9 bellard
    wpos = (sw->hw->rpos + live) % hwsamples;
1031 1d14ffa9 bellard
    samples = size >> sw->info.shift;
1032 85571bc7 bellard
1033 1d14ffa9 bellard
    dead = hwsamples - live;
1034 1d14ffa9 bellard
    swlim = ((int64_t) dead << 32) / sw->ratio;
1035 1d14ffa9 bellard
    swlim = audio_MIN (swlim, samples);
1036 1d14ffa9 bellard
    if (swlim) {
1037 1d14ffa9 bellard
        sw->conv (sw->buf, buf, swlim, &sw->vol);
1038 1d14ffa9 bellard
    }
1039 1d14ffa9 bellard
1040 1d14ffa9 bellard
    while (swlim) {
1041 1d14ffa9 bellard
        dead = hwsamples - live;
1042 1d14ffa9 bellard
        left = hwsamples - wpos;
1043 1d14ffa9 bellard
        blck = audio_MIN (dead, left);
1044 1d14ffa9 bellard
        if (!blck) {
1045 1d14ffa9 bellard
            break;
1046 1d14ffa9 bellard
        }
1047 1d14ffa9 bellard
        isamp = swlim;
1048 1d14ffa9 bellard
        osamp = blck;
1049 1d14ffa9 bellard
        st_rate_flow_mix (
1050 1d14ffa9 bellard
            sw->rate,
1051 1d14ffa9 bellard
            sw->buf + pos,
1052 1d14ffa9 bellard
            sw->hw->mix_buf + wpos,
1053 1d14ffa9 bellard
            &isamp,
1054 1d14ffa9 bellard
            &osamp
1055 1d14ffa9 bellard
            );
1056 1d14ffa9 bellard
        ret += isamp;
1057 1d14ffa9 bellard
        swlim -= isamp;
1058 1d14ffa9 bellard
        pos += isamp;
1059 1d14ffa9 bellard
        live += osamp;
1060 1d14ffa9 bellard
        wpos = (wpos + osamp) % hwsamples;
1061 1d14ffa9 bellard
        total += osamp;
1062 1d14ffa9 bellard
    }
1063 1d14ffa9 bellard
1064 1d14ffa9 bellard
    sw->total_hw_samples_mixed += total;
1065 1d14ffa9 bellard
    sw->empty = sw->total_hw_samples_mixed == 0;
1066 1d14ffa9 bellard
1067 1d14ffa9 bellard
#ifdef DEBUG_OUT
1068 1d14ffa9 bellard
    dolog (
1069 c0fe3827 bellard
        "%s: write size %d ret %d total sw %d\n",
1070 c0fe3827 bellard
        SW_NAME (sw),
1071 1d14ffa9 bellard
        size >> sw->info.shift,
1072 1d14ffa9 bellard
        ret,
1073 c0fe3827 bellard
        sw->total_hw_samples_mixed
1074 1d14ffa9 bellard
        );
1075 1d14ffa9 bellard
#endif
1076 1d14ffa9 bellard
1077 1d14ffa9 bellard
    return ret << sw->info.shift;
1078 85571bc7 bellard
}
1079 85571bc7 bellard
1080 1d14ffa9 bellard
#ifdef DEBUG_AUDIO
1081 1d14ffa9 bellard
static void audio_pcm_print_info (const char *cap, struct audio_pcm_info *info)
1082 85571bc7 bellard
{
1083 1d14ffa9 bellard
    dolog ("%s: bits %d, sign %d, freq %d, nchan %d\n",
1084 1d14ffa9 bellard
           cap, info->bits, info->sign, info->freq, info->nchannels);
1085 85571bc7 bellard
}
1086 1d14ffa9 bellard
#endif
1087 85571bc7 bellard
1088 1d14ffa9 bellard
#define DAC
1089 1d14ffa9 bellard
#include "audio_template.h"
1090 1d14ffa9 bellard
#undef DAC
1091 1d14ffa9 bellard
#include "audio_template.h"
1092 1d14ffa9 bellard
1093 713a98f8 malc
/*
1094 713a98f8 malc
 * Timer
1095 713a98f8 malc
 */
1096 713a98f8 malc
static void audio_timer (void *opaque)
1097 713a98f8 malc
{
1098 713a98f8 malc
    AudioState *s = opaque;
1099 713a98f8 malc
1100 713a98f8 malc
    audio_run ("timer");
1101 713a98f8 malc
    qemu_mod_timer (s->ts, qemu_get_clock (vm_clock) + conf.period.ticks);
1102 713a98f8 malc
}
1103 713a98f8 malc
1104 713a98f8 malc
1105 713a98f8 malc
static int audio_is_timer_needed (void)
1106 713a98f8 malc
{
1107 713a98f8 malc
    HWVoiceIn *hwi = NULL;
1108 713a98f8 malc
    HWVoiceOut *hwo = NULL;
1109 713a98f8 malc
1110 713a98f8 malc
    while ((hwo = audio_pcm_hw_find_any_enabled_out (hwo))) {
1111 713a98f8 malc
        if (!hwo->poll_mode) return 1;
1112 713a98f8 malc
    }
1113 713a98f8 malc
    while ((hwi = audio_pcm_hw_find_any_enabled_in (hwi))) {
1114 713a98f8 malc
        if (!hwi->poll_mode) return 1;
1115 713a98f8 malc
    }
1116 713a98f8 malc
    return 0;
1117 713a98f8 malc
}
1118 713a98f8 malc
1119 713a98f8 malc
static void audio_reset_timer (void)
1120 713a98f8 malc
{
1121 713a98f8 malc
    AudioState *s = &glob_audio_state;
1122 713a98f8 malc
1123 713a98f8 malc
    if (audio_is_timer_needed ()) {
1124 713a98f8 malc
        qemu_mod_timer (s->ts, qemu_get_clock (vm_clock) + 1);
1125 713a98f8 malc
    }
1126 713a98f8 malc
    else {
1127 713a98f8 malc
        qemu_del_timer (s->ts);
1128 713a98f8 malc
    }
1129 713a98f8 malc
}
1130 713a98f8 malc
1131 713a98f8 malc
/*
1132 713a98f8 malc
 * Public API
1133 713a98f8 malc
 */
1134 1d14ffa9 bellard
int AUD_write (SWVoiceOut *sw, void *buf, int size)
1135 85571bc7 bellard
{
1136 1d14ffa9 bellard
    int bytes;
1137 85571bc7 bellard
1138 1d14ffa9 bellard
    if (!sw) {
1139 1d14ffa9 bellard
        /* XXX: Consider options */
1140 1d14ffa9 bellard
        return size;
1141 1d14ffa9 bellard
    }
1142 85571bc7 bellard
1143 1d14ffa9 bellard
    if (!sw->hw->enabled) {
1144 c0fe3827 bellard
        dolog ("Writing to disabled voice %s\n", SW_NAME (sw));
1145 85571bc7 bellard
        return 0;
1146 85571bc7 bellard
    }
1147 85571bc7 bellard
1148 1d14ffa9 bellard
    bytes = sw->hw->pcm_ops->write (sw, buf, size);
1149 1d14ffa9 bellard
    return bytes;
1150 1d14ffa9 bellard
}
1151 1d14ffa9 bellard
1152 1d14ffa9 bellard
int AUD_read (SWVoiceIn *sw, void *buf, int size)
1153 1d14ffa9 bellard
{
1154 1d14ffa9 bellard
    int bytes;
1155 85571bc7 bellard
1156 1d14ffa9 bellard
    if (!sw) {
1157 1d14ffa9 bellard
        /* XXX: Consider options */
1158 1d14ffa9 bellard
        return size;
1159 85571bc7 bellard
    }
1160 1d14ffa9 bellard
1161 1d14ffa9 bellard
    if (!sw->hw->enabled) {
1162 c0fe3827 bellard
        dolog ("Reading from disabled voice %s\n", SW_NAME (sw));
1163 1d14ffa9 bellard
        return 0;
1164 85571bc7 bellard
    }
1165 1d14ffa9 bellard
1166 1d14ffa9 bellard
    bytes = sw->hw->pcm_ops->read (sw, buf, size);
1167 1d14ffa9 bellard
    return bytes;
1168 85571bc7 bellard
}
1169 85571bc7 bellard
1170 1d14ffa9 bellard
int AUD_get_buffer_size_out (SWVoiceOut *sw)
1171 85571bc7 bellard
{
1172 c0fe3827 bellard
    return sw->hw->samples << sw->hw->info.shift;
1173 1d14ffa9 bellard
}
1174 1d14ffa9 bellard
1175 1d14ffa9 bellard
void AUD_set_active_out (SWVoiceOut *sw, int on)
1176 1d14ffa9 bellard
{
1177 1d14ffa9 bellard
    HWVoiceOut *hw;
1178 85571bc7 bellard
1179 1d14ffa9 bellard
    if (!sw) {
1180 85571bc7 bellard
        return;
1181 1d14ffa9 bellard
    }
1182 85571bc7 bellard
1183 85571bc7 bellard
    hw = sw->hw;
1184 1d14ffa9 bellard
    if (sw->active != on) {
1185 978dd635 malc
        AudioState *s = &glob_audio_state;
1186 1d14ffa9 bellard
        SWVoiceOut *temp_sw;
1187 ec36b695 bellard
        SWVoiceCap *sc;
1188 1d14ffa9 bellard
1189 1d14ffa9 bellard
        if (on) {
1190 1d14ffa9 bellard
            hw->pending_disable = 0;
1191 1d14ffa9 bellard
            if (!hw->enabled) {
1192 1d14ffa9 bellard
                hw->enabled = 1;
1193 978dd635 malc
                if (s->vm_running) {
1194 713a98f8 malc
                    hw->pcm_ops->ctl_out (hw, VOICE_ENABLE, conf.try_poll_out);
1195 713a98f8 malc
                    audio_reset_timer ();
1196 978dd635 malc
                }
1197 1d14ffa9 bellard
            }
1198 1d14ffa9 bellard
        }
1199 1d14ffa9 bellard
        else {
1200 1d14ffa9 bellard
            if (hw->enabled) {
1201 1d14ffa9 bellard
                int nb_active = 0;
1202 1d14ffa9 bellard
1203 1d14ffa9 bellard
                for (temp_sw = hw->sw_head.lh_first; temp_sw;
1204 1d14ffa9 bellard
                     temp_sw = temp_sw->entries.le_next) {
1205 1d14ffa9 bellard
                    nb_active += temp_sw->active != 0;
1206 1d14ffa9 bellard
                }
1207 1d14ffa9 bellard
1208 1d14ffa9 bellard
                hw->pending_disable = nb_active == 1;
1209 1d14ffa9 bellard
            }
1210 85571bc7 bellard
        }
1211 ec36b695 bellard
1212 ec36b695 bellard
        for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) {
1213 ec36b695 bellard
            sc->sw.active = hw->enabled;
1214 8ead62cf bellard
            if (hw->enabled) {
1215 ec36b695 bellard
                audio_capture_maybe_changed (sc->cap, 1);
1216 8ead62cf bellard
            }
1217 8ead62cf bellard
        }
1218 1d14ffa9 bellard
        sw->active = on;
1219 1d14ffa9 bellard
    }
1220 1d14ffa9 bellard
}
1221 1d14ffa9 bellard
1222 1d14ffa9 bellard
void AUD_set_active_in (SWVoiceIn *sw, int on)
1223 1d14ffa9 bellard
{
1224 1d14ffa9 bellard
    HWVoiceIn *hw;
1225 1d14ffa9 bellard
1226 1d14ffa9 bellard
    if (!sw) {
1227 1d14ffa9 bellard
        return;
1228 85571bc7 bellard
    }
1229 85571bc7 bellard
1230 1d14ffa9 bellard
    hw = sw->hw;
1231 85571bc7 bellard
    if (sw->active != on) {
1232 978dd635 malc
        AudioState *s = &glob_audio_state;
1233 1d14ffa9 bellard
        SWVoiceIn *temp_sw;
1234 1d14ffa9 bellard
1235 85571bc7 bellard
        if (on) {
1236 85571bc7 bellard
            if (!hw->enabled) {
1237 85571bc7 bellard
                hw->enabled = 1;
1238 978dd635 malc
                if (s->vm_running) {
1239 713a98f8 malc
                    hw->pcm_ops->ctl_in (hw, VOICE_ENABLE, conf.try_poll_in);
1240 978dd635 malc
                }
1241 85571bc7 bellard
            }
1242 1d14ffa9 bellard
            sw->total_hw_samples_acquired = hw->total_samples_captured;
1243 85571bc7 bellard
        }
1244 85571bc7 bellard
        else {
1245 1d14ffa9 bellard
            if (hw->enabled) {
1246 85571bc7 bellard
                int nb_active = 0;
1247 1d14ffa9 bellard
1248 1d14ffa9 bellard
                for (temp_sw = hw->sw_head.lh_first; temp_sw;
1249 1d14ffa9 bellard
                     temp_sw = temp_sw->entries.le_next) {
1250 1d14ffa9 bellard
                    nb_active += temp_sw->active != 0;
1251 85571bc7 bellard
                }
1252 85571bc7 bellard
1253 85571bc7 bellard
                if (nb_active == 1) {
1254 1d14ffa9 bellard
                    hw->enabled = 0;
1255 1d14ffa9 bellard
                    hw->pcm_ops->ctl_in (hw, VOICE_DISABLE);
1256 85571bc7 bellard
                }
1257 85571bc7 bellard
            }
1258 85571bc7 bellard
        }
1259 85571bc7 bellard
        sw->active = on;
1260 85571bc7 bellard
    }
1261 85571bc7 bellard
}
1262 85571bc7 bellard
1263 1d14ffa9 bellard
static int audio_get_avail (SWVoiceIn *sw)
1264 1d14ffa9 bellard
{
1265 1d14ffa9 bellard
    int live;
1266 1d14ffa9 bellard
1267 1d14ffa9 bellard
    if (!sw) {
1268 1d14ffa9 bellard
        return 0;
1269 1d14ffa9 bellard
    }
1270 1d14ffa9 bellard
1271 1d14ffa9 bellard
    live = sw->hw->total_samples_captured - sw->total_hw_samples_acquired;
1272 1d14ffa9 bellard
    if (audio_bug (AUDIO_FUNC, live < 0 || live > sw->hw->samples)) {
1273 1d14ffa9 bellard
        dolog ("live=%d sw->hw->samples=%d\n", live, sw->hw->samples);
1274 1d14ffa9 bellard
        return 0;
1275 1d14ffa9 bellard
    }
1276 1d14ffa9 bellard
1277 1d14ffa9 bellard
    ldebug (
1278 26a76461 bellard
        "%s: get_avail live %d ret %" PRId64 "\n",
1279 c0fe3827 bellard
        SW_NAME (sw),
1280 1d14ffa9 bellard
        live, (((int64_t) live << 32) / sw->ratio) << sw->info.shift
1281 1d14ffa9 bellard
        );
1282 1d14ffa9 bellard
1283 1d14ffa9 bellard
    return (((int64_t) live << 32) / sw->ratio) << sw->info.shift;
1284 1d14ffa9 bellard
}
1285 1d14ffa9 bellard
1286 1d14ffa9 bellard
static int audio_get_free (SWVoiceOut *sw)
1287 1d14ffa9 bellard
{
1288 1d14ffa9 bellard
    int live, dead;
1289 1d14ffa9 bellard
1290 1d14ffa9 bellard
    if (!sw) {
1291 1d14ffa9 bellard
        return 0;
1292 1d14ffa9 bellard
    }
1293 1d14ffa9 bellard
1294 1d14ffa9 bellard
    live = sw->total_hw_samples_mixed;
1295 1d14ffa9 bellard
1296 1d14ffa9 bellard
    if (audio_bug (AUDIO_FUNC, live < 0 || live > sw->hw->samples)) {
1297 1d14ffa9 bellard
        dolog ("live=%d sw->hw->samples=%d\n", live, sw->hw->samples);
1298 c0fe3827 bellard
        return 0;
1299 1d14ffa9 bellard
    }
1300 1d14ffa9 bellard
1301 1d14ffa9 bellard
    dead = sw->hw->samples - live;
1302 1d14ffa9 bellard
1303 1d14ffa9 bellard
#ifdef DEBUG_OUT
1304 26a76461 bellard
    dolog ("%s: get_free live %d dead %d ret %" PRId64 "\n",
1305 c0fe3827 bellard
           SW_NAME (sw),
1306 1d14ffa9 bellard
           live, dead, (((int64_t) dead << 32) / sw->ratio) << sw->info.shift);
1307 85571bc7 bellard
#endif
1308 1d14ffa9 bellard
1309 1d14ffa9 bellard
    return (((int64_t) dead << 32) / sw->ratio) << sw->info.shift;
1310 1d14ffa9 bellard
}
1311 1d14ffa9 bellard
1312 8ead62cf bellard
static void audio_capture_mix_and_clear (HWVoiceOut *hw, int rpos, int samples)
1313 8ead62cf bellard
{
1314 8ead62cf bellard
    int n;
1315 8ead62cf bellard
1316 8ead62cf bellard
    if (hw->enabled) {
1317 ec36b695 bellard
        SWVoiceCap *sc;
1318 8ead62cf bellard
1319 ec36b695 bellard
        for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) {
1320 ec36b695 bellard
            SWVoiceOut *sw = &sc->sw;
1321 8ead62cf bellard
            int rpos2 = rpos;
1322 8ead62cf bellard
1323 8ead62cf bellard
            n = samples;
1324 8ead62cf bellard
            while (n) {
1325 8ead62cf bellard
                int till_end_of_hw = hw->samples - rpos2;
1326 8ead62cf bellard
                int to_write = audio_MIN (till_end_of_hw, n);
1327 8ead62cf bellard
                int bytes = to_write << hw->info.shift;
1328 8ead62cf bellard
                int written;
1329 8ead62cf bellard
1330 8ead62cf bellard
                sw->buf = hw->mix_buf + rpos2;
1331 8ead62cf bellard
                written = audio_pcm_sw_write (sw, NULL, bytes);
1332 8ead62cf bellard
                if (written - bytes) {
1333 ec36b695 bellard
                    dolog ("Could not mix %d bytes into a capture "
1334 ec36b695 bellard
                           "buffer, mixed %d\n",
1335 ec36b695 bellard
                           bytes, written);
1336 8ead62cf bellard
                    break;
1337 8ead62cf bellard
                }
1338 8ead62cf bellard
                n -= to_write;
1339 8ead62cf bellard
                rpos2 = (rpos2 + to_write) % hw->samples;
1340 8ead62cf bellard
            }
1341 8ead62cf bellard
        }
1342 8ead62cf bellard
    }
1343 8ead62cf bellard
1344 8ead62cf bellard
    n = audio_MIN (samples, hw->samples - rpos);
1345 8ead62cf bellard
    mixeng_clear (hw->mix_buf + rpos, n);
1346 8ead62cf bellard
    mixeng_clear (hw->mix_buf, samples - n);
1347 8ead62cf bellard
}
1348 8ead62cf bellard
1349 c0fe3827 bellard
static void audio_run_out (AudioState *s)
1350 1d14ffa9 bellard
{
1351 1d14ffa9 bellard
    HWVoiceOut *hw = NULL;
1352 1d14ffa9 bellard
    SWVoiceOut *sw;
1353 1d14ffa9 bellard
1354 1a7dafce malc
    while ((hw = audio_pcm_hw_find_any_enabled_out (hw))) {
1355 1d14ffa9 bellard
        int played;
1356 8ead62cf bellard
        int live, free, nb_live, cleanup_required, prev_rpos;
1357 1d14ffa9 bellard
1358 bdff253c malc
        live = audio_pcm_hw_get_live_out (hw, &nb_live);
1359 1d14ffa9 bellard
        if (!nb_live) {
1360 1d14ffa9 bellard
            live = 0;
1361 1d14ffa9 bellard
        }
1362 c0fe3827 bellard
1363 1d14ffa9 bellard
        if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
1364 1d14ffa9 bellard
            dolog ("live=%d hw->samples=%d\n", live, hw->samples);
1365 c0fe3827 bellard
            continue;
1366 1d14ffa9 bellard
        }
1367 1d14ffa9 bellard
1368 1d14ffa9 bellard
        if (hw->pending_disable && !nb_live) {
1369 ec36b695 bellard
            SWVoiceCap *sc;
1370 1d14ffa9 bellard
#ifdef DEBUG_OUT
1371 1d14ffa9 bellard
            dolog ("Disabling voice\n");
1372 85571bc7 bellard
#endif
1373 1d14ffa9 bellard
            hw->enabled = 0;
1374 1d14ffa9 bellard
            hw->pending_disable = 0;
1375 1d14ffa9 bellard
            hw->pcm_ops->ctl_out (hw, VOICE_DISABLE);
1376 ec36b695 bellard
            for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) {
1377 ec36b695 bellard
                sc->sw.active = 0;
1378 ec36b695 bellard
                audio_recalc_and_notify_capture (sc->cap);
1379 8ead62cf bellard
            }
1380 1d14ffa9 bellard
            continue;
1381 1d14ffa9 bellard
        }
1382 1d14ffa9 bellard
1383 1d14ffa9 bellard
        if (!live) {
1384 1d14ffa9 bellard
            for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1385 1d14ffa9 bellard
                if (sw->active) {
1386 1d14ffa9 bellard
                    free = audio_get_free (sw);
1387 1d14ffa9 bellard
                    if (free > 0) {
1388 1d14ffa9 bellard
                        sw->callback.fn (sw->callback.opaque, free);
1389 1d14ffa9 bellard
                    }
1390 1d14ffa9 bellard
                }
1391 1d14ffa9 bellard
            }
1392 1d14ffa9 bellard
            continue;
1393 1d14ffa9 bellard
        }
1394 1d14ffa9 bellard
1395 8ead62cf bellard
        prev_rpos = hw->rpos;
1396 bdff253c malc
        played = hw->pcm_ops->run_out (hw, live);
1397 1d14ffa9 bellard
        if (audio_bug (AUDIO_FUNC, hw->rpos >= hw->samples)) {
1398 1d14ffa9 bellard
            dolog ("hw->rpos=%d hw->samples=%d played=%d\n",
1399 1d14ffa9 bellard
                   hw->rpos, hw->samples, played);
1400 1d14ffa9 bellard
            hw->rpos = 0;
1401 1d14ffa9 bellard
        }
1402 1d14ffa9 bellard
1403 1d14ffa9 bellard
#ifdef DEBUG_OUT
1404 c0fe3827 bellard
        dolog ("played=%d\n", played);
1405 85571bc7 bellard
#endif
1406 1d14ffa9 bellard
1407 1d14ffa9 bellard
        if (played) {
1408 1d14ffa9 bellard
            hw->ts_helper += played;
1409 8ead62cf bellard
            audio_capture_mix_and_clear (hw, prev_rpos, played);
1410 1d14ffa9 bellard
        }
1411 1d14ffa9 bellard
1412 c0fe3827 bellard
        cleanup_required = 0;
1413 1d14ffa9 bellard
        for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1414 1d14ffa9 bellard
            if (!sw->active && sw->empty) {
1415 1d14ffa9 bellard
                continue;
1416 1d14ffa9 bellard
            }
1417 1d14ffa9 bellard
1418 1d14ffa9 bellard
            if (audio_bug (AUDIO_FUNC, played > sw->total_hw_samples_mixed)) {
1419 1d14ffa9 bellard
                dolog ("played=%d sw->total_hw_samples_mixed=%d\n",
1420 1d14ffa9 bellard
                       played, sw->total_hw_samples_mixed);
1421 1d14ffa9 bellard
                played = sw->total_hw_samples_mixed;
1422 1d14ffa9 bellard
            }
1423 1d14ffa9 bellard
1424 1d14ffa9 bellard
            sw->total_hw_samples_mixed -= played;
1425 1d14ffa9 bellard
1426 1d14ffa9 bellard
            if (!sw->total_hw_samples_mixed) {
1427 1d14ffa9 bellard
                sw->empty = 1;
1428 c0fe3827 bellard
                cleanup_required |= !sw->active && !sw->callback.fn;
1429 1d14ffa9 bellard
            }
1430 1d14ffa9 bellard
1431 1d14ffa9 bellard
            if (sw->active) {
1432 1d14ffa9 bellard
                free = audio_get_free (sw);
1433 1d14ffa9 bellard
                if (free > 0) {
1434 1d14ffa9 bellard
                    sw->callback.fn (sw->callback.opaque, free);
1435 1d14ffa9 bellard
                }
1436 1d14ffa9 bellard
            }
1437 1d14ffa9 bellard
        }
1438 c0fe3827 bellard
1439 c0fe3827 bellard
        if (cleanup_required) {
1440 ec36b695 bellard
            SWVoiceOut *sw1;
1441 ec36b695 bellard
1442 ec36b695 bellard
            sw = hw->sw_head.lh_first;
1443 ec36b695 bellard
            while (sw) {
1444 ec36b695 bellard
                sw1 = sw->entries.le_next;
1445 c0fe3827 bellard
                if (!sw->active && !sw->callback.fn) {
1446 c0fe3827 bellard
#ifdef DEBUG_PLIVE
1447 c0fe3827 bellard
                    dolog ("Finishing with old voice\n");
1448 c0fe3827 bellard
#endif
1449 1a7dafce malc
                    audio_close_out (sw);
1450 c0fe3827 bellard
                }
1451 ec36b695 bellard
                sw = sw1;
1452 c0fe3827 bellard
            }
1453 c0fe3827 bellard
        }
1454 1d14ffa9 bellard
    }
1455 1d14ffa9 bellard
}
1456 1d14ffa9 bellard
1457 c0fe3827 bellard
static void audio_run_in (AudioState *s)
1458 1d14ffa9 bellard
{
1459 1d14ffa9 bellard
    HWVoiceIn *hw = NULL;
1460 1d14ffa9 bellard
1461 1a7dafce malc
    while ((hw = audio_pcm_hw_find_any_enabled_in (hw))) {
1462 1d14ffa9 bellard
        SWVoiceIn *sw;
1463 1d14ffa9 bellard
        int captured, min;
1464 1d14ffa9 bellard
1465 1d14ffa9 bellard
        captured = hw->pcm_ops->run_in (hw);
1466 1d14ffa9 bellard
1467 1d14ffa9 bellard
        min = audio_pcm_hw_find_min_in (hw);
1468 1d14ffa9 bellard
        hw->total_samples_captured += captured - min;
1469 1d14ffa9 bellard
        hw->ts_helper += captured;
1470 1d14ffa9 bellard
1471 1d14ffa9 bellard
        for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1472 1d14ffa9 bellard
            sw->total_hw_samples_acquired -= min;
1473 1d14ffa9 bellard
1474 1d14ffa9 bellard
            if (sw->active) {
1475 1d14ffa9 bellard
                int avail;
1476 1d14ffa9 bellard
1477 1d14ffa9 bellard
                avail = audio_get_avail (sw);
1478 1d14ffa9 bellard
                if (avail > 0) {
1479 1d14ffa9 bellard
                    sw->callback.fn (sw->callback.opaque, avail);
1480 1d14ffa9 bellard
                }
1481 1d14ffa9 bellard
            }
1482 1d14ffa9 bellard
        }
1483 1d14ffa9 bellard
    }
1484 1d14ffa9 bellard
}
1485 1d14ffa9 bellard
1486 8ead62cf bellard
static void audio_run_capture (AudioState *s)
1487 8ead62cf bellard
{
1488 8ead62cf bellard
    CaptureVoiceOut *cap;
1489 8ead62cf bellard
1490 8ead62cf bellard
    for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) {
1491 8ead62cf bellard
        int live, rpos, captured;
1492 8ead62cf bellard
        HWVoiceOut *hw = &cap->hw;
1493 8ead62cf bellard
        SWVoiceOut *sw;
1494 8ead62cf bellard
1495 bdff253c malc
        captured = live = audio_pcm_hw_get_live_out (hw, NULL);
1496 8ead62cf bellard
        rpos = hw->rpos;
1497 8ead62cf bellard
        while (live) {
1498 8ead62cf bellard
            int left = hw->samples - rpos;
1499 8ead62cf bellard
            int to_capture = audio_MIN (live, left);
1500 1ea879e5 malc
            struct st_sample *src;
1501 8ead62cf bellard
            struct capture_callback *cb;
1502 8ead62cf bellard
1503 8ead62cf bellard
            src = hw->mix_buf + rpos;
1504 8ead62cf bellard
            hw->clip (cap->buf, src, to_capture);
1505 8ead62cf bellard
            mixeng_clear (src, to_capture);
1506 8ead62cf bellard
1507 8ead62cf bellard
            for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
1508 8ead62cf bellard
                cb->ops.capture (cb->opaque, cap->buf,
1509 8ead62cf bellard
                                 to_capture << hw->info.shift);
1510 8ead62cf bellard
            }
1511 8ead62cf bellard
            rpos = (rpos + to_capture) % hw->samples;
1512 8ead62cf bellard
            live -= to_capture;
1513 8ead62cf bellard
        }
1514 8ead62cf bellard
        hw->rpos = rpos;
1515 8ead62cf bellard
1516 8ead62cf bellard
        for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1517 8ead62cf bellard
            if (!sw->active && sw->empty) {
1518 8ead62cf bellard
                continue;
1519 8ead62cf bellard
            }
1520 8ead62cf bellard
1521 8ead62cf bellard
            if (audio_bug (AUDIO_FUNC, captured > sw->total_hw_samples_mixed)) {
1522 8ead62cf bellard
                dolog ("captured=%d sw->total_hw_samples_mixed=%d\n",
1523 8ead62cf bellard
                       captured, sw->total_hw_samples_mixed);
1524 8ead62cf bellard
                captured = sw->total_hw_samples_mixed;
1525 8ead62cf bellard
            }
1526 8ead62cf bellard
1527 8ead62cf bellard
            sw->total_hw_samples_mixed -= captured;
1528 8ead62cf bellard
            sw->empty = sw->total_hw_samples_mixed == 0;
1529 8ead62cf bellard
        }
1530 8ead62cf bellard
    }
1531 8ead62cf bellard
}
1532 8ead62cf bellard
1533 713a98f8 malc
void audio_run (const char *msg)
1534 571ec3d6 bellard
{
1535 713a98f8 malc
    AudioState *s = &glob_audio_state;
1536 571ec3d6 bellard
1537 571ec3d6 bellard
    audio_run_out (s);
1538 571ec3d6 bellard
    audio_run_in (s);
1539 8ead62cf bellard
    audio_run_capture (s);
1540 713a98f8 malc
#ifdef DEBUG_POLL
1541 713a98f8 malc
    {
1542 713a98f8 malc
        static double prevtime;
1543 713a98f8 malc
        double currtime;
1544 713a98f8 malc
        struct timeval tv;
1545 571ec3d6 bellard
1546 713a98f8 malc
        if (gettimeofday (&tv, NULL)) {
1547 713a98f8 malc
            perror ("audio_run: gettimeofday");
1548 713a98f8 malc
            return;
1549 713a98f8 malc
        }
1550 713a98f8 malc
1551 713a98f8 malc
        currtime = tv.tv_sec + tv.tv_usec * 1e-6;
1552 713a98f8 malc
        dolog ("Elapsed since last %s: %f\n", msg, currtime - prevtime);
1553 713a98f8 malc
        prevtime = currtime;
1554 713a98f8 malc
    }
1555 713a98f8 malc
#endif
1556 571ec3d6 bellard
}
1557 571ec3d6 bellard
1558 1d14ffa9 bellard
static struct audio_option audio_options[] = {
1559 1d14ffa9 bellard
    /* DAC */
1560 98f9f48c malc
    {
1561 98f9f48c malc
        .name  = "DAC_FIXED_SETTINGS",
1562 98f9f48c malc
        .tag   = AUD_OPT_BOOL,
1563 98f9f48c malc
        .valp  = &conf.fixed_out.enabled,
1564 98f9f48c malc
        .descr = "Use fixed settings for host DAC"
1565 98f9f48c malc
    },
1566 98f9f48c malc
    {
1567 98f9f48c malc
        .name  = "DAC_FIXED_FREQ",
1568 98f9f48c malc
        .tag   = AUD_OPT_INT,
1569 98f9f48c malc
        .valp  = &conf.fixed_out.settings.freq,
1570 98f9f48c malc
        .descr = "Frequency for fixed host DAC"
1571 98f9f48c malc
    },
1572 98f9f48c malc
    {
1573 98f9f48c malc
        .name  = "DAC_FIXED_FMT",
1574 98f9f48c malc
        .tag   = AUD_OPT_FMT,
1575 98f9f48c malc
        .valp  = &conf.fixed_out.settings.fmt,
1576 98f9f48c malc
        .descr = "Format for fixed host DAC"
1577 98f9f48c malc
    },
1578 98f9f48c malc
    {
1579 98f9f48c malc
        .name  = "DAC_FIXED_CHANNELS",
1580 98f9f48c malc
        .tag   = AUD_OPT_INT,
1581 98f9f48c malc
        .valp  = &conf.fixed_out.settings.nchannels,
1582 98f9f48c malc
        .descr = "Number of channels for fixed DAC (1 - mono, 2 - stereo)"
1583 98f9f48c malc
    },
1584 98f9f48c malc
    {
1585 98f9f48c malc
        .name  = "DAC_VOICES",
1586 98f9f48c malc
        .tag   = AUD_OPT_INT,
1587 98f9f48c malc
        .valp  = &conf.fixed_out.nb_voices,
1588 98f9f48c malc
        .descr = "Number of voices for DAC"
1589 98f9f48c malc
    },
1590 713a98f8 malc
    {
1591 713a98f8 malc
        .name  = "DAC_TRY_POLL",
1592 713a98f8 malc
        .tag   = AUD_OPT_BOOL,
1593 713a98f8 malc
        .valp  = &conf.try_poll_out,
1594 713a98f8 malc
        .descr = "Attempt using poll mode for DAC"
1595 713a98f8 malc
    },
1596 1d14ffa9 bellard
    /* ADC */
1597 98f9f48c malc
    {
1598 98f9f48c malc
        .name  = "ADC_FIXED_SETTINGS",
1599 98f9f48c malc
        .tag   = AUD_OPT_BOOL,
1600 98f9f48c malc
        .valp  = &conf.fixed_in.enabled,
1601 98f9f48c malc
        .descr = "Use fixed settings for host ADC"
1602 98f9f48c malc
    },
1603 98f9f48c malc
    {
1604 98f9f48c malc
        .name  = "ADC_FIXED_FREQ",
1605 98f9f48c malc
        .tag   = AUD_OPT_INT,
1606 98f9f48c malc
        .valp  = &conf.fixed_in.settings.freq,
1607 98f9f48c malc
        .descr = "Frequency for fixed host ADC"
1608 98f9f48c malc
    },
1609 98f9f48c malc
    {
1610 98f9f48c malc
        .name  = "ADC_FIXED_FMT",
1611 98f9f48c malc
        .tag   = AUD_OPT_FMT,
1612 98f9f48c malc
        .valp  = &conf.fixed_in.settings.fmt,
1613 98f9f48c malc
        .descr = "Format for fixed host ADC"
1614 98f9f48c malc
    },
1615 98f9f48c malc
    {
1616 98f9f48c malc
        .name  = "ADC_FIXED_CHANNELS",
1617 98f9f48c malc
        .tag   = AUD_OPT_INT,
1618 98f9f48c malc
        .valp  = &conf.fixed_in.settings.nchannels,
1619 98f9f48c malc
        .descr = "Number of channels for fixed ADC (1 - mono, 2 - stereo)"
1620 98f9f48c malc
    },
1621 98f9f48c malc
    {
1622 98f9f48c malc
        .name  = "ADC_VOICES",
1623 98f9f48c malc
        .tag   = AUD_OPT_INT,
1624 98f9f48c malc
        .valp  = &conf.fixed_in.nb_voices,
1625 98f9f48c malc
        .descr = "Number of voices for ADC"
1626 98f9f48c malc
    },
1627 713a98f8 malc
    {
1628 713a98f8 malc
        .name  = "ADC_TRY_POLL",
1629 713a98f8 malc
        .tag   = AUD_OPT_BOOL,
1630 0a90e344 Jan Kiszka
        .valp  = &conf.try_poll_in,
1631 713a98f8 malc
        .descr = "Attempt using poll mode for ADC"
1632 713a98f8 malc
    },
1633 1d14ffa9 bellard
    /* Misc */
1634 98f9f48c malc
    {
1635 98f9f48c malc
        .name  = "TIMER_PERIOD",
1636 98f9f48c malc
        .tag   = AUD_OPT_INT,
1637 98f9f48c malc
        .valp  = &conf.period.hertz,
1638 98f9f48c malc
        .descr = "Timer period in HZ (0 - use lowest possible)"
1639 98f9f48c malc
    },
1640 98f9f48c malc
    {
1641 98f9f48c malc
        .name  = "PLIVE",
1642 98f9f48c malc
        .tag   = AUD_OPT_BOOL,
1643 98f9f48c malc
        .valp  = &conf.plive,
1644 98f9f48c malc
        .descr = "(undocumented)"
1645 98f9f48c malc
    },
1646 98f9f48c malc
    {
1647 98f9f48c malc
        .name  = "LOG_TO_MONITOR",
1648 98f9f48c malc
        .tag   = AUD_OPT_BOOL,
1649 98f9f48c malc
        .valp  = &conf.log_to_monitor,
1650 713a98f8 malc
        .descr = "Print logging messages to monitor instead of stderr"
1651 98f9f48c malc
    },
1652 2700efa3 Juan Quintela
    { /* End of list */ }
1653 85571bc7 bellard
};
1654 85571bc7 bellard
1655 571ec3d6 bellard
static void audio_pp_nb_voices (const char *typ, int nb)
1656 571ec3d6 bellard
{
1657 571ec3d6 bellard
    switch (nb) {
1658 571ec3d6 bellard
    case 0:
1659 571ec3d6 bellard
        printf ("Does not support %s\n", typ);
1660 571ec3d6 bellard
        break;
1661 571ec3d6 bellard
    case 1:
1662 571ec3d6 bellard
        printf ("One %s voice\n", typ);
1663 571ec3d6 bellard
        break;
1664 571ec3d6 bellard
    case INT_MAX:
1665 571ec3d6 bellard
        printf ("Theoretically supports many %s voices\n", typ);
1666 571ec3d6 bellard
        break;
1667 571ec3d6 bellard
    default:
1668 571ec3d6 bellard
        printf ("Theoretically supports upto %d %s voices\n", nb, typ);
1669 571ec3d6 bellard
        break;
1670 571ec3d6 bellard
    }
1671 571ec3d6 bellard
1672 571ec3d6 bellard
}
1673 571ec3d6 bellard
1674 1d14ffa9 bellard
void AUD_help (void)
1675 1d14ffa9 bellard
{
1676 1d14ffa9 bellard
    size_t i;
1677 1d14ffa9 bellard
1678 1d14ffa9 bellard
    audio_process_options ("AUDIO", audio_options);
1679 b1503cda malc
    for (i = 0; i < ARRAY_SIZE (drvtab); i++) {
1680 1d14ffa9 bellard
        struct audio_driver *d = drvtab[i];
1681 1d14ffa9 bellard
        if (d->options) {
1682 1d14ffa9 bellard
            audio_process_options (d->name, d->options);
1683 1d14ffa9 bellard
        }
1684 1d14ffa9 bellard
    }
1685 1d14ffa9 bellard
1686 1d14ffa9 bellard
    printf ("Audio options:\n");
1687 1d14ffa9 bellard
    audio_print_options ("AUDIO", audio_options);
1688 1d14ffa9 bellard
    printf ("\n");
1689 1d14ffa9 bellard
1690 1d14ffa9 bellard
    printf ("Available drivers:\n");
1691 1d14ffa9 bellard
1692 b1503cda malc
    for (i = 0; i < ARRAY_SIZE (drvtab); i++) {
1693 1d14ffa9 bellard
        struct audio_driver *d = drvtab[i];
1694 1d14ffa9 bellard
1695 1d14ffa9 bellard
        printf ("Name: %s\n", d->name);
1696 1d14ffa9 bellard
        printf ("Description: %s\n", d->descr);
1697 1d14ffa9 bellard
1698 571ec3d6 bellard
        audio_pp_nb_voices ("playback", d->max_voices_out);
1699 571ec3d6 bellard
        audio_pp_nb_voices ("capture", d->max_voices_in);
1700 1d14ffa9 bellard
1701 1d14ffa9 bellard
        if (d->options) {
1702 1d14ffa9 bellard
            printf ("Options:\n");
1703 1d14ffa9 bellard
            audio_print_options (d->name, d->options);
1704 1d14ffa9 bellard
        }
1705 1d14ffa9 bellard
        else {
1706 1d14ffa9 bellard
            printf ("No options\n");
1707 1d14ffa9 bellard
        }
1708 1d14ffa9 bellard
        printf ("\n");
1709 1d14ffa9 bellard
    }
1710 1d14ffa9 bellard
1711 1d14ffa9 bellard
    printf (
1712 1d14ffa9 bellard
        "Options are settable through environment variables.\n"
1713 1d14ffa9 bellard
        "Example:\n"
1714 1d14ffa9 bellard
#ifdef _WIN32
1715 1d14ffa9 bellard
        "  set QEMU_AUDIO_DRV=wav\n"
1716 571ec3d6 bellard
        "  set QEMU_WAV_PATH=c:\\tune.wav\n"
1717 1d14ffa9 bellard
#else
1718 1d14ffa9 bellard
        "  export QEMU_AUDIO_DRV=wav\n"
1719 1d14ffa9 bellard
        "  export QEMU_WAV_PATH=$HOME/tune.wav\n"
1720 1d14ffa9 bellard
        "(for csh replace export with setenv in the above)\n"
1721 1d14ffa9 bellard
#endif
1722 1d14ffa9 bellard
        "  qemu ...\n\n"
1723 1d14ffa9 bellard
        );
1724 1d14ffa9 bellard
}
1725 1d14ffa9 bellard
1726 c0fe3827 bellard
static int audio_driver_init (AudioState *s, struct audio_driver *drv)
1727 85571bc7 bellard
{
1728 1d14ffa9 bellard
    if (drv->options) {
1729 1d14ffa9 bellard
        audio_process_options (drv->name, drv->options);
1730 1d14ffa9 bellard
    }
1731 c0fe3827 bellard
    s->drv_opaque = drv->init ();
1732 1d14ffa9 bellard
1733 c0fe3827 bellard
    if (s->drv_opaque) {
1734 1a7dafce malc
        audio_init_nb_voices_out (drv);
1735 1a7dafce malc
        audio_init_nb_voices_in (drv);
1736 c0fe3827 bellard
        s->drv = drv;
1737 1d14ffa9 bellard
        return 0;
1738 85571bc7 bellard
    }
1739 85571bc7 bellard
    else {
1740 1d14ffa9 bellard
        dolog ("Could not init `%s' audio driver\n", drv->name);
1741 1d14ffa9 bellard
        return -1;
1742 85571bc7 bellard
    }
1743 85571bc7 bellard
}
1744 85571bc7 bellard
1745 9781e040 aliguori
static void audio_vm_change_state_handler (void *opaque, int running,
1746 9781e040 aliguori
                                           int reason)
1747 85571bc7 bellard
{
1748 c0fe3827 bellard
    AudioState *s = opaque;
1749 1d14ffa9 bellard
    HWVoiceOut *hwo = NULL;
1750 1d14ffa9 bellard
    HWVoiceIn *hwi = NULL;
1751 541e0844 bellard
    int op = running ? VOICE_ENABLE : VOICE_DISABLE;
1752 1d14ffa9 bellard
1753 978dd635 malc
    s->vm_running = running;
1754 1a7dafce malc
    while ((hwo = audio_pcm_hw_find_any_enabled_out (hwo))) {
1755 713a98f8 malc
        hwo->pcm_ops->ctl_out (hwo, op, conf.try_poll_out);
1756 1d14ffa9 bellard
    }
1757 85571bc7 bellard
1758 1a7dafce malc
    while ((hwi = audio_pcm_hw_find_any_enabled_in (hwi))) {
1759 713a98f8 malc
        hwi->pcm_ops->ctl_in (hwi, op, conf.try_poll_in);
1760 85571bc7 bellard
    }
1761 713a98f8 malc
    audio_reset_timer ();
1762 85571bc7 bellard
}
1763 85571bc7 bellard
1764 85571bc7 bellard
static void audio_atexit (void)
1765 85571bc7 bellard
{
1766 c0fe3827 bellard
    AudioState *s = &glob_audio_state;
1767 1d14ffa9 bellard
    HWVoiceOut *hwo = NULL;
1768 1d14ffa9 bellard
    HWVoiceIn *hwi = NULL;
1769 1d14ffa9 bellard
1770 1a7dafce malc
    while ((hwo = audio_pcm_hw_find_any_enabled_out (hwo))) {
1771 ec36b695 bellard
        SWVoiceCap *sc;
1772 8ead62cf bellard
1773 571ec3d6 bellard
        hwo->pcm_ops->ctl_out (hwo, VOICE_DISABLE);
1774 1d14ffa9 bellard
        hwo->pcm_ops->fini_out (hwo);
1775 8ead62cf bellard
1776 ec36b695 bellard
        for (sc = hwo->cap_head.lh_first; sc; sc = sc->entries.le_next) {
1777 ec36b695 bellard
            CaptureVoiceOut *cap = sc->cap;
1778 ec36b695 bellard
            struct capture_callback *cb;
1779 ec36b695 bellard
1780 ec36b695 bellard
            for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
1781 ec36b695 bellard
                cb->ops.destroy (cb->opaque);
1782 ec36b695 bellard
            }
1783 8ead62cf bellard
        }
1784 1d14ffa9 bellard
    }
1785 85571bc7 bellard
1786 1a7dafce malc
    while ((hwi = audio_pcm_hw_find_any_enabled_in (hwi))) {
1787 571ec3d6 bellard
        hwi->pcm_ops->ctl_in (hwi, VOICE_DISABLE);
1788 1d14ffa9 bellard
        hwi->pcm_ops->fini_in (hwi);
1789 85571bc7 bellard
    }
1790 c0fe3827 bellard
1791 c0fe3827 bellard
    if (s->drv) {
1792 c0fe3827 bellard
        s->drv->fini (s->drv_opaque);
1793 c0fe3827 bellard
    }
1794 85571bc7 bellard
}
1795 85571bc7 bellard
1796 d959fce9 Juan Quintela
static const VMStateDescription vmstate_audio = {
1797 d959fce9 Juan Quintela
    .name = "audio",
1798 d959fce9 Juan Quintela
    .version_id = 1,
1799 d959fce9 Juan Quintela
    .minimum_version_id = 1,
1800 d959fce9 Juan Quintela
    .minimum_version_id_old = 1,
1801 d959fce9 Juan Quintela
    .fields      = (VMStateField []) {
1802 d959fce9 Juan Quintela
        VMSTATE_END_OF_LIST()
1803 1d14ffa9 bellard
    }
1804 d959fce9 Juan Quintela
};
1805 85571bc7 bellard
1806 1a7dafce malc
static void audio_init (void)
1807 85571bc7 bellard
{
1808 1d14ffa9 bellard
    size_t i;
1809 85571bc7 bellard
    int done = 0;
1810 85571bc7 bellard
    const char *drvname;
1811 713a98f8 malc
    VMChangeStateEntry *e;
1812 c0fe3827 bellard
    AudioState *s = &glob_audio_state;
1813 1d14ffa9 bellard
1814 0d9acba8 Paul Brook
    if (s->drv) {
1815 1a7dafce malc
        return;
1816 0d9acba8 Paul Brook
    }
1817 0d9acba8 Paul Brook
1818 72cf2d4f Blue Swirl
    QLIST_INIT (&s->hw_head_out);
1819 72cf2d4f Blue Swirl
    QLIST_INIT (&s->hw_head_in);
1820 72cf2d4f Blue Swirl
    QLIST_INIT (&s->cap_head);
1821 571ec3d6 bellard
    atexit (audio_atexit);
1822 571ec3d6 bellard
1823 571ec3d6 bellard
    s->ts = qemu_new_timer (vm_clock, audio_timer, s);
1824 571ec3d6 bellard
    if (!s->ts) {
1825 0d9acba8 Paul Brook
        hw_error("Could not create audio timer\n");
1826 571ec3d6 bellard
    }
1827 571ec3d6 bellard
1828 1d14ffa9 bellard
    audio_process_options ("AUDIO", audio_options);
1829 1d14ffa9 bellard
1830 c0fe3827 bellard
    s->nb_hw_voices_out = conf.fixed_out.nb_voices;
1831 c0fe3827 bellard
    s->nb_hw_voices_in = conf.fixed_in.nb_voices;
1832 c0fe3827 bellard
1833 1d14ffa9 bellard
    if (s->nb_hw_voices_out <= 0) {
1834 571ec3d6 bellard
        dolog ("Bogus number of playback voices %d, setting to 1\n",
1835 1d14ffa9 bellard
               s->nb_hw_voices_out);
1836 1d14ffa9 bellard
        s->nb_hw_voices_out = 1;
1837 1d14ffa9 bellard
    }
1838 1d14ffa9 bellard
1839 1d14ffa9 bellard
    if (s->nb_hw_voices_in <= 0) {
1840 571ec3d6 bellard
        dolog ("Bogus number of capture voices %d, setting to 0\n",
1841 1d14ffa9 bellard
               s->nb_hw_voices_in);
1842 571ec3d6 bellard
        s->nb_hw_voices_in = 0;
1843 1d14ffa9 bellard
    }
1844 85571bc7 bellard
1845 1d14ffa9 bellard
    {
1846 1d14ffa9 bellard
        int def;
1847 1d14ffa9 bellard
        drvname = audio_get_conf_str ("QEMU_AUDIO_DRV", NULL, &def);
1848 1d14ffa9 bellard
    }
1849 85571bc7 bellard
1850 85571bc7 bellard
    if (drvname) {
1851 85571bc7 bellard
        int found = 0;
1852 1d14ffa9 bellard
1853 b1503cda malc
        for (i = 0; i < ARRAY_SIZE (drvtab); i++) {
1854 85571bc7 bellard
            if (!strcmp (drvname, drvtab[i]->name)) {
1855 c0fe3827 bellard
                done = !audio_driver_init (s, drvtab[i]);
1856 85571bc7 bellard
                found = 1;
1857 85571bc7 bellard
                break;
1858 85571bc7 bellard
            }
1859 85571bc7 bellard
        }
1860 1d14ffa9 bellard
1861 85571bc7 bellard
        if (!found) {
1862 85571bc7 bellard
            dolog ("Unknown audio driver `%s'\n", drvname);
1863 1d14ffa9 bellard
            dolog ("Run with -audio-help to list available drivers\n");
1864 85571bc7 bellard
        }
1865 85571bc7 bellard
    }
1866 85571bc7 bellard
1867 85571bc7 bellard
    if (!done) {
1868 b1503cda malc
        for (i = 0; !done && i < ARRAY_SIZE (drvtab); i++) {
1869 1d14ffa9 bellard
            if (drvtab[i]->can_be_default) {
1870 c0fe3827 bellard
                done = !audio_driver_init (s, drvtab[i]);
1871 1d14ffa9 bellard
            }
1872 85571bc7 bellard
        }
1873 85571bc7 bellard
    }
1874 85571bc7 bellard
1875 85571bc7 bellard
    if (!done) {
1876 c0fe3827 bellard
        done = !audio_driver_init (s, &no_audio_driver);
1877 c0fe3827 bellard
        if (!done) {
1878 0d9acba8 Paul Brook
            hw_error("Could not initialize audio subsystem\n");
1879 1d14ffa9 bellard
        }
1880 1d14ffa9 bellard
        else {
1881 c0fe3827 bellard
            dolog ("warning: Using timer based audio emulation\n");
1882 1d14ffa9 bellard
        }
1883 85571bc7 bellard
    }
1884 1d14ffa9 bellard
1885 0d9acba8 Paul Brook
    if (conf.period.hertz <= 0) {
1886 0d9acba8 Paul Brook
        if (conf.period.hertz < 0) {
1887 0d9acba8 Paul Brook
            dolog ("warning: Timer period is negative - %d "
1888 0d9acba8 Paul Brook
                   "treating as zero\n",
1889 0d9acba8 Paul Brook
                   conf.period.hertz);
1890 571ec3d6 bellard
        }
1891 0d9acba8 Paul Brook
        conf.period.ticks = 1;
1892 0d9acba8 Paul Brook
    } else {
1893 4f4cc0ef malc
        conf.period.ticks =
1894 4f4cc0ef malc
            muldiv64 (1, get_ticks_per_sec (), conf.period.hertz);
1895 1d14ffa9 bellard
    }
1896 0d9acba8 Paul Brook
1897 0d9acba8 Paul Brook
    e = qemu_add_vm_change_state_handler (audio_vm_change_state_handler, s);
1898 0d9acba8 Paul Brook
    if (!e) {
1899 0d9acba8 Paul Brook
        dolog ("warning: Could not register change state handler\n"
1900 0d9acba8 Paul Brook
               "(Audio can continue looping even after stopping the VM)\n");
1901 1d14ffa9 bellard
    }
1902 1d14ffa9 bellard
1903 72cf2d4f Blue Swirl
    QLIST_INIT (&s->card_head);
1904 d959fce9 Juan Quintela
    vmstate_register (0, &vmstate_audio, s);
1905 85571bc7 bellard
}
1906 8ead62cf bellard
1907 1a7dafce malc
void AUD_register_card (const char *name, QEMUSoundCard *card)
1908 1a7dafce malc
{
1909 1a7dafce malc
    audio_init ();
1910 1a7dafce malc
    card->name = qemu_strdup (name);
1911 1a7dafce malc
    memset (&card->entries, 0, sizeof (card->entries));
1912 72cf2d4f Blue Swirl
    QLIST_INSERT_HEAD (&glob_audio_state.card_head, card, entries);
1913 1a7dafce malc
}
1914 1a7dafce malc
1915 1a7dafce malc
void AUD_remove_card (QEMUSoundCard *card)
1916 1a7dafce malc
{
1917 72cf2d4f Blue Swirl
    QLIST_REMOVE (card, entries);
1918 1a7dafce malc
    qemu_free (card->name);
1919 1a7dafce malc
}
1920 1a7dafce malc
1921 1a7dafce malc
1922 ec36b695 bellard
CaptureVoiceOut *AUD_add_capture (
1923 1ea879e5 malc
    struct audsettings *as,
1924 8ead62cf bellard
    struct audio_capture_ops *ops,
1925 8ead62cf bellard
    void *cb_opaque
1926 8ead62cf bellard
    )
1927 8ead62cf bellard
{
1928 1a7dafce malc
    AudioState *s = &glob_audio_state;
1929 8ead62cf bellard
    CaptureVoiceOut *cap;
1930 8ead62cf bellard
    struct capture_callback *cb;
1931 8ead62cf bellard
1932 ec36b695 bellard
    if (audio_validate_settings (as)) {
1933 8ead62cf bellard
        dolog ("Invalid settings were passed when trying to add capture\n");
1934 8ead62cf bellard
        audio_print_settings (as);
1935 ec36b695 bellard
        goto err0;
1936 8ead62cf bellard
    }
1937 8ead62cf bellard
1938 8ead62cf bellard
    cb = audio_calloc (AUDIO_FUNC, 1, sizeof (*cb));
1939 8ead62cf bellard
    if (!cb) {
1940 8ead62cf bellard
        dolog ("Could not allocate capture callback information, size %zu\n",
1941 8ead62cf bellard
               sizeof (*cb));
1942 8ead62cf bellard
        goto err0;
1943 8ead62cf bellard
    }
1944 8ead62cf bellard
    cb->ops = *ops;
1945 8ead62cf bellard
    cb->opaque = cb_opaque;
1946 8ead62cf bellard
1947 1a7dafce malc
    cap = audio_pcm_capture_find_specific (as);
1948 8ead62cf bellard
    if (cap) {
1949 72cf2d4f Blue Swirl
        QLIST_INSERT_HEAD (&cap->cb_head, cb, entries);
1950 ec36b695 bellard
        return cap;
1951 8ead62cf bellard
    }
1952 8ead62cf bellard
    else {
1953 8ead62cf bellard
        HWVoiceOut *hw;
1954 8ead62cf bellard
        CaptureVoiceOut *cap;
1955 8ead62cf bellard
1956 8ead62cf bellard
        cap = audio_calloc (AUDIO_FUNC, 1, sizeof (*cap));
1957 8ead62cf bellard
        if (!cap) {
1958 8ead62cf bellard
            dolog ("Could not allocate capture voice, size %zu\n",
1959 8ead62cf bellard
                   sizeof (*cap));
1960 8ead62cf bellard
            goto err1;
1961 8ead62cf bellard
        }
1962 8ead62cf bellard
1963 8ead62cf bellard
        hw = &cap->hw;
1964 72cf2d4f Blue Swirl
        QLIST_INIT (&hw->sw_head);
1965 72cf2d4f Blue Swirl
        QLIST_INIT (&cap->cb_head);
1966 8ead62cf bellard
1967 8ead62cf bellard
        /* XXX find a more elegant way */
1968 8ead62cf bellard
        hw->samples = 4096 * 4;
1969 8ead62cf bellard
        hw->mix_buf = audio_calloc (AUDIO_FUNC, hw->samples,
1970 1ea879e5 malc
                                    sizeof (struct st_sample));
1971 8ead62cf bellard
        if (!hw->mix_buf) {
1972 8ead62cf bellard
            dolog ("Could not allocate capture mix buffer (%d samples)\n",
1973 8ead62cf bellard
                   hw->samples);
1974 8ead62cf bellard
            goto err2;
1975 8ead62cf bellard
        }
1976 8ead62cf bellard
1977 d929eba5 bellard
        audio_pcm_init_info (&hw->info, as);
1978 8ead62cf bellard
1979 8ead62cf bellard
        cap->buf = audio_calloc (AUDIO_FUNC, hw->samples, 1 << hw->info.shift);
1980 8ead62cf bellard
        if (!cap->buf) {
1981 8ead62cf bellard
            dolog ("Could not allocate capture buffer "
1982 8ead62cf bellard
                   "(%d samples, each %d bytes)\n",
1983 8ead62cf bellard
                   hw->samples, 1 << hw->info.shift);
1984 8ead62cf bellard
            goto err3;
1985 8ead62cf bellard
        }
1986 8ead62cf bellard
1987 8ead62cf bellard
        hw->clip = mixeng_clip
1988 8ead62cf bellard
            [hw->info.nchannels == 2]
1989 8ead62cf bellard
            [hw->info.sign]
1990 d929eba5 bellard
            [hw->info.swap_endianness]
1991 f941aa25 ths
            [audio_bits_to_index (hw->info.bits)];
1992 8ead62cf bellard
1993 72cf2d4f Blue Swirl
        QLIST_INSERT_HEAD (&s->cap_head, cap, entries);
1994 72cf2d4f Blue Swirl
        QLIST_INSERT_HEAD (&cap->cb_head, cb, entries);
1995 8ead62cf bellard
1996 8ead62cf bellard
        hw = NULL;
1997 1a7dafce malc
        while ((hw = audio_pcm_hw_find_any_out (hw))) {
1998 1a7dafce malc
            audio_attach_capture (hw);
1999 8ead62cf bellard
        }
2000 ec36b695 bellard
        return cap;
2001 8ead62cf bellard
2002 8ead62cf bellard
    err3:
2003 8ead62cf bellard
        qemu_free (cap->hw.mix_buf);
2004 8ead62cf bellard
    err2:
2005 8ead62cf bellard
        qemu_free (cap);
2006 8ead62cf bellard
    err1:
2007 8ead62cf bellard
        qemu_free (cb);
2008 8ead62cf bellard
    err0:
2009 ec36b695 bellard
        return NULL;
2010 ec36b695 bellard
    }
2011 ec36b695 bellard
}
2012 ec36b695 bellard
2013 ec36b695 bellard
void AUD_del_capture (CaptureVoiceOut *cap, void *cb_opaque)
2014 ec36b695 bellard
{
2015 ec36b695 bellard
    struct capture_callback *cb;
2016 ec36b695 bellard
2017 ec36b695 bellard
    for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
2018 ec36b695 bellard
        if (cb->opaque == cb_opaque) {
2019 ec36b695 bellard
            cb->ops.destroy (cb_opaque);
2020 72cf2d4f Blue Swirl
            QLIST_REMOVE (cb, entries);
2021 ec36b695 bellard
            qemu_free (cb);
2022 ec36b695 bellard
2023 ec36b695 bellard
            if (!cap->cb_head.lh_first) {
2024 ec36b695 bellard
                SWVoiceOut *sw = cap->hw.sw_head.lh_first, *sw1;
2025 a3c25997 bellard
2026 ec36b695 bellard
                while (sw) {
2027 a3c25997 bellard
                    SWVoiceCap *sc = (SWVoiceCap *) sw;
2028 ec36b695 bellard
#ifdef DEBUG_CAPTURE
2029 ec36b695 bellard
                    dolog ("freeing %s\n", sw->name);
2030 ec36b695 bellard
#endif
2031 a3c25997 bellard
2032 ec36b695 bellard
                    sw1 = sw->entries.le_next;
2033 ec36b695 bellard
                    if (sw->rate) {
2034 ec36b695 bellard
                        st_rate_stop (sw->rate);
2035 ec36b695 bellard
                        sw->rate = NULL;
2036 ec36b695 bellard
                    }
2037 72cf2d4f Blue Swirl
                    QLIST_REMOVE (sw, entries);
2038 72cf2d4f Blue Swirl
                    QLIST_REMOVE (sc, entries);
2039 a3c25997 bellard
                    qemu_free (sc);
2040 ec36b695 bellard
                    sw = sw1;
2041 ec36b695 bellard
                }
2042 72cf2d4f Blue Swirl
                QLIST_REMOVE (cap, entries);
2043 ec36b695 bellard
                qemu_free (cap);
2044 ec36b695 bellard
            }
2045 ec36b695 bellard
            return;
2046 ec36b695 bellard
        }
2047 8ead62cf bellard
    }
2048 8ead62cf bellard
}
2049 683efdcb balrog
2050 683efdcb balrog
void AUD_set_volume_out (SWVoiceOut *sw, int mute, uint8_t lvol, uint8_t rvol)
2051 683efdcb balrog
{
2052 683efdcb balrog
    if (sw) {
2053 683efdcb balrog
        sw->vol.mute = mute;
2054 683efdcb balrog
        sw->vol.l = nominal_volume.l * lvol / 255;
2055 683efdcb balrog
        sw->vol.r = nominal_volume.r * rvol / 255;
2056 683efdcb balrog
    }
2057 683efdcb balrog
}
2058 683efdcb balrog
2059 683efdcb balrog
void AUD_set_volume_in (SWVoiceIn *sw, int mute, uint8_t lvol, uint8_t rvol)
2060 683efdcb balrog
{
2061 683efdcb balrog
    if (sw) {
2062 683efdcb balrog
        sw->vol.mute = mute;
2063 683efdcb balrog
        sw->vol.l = nominal_volume.l * lvol / 255;
2064 683efdcb balrog
        sw->vol.r = nominal_volume.r * rvol / 255;
2065 683efdcb balrog
    }
2066 683efdcb balrog
}