Statistics
| Branch: | Revision:

root / audio / audio.c @ 4efbe58f

History | View | Annotate | Download (48.4 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 87ecb68b pbrook
#include "console.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 85571bc7 bellard
38 c0fe3827 bellard
#define SW_NAME(sw) (sw)->name ? (sw)->name : "unknown"
39 c0fe3827 bellard
40 1d14ffa9 bellard
static struct audio_driver *drvtab[] = {
41 0c58ac1c malc
    AUDIO_DRIVERS
42 1d14ffa9 bellard
    &no_audio_driver,
43 1d14ffa9 bellard
    &wav_audio_driver
44 1d14ffa9 bellard
};
45 85571bc7 bellard
46 c0fe3827 bellard
struct fixed_settings {
47 c0fe3827 bellard
    int enabled;
48 c0fe3827 bellard
    int nb_voices;
49 c0fe3827 bellard
    int greedy;
50 1ea879e5 malc
    struct audsettings settings;
51 c0fe3827 bellard
};
52 c0fe3827 bellard
53 c0fe3827 bellard
static struct {
54 c0fe3827 bellard
    struct fixed_settings fixed_out;
55 c0fe3827 bellard
    struct fixed_settings fixed_in;
56 c0fe3827 bellard
    union {
57 c310de86 malc
        int hertz;
58 c0fe3827 bellard
        int64_t ticks;
59 c0fe3827 bellard
    } period;
60 c0fe3827 bellard
    int plive;
61 541e0844 bellard
    int log_to_monitor;
62 c0fe3827 bellard
} conf = {
63 c0fe3827 bellard
    {                           /* DAC fixed settings */
64 c0fe3827 bellard
        1,                      /* enabled */
65 c0fe3827 bellard
        1,                      /* nb_voices */
66 c0fe3827 bellard
        1,                      /* greedy */
67 c0fe3827 bellard
        {
68 c0fe3827 bellard
            44100,              /* freq */
69 c0fe3827 bellard
            2,                  /* nchannels */
70 f941aa25 ths
            AUD_FMT_S16,        /* fmt */
71 f941aa25 ths
            AUDIO_HOST_ENDIANNESS
72 c0fe3827 bellard
        }
73 c0fe3827 bellard
    },
74 c0fe3827 bellard
75 c0fe3827 bellard
    {                           /* ADC fixed settings */
76 c0fe3827 bellard
        1,                      /* enabled */
77 c0fe3827 bellard
        1,                      /* nb_voices */
78 c0fe3827 bellard
        1,                      /* greedy */
79 c0fe3827 bellard
        {
80 c0fe3827 bellard
            44100,              /* freq */
81 c0fe3827 bellard
            2,                  /* nchannels */
82 f941aa25 ths
            AUD_FMT_S16,        /* fmt */
83 f941aa25 ths
            AUDIO_HOST_ENDIANNESS
84 c0fe3827 bellard
        }
85 c0fe3827 bellard
    },
86 c0fe3827 bellard
87 d64394f7 malc
    { 250 },                    /* period */
88 541e0844 bellard
    0,                          /* plive */
89 571ec3d6 bellard
    0                           /* log_to_monitor */
90 1d14ffa9 bellard
};
91 1d14ffa9 bellard
92 c0fe3827 bellard
static AudioState glob_audio_state;
93 c0fe3827 bellard
94 1ea879e5 malc
struct mixeng_volume nominal_volume = {
95 1d14ffa9 bellard
    0,
96 1d14ffa9 bellard
#ifdef FLOAT_MIXENG
97 1d14ffa9 bellard
    1.0,
98 1d14ffa9 bellard
    1.0
99 1d14ffa9 bellard
#else
100 683efdcb balrog
    1ULL << 32,
101 683efdcb balrog
    1ULL << 32
102 1d14ffa9 bellard
#endif
103 85571bc7 bellard
};
104 85571bc7 bellard
105 85571bc7 bellard
/* http://www.df.lth.se/~john_e/gems/gem002d.html */
106 85571bc7 bellard
/* http://www.multi-platforms.com/Tips/PopCount.htm */
107 85571bc7 bellard
uint32_t popcount (uint32_t u)
108 85571bc7 bellard
{
109 85571bc7 bellard
    u = ((u&0x55555555) + ((u>>1)&0x55555555));
110 85571bc7 bellard
    u = ((u&0x33333333) + ((u>>2)&0x33333333));
111 85571bc7 bellard
    u = ((u&0x0f0f0f0f) + ((u>>4)&0x0f0f0f0f));
112 85571bc7 bellard
    u = ((u&0x00ff00ff) + ((u>>8)&0x00ff00ff));
113 85571bc7 bellard
    u = ( u&0x0000ffff) + (u>>16);
114 85571bc7 bellard
    return u;
115 85571bc7 bellard
}
116 85571bc7 bellard
117 85571bc7 bellard
inline uint32_t lsbindex (uint32_t u)
118 85571bc7 bellard
{
119 85571bc7 bellard
    return popcount ((u&-u)-1);
120 85571bc7 bellard
}
121 85571bc7 bellard
122 1d14ffa9 bellard
#ifdef AUDIO_IS_FLAWLESS_AND_NO_CHECKS_ARE_REQURIED
123 1d14ffa9 bellard
#error No its not
124 1d14ffa9 bellard
#else
125 1d14ffa9 bellard
int audio_bug (const char *funcname, int cond)
126 85571bc7 bellard
{
127 1d14ffa9 bellard
    if (cond) {
128 1d14ffa9 bellard
        static int shown;
129 1d14ffa9 bellard
130 8ead62cf bellard
        AUD_log (NULL, "A bug was just triggered in %s\n", funcname);
131 1d14ffa9 bellard
        if (!shown) {
132 1d14ffa9 bellard
            shown = 1;
133 1d14ffa9 bellard
            AUD_log (NULL, "Save all your work and restart without audio\n");
134 1d14ffa9 bellard
            AUD_log (NULL, "Please send bug report to malc@pulsesoft.com\n");
135 1d14ffa9 bellard
            AUD_log (NULL, "I am sorry\n");
136 1d14ffa9 bellard
        }
137 1d14ffa9 bellard
        AUD_log (NULL, "Context:\n");
138 1d14ffa9 bellard
139 1d14ffa9 bellard
#if defined AUDIO_BREAKPOINT_ON_BUG
140 1d14ffa9 bellard
#  if defined HOST_I386
141 1d14ffa9 bellard
#    if defined __GNUC__
142 1d14ffa9 bellard
        __asm__ ("int3");
143 1d14ffa9 bellard
#    elif defined _MSC_VER
144 1d14ffa9 bellard
        _asm _emit 0xcc;
145 1d14ffa9 bellard
#    else
146 1d14ffa9 bellard
        abort ();
147 1d14ffa9 bellard
#    endif
148 1d14ffa9 bellard
#  else
149 1d14ffa9 bellard
        abort ();
150 1d14ffa9 bellard
#  endif
151 1d14ffa9 bellard
#endif
152 85571bc7 bellard
    }
153 85571bc7 bellard
154 1d14ffa9 bellard
    return cond;
155 85571bc7 bellard
}
156 1d14ffa9 bellard
#endif
157 85571bc7 bellard
158 f941aa25 ths
static inline int audio_bits_to_index (int bits)
159 f941aa25 ths
{
160 f941aa25 ths
    switch (bits) {
161 f941aa25 ths
    case 8:
162 f941aa25 ths
        return 0;
163 f941aa25 ths
164 f941aa25 ths
    case 16:
165 f941aa25 ths
        return 1;
166 f941aa25 ths
167 f941aa25 ths
    case 32:
168 f941aa25 ths
        return 2;
169 f941aa25 ths
170 f941aa25 ths
    default:
171 f941aa25 ths
        audio_bug ("bits_to_index", 1);
172 f941aa25 ths
        AUD_log (NULL, "invalid bits %d\n", bits);
173 f941aa25 ths
        return 0;
174 f941aa25 ths
    }
175 f941aa25 ths
}
176 f941aa25 ths
177 c0fe3827 bellard
void *audio_calloc (const char *funcname, int nmemb, size_t size)
178 c0fe3827 bellard
{
179 c0fe3827 bellard
    int cond;
180 c0fe3827 bellard
    size_t len;
181 c0fe3827 bellard
182 c0fe3827 bellard
    len = nmemb * size;
183 c0fe3827 bellard
    cond = !nmemb || !size;
184 c0fe3827 bellard
    cond |= nmemb < 0;
185 c0fe3827 bellard
    cond |= len < size;
186 c0fe3827 bellard
187 c0fe3827 bellard
    if (audio_bug ("audio_calloc", cond)) {
188 c0fe3827 bellard
        AUD_log (NULL, "%s passed invalid arguments to audio_calloc\n",
189 c0fe3827 bellard
                 funcname);
190 541e0844 bellard
        AUD_log (NULL, "nmemb=%d size=%zu (len=%zu)\n", nmemb, size, len);
191 c0fe3827 bellard
        return NULL;
192 c0fe3827 bellard
    }
193 c0fe3827 bellard
194 c0fe3827 bellard
    return qemu_mallocz (len);
195 c0fe3827 bellard
}
196 c0fe3827 bellard
197 1d14ffa9 bellard
static char *audio_alloc_prefix (const char *s)
198 85571bc7 bellard
{
199 1d14ffa9 bellard
    const char qemu_prefix[] = "QEMU_";
200 1d14ffa9 bellard
    size_t len;
201 1d14ffa9 bellard
    char *r;
202 85571bc7 bellard
203 1d14ffa9 bellard
    if (!s) {
204 1d14ffa9 bellard
        return NULL;
205 1d14ffa9 bellard
    }
206 85571bc7 bellard
207 1d14ffa9 bellard
    len = strlen (s);
208 a3772d4d blueswir1
    r = qemu_malloc (len + sizeof (qemu_prefix));
209 85571bc7 bellard
210 1d14ffa9 bellard
    if (r) {
211 1d14ffa9 bellard
        size_t i;
212 1d14ffa9 bellard
        char *u = r + sizeof (qemu_prefix) - 1;
213 85571bc7 bellard
214 363a37d5 blueswir1
        pstrcpy (r, len + sizeof (qemu_prefix), qemu_prefix);
215 2d6f8971 malc
        pstrcat (r, len + sizeof (qemu_prefix), s);
216 1d14ffa9 bellard
217 1d14ffa9 bellard
        for (i = 0; i < len; ++i) {
218 cd390083 blueswir1
            u[i] = qemu_toupper(u[i]);
219 1d14ffa9 bellard
        }
220 85571bc7 bellard
    }
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 541e0844 bellard
            term_printf ("%s: ", cap);
334 541e0844 bellard
        }
335 541e0844 bellard
336 541e0844 bellard
        term_vprintf (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
        if (!optname) {
464 c0fe3827 bellard
            dolog ("Could not allocate memory for option name `%s'\n",
465 1d14ffa9 bellard
                   opt->name);
466 1d14ffa9 bellard
            continue;
467 1d14ffa9 bellard
        }
468 1d14ffa9 bellard
469 363a37d5 blueswir1
        pstrcpy (optname, optlen, qemu_prefix);
470 c0fe3827 bellard
471 c0fe3827 bellard
        /* copy while upper-casing, including trailing zero */
472 1d14ffa9 bellard
        for (i = 0; i <= preflen; ++i) {
473 cd390083 blueswir1
            optname[i + sizeof (qemu_prefix) - 1] = qemu_toupper(prefix[i]);
474 1d14ffa9 bellard
        }
475 363a37d5 blueswir1
        pstrcat (optname, optlen, "_");
476 363a37d5 blueswir1
        pstrcat (optname, optlen, opt->name);
477 1d14ffa9 bellard
478 1d14ffa9 bellard
        def = 1;
479 1d14ffa9 bellard
        switch (opt->tag) {
480 1d14ffa9 bellard
        case AUD_OPT_BOOL:
481 1d14ffa9 bellard
        case AUD_OPT_INT:
482 1d14ffa9 bellard
            {
483 1d14ffa9 bellard
                int *intp = opt->valp;
484 1d14ffa9 bellard
                *intp = audio_get_conf_int (optname, *intp, &def);
485 1d14ffa9 bellard
            }
486 1d14ffa9 bellard
            break;
487 1d14ffa9 bellard
488 1d14ffa9 bellard
        case AUD_OPT_FMT:
489 1d14ffa9 bellard
            {
490 1d14ffa9 bellard
                audfmt_e *fmtp = opt->valp;
491 1d14ffa9 bellard
                *fmtp = audio_get_conf_fmt (optname, *fmtp, &def);
492 1d14ffa9 bellard
            }
493 1d14ffa9 bellard
            break;
494 1d14ffa9 bellard
495 1d14ffa9 bellard
        case AUD_OPT_STR:
496 1d14ffa9 bellard
            {
497 1d14ffa9 bellard
                const char **strp = opt->valp;
498 1d14ffa9 bellard
                *strp = audio_get_conf_str (optname, *strp, &def);
499 1d14ffa9 bellard
            }
500 1d14ffa9 bellard
            break;
501 1d14ffa9 bellard
502 1d14ffa9 bellard
        default:
503 1d14ffa9 bellard
            dolog ("Bad value tag for option `%s' - %d\n",
504 1d14ffa9 bellard
                   optname, opt->tag);
505 85571bc7 bellard
            break;
506 85571bc7 bellard
        }
507 85571bc7 bellard
508 fe8f096b ths
        if (!opt->overriddenp) {
509 fe8f096b ths
            opt->overriddenp = &opt->overridden;
510 1d14ffa9 bellard
        }
511 fe8f096b ths
        *opt->overriddenp = !def;
512 1d14ffa9 bellard
        qemu_free (optname);
513 1d14ffa9 bellard
    }
514 85571bc7 bellard
}
515 85571bc7 bellard
516 1ea879e5 malc
static void audio_print_settings (struct audsettings *as)
517 c0fe3827 bellard
{
518 c0fe3827 bellard
    dolog ("frequency=%d nchannels=%d fmt=", as->freq, as->nchannels);
519 c0fe3827 bellard
520 c0fe3827 bellard
    switch (as->fmt) {
521 c0fe3827 bellard
    case AUD_FMT_S8:
522 c0fe3827 bellard
        AUD_log (NULL, "S8");
523 c0fe3827 bellard
        break;
524 c0fe3827 bellard
    case AUD_FMT_U8:
525 c0fe3827 bellard
        AUD_log (NULL, "U8");
526 c0fe3827 bellard
        break;
527 c0fe3827 bellard
    case AUD_FMT_S16:
528 c0fe3827 bellard
        AUD_log (NULL, "S16");
529 c0fe3827 bellard
        break;
530 c0fe3827 bellard
    case AUD_FMT_U16:
531 c0fe3827 bellard
        AUD_log (NULL, "U16");
532 c0fe3827 bellard
        break;
533 d50997f9 malc
    case AUD_FMT_S32:
534 d50997f9 malc
        AUD_log (NULL, "S32");
535 d50997f9 malc
        break;
536 d50997f9 malc
    case AUD_FMT_U32:
537 d50997f9 malc
        AUD_log (NULL, "U32");
538 d50997f9 malc
        break;
539 c0fe3827 bellard
    default:
540 c0fe3827 bellard
        AUD_log (NULL, "invalid(%d)", as->fmt);
541 c0fe3827 bellard
        break;
542 c0fe3827 bellard
    }
543 ec36b695 bellard
544 ec36b695 bellard
    AUD_log (NULL, " endianness=");
545 d929eba5 bellard
    switch (as->endianness) {
546 d929eba5 bellard
    case 0:
547 d929eba5 bellard
        AUD_log (NULL, "little");
548 d929eba5 bellard
        break;
549 d929eba5 bellard
    case 1:
550 d929eba5 bellard
        AUD_log (NULL, "big");
551 d929eba5 bellard
        break;
552 d929eba5 bellard
    default:
553 d929eba5 bellard
        AUD_log (NULL, "invalid");
554 d929eba5 bellard
        break;
555 d929eba5 bellard
    }
556 c0fe3827 bellard
    AUD_log (NULL, "\n");
557 c0fe3827 bellard
}
558 c0fe3827 bellard
559 1ea879e5 malc
static int audio_validate_settings (struct audsettings *as)
560 c0fe3827 bellard
{
561 c0fe3827 bellard
    int invalid;
562 c0fe3827 bellard
563 c0fe3827 bellard
    invalid = as->nchannels != 1 && as->nchannels != 2;
564 d929eba5 bellard
    invalid |= as->endianness != 0 && as->endianness != 1;
565 c0fe3827 bellard
566 c0fe3827 bellard
    switch (as->fmt) {
567 c0fe3827 bellard
    case AUD_FMT_S8:
568 c0fe3827 bellard
    case AUD_FMT_U8:
569 c0fe3827 bellard
    case AUD_FMT_S16:
570 c0fe3827 bellard
    case AUD_FMT_U16:
571 f941aa25 ths
    case AUD_FMT_S32:
572 f941aa25 ths
    case AUD_FMT_U32:
573 c0fe3827 bellard
        break;
574 c0fe3827 bellard
    default:
575 c0fe3827 bellard
        invalid = 1;
576 c0fe3827 bellard
        break;
577 c0fe3827 bellard
    }
578 c0fe3827 bellard
579 c0fe3827 bellard
    invalid |= as->freq <= 0;
580 d929eba5 bellard
    return invalid ? -1 : 0;
581 c0fe3827 bellard
}
582 c0fe3827 bellard
583 1ea879e5 malc
static int audio_pcm_info_eq (struct audio_pcm_info *info, struct audsettings *as)
584 85571bc7 bellard
{
585 1d14ffa9 bellard
    int bits = 8, sign = 0;
586 85571bc7 bellard
587 c0fe3827 bellard
    switch (as->fmt) {
588 1d14ffa9 bellard
    case AUD_FMT_S8:
589 1d14ffa9 bellard
        sign = 1;
590 1d14ffa9 bellard
    case AUD_FMT_U8:
591 1d14ffa9 bellard
        break;
592 1d14ffa9 bellard
593 1d14ffa9 bellard
    case AUD_FMT_S16:
594 1d14ffa9 bellard
        sign = 1;
595 1d14ffa9 bellard
    case AUD_FMT_U16:
596 1d14ffa9 bellard
        bits = 16;
597 1d14ffa9 bellard
        break;
598 f941aa25 ths
599 f941aa25 ths
    case AUD_FMT_S32:
600 f941aa25 ths
        sign = 1;
601 f941aa25 ths
    case AUD_FMT_U32:
602 f941aa25 ths
        bits = 32;
603 f941aa25 ths
        break;
604 85571bc7 bellard
    }
605 c0fe3827 bellard
    return info->freq == as->freq
606 c0fe3827 bellard
        && info->nchannels == as->nchannels
607 1d14ffa9 bellard
        && info->sign == sign
608 d929eba5 bellard
        && info->bits == bits
609 d929eba5 bellard
        && info->swap_endianness == (as->endianness != AUDIO_HOST_ENDIANNESS);
610 1d14ffa9 bellard
}
611 85571bc7 bellard
612 1ea879e5 malc
void audio_pcm_init_info (struct audio_pcm_info *info, struct audsettings *as)
613 1d14ffa9 bellard
{
614 f941aa25 ths
    int bits = 8, sign = 0, shift = 0;
615 1d14ffa9 bellard
616 c0fe3827 bellard
    switch (as->fmt) {
617 85571bc7 bellard
    case AUD_FMT_S8:
618 85571bc7 bellard
        sign = 1;
619 85571bc7 bellard
    case AUD_FMT_U8:
620 85571bc7 bellard
        break;
621 85571bc7 bellard
622 85571bc7 bellard
    case AUD_FMT_S16:
623 85571bc7 bellard
        sign = 1;
624 85571bc7 bellard
    case AUD_FMT_U16:
625 85571bc7 bellard
        bits = 16;
626 f941aa25 ths
        shift = 1;
627 f941aa25 ths
        break;
628 f941aa25 ths
629 f941aa25 ths
    case AUD_FMT_S32:
630 f941aa25 ths
        sign = 1;
631 f941aa25 ths
    case AUD_FMT_U32:
632 f941aa25 ths
        bits = 32;
633 f941aa25 ths
        shift = 2;
634 85571bc7 bellard
        break;
635 85571bc7 bellard
    }
636 85571bc7 bellard
637 c0fe3827 bellard
    info->freq = as->freq;
638 1d14ffa9 bellard
    info->bits = bits;
639 1d14ffa9 bellard
    info->sign = sign;
640 c0fe3827 bellard
    info->nchannels = as->nchannels;
641 f941aa25 ths
    info->shift = (as->nchannels == 2) + shift;
642 1d14ffa9 bellard
    info->align = (1 << info->shift) - 1;
643 1d14ffa9 bellard
    info->bytes_per_second = info->freq << info->shift;
644 d929eba5 bellard
    info->swap_endianness = (as->endianness != AUDIO_HOST_ENDIANNESS);
645 85571bc7 bellard
}
646 85571bc7 bellard
647 1d14ffa9 bellard
void audio_pcm_info_clear_buf (struct audio_pcm_info *info, void *buf, int len)
648 85571bc7 bellard
{
649 1d14ffa9 bellard
    if (!len) {
650 1d14ffa9 bellard
        return;
651 1d14ffa9 bellard
    }
652 1d14ffa9 bellard
653 1d14ffa9 bellard
    if (info->sign) {
654 e2f909be bellard
        memset (buf, 0x00, len << info->shift);
655 85571bc7 bellard
    }
656 85571bc7 bellard
    else {
657 f941aa25 ths
        switch (info->bits) {
658 f941aa25 ths
        case 8:
659 e2f909be bellard
            memset (buf, 0x80, len << info->shift);
660 f941aa25 ths
            break;
661 1d14ffa9 bellard
662 f941aa25 ths
        case 16:
663 f941aa25 ths
            {
664 f941aa25 ths
                int i;
665 f941aa25 ths
                uint16_t *p = buf;
666 f941aa25 ths
                int shift = info->nchannels - 1;
667 f941aa25 ths
                short s = INT16_MAX;
668 f941aa25 ths
669 f941aa25 ths
                if (info->swap_endianness) {
670 f941aa25 ths
                    s = bswap16 (s);
671 f941aa25 ths
                }
672 f941aa25 ths
673 f941aa25 ths
                for (i = 0; i < len << shift; i++) {
674 f941aa25 ths
                    p[i] = s;
675 f941aa25 ths
                }
676 1d14ffa9 bellard
            }
677 f941aa25 ths
            break;
678 f941aa25 ths
679 f941aa25 ths
        case 32:
680 f941aa25 ths
            {
681 f941aa25 ths
                int i;
682 f941aa25 ths
                uint32_t *p = buf;
683 f941aa25 ths
                int shift = info->nchannels - 1;
684 f941aa25 ths
                int32_t s = INT32_MAX;
685 f941aa25 ths
686 f941aa25 ths
                if (info->swap_endianness) {
687 f941aa25 ths
                    s = bswap32 (s);
688 f941aa25 ths
                }
689 1d14ffa9 bellard
690 f941aa25 ths
                for (i = 0; i < len << shift; i++) {
691 f941aa25 ths
                    p[i] = s;
692 f941aa25 ths
                }
693 1d14ffa9 bellard
            }
694 f941aa25 ths
            break;
695 f941aa25 ths
696 f941aa25 ths
        default:
697 f941aa25 ths
            AUD_log (NULL, "audio_pcm_info_clear_buf: invalid bits %d\n",
698 f941aa25 ths
                     info->bits);
699 f941aa25 ths
            break;
700 1d14ffa9 bellard
        }
701 85571bc7 bellard
    }
702 85571bc7 bellard
}
703 85571bc7 bellard
704 1d14ffa9 bellard
/*
705 8ead62cf bellard
 * Capture
706 8ead62cf bellard
 */
707 1ea879e5 malc
static void noop_conv (struct st_sample *dst, const void *src,
708 1ea879e5 malc
                       int samples, struct mixeng_volume *vol)
709 8ead62cf bellard
{
710 8ead62cf bellard
    (void) src;
711 8ead62cf bellard
    (void) dst;
712 8ead62cf bellard
    (void) samples;
713 8ead62cf bellard
    (void) vol;
714 8ead62cf bellard
}
715 8ead62cf bellard
716 8ead62cf bellard
static CaptureVoiceOut *audio_pcm_capture_find_specific (
717 8ead62cf bellard
    AudioState *s,
718 1ea879e5 malc
    struct audsettings *as
719 8ead62cf bellard
    )
720 8ead62cf bellard
{
721 8ead62cf bellard
    CaptureVoiceOut *cap;
722 8ead62cf bellard
723 8ead62cf bellard
    for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) {
724 d929eba5 bellard
        if (audio_pcm_info_eq (&cap->hw.info, as)) {
725 8ead62cf bellard
            return cap;
726 8ead62cf bellard
        }
727 8ead62cf bellard
    }
728 8ead62cf bellard
    return NULL;
729 8ead62cf bellard
}
730 8ead62cf bellard
731 ec36b695 bellard
static void audio_notify_capture (CaptureVoiceOut *cap, audcnotification_e cmd)
732 8ead62cf bellard
{
733 ec36b695 bellard
    struct capture_callback *cb;
734 ec36b695 bellard
735 ec36b695 bellard
#ifdef DEBUG_CAPTURE
736 ec36b695 bellard
    dolog ("notification %d sent\n", cmd);
737 ec36b695 bellard
#endif
738 ec36b695 bellard
    for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
739 ec36b695 bellard
        cb->ops.notify (cb->opaque, cmd);
740 ec36b695 bellard
    }
741 ec36b695 bellard
}
742 8ead62cf bellard
743 ec36b695 bellard
static void audio_capture_maybe_changed (CaptureVoiceOut *cap, int enabled)
744 ec36b695 bellard
{
745 ec36b695 bellard
    if (cap->hw.enabled != enabled) {
746 ec36b695 bellard
        audcnotification_e cmd;
747 8ead62cf bellard
        cap->hw.enabled = enabled;
748 ec36b695 bellard
        cmd = enabled ? AUD_CNOTIFY_ENABLE : AUD_CNOTIFY_DISABLE;
749 ec36b695 bellard
        audio_notify_capture (cap, cmd);
750 8ead62cf bellard
    }
751 8ead62cf bellard
}
752 8ead62cf bellard
753 8ead62cf bellard
static void audio_recalc_and_notify_capture (CaptureVoiceOut *cap)
754 8ead62cf bellard
{
755 8ead62cf bellard
    HWVoiceOut *hw = &cap->hw;
756 8ead62cf bellard
    SWVoiceOut *sw;
757 8ead62cf bellard
    int enabled = 0;
758 8ead62cf bellard
759 ec36b695 bellard
    for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
760 8ead62cf bellard
        if (sw->active) {
761 8ead62cf bellard
            enabled = 1;
762 8ead62cf bellard
            break;
763 8ead62cf bellard
        }
764 8ead62cf bellard
    }
765 ec36b695 bellard
    audio_capture_maybe_changed (cap, enabled);
766 8ead62cf bellard
}
767 8ead62cf bellard
768 8ead62cf bellard
static void audio_detach_capture (HWVoiceOut *hw)
769 8ead62cf bellard
{
770 ec36b695 bellard
    SWVoiceCap *sc = hw->cap_head.lh_first;
771 ec36b695 bellard
772 ec36b695 bellard
    while (sc) {
773 ec36b695 bellard
        SWVoiceCap *sc1 = sc->entries.le_next;
774 ec36b695 bellard
        SWVoiceOut *sw = &sc->sw;
775 ec36b695 bellard
        CaptureVoiceOut *cap = sc->cap;
776 ec36b695 bellard
        int was_active = sw->active;
777 8ead62cf bellard
778 8ead62cf bellard
        if (sw->rate) {
779 8ead62cf bellard
            st_rate_stop (sw->rate);
780 8ead62cf bellard
            sw->rate = NULL;
781 8ead62cf bellard
        }
782 8ead62cf bellard
783 8ead62cf bellard
        LIST_REMOVE (sw, entries);
784 ec36b695 bellard
        LIST_REMOVE (sc, entries);
785 ec36b695 bellard
        qemu_free (sc);
786 ec36b695 bellard
        if (was_active) {
787 ec36b695 bellard
            /* We have removed soft voice from the capture:
788 ec36b695 bellard
               this might have changed the overall status of the capture
789 ec36b695 bellard
               since this might have been the only active voice */
790 ec36b695 bellard
            audio_recalc_and_notify_capture (cap);
791 ec36b695 bellard
        }
792 ec36b695 bellard
        sc = sc1;
793 8ead62cf bellard
    }
794 8ead62cf bellard
}
795 8ead62cf bellard
796 8ead62cf bellard
static int audio_attach_capture (AudioState *s, HWVoiceOut *hw)
797 8ead62cf bellard
{
798 8ead62cf bellard
    CaptureVoiceOut *cap;
799 8ead62cf bellard
800 8ead62cf bellard
    audio_detach_capture (hw);
801 8ead62cf bellard
    for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) {
802 ec36b695 bellard
        SWVoiceCap *sc;
803 8ead62cf bellard
        SWVoiceOut *sw;
804 ec36b695 bellard
        HWVoiceOut *hw_cap = &cap->hw;
805 8ead62cf bellard
806 ec36b695 bellard
        sc = audio_calloc (AUDIO_FUNC, 1, sizeof (*sc));
807 ec36b695 bellard
        if (!sc) {
808 8ead62cf bellard
            dolog ("Could not allocate soft capture voice (%zu bytes)\n",
809 ec36b695 bellard
                   sizeof (*sc));
810 8ead62cf bellard
            return -1;
811 8ead62cf bellard
        }
812 8ead62cf bellard
813 ec36b695 bellard
        sc->cap = cap;
814 ec36b695 bellard
        sw = &sc->sw;
815 8ead62cf bellard
        sw->hw = hw_cap;
816 ec36b695 bellard
        sw->info = hw->info;
817 8ead62cf bellard
        sw->empty = 1;
818 8ead62cf bellard
        sw->active = hw->enabled;
819 8ead62cf bellard
        sw->conv = noop_conv;
820 8ead62cf bellard
        sw->ratio = ((int64_t) hw_cap->info.freq << 32) / sw->info.freq;
821 8ead62cf bellard
        sw->rate = st_rate_start (sw->info.freq, hw_cap->info.freq);
822 8ead62cf bellard
        if (!sw->rate) {
823 8ead62cf bellard
            dolog ("Could not start rate conversion for `%s'\n", SW_NAME (sw));
824 8ead62cf bellard
            qemu_free (sw);
825 8ead62cf bellard
            return -1;
826 8ead62cf bellard
        }
827 8ead62cf bellard
        LIST_INSERT_HEAD (&hw_cap->sw_head, sw, entries);
828 ec36b695 bellard
        LIST_INSERT_HEAD (&hw->cap_head, sc, entries);
829 ec36b695 bellard
#ifdef DEBUG_CAPTURE
830 ec36b695 bellard
        asprintf (&sw->name, "for %p %d,%d,%d",
831 ec36b695 bellard
                  hw, sw->info.freq, sw->info.bits, sw->info.nchannels);
832 ec36b695 bellard
        dolog ("Added %s active = %d\n", sw->name, sw->active);
833 ec36b695 bellard
#endif
834 8ead62cf bellard
        if (sw->active) {
835 ec36b695 bellard
            audio_capture_maybe_changed (cap, 1);
836 8ead62cf bellard
        }
837 8ead62cf bellard
    }
838 8ead62cf bellard
    return 0;
839 8ead62cf bellard
}
840 8ead62cf bellard
841 8ead62cf bellard
/*
842 1d14ffa9 bellard
 * Hard voice (capture)
843 1d14ffa9 bellard
 */
844 c0fe3827 bellard
static int audio_pcm_hw_find_min_in (HWVoiceIn *hw)
845 85571bc7 bellard
{
846 1d14ffa9 bellard
    SWVoiceIn *sw;
847 1d14ffa9 bellard
    int m = hw->total_samples_captured;
848 1d14ffa9 bellard
849 1d14ffa9 bellard
    for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
850 1d14ffa9 bellard
        if (sw->active) {
851 1d14ffa9 bellard
            m = audio_MIN (m, sw->total_hw_samples_acquired);
852 1d14ffa9 bellard
        }
853 85571bc7 bellard
    }
854 1d14ffa9 bellard
    return m;
855 85571bc7 bellard
}
856 85571bc7 bellard
857 1d14ffa9 bellard
int audio_pcm_hw_get_live_in (HWVoiceIn *hw)
858 85571bc7 bellard
{
859 1d14ffa9 bellard
    int live = hw->total_samples_captured - audio_pcm_hw_find_min_in (hw);
860 1d14ffa9 bellard
    if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
861 1d14ffa9 bellard
        dolog ("live=%d hw->samples=%d\n", live, hw->samples);
862 1d14ffa9 bellard
        return 0;
863 85571bc7 bellard
    }
864 1d14ffa9 bellard
    return live;
865 85571bc7 bellard
}
866 85571bc7 bellard
867 1d14ffa9 bellard
/*
868 1d14ffa9 bellard
 * Soft voice (capture)
869 1d14ffa9 bellard
 */
870 1d14ffa9 bellard
static int audio_pcm_sw_get_rpos_in (SWVoiceIn *sw)
871 1d14ffa9 bellard
{
872 1d14ffa9 bellard
    HWVoiceIn *hw = sw->hw;
873 1d14ffa9 bellard
    int live = hw->total_samples_captured - sw->total_hw_samples_acquired;
874 1d14ffa9 bellard
    int rpos;
875 1d14ffa9 bellard
876 1d14ffa9 bellard
    if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
877 1d14ffa9 bellard
        dolog ("live=%d hw->samples=%d\n", live, hw->samples);
878 1d14ffa9 bellard
        return 0;
879 1d14ffa9 bellard
    }
880 1d14ffa9 bellard
881 1d14ffa9 bellard
    rpos = hw->wpos - live;
882 1d14ffa9 bellard
    if (rpos >= 0) {
883 1d14ffa9 bellard
        return rpos;
884 85571bc7 bellard
    }
885 85571bc7 bellard
    else {
886 1d14ffa9 bellard
        return hw->samples + rpos;
887 85571bc7 bellard
    }
888 85571bc7 bellard
}
889 85571bc7 bellard
890 1d14ffa9 bellard
int audio_pcm_sw_read (SWVoiceIn *sw, void *buf, int size)
891 85571bc7 bellard
{
892 1d14ffa9 bellard
    HWVoiceIn *hw = sw->hw;
893 1d14ffa9 bellard
    int samples, live, ret = 0, swlim, isamp, osamp, rpos, total = 0;
894 1ea879e5 malc
    struct st_sample *src, *dst = sw->buf;
895 1d14ffa9 bellard
896 1d14ffa9 bellard
    rpos = audio_pcm_sw_get_rpos_in (sw) % hw->samples;
897 1d14ffa9 bellard
898 1d14ffa9 bellard
    live = hw->total_samples_captured - sw->total_hw_samples_acquired;
899 1d14ffa9 bellard
    if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
900 1d14ffa9 bellard
        dolog ("live_in=%d hw->samples=%d\n", live, hw->samples);
901 1d14ffa9 bellard
        return 0;
902 1d14ffa9 bellard
    }
903 1d14ffa9 bellard
904 1d14ffa9 bellard
    samples = size >> sw->info.shift;
905 1d14ffa9 bellard
    if (!live) {
906 1d14ffa9 bellard
        return 0;
907 1d14ffa9 bellard
    }
908 85571bc7 bellard
909 1d14ffa9 bellard
    swlim = (live * sw->ratio) >> 32;
910 1d14ffa9 bellard
    swlim = audio_MIN (swlim, samples);
911 85571bc7 bellard
912 1d14ffa9 bellard
    while (swlim) {
913 1d14ffa9 bellard
        src = hw->conv_buf + rpos;
914 1d14ffa9 bellard
        isamp = hw->wpos - rpos;
915 1d14ffa9 bellard
        /* XXX: <= ? */
916 1d14ffa9 bellard
        if (isamp <= 0) {
917 1d14ffa9 bellard
            isamp = hw->samples - rpos;
918 1d14ffa9 bellard
        }
919 85571bc7 bellard
920 1d14ffa9 bellard
        if (!isamp) {
921 1d14ffa9 bellard
            break;
922 1d14ffa9 bellard
        }
923 1d14ffa9 bellard
        osamp = swlim;
924 85571bc7 bellard
925 1d14ffa9 bellard
        if (audio_bug (AUDIO_FUNC, osamp < 0)) {
926 1d14ffa9 bellard
            dolog ("osamp=%d\n", osamp);
927 c0fe3827 bellard
            return 0;
928 1d14ffa9 bellard
        }
929 85571bc7 bellard
930 1d14ffa9 bellard
        st_rate_flow (sw->rate, src, dst, &isamp, &osamp);
931 1d14ffa9 bellard
        swlim -= osamp;
932 1d14ffa9 bellard
        rpos = (rpos + isamp) % hw->samples;
933 1d14ffa9 bellard
        dst += osamp;
934 1d14ffa9 bellard
        ret += osamp;
935 1d14ffa9 bellard
        total += isamp;
936 1d14ffa9 bellard
    }
937 85571bc7 bellard
938 571ec3d6 bellard
    sw->clip (buf, sw->buf, ret);
939 1d14ffa9 bellard
    sw->total_hw_samples_acquired += total;
940 1d14ffa9 bellard
    return ret << sw->info.shift;
941 85571bc7 bellard
}
942 85571bc7 bellard
943 1d14ffa9 bellard
/*
944 1d14ffa9 bellard
 * Hard voice (playback)
945 1d14ffa9 bellard
 */
946 c0fe3827 bellard
static int audio_pcm_hw_find_min_out (HWVoiceOut *hw, int *nb_livep)
947 1d14ffa9 bellard
{
948 c0fe3827 bellard
    SWVoiceOut *sw;
949 c0fe3827 bellard
    int m = INT_MAX;
950 c0fe3827 bellard
    int nb_live = 0;
951 85571bc7 bellard
952 c0fe3827 bellard
    for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
953 c0fe3827 bellard
        if (sw->active || !sw->empty) {
954 c0fe3827 bellard
            m = audio_MIN (m, sw->total_hw_samples_mixed);
955 c0fe3827 bellard
            nb_live += 1;
956 c0fe3827 bellard
        }
957 85571bc7 bellard
    }
958 c0fe3827 bellard
959 c0fe3827 bellard
    *nb_livep = nb_live;
960 c0fe3827 bellard
    return m;
961 1d14ffa9 bellard
}
962 85571bc7 bellard
963 1d14ffa9 bellard
int audio_pcm_hw_get_live_out2 (HWVoiceOut *hw, int *nb_live)
964 1d14ffa9 bellard
{
965 1d14ffa9 bellard
    int smin;
966 85571bc7 bellard
967 1d14ffa9 bellard
    smin = audio_pcm_hw_find_min_out (hw, nb_live);
968 1d14ffa9 bellard
969 1d14ffa9 bellard
    if (!*nb_live) {
970 1d14ffa9 bellard
        return 0;
971 85571bc7 bellard
    }
972 85571bc7 bellard
    else {
973 1d14ffa9 bellard
        int live = smin;
974 1d14ffa9 bellard
975 1d14ffa9 bellard
        if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
976 1d14ffa9 bellard
            dolog ("live=%d hw->samples=%d\n", live, hw->samples);
977 1d14ffa9 bellard
            return 0;
978 85571bc7 bellard
        }
979 1d14ffa9 bellard
        return live;
980 85571bc7 bellard
    }
981 1d14ffa9 bellard
}
982 1d14ffa9 bellard
983 1d14ffa9 bellard
int audio_pcm_hw_get_live_out (HWVoiceOut *hw)
984 1d14ffa9 bellard
{
985 1d14ffa9 bellard
    int nb_live;
986 1d14ffa9 bellard
    int live;
987 85571bc7 bellard
988 1d14ffa9 bellard
    live = audio_pcm_hw_get_live_out2 (hw, &nb_live);
989 1d14ffa9 bellard
    if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
990 1d14ffa9 bellard
        dolog ("live=%d hw->samples=%d\n", live, hw->samples);
991 1d14ffa9 bellard
        return 0;
992 85571bc7 bellard
    }
993 1d14ffa9 bellard
    return live;
994 85571bc7 bellard
}
995 85571bc7 bellard
996 1d14ffa9 bellard
/*
997 1d14ffa9 bellard
 * Soft voice (playback)
998 1d14ffa9 bellard
 */
999 1d14ffa9 bellard
int audio_pcm_sw_write (SWVoiceOut *sw, void *buf, int size)
1000 85571bc7 bellard
{
1001 1d14ffa9 bellard
    int hwsamples, samples, isamp, osamp, wpos, live, dead, left, swlim, blck;
1002 1d14ffa9 bellard
    int ret = 0, pos = 0, total = 0;
1003 85571bc7 bellard
1004 1d14ffa9 bellard
    if (!sw) {
1005 1d14ffa9 bellard
        return size;
1006 1d14ffa9 bellard
    }
1007 85571bc7 bellard
1008 1d14ffa9 bellard
    hwsamples = sw->hw->samples;
1009 85571bc7 bellard
1010 1d14ffa9 bellard
    live = sw->total_hw_samples_mixed;
1011 1d14ffa9 bellard
    if (audio_bug (AUDIO_FUNC, live < 0 || live > hwsamples)){
1012 1d14ffa9 bellard
        dolog ("live=%d hw->samples=%d\n", live, hwsamples);
1013 1d14ffa9 bellard
        return 0;
1014 1d14ffa9 bellard
    }
1015 85571bc7 bellard
1016 1d14ffa9 bellard
    if (live == hwsamples) {
1017 ec36b695 bellard
#ifdef DEBUG_OUT
1018 ec36b695 bellard
        dolog ("%s is full %d\n", sw->name, live);
1019 ec36b695 bellard
#endif
1020 1d14ffa9 bellard
        return 0;
1021 1d14ffa9 bellard
    }
1022 85571bc7 bellard
1023 1d14ffa9 bellard
    wpos = (sw->hw->rpos + live) % hwsamples;
1024 1d14ffa9 bellard
    samples = size >> sw->info.shift;
1025 85571bc7 bellard
1026 1d14ffa9 bellard
    dead = hwsamples - live;
1027 1d14ffa9 bellard
    swlim = ((int64_t) dead << 32) / sw->ratio;
1028 1d14ffa9 bellard
    swlim = audio_MIN (swlim, samples);
1029 1d14ffa9 bellard
    if (swlim) {
1030 1d14ffa9 bellard
        sw->conv (sw->buf, buf, swlim, &sw->vol);
1031 1d14ffa9 bellard
    }
1032 1d14ffa9 bellard
1033 1d14ffa9 bellard
    while (swlim) {
1034 1d14ffa9 bellard
        dead = hwsamples - live;
1035 1d14ffa9 bellard
        left = hwsamples - wpos;
1036 1d14ffa9 bellard
        blck = audio_MIN (dead, left);
1037 1d14ffa9 bellard
        if (!blck) {
1038 1d14ffa9 bellard
            break;
1039 1d14ffa9 bellard
        }
1040 1d14ffa9 bellard
        isamp = swlim;
1041 1d14ffa9 bellard
        osamp = blck;
1042 1d14ffa9 bellard
        st_rate_flow_mix (
1043 1d14ffa9 bellard
            sw->rate,
1044 1d14ffa9 bellard
            sw->buf + pos,
1045 1d14ffa9 bellard
            sw->hw->mix_buf + wpos,
1046 1d14ffa9 bellard
            &isamp,
1047 1d14ffa9 bellard
            &osamp
1048 1d14ffa9 bellard
            );
1049 1d14ffa9 bellard
        ret += isamp;
1050 1d14ffa9 bellard
        swlim -= isamp;
1051 1d14ffa9 bellard
        pos += isamp;
1052 1d14ffa9 bellard
        live += osamp;
1053 1d14ffa9 bellard
        wpos = (wpos + osamp) % hwsamples;
1054 1d14ffa9 bellard
        total += osamp;
1055 1d14ffa9 bellard
    }
1056 1d14ffa9 bellard
1057 1d14ffa9 bellard
    sw->total_hw_samples_mixed += total;
1058 1d14ffa9 bellard
    sw->empty = sw->total_hw_samples_mixed == 0;
1059 1d14ffa9 bellard
1060 1d14ffa9 bellard
#ifdef DEBUG_OUT
1061 1d14ffa9 bellard
    dolog (
1062 c0fe3827 bellard
        "%s: write size %d ret %d total sw %d\n",
1063 c0fe3827 bellard
        SW_NAME (sw),
1064 1d14ffa9 bellard
        size >> sw->info.shift,
1065 1d14ffa9 bellard
        ret,
1066 c0fe3827 bellard
        sw->total_hw_samples_mixed
1067 1d14ffa9 bellard
        );
1068 1d14ffa9 bellard
#endif
1069 1d14ffa9 bellard
1070 1d14ffa9 bellard
    return ret << sw->info.shift;
1071 85571bc7 bellard
}
1072 85571bc7 bellard
1073 1d14ffa9 bellard
#ifdef DEBUG_AUDIO
1074 1d14ffa9 bellard
static void audio_pcm_print_info (const char *cap, struct audio_pcm_info *info)
1075 85571bc7 bellard
{
1076 1d14ffa9 bellard
    dolog ("%s: bits %d, sign %d, freq %d, nchan %d\n",
1077 1d14ffa9 bellard
           cap, info->bits, info->sign, info->freq, info->nchannels);
1078 85571bc7 bellard
}
1079 1d14ffa9 bellard
#endif
1080 85571bc7 bellard
1081 1d14ffa9 bellard
#define DAC
1082 1d14ffa9 bellard
#include "audio_template.h"
1083 1d14ffa9 bellard
#undef DAC
1084 1d14ffa9 bellard
#include "audio_template.h"
1085 1d14ffa9 bellard
1086 1d14ffa9 bellard
int AUD_write (SWVoiceOut *sw, void *buf, int size)
1087 85571bc7 bellard
{
1088 1d14ffa9 bellard
    int bytes;
1089 85571bc7 bellard
1090 1d14ffa9 bellard
    if (!sw) {
1091 1d14ffa9 bellard
        /* XXX: Consider options */
1092 1d14ffa9 bellard
        return size;
1093 1d14ffa9 bellard
    }
1094 85571bc7 bellard
1095 1d14ffa9 bellard
    if (!sw->hw->enabled) {
1096 c0fe3827 bellard
        dolog ("Writing to disabled voice %s\n", SW_NAME (sw));
1097 85571bc7 bellard
        return 0;
1098 85571bc7 bellard
    }
1099 85571bc7 bellard
1100 1d14ffa9 bellard
    bytes = sw->hw->pcm_ops->write (sw, buf, size);
1101 1d14ffa9 bellard
    return bytes;
1102 1d14ffa9 bellard
}
1103 1d14ffa9 bellard
1104 1d14ffa9 bellard
int AUD_read (SWVoiceIn *sw, void *buf, int size)
1105 1d14ffa9 bellard
{
1106 1d14ffa9 bellard
    int bytes;
1107 85571bc7 bellard
1108 1d14ffa9 bellard
    if (!sw) {
1109 1d14ffa9 bellard
        /* XXX: Consider options */
1110 1d14ffa9 bellard
        return size;
1111 85571bc7 bellard
    }
1112 1d14ffa9 bellard
1113 1d14ffa9 bellard
    if (!sw->hw->enabled) {
1114 c0fe3827 bellard
        dolog ("Reading from disabled voice %s\n", SW_NAME (sw));
1115 1d14ffa9 bellard
        return 0;
1116 85571bc7 bellard
    }
1117 1d14ffa9 bellard
1118 1d14ffa9 bellard
    bytes = sw->hw->pcm_ops->read (sw, buf, size);
1119 1d14ffa9 bellard
    return bytes;
1120 85571bc7 bellard
}
1121 85571bc7 bellard
1122 1d14ffa9 bellard
int AUD_get_buffer_size_out (SWVoiceOut *sw)
1123 85571bc7 bellard
{
1124 c0fe3827 bellard
    return sw->hw->samples << sw->hw->info.shift;
1125 1d14ffa9 bellard
}
1126 1d14ffa9 bellard
1127 1d14ffa9 bellard
void AUD_set_active_out (SWVoiceOut *sw, int on)
1128 1d14ffa9 bellard
{
1129 1d14ffa9 bellard
    HWVoiceOut *hw;
1130 85571bc7 bellard
1131 1d14ffa9 bellard
    if (!sw) {
1132 85571bc7 bellard
        return;
1133 1d14ffa9 bellard
    }
1134 85571bc7 bellard
1135 85571bc7 bellard
    hw = sw->hw;
1136 1d14ffa9 bellard
    if (sw->active != on) {
1137 1d14ffa9 bellard
        SWVoiceOut *temp_sw;
1138 ec36b695 bellard
        SWVoiceCap *sc;
1139 1d14ffa9 bellard
1140 1d14ffa9 bellard
        if (on) {
1141 1d14ffa9 bellard
            hw->pending_disable = 0;
1142 1d14ffa9 bellard
            if (!hw->enabled) {
1143 1d14ffa9 bellard
                hw->enabled = 1;
1144 1d14ffa9 bellard
                hw->pcm_ops->ctl_out (hw, VOICE_ENABLE);
1145 1d14ffa9 bellard
            }
1146 1d14ffa9 bellard
        }
1147 1d14ffa9 bellard
        else {
1148 1d14ffa9 bellard
            if (hw->enabled) {
1149 1d14ffa9 bellard
                int nb_active = 0;
1150 1d14ffa9 bellard
1151 1d14ffa9 bellard
                for (temp_sw = hw->sw_head.lh_first; temp_sw;
1152 1d14ffa9 bellard
                     temp_sw = temp_sw->entries.le_next) {
1153 1d14ffa9 bellard
                    nb_active += temp_sw->active != 0;
1154 1d14ffa9 bellard
                }
1155 1d14ffa9 bellard
1156 1d14ffa9 bellard
                hw->pending_disable = nb_active == 1;
1157 1d14ffa9 bellard
            }
1158 85571bc7 bellard
        }
1159 ec36b695 bellard
1160 ec36b695 bellard
        for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) {
1161 ec36b695 bellard
            sc->sw.active = hw->enabled;
1162 8ead62cf bellard
            if (hw->enabled) {
1163 ec36b695 bellard
                audio_capture_maybe_changed (sc->cap, 1);
1164 8ead62cf bellard
            }
1165 8ead62cf bellard
        }
1166 1d14ffa9 bellard
        sw->active = on;
1167 1d14ffa9 bellard
    }
1168 1d14ffa9 bellard
}
1169 1d14ffa9 bellard
1170 1d14ffa9 bellard
void AUD_set_active_in (SWVoiceIn *sw, int on)
1171 1d14ffa9 bellard
{
1172 1d14ffa9 bellard
    HWVoiceIn *hw;
1173 1d14ffa9 bellard
1174 1d14ffa9 bellard
    if (!sw) {
1175 1d14ffa9 bellard
        return;
1176 85571bc7 bellard
    }
1177 85571bc7 bellard
1178 1d14ffa9 bellard
    hw = sw->hw;
1179 85571bc7 bellard
    if (sw->active != on) {
1180 1d14ffa9 bellard
        SWVoiceIn *temp_sw;
1181 1d14ffa9 bellard
1182 85571bc7 bellard
        if (on) {
1183 85571bc7 bellard
            if (!hw->enabled) {
1184 85571bc7 bellard
                hw->enabled = 1;
1185 1d14ffa9 bellard
                hw->pcm_ops->ctl_in (hw, VOICE_ENABLE);
1186 85571bc7 bellard
            }
1187 1d14ffa9 bellard
            sw->total_hw_samples_acquired = hw->total_samples_captured;
1188 85571bc7 bellard
        }
1189 85571bc7 bellard
        else {
1190 1d14ffa9 bellard
            if (hw->enabled) {
1191 85571bc7 bellard
                int nb_active = 0;
1192 1d14ffa9 bellard
1193 1d14ffa9 bellard
                for (temp_sw = hw->sw_head.lh_first; temp_sw;
1194 1d14ffa9 bellard
                     temp_sw = temp_sw->entries.le_next) {
1195 1d14ffa9 bellard
                    nb_active += temp_sw->active != 0;
1196 85571bc7 bellard
                }
1197 85571bc7 bellard
1198 85571bc7 bellard
                if (nb_active == 1) {
1199 1d14ffa9 bellard
                    hw->enabled = 0;
1200 1d14ffa9 bellard
                    hw->pcm_ops->ctl_in (hw, VOICE_DISABLE);
1201 85571bc7 bellard
                }
1202 85571bc7 bellard
            }
1203 85571bc7 bellard
        }
1204 85571bc7 bellard
        sw->active = on;
1205 85571bc7 bellard
    }
1206 85571bc7 bellard
}
1207 85571bc7 bellard
1208 1d14ffa9 bellard
static int audio_get_avail (SWVoiceIn *sw)
1209 1d14ffa9 bellard
{
1210 1d14ffa9 bellard
    int live;
1211 1d14ffa9 bellard
1212 1d14ffa9 bellard
    if (!sw) {
1213 1d14ffa9 bellard
        return 0;
1214 1d14ffa9 bellard
    }
1215 1d14ffa9 bellard
1216 1d14ffa9 bellard
    live = sw->hw->total_samples_captured - sw->total_hw_samples_acquired;
1217 1d14ffa9 bellard
    if (audio_bug (AUDIO_FUNC, live < 0 || live > sw->hw->samples)) {
1218 1d14ffa9 bellard
        dolog ("live=%d sw->hw->samples=%d\n", live, sw->hw->samples);
1219 1d14ffa9 bellard
        return 0;
1220 1d14ffa9 bellard
    }
1221 1d14ffa9 bellard
1222 1d14ffa9 bellard
    ldebug (
1223 26a76461 bellard
        "%s: get_avail live %d ret %" PRId64 "\n",
1224 c0fe3827 bellard
        SW_NAME (sw),
1225 1d14ffa9 bellard
        live, (((int64_t) live << 32) / sw->ratio) << sw->info.shift
1226 1d14ffa9 bellard
        );
1227 1d14ffa9 bellard
1228 1d14ffa9 bellard
    return (((int64_t) live << 32) / sw->ratio) << sw->info.shift;
1229 1d14ffa9 bellard
}
1230 1d14ffa9 bellard
1231 1d14ffa9 bellard
static int audio_get_free (SWVoiceOut *sw)
1232 1d14ffa9 bellard
{
1233 1d14ffa9 bellard
    int live, dead;
1234 1d14ffa9 bellard
1235 1d14ffa9 bellard
    if (!sw) {
1236 1d14ffa9 bellard
        return 0;
1237 1d14ffa9 bellard
    }
1238 1d14ffa9 bellard
1239 1d14ffa9 bellard
    live = sw->total_hw_samples_mixed;
1240 1d14ffa9 bellard
1241 1d14ffa9 bellard
    if (audio_bug (AUDIO_FUNC, live < 0 || live > sw->hw->samples)) {
1242 1d14ffa9 bellard
        dolog ("live=%d sw->hw->samples=%d\n", live, sw->hw->samples);
1243 c0fe3827 bellard
        return 0;
1244 1d14ffa9 bellard
    }
1245 1d14ffa9 bellard
1246 1d14ffa9 bellard
    dead = sw->hw->samples - live;
1247 1d14ffa9 bellard
1248 1d14ffa9 bellard
#ifdef DEBUG_OUT
1249 26a76461 bellard
    dolog ("%s: get_free live %d dead %d ret %" PRId64 "\n",
1250 c0fe3827 bellard
           SW_NAME (sw),
1251 1d14ffa9 bellard
           live, dead, (((int64_t) dead << 32) / sw->ratio) << sw->info.shift);
1252 85571bc7 bellard
#endif
1253 1d14ffa9 bellard
1254 1d14ffa9 bellard
    return (((int64_t) dead << 32) / sw->ratio) << sw->info.shift;
1255 1d14ffa9 bellard
}
1256 1d14ffa9 bellard
1257 8ead62cf bellard
static void audio_capture_mix_and_clear (HWVoiceOut *hw, int rpos, int samples)
1258 8ead62cf bellard
{
1259 8ead62cf bellard
    int n;
1260 8ead62cf bellard
1261 8ead62cf bellard
    if (hw->enabled) {
1262 ec36b695 bellard
        SWVoiceCap *sc;
1263 8ead62cf bellard
1264 ec36b695 bellard
        for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) {
1265 ec36b695 bellard
            SWVoiceOut *sw = &sc->sw;
1266 8ead62cf bellard
            int rpos2 = rpos;
1267 8ead62cf bellard
1268 8ead62cf bellard
            n = samples;
1269 8ead62cf bellard
            while (n) {
1270 8ead62cf bellard
                int till_end_of_hw = hw->samples - rpos2;
1271 8ead62cf bellard
                int to_write = audio_MIN (till_end_of_hw, n);
1272 8ead62cf bellard
                int bytes = to_write << hw->info.shift;
1273 8ead62cf bellard
                int written;
1274 8ead62cf bellard
1275 8ead62cf bellard
                sw->buf = hw->mix_buf + rpos2;
1276 8ead62cf bellard
                written = audio_pcm_sw_write (sw, NULL, bytes);
1277 8ead62cf bellard
                if (written - bytes) {
1278 ec36b695 bellard
                    dolog ("Could not mix %d bytes into a capture "
1279 ec36b695 bellard
                           "buffer, mixed %d\n",
1280 ec36b695 bellard
                           bytes, written);
1281 8ead62cf bellard
                    break;
1282 8ead62cf bellard
                }
1283 8ead62cf bellard
                n -= to_write;
1284 8ead62cf bellard
                rpos2 = (rpos2 + to_write) % hw->samples;
1285 8ead62cf bellard
            }
1286 8ead62cf bellard
        }
1287 8ead62cf bellard
    }
1288 8ead62cf bellard
1289 8ead62cf bellard
    n = audio_MIN (samples, hw->samples - rpos);
1290 8ead62cf bellard
    mixeng_clear (hw->mix_buf + rpos, n);
1291 8ead62cf bellard
    mixeng_clear (hw->mix_buf, samples - n);
1292 8ead62cf bellard
}
1293 8ead62cf bellard
1294 c0fe3827 bellard
static void audio_run_out (AudioState *s)
1295 1d14ffa9 bellard
{
1296 1d14ffa9 bellard
    HWVoiceOut *hw = NULL;
1297 1d14ffa9 bellard
    SWVoiceOut *sw;
1298 1d14ffa9 bellard
1299 c0fe3827 bellard
    while ((hw = audio_pcm_hw_find_any_enabled_out (s, hw))) {
1300 1d14ffa9 bellard
        int played;
1301 8ead62cf bellard
        int live, free, nb_live, cleanup_required, prev_rpos;
1302 1d14ffa9 bellard
1303 1d14ffa9 bellard
        live = audio_pcm_hw_get_live_out2 (hw, &nb_live);
1304 1d14ffa9 bellard
        if (!nb_live) {
1305 1d14ffa9 bellard
            live = 0;
1306 1d14ffa9 bellard
        }
1307 c0fe3827 bellard
1308 1d14ffa9 bellard
        if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
1309 1d14ffa9 bellard
            dolog ("live=%d hw->samples=%d\n", live, hw->samples);
1310 c0fe3827 bellard
            continue;
1311 1d14ffa9 bellard
        }
1312 1d14ffa9 bellard
1313 1d14ffa9 bellard
        if (hw->pending_disable && !nb_live) {
1314 ec36b695 bellard
            SWVoiceCap *sc;
1315 1d14ffa9 bellard
#ifdef DEBUG_OUT
1316 1d14ffa9 bellard
            dolog ("Disabling voice\n");
1317 85571bc7 bellard
#endif
1318 1d14ffa9 bellard
            hw->enabled = 0;
1319 1d14ffa9 bellard
            hw->pending_disable = 0;
1320 1d14ffa9 bellard
            hw->pcm_ops->ctl_out (hw, VOICE_DISABLE);
1321 ec36b695 bellard
            for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) {
1322 ec36b695 bellard
                sc->sw.active = 0;
1323 ec36b695 bellard
                audio_recalc_and_notify_capture (sc->cap);
1324 8ead62cf bellard
            }
1325 1d14ffa9 bellard
            continue;
1326 1d14ffa9 bellard
        }
1327 1d14ffa9 bellard
1328 1d14ffa9 bellard
        if (!live) {
1329 1d14ffa9 bellard
            for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1330 1d14ffa9 bellard
                if (sw->active) {
1331 1d14ffa9 bellard
                    free = audio_get_free (sw);
1332 1d14ffa9 bellard
                    if (free > 0) {
1333 1d14ffa9 bellard
                        sw->callback.fn (sw->callback.opaque, free);
1334 1d14ffa9 bellard
                    }
1335 1d14ffa9 bellard
                }
1336 1d14ffa9 bellard
            }
1337 1d14ffa9 bellard
            continue;
1338 1d14ffa9 bellard
        }
1339 1d14ffa9 bellard
1340 8ead62cf bellard
        prev_rpos = hw->rpos;
1341 1d14ffa9 bellard
        played = hw->pcm_ops->run_out (hw);
1342 1d14ffa9 bellard
        if (audio_bug (AUDIO_FUNC, hw->rpos >= hw->samples)) {
1343 1d14ffa9 bellard
            dolog ("hw->rpos=%d hw->samples=%d played=%d\n",
1344 1d14ffa9 bellard
                   hw->rpos, hw->samples, played);
1345 1d14ffa9 bellard
            hw->rpos = 0;
1346 1d14ffa9 bellard
        }
1347 1d14ffa9 bellard
1348 1d14ffa9 bellard
#ifdef DEBUG_OUT
1349 c0fe3827 bellard
        dolog ("played=%d\n", played);
1350 85571bc7 bellard
#endif
1351 1d14ffa9 bellard
1352 1d14ffa9 bellard
        if (played) {
1353 1d14ffa9 bellard
            hw->ts_helper += played;
1354 8ead62cf bellard
            audio_capture_mix_and_clear (hw, prev_rpos, played);
1355 1d14ffa9 bellard
        }
1356 1d14ffa9 bellard
1357 c0fe3827 bellard
        cleanup_required = 0;
1358 1d14ffa9 bellard
        for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1359 1d14ffa9 bellard
            if (!sw->active && sw->empty) {
1360 1d14ffa9 bellard
                continue;
1361 1d14ffa9 bellard
            }
1362 1d14ffa9 bellard
1363 1d14ffa9 bellard
            if (audio_bug (AUDIO_FUNC, played > sw->total_hw_samples_mixed)) {
1364 1d14ffa9 bellard
                dolog ("played=%d sw->total_hw_samples_mixed=%d\n",
1365 1d14ffa9 bellard
                       played, sw->total_hw_samples_mixed);
1366 1d14ffa9 bellard
                played = sw->total_hw_samples_mixed;
1367 1d14ffa9 bellard
            }
1368 1d14ffa9 bellard
1369 1d14ffa9 bellard
            sw->total_hw_samples_mixed -= played;
1370 1d14ffa9 bellard
1371 1d14ffa9 bellard
            if (!sw->total_hw_samples_mixed) {
1372 1d14ffa9 bellard
                sw->empty = 1;
1373 c0fe3827 bellard
                cleanup_required |= !sw->active && !sw->callback.fn;
1374 1d14ffa9 bellard
            }
1375 1d14ffa9 bellard
1376 1d14ffa9 bellard
            if (sw->active) {
1377 1d14ffa9 bellard
                free = audio_get_free (sw);
1378 1d14ffa9 bellard
                if (free > 0) {
1379 1d14ffa9 bellard
                    sw->callback.fn (sw->callback.opaque, free);
1380 1d14ffa9 bellard
                }
1381 1d14ffa9 bellard
            }
1382 1d14ffa9 bellard
        }
1383 c0fe3827 bellard
1384 c0fe3827 bellard
        if (cleanup_required) {
1385 ec36b695 bellard
            SWVoiceOut *sw1;
1386 ec36b695 bellard
1387 ec36b695 bellard
            sw = hw->sw_head.lh_first;
1388 ec36b695 bellard
            while (sw) {
1389 ec36b695 bellard
                sw1 = sw->entries.le_next;
1390 c0fe3827 bellard
                if (!sw->active && !sw->callback.fn) {
1391 c0fe3827 bellard
#ifdef DEBUG_PLIVE
1392 c0fe3827 bellard
                    dolog ("Finishing with old voice\n");
1393 c0fe3827 bellard
#endif
1394 c0fe3827 bellard
                    audio_close_out (s, sw);
1395 c0fe3827 bellard
                }
1396 ec36b695 bellard
                sw = sw1;
1397 c0fe3827 bellard
            }
1398 c0fe3827 bellard
        }
1399 1d14ffa9 bellard
    }
1400 1d14ffa9 bellard
}
1401 1d14ffa9 bellard
1402 c0fe3827 bellard
static void audio_run_in (AudioState *s)
1403 1d14ffa9 bellard
{
1404 1d14ffa9 bellard
    HWVoiceIn *hw = NULL;
1405 1d14ffa9 bellard
1406 c0fe3827 bellard
    while ((hw = audio_pcm_hw_find_any_enabled_in (s, hw))) {
1407 1d14ffa9 bellard
        SWVoiceIn *sw;
1408 1d14ffa9 bellard
        int captured, min;
1409 1d14ffa9 bellard
1410 1d14ffa9 bellard
        captured = hw->pcm_ops->run_in (hw);
1411 1d14ffa9 bellard
1412 1d14ffa9 bellard
        min = audio_pcm_hw_find_min_in (hw);
1413 1d14ffa9 bellard
        hw->total_samples_captured += captured - min;
1414 1d14ffa9 bellard
        hw->ts_helper += captured;
1415 1d14ffa9 bellard
1416 1d14ffa9 bellard
        for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1417 1d14ffa9 bellard
            sw->total_hw_samples_acquired -= min;
1418 1d14ffa9 bellard
1419 1d14ffa9 bellard
            if (sw->active) {
1420 1d14ffa9 bellard
                int avail;
1421 1d14ffa9 bellard
1422 1d14ffa9 bellard
                avail = audio_get_avail (sw);
1423 1d14ffa9 bellard
                if (avail > 0) {
1424 1d14ffa9 bellard
                    sw->callback.fn (sw->callback.opaque, avail);
1425 1d14ffa9 bellard
                }
1426 1d14ffa9 bellard
            }
1427 1d14ffa9 bellard
        }
1428 1d14ffa9 bellard
    }
1429 1d14ffa9 bellard
}
1430 1d14ffa9 bellard
1431 8ead62cf bellard
static void audio_run_capture (AudioState *s)
1432 8ead62cf bellard
{
1433 8ead62cf bellard
    CaptureVoiceOut *cap;
1434 8ead62cf bellard
1435 8ead62cf bellard
    for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) {
1436 8ead62cf bellard
        int live, rpos, captured;
1437 8ead62cf bellard
        HWVoiceOut *hw = &cap->hw;
1438 8ead62cf bellard
        SWVoiceOut *sw;
1439 8ead62cf bellard
1440 8ead62cf bellard
        captured = live = audio_pcm_hw_get_live_out (hw);
1441 8ead62cf bellard
        rpos = hw->rpos;
1442 8ead62cf bellard
        while (live) {
1443 8ead62cf bellard
            int left = hw->samples - rpos;
1444 8ead62cf bellard
            int to_capture = audio_MIN (live, left);
1445 1ea879e5 malc
            struct st_sample *src;
1446 8ead62cf bellard
            struct capture_callback *cb;
1447 8ead62cf bellard
1448 8ead62cf bellard
            src = hw->mix_buf + rpos;
1449 8ead62cf bellard
            hw->clip (cap->buf, src, to_capture);
1450 8ead62cf bellard
            mixeng_clear (src, to_capture);
1451 8ead62cf bellard
1452 8ead62cf bellard
            for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
1453 8ead62cf bellard
                cb->ops.capture (cb->opaque, cap->buf,
1454 8ead62cf bellard
                                 to_capture << hw->info.shift);
1455 8ead62cf bellard
            }
1456 8ead62cf bellard
            rpos = (rpos + to_capture) % hw->samples;
1457 8ead62cf bellard
            live -= to_capture;
1458 8ead62cf bellard
        }
1459 8ead62cf bellard
        hw->rpos = rpos;
1460 8ead62cf bellard
1461 8ead62cf bellard
        for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1462 8ead62cf bellard
            if (!sw->active && sw->empty) {
1463 8ead62cf bellard
                continue;
1464 8ead62cf bellard
            }
1465 8ead62cf bellard
1466 8ead62cf bellard
            if (audio_bug (AUDIO_FUNC, captured > sw->total_hw_samples_mixed)) {
1467 8ead62cf bellard
                dolog ("captured=%d sw->total_hw_samples_mixed=%d\n",
1468 8ead62cf bellard
                       captured, sw->total_hw_samples_mixed);
1469 8ead62cf bellard
                captured = sw->total_hw_samples_mixed;
1470 8ead62cf bellard
            }
1471 8ead62cf bellard
1472 8ead62cf bellard
            sw->total_hw_samples_mixed -= captured;
1473 8ead62cf bellard
            sw->empty = sw->total_hw_samples_mixed == 0;
1474 8ead62cf bellard
        }
1475 8ead62cf bellard
    }
1476 8ead62cf bellard
}
1477 8ead62cf bellard
1478 571ec3d6 bellard
static void audio_timer (void *opaque)
1479 571ec3d6 bellard
{
1480 571ec3d6 bellard
    AudioState *s = opaque;
1481 571ec3d6 bellard
1482 571ec3d6 bellard
    audio_run_out (s);
1483 571ec3d6 bellard
    audio_run_in (s);
1484 8ead62cf bellard
    audio_run_capture (s);
1485 571ec3d6 bellard
1486 571ec3d6 bellard
    qemu_mod_timer (s->ts, qemu_get_clock (vm_clock) + conf.period.ticks);
1487 571ec3d6 bellard
}
1488 571ec3d6 bellard
1489 1d14ffa9 bellard
static struct audio_option audio_options[] = {
1490 1d14ffa9 bellard
    /* DAC */
1491 c0fe3827 bellard
    {"DAC_FIXED_SETTINGS", AUD_OPT_BOOL, &conf.fixed_out.enabled,
1492 1d14ffa9 bellard
     "Use fixed settings for host DAC", NULL, 0},
1493 1d14ffa9 bellard
1494 c0fe3827 bellard
    {"DAC_FIXED_FREQ", AUD_OPT_INT, &conf.fixed_out.settings.freq,
1495 1d14ffa9 bellard
     "Frequency for fixed host DAC", NULL, 0},
1496 1d14ffa9 bellard
1497 c0fe3827 bellard
    {"DAC_FIXED_FMT", AUD_OPT_FMT, &conf.fixed_out.settings.fmt,
1498 1d14ffa9 bellard
     "Format for fixed host DAC", NULL, 0},
1499 1d14ffa9 bellard
1500 c0fe3827 bellard
    {"DAC_FIXED_CHANNELS", AUD_OPT_INT, &conf.fixed_out.settings.nchannels,
1501 1d14ffa9 bellard
     "Number of channels for fixed DAC (1 - mono, 2 - stereo)", NULL, 0},
1502 1d14ffa9 bellard
1503 c0fe3827 bellard
    {"DAC_VOICES", AUD_OPT_INT, &conf.fixed_out.nb_voices,
1504 1d14ffa9 bellard
     "Number of voices for DAC", NULL, 0},
1505 1d14ffa9 bellard
1506 1d14ffa9 bellard
    /* ADC */
1507 c0fe3827 bellard
    {"ADC_FIXED_SETTINGS", AUD_OPT_BOOL, &conf.fixed_in.enabled,
1508 1d14ffa9 bellard
     "Use fixed settings for host ADC", NULL, 0},
1509 1d14ffa9 bellard
1510 c0fe3827 bellard
    {"ADC_FIXED_FREQ", AUD_OPT_INT, &conf.fixed_in.settings.freq,
1511 c0fe3827 bellard
     "Frequency for fixed host ADC", NULL, 0},
1512 1d14ffa9 bellard
1513 c0fe3827 bellard
    {"ADC_FIXED_FMT", AUD_OPT_FMT, &conf.fixed_in.settings.fmt,
1514 c0fe3827 bellard
     "Format for fixed host ADC", NULL, 0},
1515 1d14ffa9 bellard
1516 c0fe3827 bellard
    {"ADC_FIXED_CHANNELS", AUD_OPT_INT, &conf.fixed_in.settings.nchannels,
1517 1d14ffa9 bellard
     "Number of channels for fixed ADC (1 - mono, 2 - stereo)", NULL, 0},
1518 1d14ffa9 bellard
1519 c0fe3827 bellard
    {"ADC_VOICES", AUD_OPT_INT, &conf.fixed_in.nb_voices,
1520 1d14ffa9 bellard
     "Number of voices for ADC", NULL, 0},
1521 1d14ffa9 bellard
1522 1d14ffa9 bellard
    /* Misc */
1523 c310de86 malc
    {"TIMER_PERIOD", AUD_OPT_INT, &conf.period.hertz,
1524 c0fe3827 bellard
     "Timer period in HZ (0 - use lowest possible)", NULL, 0},
1525 1d14ffa9 bellard
1526 c0fe3827 bellard
    {"PLIVE", AUD_OPT_BOOL, &conf.plive,
1527 1d14ffa9 bellard
     "(undocumented)", NULL, 0},
1528 1d14ffa9 bellard
1529 541e0844 bellard
    {"LOG_TO_MONITOR", AUD_OPT_BOOL, &conf.log_to_monitor,
1530 b60aee00 aurel32
     "print logging messages to monitor instead of stderr", NULL, 0},
1531 541e0844 bellard
1532 1d14ffa9 bellard
    {NULL, 0, NULL, NULL, NULL, 0}
1533 85571bc7 bellard
};
1534 85571bc7 bellard
1535 571ec3d6 bellard
static void audio_pp_nb_voices (const char *typ, int nb)
1536 571ec3d6 bellard
{
1537 571ec3d6 bellard
    switch (nb) {
1538 571ec3d6 bellard
    case 0:
1539 571ec3d6 bellard
        printf ("Does not support %s\n", typ);
1540 571ec3d6 bellard
        break;
1541 571ec3d6 bellard
    case 1:
1542 571ec3d6 bellard
        printf ("One %s voice\n", typ);
1543 571ec3d6 bellard
        break;
1544 571ec3d6 bellard
    case INT_MAX:
1545 571ec3d6 bellard
        printf ("Theoretically supports many %s voices\n", typ);
1546 571ec3d6 bellard
        break;
1547 571ec3d6 bellard
    default:
1548 571ec3d6 bellard
        printf ("Theoretically supports upto %d %s voices\n", nb, typ);
1549 571ec3d6 bellard
        break;
1550 571ec3d6 bellard
    }
1551 571ec3d6 bellard
1552 571ec3d6 bellard
}
1553 571ec3d6 bellard
1554 1d14ffa9 bellard
void AUD_help (void)
1555 1d14ffa9 bellard
{
1556 1d14ffa9 bellard
    size_t i;
1557 1d14ffa9 bellard
1558 1d14ffa9 bellard
    audio_process_options ("AUDIO", audio_options);
1559 1d14ffa9 bellard
    for (i = 0; i < sizeof (drvtab) / sizeof (drvtab[0]); i++) {
1560 1d14ffa9 bellard
        struct audio_driver *d = drvtab[i];
1561 1d14ffa9 bellard
        if (d->options) {
1562 1d14ffa9 bellard
            audio_process_options (d->name, d->options);
1563 1d14ffa9 bellard
        }
1564 1d14ffa9 bellard
    }
1565 1d14ffa9 bellard
1566 1d14ffa9 bellard
    printf ("Audio options:\n");
1567 1d14ffa9 bellard
    audio_print_options ("AUDIO", audio_options);
1568 1d14ffa9 bellard
    printf ("\n");
1569 1d14ffa9 bellard
1570 1d14ffa9 bellard
    printf ("Available drivers:\n");
1571 1d14ffa9 bellard
1572 1d14ffa9 bellard
    for (i = 0; i < sizeof (drvtab) / sizeof (drvtab[0]); i++) {
1573 1d14ffa9 bellard
        struct audio_driver *d = drvtab[i];
1574 1d14ffa9 bellard
1575 1d14ffa9 bellard
        printf ("Name: %s\n", d->name);
1576 1d14ffa9 bellard
        printf ("Description: %s\n", d->descr);
1577 1d14ffa9 bellard
1578 571ec3d6 bellard
        audio_pp_nb_voices ("playback", d->max_voices_out);
1579 571ec3d6 bellard
        audio_pp_nb_voices ("capture", d->max_voices_in);
1580 1d14ffa9 bellard
1581 1d14ffa9 bellard
        if (d->options) {
1582 1d14ffa9 bellard
            printf ("Options:\n");
1583 1d14ffa9 bellard
            audio_print_options (d->name, d->options);
1584 1d14ffa9 bellard
        }
1585 1d14ffa9 bellard
        else {
1586 1d14ffa9 bellard
            printf ("No options\n");
1587 1d14ffa9 bellard
        }
1588 1d14ffa9 bellard
        printf ("\n");
1589 1d14ffa9 bellard
    }
1590 1d14ffa9 bellard
1591 1d14ffa9 bellard
    printf (
1592 1d14ffa9 bellard
        "Options are settable through environment variables.\n"
1593 1d14ffa9 bellard
        "Example:\n"
1594 1d14ffa9 bellard
#ifdef _WIN32
1595 1d14ffa9 bellard
        "  set QEMU_AUDIO_DRV=wav\n"
1596 571ec3d6 bellard
        "  set QEMU_WAV_PATH=c:\\tune.wav\n"
1597 1d14ffa9 bellard
#else
1598 1d14ffa9 bellard
        "  export QEMU_AUDIO_DRV=wav\n"
1599 1d14ffa9 bellard
        "  export QEMU_WAV_PATH=$HOME/tune.wav\n"
1600 1d14ffa9 bellard
        "(for csh replace export with setenv in the above)\n"
1601 1d14ffa9 bellard
#endif
1602 1d14ffa9 bellard
        "  qemu ...\n\n"
1603 1d14ffa9 bellard
        );
1604 1d14ffa9 bellard
}
1605 1d14ffa9 bellard
1606 c0fe3827 bellard
static int audio_driver_init (AudioState *s, struct audio_driver *drv)
1607 85571bc7 bellard
{
1608 1d14ffa9 bellard
    if (drv->options) {
1609 1d14ffa9 bellard
        audio_process_options (drv->name, drv->options);
1610 1d14ffa9 bellard
    }
1611 c0fe3827 bellard
    s->drv_opaque = drv->init ();
1612 1d14ffa9 bellard
1613 c0fe3827 bellard
    if (s->drv_opaque) {
1614 571ec3d6 bellard
        audio_init_nb_voices_out (s, drv);
1615 571ec3d6 bellard
        audio_init_nb_voices_in (s, drv);
1616 c0fe3827 bellard
        s->drv = drv;
1617 1d14ffa9 bellard
        return 0;
1618 85571bc7 bellard
    }
1619 85571bc7 bellard
    else {
1620 1d14ffa9 bellard
        dolog ("Could not init `%s' audio driver\n", drv->name);
1621 1d14ffa9 bellard
        return -1;
1622 85571bc7 bellard
    }
1623 85571bc7 bellard
}
1624 85571bc7 bellard
1625 541e0844 bellard
static void audio_vm_change_state_handler (void *opaque, int running)
1626 85571bc7 bellard
{
1627 c0fe3827 bellard
    AudioState *s = opaque;
1628 1d14ffa9 bellard
    HWVoiceOut *hwo = NULL;
1629 1d14ffa9 bellard
    HWVoiceIn *hwi = NULL;
1630 541e0844 bellard
    int op = running ? VOICE_ENABLE : VOICE_DISABLE;
1631 1d14ffa9 bellard
1632 541e0844 bellard
    while ((hwo = audio_pcm_hw_find_any_enabled_out (s, hwo))) {
1633 541e0844 bellard
        hwo->pcm_ops->ctl_out (hwo, op);
1634 1d14ffa9 bellard
    }
1635 85571bc7 bellard
1636 541e0844 bellard
    while ((hwi = audio_pcm_hw_find_any_enabled_in (s, hwi))) {
1637 541e0844 bellard
        hwi->pcm_ops->ctl_in (hwi, op);
1638 85571bc7 bellard
    }
1639 85571bc7 bellard
}
1640 85571bc7 bellard
1641 85571bc7 bellard
static void audio_atexit (void)
1642 85571bc7 bellard
{
1643 c0fe3827 bellard
    AudioState *s = &glob_audio_state;
1644 1d14ffa9 bellard
    HWVoiceOut *hwo = NULL;
1645 1d14ffa9 bellard
    HWVoiceIn *hwi = NULL;
1646 1d14ffa9 bellard
1647 571ec3d6 bellard
    while ((hwo = audio_pcm_hw_find_any_enabled_out (s, hwo))) {
1648 ec36b695 bellard
        SWVoiceCap *sc;
1649 8ead62cf bellard
1650 571ec3d6 bellard
        hwo->pcm_ops->ctl_out (hwo, VOICE_DISABLE);
1651 1d14ffa9 bellard
        hwo->pcm_ops->fini_out (hwo);
1652 8ead62cf bellard
1653 ec36b695 bellard
        for (sc = hwo->cap_head.lh_first; sc; sc = sc->entries.le_next) {
1654 ec36b695 bellard
            CaptureVoiceOut *cap = sc->cap;
1655 ec36b695 bellard
            struct capture_callback *cb;
1656 ec36b695 bellard
1657 ec36b695 bellard
            for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
1658 ec36b695 bellard
                cb->ops.destroy (cb->opaque);
1659 ec36b695 bellard
            }
1660 8ead62cf bellard
        }
1661 1d14ffa9 bellard
    }
1662 85571bc7 bellard
1663 571ec3d6 bellard
    while ((hwi = audio_pcm_hw_find_any_enabled_in (s, hwi))) {
1664 571ec3d6 bellard
        hwi->pcm_ops->ctl_in (hwi, VOICE_DISABLE);
1665 1d14ffa9 bellard
        hwi->pcm_ops->fini_in (hwi);
1666 85571bc7 bellard
    }
1667 c0fe3827 bellard
1668 c0fe3827 bellard
    if (s->drv) {
1669 c0fe3827 bellard
        s->drv->fini (s->drv_opaque);
1670 c0fe3827 bellard
    }
1671 85571bc7 bellard
}
1672 85571bc7 bellard
1673 85571bc7 bellard
static void audio_save (QEMUFile *f, void *opaque)
1674 85571bc7 bellard
{
1675 1d14ffa9 bellard
    (void) f;
1676 1d14ffa9 bellard
    (void) opaque;
1677 85571bc7 bellard
}
1678 85571bc7 bellard
1679 85571bc7 bellard
static int audio_load (QEMUFile *f, void *opaque, int version_id)
1680 85571bc7 bellard
{
1681 1d14ffa9 bellard
    (void) f;
1682 1d14ffa9 bellard
    (void) opaque;
1683 1d14ffa9 bellard
1684 1d14ffa9 bellard
    if (version_id != 1) {
1685 85571bc7 bellard
        return -EINVAL;
1686 1d14ffa9 bellard
    }
1687 85571bc7 bellard
1688 85571bc7 bellard
    return 0;
1689 85571bc7 bellard
}
1690 85571bc7 bellard
1691 c0fe3827 bellard
void AUD_register_card (AudioState *s, const char *name, QEMUSoundCard *card)
1692 c0fe3827 bellard
{
1693 c0fe3827 bellard
    card->audio = s;
1694 c0fe3827 bellard
    card->name = qemu_strdup (name);
1695 c0fe3827 bellard
    memset (&card->entries, 0, sizeof (card->entries));
1696 c0fe3827 bellard
    LIST_INSERT_HEAD (&s->card_head, card, entries);
1697 c0fe3827 bellard
}
1698 c0fe3827 bellard
1699 c0fe3827 bellard
void AUD_remove_card (QEMUSoundCard *card)
1700 c0fe3827 bellard
{
1701 c0fe3827 bellard
    LIST_REMOVE (card, entries);
1702 c0fe3827 bellard
    card->audio = NULL;
1703 c0fe3827 bellard
    qemu_free (card->name);
1704 c0fe3827 bellard
}
1705 c0fe3827 bellard
1706 c0fe3827 bellard
AudioState *AUD_init (void)
1707 85571bc7 bellard
{
1708 1d14ffa9 bellard
    size_t i;
1709 85571bc7 bellard
    int done = 0;
1710 85571bc7 bellard
    const char *drvname;
1711 c0fe3827 bellard
    AudioState *s = &glob_audio_state;
1712 1d14ffa9 bellard
1713 571ec3d6 bellard
    LIST_INIT (&s->hw_head_out);
1714 571ec3d6 bellard
    LIST_INIT (&s->hw_head_in);
1715 8ead62cf bellard
    LIST_INIT (&s->cap_head);
1716 571ec3d6 bellard
    atexit (audio_atexit);
1717 571ec3d6 bellard
1718 571ec3d6 bellard
    s->ts = qemu_new_timer (vm_clock, audio_timer, s);
1719 571ec3d6 bellard
    if (!s->ts) {
1720 571ec3d6 bellard
        dolog ("Could not create audio timer\n");
1721 571ec3d6 bellard
        return NULL;
1722 571ec3d6 bellard
    }
1723 571ec3d6 bellard
1724 1d14ffa9 bellard
    audio_process_options ("AUDIO", audio_options);
1725 1d14ffa9 bellard
1726 c0fe3827 bellard
    s->nb_hw_voices_out = conf.fixed_out.nb_voices;
1727 c0fe3827 bellard
    s->nb_hw_voices_in = conf.fixed_in.nb_voices;
1728 c0fe3827 bellard
1729 1d14ffa9 bellard
    if (s->nb_hw_voices_out <= 0) {
1730 571ec3d6 bellard
        dolog ("Bogus number of playback voices %d, setting to 1\n",
1731 1d14ffa9 bellard
               s->nb_hw_voices_out);
1732 1d14ffa9 bellard
        s->nb_hw_voices_out = 1;
1733 1d14ffa9 bellard
    }
1734 1d14ffa9 bellard
1735 1d14ffa9 bellard
    if (s->nb_hw_voices_in <= 0) {
1736 571ec3d6 bellard
        dolog ("Bogus number of capture voices %d, setting to 0\n",
1737 1d14ffa9 bellard
               s->nb_hw_voices_in);
1738 571ec3d6 bellard
        s->nb_hw_voices_in = 0;
1739 1d14ffa9 bellard
    }
1740 85571bc7 bellard
1741 1d14ffa9 bellard
    {
1742 1d14ffa9 bellard
        int def;
1743 1d14ffa9 bellard
        drvname = audio_get_conf_str ("QEMU_AUDIO_DRV", NULL, &def);
1744 1d14ffa9 bellard
    }
1745 85571bc7 bellard
1746 85571bc7 bellard
    if (drvname) {
1747 85571bc7 bellard
        int found = 0;
1748 1d14ffa9 bellard
1749 85571bc7 bellard
        for (i = 0; i < sizeof (drvtab) / sizeof (drvtab[0]); i++) {
1750 85571bc7 bellard
            if (!strcmp (drvname, drvtab[i]->name)) {
1751 c0fe3827 bellard
                done = !audio_driver_init (s, drvtab[i]);
1752 85571bc7 bellard
                found = 1;
1753 85571bc7 bellard
                break;
1754 85571bc7 bellard
            }
1755 85571bc7 bellard
        }
1756 1d14ffa9 bellard
1757 85571bc7 bellard
        if (!found) {
1758 85571bc7 bellard
            dolog ("Unknown audio driver `%s'\n", drvname);
1759 1d14ffa9 bellard
            dolog ("Run with -audio-help to list available drivers\n");
1760 85571bc7 bellard
        }
1761 85571bc7 bellard
    }
1762 85571bc7 bellard
1763 85571bc7 bellard
    if (!done) {
1764 85571bc7 bellard
        for (i = 0; !done && i < sizeof (drvtab) / sizeof (drvtab[0]); i++) {
1765 1d14ffa9 bellard
            if (drvtab[i]->can_be_default) {
1766 c0fe3827 bellard
                done = !audio_driver_init (s, drvtab[i]);
1767 1d14ffa9 bellard
            }
1768 85571bc7 bellard
        }
1769 85571bc7 bellard
    }
1770 85571bc7 bellard
1771 85571bc7 bellard
    if (!done) {
1772 c0fe3827 bellard
        done = !audio_driver_init (s, &no_audio_driver);
1773 c0fe3827 bellard
        if (!done) {
1774 c0fe3827 bellard
            dolog ("Could not initialize audio subsystem\n");
1775 1d14ffa9 bellard
        }
1776 1d14ffa9 bellard
        else {
1777 c0fe3827 bellard
            dolog ("warning: Using timer based audio emulation\n");
1778 1d14ffa9 bellard
        }
1779 85571bc7 bellard
    }
1780 1d14ffa9 bellard
1781 c0fe3827 bellard
    if (done) {
1782 571ec3d6 bellard
        VMChangeStateEntry *e;
1783 571ec3d6 bellard
1784 c310de86 malc
        if (conf.period.hertz <= 0) {
1785 c310de86 malc
            if (conf.period.hertz < 0) {
1786 c0fe3827 bellard
                dolog ("warning: Timer period is negative - %d "
1787 c0fe3827 bellard
                       "treating as zero\n",
1788 c310de86 malc
                       conf.period.hertz);
1789 c0fe3827 bellard
            }
1790 c0fe3827 bellard
            conf.period.ticks = 1;
1791 1d14ffa9 bellard
        }
1792 c0fe3827 bellard
        else {
1793 c310de86 malc
            conf.period.ticks = ticks_per_sec / conf.period.hertz;
1794 c0fe3827 bellard
        }
1795 c0fe3827 bellard
1796 571ec3d6 bellard
        e = qemu_add_vm_change_state_handler (audio_vm_change_state_handler, s);
1797 571ec3d6 bellard
        if (!e) {
1798 571ec3d6 bellard
            dolog ("warning: Could not register change state handler\n"
1799 571ec3d6 bellard
                   "(Audio can continue looping even after stopping the VM)\n");
1800 571ec3d6 bellard
        }
1801 1d14ffa9 bellard
    }
1802 1d14ffa9 bellard
    else {
1803 c0fe3827 bellard
        qemu_del_timer (s->ts);
1804 c0fe3827 bellard
        return NULL;
1805 1d14ffa9 bellard
    }
1806 1d14ffa9 bellard
1807 c0fe3827 bellard
    LIST_INIT (&s->card_head);
1808 c0fe3827 bellard
    register_savevm ("audio", 0, 1, audio_save, audio_load, s);
1809 c0fe3827 bellard
    qemu_mod_timer (s->ts, qemu_get_clock (vm_clock) + conf.period.ticks);
1810 c0fe3827 bellard
    return s;
1811 85571bc7 bellard
}
1812 8ead62cf bellard
1813 ec36b695 bellard
CaptureVoiceOut *AUD_add_capture (
1814 8ead62cf bellard
    AudioState *s,
1815 1ea879e5 malc
    struct audsettings *as,
1816 8ead62cf bellard
    struct audio_capture_ops *ops,
1817 8ead62cf bellard
    void *cb_opaque
1818 8ead62cf bellard
    )
1819 8ead62cf bellard
{
1820 8ead62cf bellard
    CaptureVoiceOut *cap;
1821 8ead62cf bellard
    struct capture_callback *cb;
1822 8ead62cf bellard
1823 8ead62cf bellard
    if (!s) {
1824 8ead62cf bellard
        /* XXX suppress */
1825 8ead62cf bellard
        s = &glob_audio_state;
1826 8ead62cf bellard
    }
1827 8ead62cf bellard
1828 ec36b695 bellard
    if (audio_validate_settings (as)) {
1829 8ead62cf bellard
        dolog ("Invalid settings were passed when trying to add capture\n");
1830 8ead62cf bellard
        audio_print_settings (as);
1831 ec36b695 bellard
        goto err0;
1832 8ead62cf bellard
    }
1833 8ead62cf bellard
1834 8ead62cf bellard
    cb = audio_calloc (AUDIO_FUNC, 1, sizeof (*cb));
1835 8ead62cf bellard
    if (!cb) {
1836 8ead62cf bellard
        dolog ("Could not allocate capture callback information, size %zu\n",
1837 8ead62cf bellard
               sizeof (*cb));
1838 8ead62cf bellard
        goto err0;
1839 8ead62cf bellard
    }
1840 8ead62cf bellard
    cb->ops = *ops;
1841 8ead62cf bellard
    cb->opaque = cb_opaque;
1842 8ead62cf bellard
1843 d929eba5 bellard
    cap = audio_pcm_capture_find_specific (s, as);
1844 8ead62cf bellard
    if (cap) {
1845 8ead62cf bellard
        LIST_INSERT_HEAD (&cap->cb_head, cb, entries);
1846 ec36b695 bellard
        return cap;
1847 8ead62cf bellard
    }
1848 8ead62cf bellard
    else {
1849 8ead62cf bellard
        HWVoiceOut *hw;
1850 8ead62cf bellard
        CaptureVoiceOut *cap;
1851 8ead62cf bellard
1852 8ead62cf bellard
        cap = audio_calloc (AUDIO_FUNC, 1, sizeof (*cap));
1853 8ead62cf bellard
        if (!cap) {
1854 8ead62cf bellard
            dolog ("Could not allocate capture voice, size %zu\n",
1855 8ead62cf bellard
                   sizeof (*cap));
1856 8ead62cf bellard
            goto err1;
1857 8ead62cf bellard
        }
1858 8ead62cf bellard
1859 8ead62cf bellard
        hw = &cap->hw;
1860 8ead62cf bellard
        LIST_INIT (&hw->sw_head);
1861 8ead62cf bellard
        LIST_INIT (&cap->cb_head);
1862 8ead62cf bellard
1863 8ead62cf bellard
        /* XXX find a more elegant way */
1864 8ead62cf bellard
        hw->samples = 4096 * 4;
1865 8ead62cf bellard
        hw->mix_buf = audio_calloc (AUDIO_FUNC, hw->samples,
1866 1ea879e5 malc
                                    sizeof (struct st_sample));
1867 8ead62cf bellard
        if (!hw->mix_buf) {
1868 8ead62cf bellard
            dolog ("Could not allocate capture mix buffer (%d samples)\n",
1869 8ead62cf bellard
                   hw->samples);
1870 8ead62cf bellard
            goto err2;
1871 8ead62cf bellard
        }
1872 8ead62cf bellard
1873 d929eba5 bellard
        audio_pcm_init_info (&hw->info, as);
1874 8ead62cf bellard
1875 8ead62cf bellard
        cap->buf = audio_calloc (AUDIO_FUNC, hw->samples, 1 << hw->info.shift);
1876 8ead62cf bellard
        if (!cap->buf) {
1877 8ead62cf bellard
            dolog ("Could not allocate capture buffer "
1878 8ead62cf bellard
                   "(%d samples, each %d bytes)\n",
1879 8ead62cf bellard
                   hw->samples, 1 << hw->info.shift);
1880 8ead62cf bellard
            goto err3;
1881 8ead62cf bellard
        }
1882 8ead62cf bellard
1883 8ead62cf bellard
        hw->clip = mixeng_clip
1884 8ead62cf bellard
            [hw->info.nchannels == 2]
1885 8ead62cf bellard
            [hw->info.sign]
1886 d929eba5 bellard
            [hw->info.swap_endianness]
1887 f941aa25 ths
            [audio_bits_to_index (hw->info.bits)];
1888 8ead62cf bellard
1889 8ead62cf bellard
        LIST_INSERT_HEAD (&s->cap_head, cap, entries);
1890 8ead62cf bellard
        LIST_INSERT_HEAD (&cap->cb_head, cb, entries);
1891 8ead62cf bellard
1892 8ead62cf bellard
        hw = NULL;
1893 8ead62cf bellard
        while ((hw = audio_pcm_hw_find_any_out (s, hw))) {
1894 8ead62cf bellard
            audio_attach_capture (s, hw);
1895 8ead62cf bellard
        }
1896 ec36b695 bellard
        return cap;
1897 8ead62cf bellard
1898 8ead62cf bellard
    err3:
1899 8ead62cf bellard
        qemu_free (cap->hw.mix_buf);
1900 8ead62cf bellard
    err2:
1901 8ead62cf bellard
        qemu_free (cap);
1902 8ead62cf bellard
    err1:
1903 8ead62cf bellard
        qemu_free (cb);
1904 8ead62cf bellard
    err0:
1905 ec36b695 bellard
        return NULL;
1906 ec36b695 bellard
    }
1907 ec36b695 bellard
}
1908 ec36b695 bellard
1909 ec36b695 bellard
void AUD_del_capture (CaptureVoiceOut *cap, void *cb_opaque)
1910 ec36b695 bellard
{
1911 ec36b695 bellard
    struct capture_callback *cb;
1912 ec36b695 bellard
1913 ec36b695 bellard
    for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
1914 ec36b695 bellard
        if (cb->opaque == cb_opaque) {
1915 ec36b695 bellard
            cb->ops.destroy (cb_opaque);
1916 ec36b695 bellard
            LIST_REMOVE (cb, entries);
1917 ec36b695 bellard
            qemu_free (cb);
1918 ec36b695 bellard
1919 ec36b695 bellard
            if (!cap->cb_head.lh_first) {
1920 ec36b695 bellard
                SWVoiceOut *sw = cap->hw.sw_head.lh_first, *sw1;
1921 a3c25997 bellard
1922 ec36b695 bellard
                while (sw) {
1923 a3c25997 bellard
                    SWVoiceCap *sc = (SWVoiceCap *) sw;
1924 ec36b695 bellard
#ifdef DEBUG_CAPTURE
1925 ec36b695 bellard
                    dolog ("freeing %s\n", sw->name);
1926 ec36b695 bellard
#endif
1927 a3c25997 bellard
1928 ec36b695 bellard
                    sw1 = sw->entries.le_next;
1929 ec36b695 bellard
                    if (sw->rate) {
1930 ec36b695 bellard
                        st_rate_stop (sw->rate);
1931 ec36b695 bellard
                        sw->rate = NULL;
1932 ec36b695 bellard
                    }
1933 ec36b695 bellard
                    LIST_REMOVE (sw, entries);
1934 a3c25997 bellard
                    LIST_REMOVE (sc, entries);
1935 a3c25997 bellard
                    qemu_free (sc);
1936 ec36b695 bellard
                    sw = sw1;
1937 ec36b695 bellard
                }
1938 ec36b695 bellard
                LIST_REMOVE (cap, entries);
1939 ec36b695 bellard
                qemu_free (cap);
1940 ec36b695 bellard
            }
1941 ec36b695 bellard
            return;
1942 ec36b695 bellard
        }
1943 8ead62cf bellard
    }
1944 8ead62cf bellard
}
1945 683efdcb balrog
1946 683efdcb balrog
void AUD_set_volume_out (SWVoiceOut *sw, int mute, uint8_t lvol, uint8_t rvol)
1947 683efdcb balrog
{
1948 683efdcb balrog
    if (sw) {
1949 683efdcb balrog
        sw->vol.mute = mute;
1950 683efdcb balrog
        sw->vol.l = nominal_volume.l * lvol / 255;
1951 683efdcb balrog
        sw->vol.r = nominal_volume.r * rvol / 255;
1952 683efdcb balrog
    }
1953 683efdcb balrog
}
1954 683efdcb balrog
1955 683efdcb balrog
void AUD_set_volume_in (SWVoiceIn *sw, int mute, uint8_t lvol, uint8_t rvol)
1956 683efdcb balrog
{
1957 683efdcb balrog
    if (sw) {
1958 683efdcb balrog
        sw->vol.mute = mute;
1959 683efdcb balrog
        sw->vol.l = nominal_volume.l * lvol / 255;
1960 683efdcb balrog
        sw->vol.r = nominal_volume.r * rvol / 255;
1961 683efdcb balrog
    }
1962 683efdcb balrog
}