Statistics
| Branch: | Revision:

root / audio / audio.c @ c0fe3827

History | View | Annotate | Download (40.8 kB)

1
/*
2
 * QEMU Audio subsystem
3
 *
4
 * Copyright (c) 2003-2005 Vassili Karpov (malc)
5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7
 * of this software and associated documentation files (the "Software"), to deal
8
 * in the Software without restriction, including without limitation the rights
9
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
 * copies of the Software, and to permit persons to whom the Software is
11
 * furnished to do so, subject to the following conditions:
12
 *
13
 * The above copyright notice and this permission notice shall be included in
14
 * all copies or substantial portions of the Software.
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
 * THE SOFTWARE.
23
 */
24
#include "vl.h"
25

    
26
#define AUDIO_CAP "audio"
27
#include "audio_int.h"
28

    
29
/* #define DEBUG_PLIVE */
30
/* #define DEBUG_LIVE */
31
/* #define DEBUG_OUT */
32

    
33
#define SW_NAME(sw) (sw)->name ? (sw)->name : "unknown"
34

    
35
static struct audio_driver *drvtab[] = {
36
#ifdef CONFIG_OSS
37
    &oss_audio_driver,
38
#endif
39
#ifdef CONFIG_ALSA
40
    &alsa_audio_driver,
41
#endif
42
#ifdef CONFIG_COREAUDIO
43
    &coreaudio_audio_driver,
44
#endif
45
#ifdef CONFIG_DSOUND
46
    &dsound_audio_driver,
47
#endif
48
#ifdef CONFIG_FMOD
49
    &fmod_audio_driver,
50
#endif
51
#ifdef CONFIG_SDL
52
    &sdl_audio_driver,
53
#endif
54
    &no_audio_driver,
55
    &wav_audio_driver
56
};
57

    
58
struct fixed_settings {
59
    int enabled;
60
    int nb_voices;
61
    int greedy;
62
    audsettings_t settings;
63
};
64

    
65
static struct {
66
    struct fixed_settings fixed_out;
67
    struct fixed_settings fixed_in;
68
    union {
69
        int hz;
70
        int64_t ticks;
71
    } period;
72
    int plive;
73
} conf = {
74
    {                           /* DAC fixed settings */
75
        1,                      /* enabled */
76
        1,                      /* nb_voices */
77
        1,                      /* greedy */
78
        {
79
            44100,              /* freq */
80
            2,                  /* nchannels */
81
            AUD_FMT_S16         /* fmt */
82
        }
83
    },
84

    
85
    {                           /* ADC fixed settings */
86
        1,                      /* enabled */
87
        1,                      /* nb_voices */
88
        1,                      /* greedy */
89
        {
90
            44100,              /* freq */
91
            2,                  /* nchannels */
92
            AUD_FMT_S16         /* fmt */
93
        }
94
    },
95

    
96
    { 0 },                      /* period */
97
    0                           /* plive */
98
};
99

    
100
static AudioState glob_audio_state;
101

    
102
volume_t nominal_volume = {
103
    0,
104
#ifdef FLOAT_MIXENG
105
    1.0,
106
    1.0
107
#else
108
    UINT_MAX,
109
    UINT_MAX
110
#endif
111
};
112

    
113
/* http://www.df.lth.se/~john_e/gems/gem002d.html */
114
/* http://www.multi-platforms.com/Tips/PopCount.htm */
115
uint32_t popcount (uint32_t u)
116
{
117
    u = ((u&0x55555555) + ((u>>1)&0x55555555));
118
    u = ((u&0x33333333) + ((u>>2)&0x33333333));
119
    u = ((u&0x0f0f0f0f) + ((u>>4)&0x0f0f0f0f));
120
    u = ((u&0x00ff00ff) + ((u>>8)&0x00ff00ff));
121
    u = ( u&0x0000ffff) + (u>>16);
122
    return u;
123
}
124

    
125
inline uint32_t lsbindex (uint32_t u)
126
{
127
    return popcount ((u&-u)-1);
128
}
129

    
130
#ifdef AUDIO_IS_FLAWLESS_AND_NO_CHECKS_ARE_REQURIED
131
#error No its not
132
#else
133
int audio_bug (const char *funcname, int cond)
134
{
135
    if (cond) {
136
        static int shown;
137

    
138
        AUD_log (NULL, "Error a bug that was just triggered in %s\n", funcname);
139
        if (!shown) {
140
            shown = 1;
141
            AUD_log (NULL, "Save all your work and restart without audio\n");
142
            AUD_log (NULL, "Please send bug report to malc@pulsesoft.com\n");
143
            AUD_log (NULL, "I am sorry\n");
144
        }
145
        AUD_log (NULL, "Context:\n");
146

    
147
#if defined AUDIO_BREAKPOINT_ON_BUG
148
#  if defined HOST_I386
149
#    if defined __GNUC__
150
        __asm__ ("int3");
151
#    elif defined _MSC_VER
152
        _asm _emit 0xcc;
153
#    else
154
        abort ();
155
#    endif
156
#  else
157
        abort ();
158
#  endif
159
#endif
160
    }
161

    
162
    return cond;
163
}
164
#endif
165

    
166
void *audio_calloc (const char *funcname, int nmemb, size_t size)
167
{
168
    int cond;
169
    size_t len;
170

    
171
    len = nmemb * size;
172
    cond = !nmemb || !size;
173
    cond |= nmemb < 0;
174
    cond |= len < size;
175

    
176
    if (audio_bug ("audio_calloc", cond)) {
177
        AUD_log (NULL, "%s passed invalid arguments to audio_calloc\n",
178
                 funcname);
179
        AUD_log (NULL, "nmemb=%d size=%d (len=%d)\n", nmemb, size, len);
180
        return NULL;
181
    }
182

    
183
    return qemu_mallocz (len);
184
}
185

    
186
static char *audio_alloc_prefix (const char *s)
187
{
188
    const char qemu_prefix[] = "QEMU_";
189
    size_t len;
190
    char *r;
191

    
192
    if (!s) {
193
        return NULL;
194
    }
195

    
196
    len = strlen (s);
197
    r = qemu_malloc (len + sizeof (qemu_prefix));
198

    
199
    if (r) {
200
        size_t i;
201
        char *u = r + sizeof (qemu_prefix) - 1;
202

    
203
        strcpy (r, qemu_prefix);
204
        strcat (r, s);
205

    
206
        for (i = 0; i < len; ++i) {
207
            u[i] = toupper (u[i]);
208
        }
209
    }
210
    return r;
211
}
212

    
213
const char *audio_audfmt_to_string (audfmt_e fmt)
214
{
215
    switch (fmt) {
216
    case AUD_FMT_U8:
217
        return "U8";
218

    
219
    case AUD_FMT_U16:
220
        return "U16";
221

    
222
    case AUD_FMT_S8:
223
        return "S8";
224

    
225
    case AUD_FMT_S16:
226
        return "S16";
227
    }
228

    
229
    dolog ("Bogus audfmt %d returning S16\n", fmt);
230
    return "S16";
231
}
232

    
233
audfmt_e audio_string_to_audfmt (const char *s, audfmt_e defval, int *defaultp)
234
{
235
    if (!strcasecmp (s, "u8")) {
236
        *defaultp = 0;
237
        return AUD_FMT_U8;
238
    }
239
    else if (!strcasecmp (s, "u16")) {
240
        *defaultp = 0;
241
        return AUD_FMT_U16;
242
    }
243
    else if (!strcasecmp (s, "s8")) {
244
        *defaultp = 0;
245
        return AUD_FMT_S8;
246
    }
247
    else if (!strcasecmp (s, "s16")) {
248
        *defaultp = 0;
249
        return AUD_FMT_S16;
250
    }
251
    else {
252
        dolog ("Bogus audio format `%s' using %s\n",
253
               s, audio_audfmt_to_string (defval));
254
        *defaultp = 1;
255
        return defval;
256
    }
257
}
258

    
259
static audfmt_e audio_get_conf_fmt (const char *envname,
260
                                    audfmt_e defval,
261
                                    int *defaultp)
262
{
263
    const char *var = getenv (envname);
264
    if (!var) {
265
        *defaultp = 1;
266
        return defval;
267
    }
268
    return audio_string_to_audfmt (var, defval, defaultp);
269
}
270

    
271
static int audio_get_conf_int (const char *key, int defval, int *defaultp)
272
{
273
    int val;
274
    char *strval;
275

    
276
    strval = getenv (key);
277
    if (strval) {
278
        *defaultp = 0;
279
        val = atoi (strval);
280
        return val;
281
    }
282
    else {
283
        *defaultp = 1;
284
        return defval;
285
    }
286
}
287

    
288
static const char *audio_get_conf_str (const char *key,
289
                                       const char *defval,
290
                                       int *defaultp)
291
{
292
    const char *val = getenv (key);
293
    if (!val) {
294
        *defaultp = 1;
295
        return defval;
296
    }
297
    else {
298
        *defaultp = 0;
299
        return val;
300
    }
301
}
302

    
303
void AUD_log (const char *cap, const char *fmt, ...)
304
{
305
    va_list ap;
306
    if (cap) {
307
        fprintf (stderr, "%s: ", cap);
308
    }
309
    va_start (ap, fmt);
310
    vfprintf (stderr, fmt, ap);
311
    va_end (ap);
312
}
313

    
314
void AUD_vlog (const char *cap, const char *fmt, va_list ap)
315
{
316
    if (cap) {
317
        fprintf (stderr, "%s: ", cap);
318
    }
319
    vfprintf (stderr, fmt, ap);
320
}
321

    
322
static void audio_print_options (const char *prefix,
323
                                 struct audio_option *opt)
324
{
325
    char *uprefix;
326

    
327
    if (!prefix) {
328
        dolog ("No prefix specified\n");
329
        return;
330
    }
331

    
332
    if (!opt) {
333
        dolog ("No options\n");
334
        return;
335
    }
336

    
337
    uprefix = audio_alloc_prefix (prefix);
338

    
339
    for (; opt->name; opt++) {
340
        const char *state = "default";
341
        printf ("  %s_%s: ", uprefix, opt->name);
342

    
343
        if (opt->overridenp && *opt->overridenp) {
344
            state = "current";
345
        }
346

    
347
        switch (opt->tag) {
348
        case AUD_OPT_BOOL:
349
            {
350
                int *intp = opt->valp;
351
                printf ("boolean, %s = %d\n", state, *intp ? 1 : 0);
352
            }
353
            break;
354

    
355
        case AUD_OPT_INT:
356
            {
357
                int *intp = opt->valp;
358
                printf ("integer, %s = %d\n", state, *intp);
359
            }
360
            break;
361

    
362
        case AUD_OPT_FMT:
363
            {
364
                audfmt_e *fmtp = opt->valp;
365
                printf (
366
                    "format, %s = %s, (one of: U8 S8 U16 S16)\n",
367
                    state,
368
                    audio_audfmt_to_string (*fmtp)
369
                    );
370
            }
371
            break;
372

    
373
        case AUD_OPT_STR:
374
            {
375
                const char **strp = opt->valp;
376
                printf ("string, %s = %s\n",
377
                        state,
378
                        *strp ? *strp : "(not set)");
379
            }
380
            break;
381

    
382
        default:
383
            printf ("???\n");
384
            dolog ("Bad value tag for option %s_%s %d\n",
385
                   uprefix, opt->name, opt->tag);
386
            break;
387
        }
388
        printf ("    %s\n", opt->descr);
389
    }
390

    
391
    qemu_free (uprefix);
392
}
393

    
394
static void audio_process_options (const char *prefix,
395
                                   struct audio_option *opt)
396
{
397
    char *optname;
398
    const char qemu_prefix[] = "QEMU_";
399
    size_t preflen;
400

    
401
    if (audio_bug (AUDIO_FUNC, !prefix)) {
402
        dolog ("prefix = NULL\n");
403
        return;
404
    }
405

    
406
    if (audio_bug (AUDIO_FUNC, !opt)) {
407
        dolog ("opt = NULL\n");
408
        return;
409
    }
410

    
411
    preflen = strlen (prefix);
412

    
413
    for (; opt->name; opt++) {
414
        size_t len, i;
415
        int def;
416

    
417
        if (!opt->valp) {
418
            dolog ("Option value pointer for `%s' is not set\n",
419
                   opt->name);
420
            continue;
421
        }
422

    
423
        len = strlen (opt->name);
424
        /* len of opt->name + len of prefix + size of qemu_prefix
425
         * (includes trailing zero) + zero + underscore (on behalf of
426
         * sizeof) */
427
        optname = qemu_malloc (len + preflen + sizeof (qemu_prefix) + 1);
428
        if (!optname) {
429
            dolog ("Could not allocate memory for option name `%s'\n",
430
                   opt->name);
431
            continue;
432
        }
433

    
434
        strcpy (optname, qemu_prefix);
435

    
436
        /* copy while upper-casing, including trailing zero */
437
        for (i = 0; i <= preflen; ++i) {
438
            optname[i + sizeof (qemu_prefix) - 1] = toupper (prefix[i]);
439
        }
440
        strcat (optname, "_");
441
        strcat (optname, opt->name);
442

    
443
        def = 1;
444
        switch (opt->tag) {
445
        case AUD_OPT_BOOL:
446
        case AUD_OPT_INT:
447
            {
448
                int *intp = opt->valp;
449
                *intp = audio_get_conf_int (optname, *intp, &def);
450
            }
451
            break;
452

    
453
        case AUD_OPT_FMT:
454
            {
455
                audfmt_e *fmtp = opt->valp;
456
                *fmtp = audio_get_conf_fmt (optname, *fmtp, &def);
457
            }
458
            break;
459

    
460
        case AUD_OPT_STR:
461
            {
462
                const char **strp = opt->valp;
463
                *strp = audio_get_conf_str (optname, *strp, &def);
464
            }
465
            break;
466

    
467
        default:
468
            dolog ("Bad value tag for option `%s' - %d\n",
469
                   optname, opt->tag);
470
            break;
471
        }
472

    
473
        if (!opt->overridenp) {
474
            opt->overridenp = &opt->overriden;
475
        }
476
        *opt->overridenp = !def;
477
        qemu_free (optname);
478
    }
479
}
480

    
481
static void audio_print_settings (audsettings_t *as)
482
{
483
    dolog ("frequency=%d nchannels=%d fmt=", as->freq, as->nchannels);
484

    
485
    switch (as->fmt) {
486
    case AUD_FMT_S8:
487
        AUD_log (NULL, "S8");
488
        break;
489
    case AUD_FMT_U8:
490
        AUD_log (NULL, "U8");
491
        break;
492
    case AUD_FMT_S16:
493
        AUD_log (NULL, "S16");
494
        break;
495
    case AUD_FMT_U16:
496
        AUD_log (NULL, "U16");
497
        break;
498
    default:
499
        AUD_log (NULL, "invalid(%d)", as->fmt);
500
        break;
501
    }
502
    AUD_log (NULL, "\n");
503
}
504

    
505
static int audio_validate_settigs (audsettings_t *as)
506
{
507
    int invalid;
508

    
509
    invalid = as->nchannels != 1 && as->nchannels != 2;
510

    
511
    switch (as->fmt) {
512
    case AUD_FMT_S8:
513
    case AUD_FMT_U8:
514
    case AUD_FMT_S16:
515
    case AUD_FMT_U16:
516
        break;
517
    default:
518
        invalid = 1;
519
        break;
520
    }
521

    
522
    invalid |= as->freq <= 0;
523

    
524
    if (invalid) {
525
        return -1;
526
    }
527
    return 0;
528
}
529

    
530
static int audio_pcm_info_eq (struct audio_pcm_info *info, audsettings_t *as)
531
{
532
    int bits = 8, sign = 0;
533

    
534
    switch (as->fmt) {
535
    case AUD_FMT_S8:
536
        sign = 1;
537
    case AUD_FMT_U8:
538
        break;
539

    
540
    case AUD_FMT_S16:
541
        sign = 1;
542
    case AUD_FMT_U16:
543
        bits = 16;
544
        break;
545
    }
546
    return info->freq == as->freq
547
        && info->nchannels == as->nchannels
548
        && info->sign == sign
549
        && info->bits == bits;
550
}
551

    
552
void audio_pcm_init_info (
553
    struct audio_pcm_info *info,
554
    audsettings_t *as,
555
    int swap_endian
556
    )
557
{
558
    int bits = 8, sign = 0;
559

    
560
    switch (as->fmt) {
561
    case AUD_FMT_S8:
562
        sign = 1;
563
    case AUD_FMT_U8:
564
        break;
565

    
566
    case AUD_FMT_S16:
567
        sign = 1;
568
    case AUD_FMT_U16:
569
        bits = 16;
570
        break;
571
    }
572

    
573
    info->freq = as->freq;
574
    info->bits = bits;
575
    info->sign = sign;
576
    info->nchannels = as->nchannels;
577
    info->shift = (as->nchannels == 2) + (bits == 16);
578
    info->align = (1 << info->shift) - 1;
579
    info->bytes_per_second = info->freq << info->shift;
580
    info->swap_endian = swap_endian;
581
}
582

    
583
void audio_pcm_info_clear_buf (struct audio_pcm_info *info, void *buf, int len)
584
{
585
    if (!len) {
586
        return;
587
    }
588

    
589
    if (info->sign) {
590
        memset (buf, len << info->shift, 0x00);
591
    }
592
    else {
593
        if (info->bits == 8) {
594
            memset (buf, len << info->shift, 0x80);
595
        }
596
        else {
597
            int i;
598
            uint16_t *p = buf;
599
            int shift = info->nchannels - 1;
600
            short s = INT16_MAX;
601

    
602
            if (info->swap_endian) {
603
                s = bswap16 (s);
604
            }
605

    
606
            for (i = 0; i < len << shift; i++) {
607
                p[i] = s;
608
            }
609
        }
610
    }
611
}
612

    
613
/*
614
 * Hard voice (capture)
615
 */
616
static void audio_pcm_hw_free_resources_in (HWVoiceIn *hw)
617
{
618
    if (hw->conv_buf) {
619
        qemu_free (hw->conv_buf);
620
    }
621
    hw->conv_buf = NULL;
622
}
623

    
624
static int audio_pcm_hw_alloc_resources_in (HWVoiceIn *hw)
625
{
626
    hw->conv_buf = audio_calloc (AUDIO_FUNC, hw->samples, sizeof (st_sample_t));
627
    if (!hw->conv_buf) {
628
        dolog ("Could not allocate ADC conversion buffer (%d bytes)\n",
629
               hw->samples * sizeof (st_sample_t));
630
        return -1;
631
    }
632
    return 0;
633
}
634

    
635
static int audio_pcm_hw_find_min_in (HWVoiceIn *hw)
636
{
637
    SWVoiceIn *sw;
638
    int m = hw->total_samples_captured;
639

    
640
    for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
641
        if (sw->active) {
642
            m = audio_MIN (m, sw->total_hw_samples_acquired);
643
        }
644
    }
645
    return m;
646
}
647

    
648
int audio_pcm_hw_get_live_in (HWVoiceIn *hw)
649
{
650
    int live = hw->total_samples_captured - audio_pcm_hw_find_min_in (hw);
651
    if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
652
        dolog ("live=%d hw->samples=%d\n", live, hw->samples);
653
        return 0;
654
    }
655
    return live;
656
}
657

    
658
/*
659
 * Soft voice (capture)
660
 */
661
static void audio_pcm_sw_free_resources_in (SWVoiceIn *sw)
662
{
663
    if (sw->conv_buf) {
664
        qemu_free (sw->conv_buf);
665
    }
666

    
667
    if (sw->rate) {
668
        st_rate_stop (sw->rate);
669
    }
670

    
671
    sw->conv_buf = NULL;
672
    sw->rate = NULL;
673
}
674

    
675
static int audio_pcm_sw_alloc_resources_in (SWVoiceIn *sw)
676
{
677
    int samples = ((int64_t) sw->hw->samples << 32) / sw->ratio;
678
    sw->conv_buf = audio_calloc (AUDIO_FUNC, samples, sizeof (st_sample_t));
679
    if (!sw->conv_buf) {
680
        dolog ("Could not allocate buffer for `%s' (%d bytes)\n",
681
               SW_NAME (sw), samples * sizeof (st_sample_t));
682
        return -1;
683
    }
684

    
685
    sw->rate = st_rate_start (sw->hw->info.freq, sw->info.freq);
686
    if (!sw->rate) {
687
        qemu_free (sw->conv_buf);
688
        sw->conv_buf = NULL;
689
        return -1;
690
    }
691
    return 0;
692
}
693

    
694
static int audio_pcm_sw_init_in (
695
    SWVoiceIn *sw,
696
    HWVoiceIn *hw,
697
    const char *name,
698
    audsettings_t *as
699
    )
700
{
701
    /* None of the cards emulated by QEMU are big-endian
702
       hence following shortcut */
703
    audio_pcm_init_info (&sw->info, as, audio_need_to_swap_endian (0));
704
    sw->hw = hw;
705
    sw->ratio = ((int64_t) sw->info.freq << 32) / sw->hw->info.freq;
706

    
707
    sw->clip =
708
        mixeng_clip
709
        [sw->info.nchannels == 2]
710
        [sw->info.sign]
711
        [sw->info.swap_endian]
712
        [sw->info.bits == 16];
713

    
714
    sw->name = qemu_strdup (name);
715
    audio_pcm_sw_free_resources_in (sw);
716
    return audio_pcm_sw_alloc_resources_in (sw);
717
}
718

    
719
static int audio_pcm_sw_get_rpos_in (SWVoiceIn *sw)
720
{
721
    HWVoiceIn *hw = sw->hw;
722
    int live = hw->total_samples_captured - sw->total_hw_samples_acquired;
723
    int rpos;
724

    
725
    if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
726
        dolog ("live=%d hw->samples=%d\n", live, hw->samples);
727
        return 0;
728
    }
729

    
730
    rpos = hw->wpos - live;
731
    if (rpos >= 0) {
732
        return rpos;
733
    }
734
    else {
735
        return hw->samples + rpos;
736
    }
737
}
738

    
739
int audio_pcm_sw_read (SWVoiceIn *sw, void *buf, int size)
740
{
741
    HWVoiceIn *hw = sw->hw;
742
    int samples, live, ret = 0, swlim, isamp, osamp, rpos, total = 0;
743
    st_sample_t *src, *dst = sw->conv_buf;
744

    
745
    rpos = audio_pcm_sw_get_rpos_in (sw) % hw->samples;
746

    
747
    live = hw->total_samples_captured - sw->total_hw_samples_acquired;
748
    if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
749
        dolog ("live_in=%d hw->samples=%d\n", live, hw->samples);
750
        return 0;
751
    }
752

    
753
    samples = size >> sw->info.shift;
754
    if (!live) {
755
        return 0;
756
    }
757

    
758
    swlim = (live * sw->ratio) >> 32;
759
    swlim = audio_MIN (swlim, samples);
760

    
761
    while (swlim) {
762
        src = hw->conv_buf + rpos;
763
        isamp = hw->wpos - rpos;
764
        /* XXX: <= ? */
765
        if (isamp <= 0) {
766
            isamp = hw->samples - rpos;
767
        }
768

    
769
        if (!isamp) {
770
            break;
771
        }
772
        osamp = swlim;
773

    
774
        if (audio_bug (AUDIO_FUNC, osamp < 0)) {
775
            dolog ("osamp=%d\n", osamp);
776
            return 0;
777
        }
778

    
779
        st_rate_flow (sw->rate, src, dst, &isamp, &osamp);
780
        swlim -= osamp;
781
        rpos = (rpos + isamp) % hw->samples;
782
        dst += osamp;
783
        ret += osamp;
784
        total += isamp;
785
    }
786

    
787
    sw->clip (buf, sw->conv_buf, ret);
788
    sw->total_hw_samples_acquired += total;
789
    return ret << sw->info.shift;
790
}
791

    
792
/*
793
 * Hard voice (playback)
794
 */
795
static void audio_pcm_hw_free_resources_out (HWVoiceOut *hw)
796
{
797
    if (hw->mix_buf) {
798
        qemu_free (hw->mix_buf);
799
    }
800

    
801
    hw->mix_buf = NULL;
802
}
803

    
804
static int audio_pcm_hw_alloc_resources_out (HWVoiceOut *hw)
805
{
806
    hw->mix_buf = audio_calloc (AUDIO_FUNC, hw->samples, sizeof (st_sample_t));
807
    if (!hw->mix_buf) {
808
        dolog ("Could not allocate DAC mixing buffer (%d bytes)\n",
809
               hw->samples * sizeof (st_sample_t));
810
        return -1;
811
    }
812

    
813
    return 0;
814
}
815

    
816
static int audio_pcm_hw_find_min_out (HWVoiceOut *hw, int *nb_livep)
817
{
818
    SWVoiceOut *sw;
819
    int m = INT_MAX;
820
    int nb_live = 0;
821

    
822
    for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
823
        if (sw->active || !sw->empty) {
824
            m = audio_MIN (m, sw->total_hw_samples_mixed);
825
            nb_live += 1;
826
        }
827
    }
828

    
829
    *nb_livep = nb_live;
830
    return m;
831
}
832

    
833
int audio_pcm_hw_get_live_out2 (HWVoiceOut *hw, int *nb_live)
834
{
835
    int smin;
836

    
837
    smin = audio_pcm_hw_find_min_out (hw, nb_live);
838

    
839
    if (!*nb_live) {
840
        return 0;
841
    }
842
    else {
843
        int live = smin;
844

    
845
        if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
846
            dolog ("live=%d hw->samples=%d\n", live, hw->samples);
847
            return 0;
848
        }
849
        return live;
850
    }
851
}
852

    
853
int audio_pcm_hw_get_live_out (HWVoiceOut *hw)
854
{
855
    int nb_live;
856
    int live;
857

    
858
    live = audio_pcm_hw_get_live_out2 (hw, &nb_live);
859
    if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
860
        dolog ("live=%d hw->samples=%d\n", live, hw->samples);
861
        return 0;
862
    }
863
    return live;
864
}
865

    
866
/*
867
 * Soft voice (playback)
868
 */
869
static void audio_pcm_sw_free_resources_out (SWVoiceOut *sw)
870
{
871
    if (sw->buf) {
872
        qemu_free (sw->buf);
873
    }
874

    
875
    if (sw->rate) {
876
        st_rate_stop (sw->rate);
877
    }
878

    
879
    sw->buf = NULL;
880
    sw->rate = NULL;
881
}
882

    
883
static int audio_pcm_sw_alloc_resources_out (SWVoiceOut *sw)
884
{
885
    sw->buf = audio_calloc (AUDIO_FUNC, sw->hw->samples, sizeof (st_sample_t));
886
    if (!sw->buf) {
887
        dolog ("Could not allocate buffer for `%s' (%d bytes)\n",
888
               SW_NAME (sw), sw->hw->samples * sizeof (st_sample_t));
889
        return -1;
890
    }
891

    
892
    sw->rate = st_rate_start (sw->info.freq, sw->hw->info.freq);
893
    if (!sw->rate) {
894
        qemu_free (sw->buf);
895
        sw->buf = NULL;
896
        return -1;
897
    }
898
    return 0;
899
}
900

    
901
static int audio_pcm_sw_init_out (
902
    SWVoiceOut *sw,
903
    HWVoiceOut *hw,
904
    const char *name,
905
    audsettings_t *as
906
    )
907
{
908
    /* None of the cards emulated by QEMU are big-endian
909
       hence following shortcut */
910
    audio_pcm_init_info (&sw->info, as, audio_need_to_swap_endian (0));
911
    sw->hw = hw;
912
    sw->empty = 1;
913
    sw->active = 0;
914
    sw->ratio = ((int64_t) sw->hw->info.freq << 32) / sw->info.freq;
915
    sw->total_hw_samples_mixed = 0;
916

    
917
    sw->conv =
918
        mixeng_conv
919
        [sw->info.nchannels == 2]
920
        [sw->info.sign]
921
        [sw->info.swap_endian]
922
        [sw->info.bits == 16];
923
    sw->name = qemu_strdup (name);
924

    
925
    audio_pcm_sw_free_resources_out (sw);
926
    return audio_pcm_sw_alloc_resources_out (sw);
927
}
928

    
929
int audio_pcm_sw_write (SWVoiceOut *sw, void *buf, int size)
930
{
931
    int hwsamples, samples, isamp, osamp, wpos, live, dead, left, swlim, blck;
932
    int ret = 0, pos = 0, total = 0;
933

    
934
    if (!sw) {
935
        return size;
936
    }
937

    
938
    hwsamples = sw->hw->samples;
939

    
940
    live = sw->total_hw_samples_mixed;
941
    if (audio_bug (AUDIO_FUNC, live < 0 || live > hwsamples)){
942
        dolog ("live=%d hw->samples=%d\n", live, hwsamples);
943
        return 0;
944
    }
945

    
946
    if (live == hwsamples) {
947
        return 0;
948
    }
949

    
950
    wpos = (sw->hw->rpos + live) % hwsamples;
951
    samples = size >> sw->info.shift;
952

    
953
    dead = hwsamples - live;
954
    swlim = ((int64_t) dead << 32) / sw->ratio;
955
    swlim = audio_MIN (swlim, samples);
956
    if (swlim) {
957
        sw->conv (sw->buf, buf, swlim, &sw->vol);
958
    }
959

    
960
    while (swlim) {
961
        dead = hwsamples - live;
962
        left = hwsamples - wpos;
963
        blck = audio_MIN (dead, left);
964
        if (!blck) {
965
            break;
966
        }
967
        isamp = swlim;
968
        osamp = blck;
969
        st_rate_flow_mix (
970
            sw->rate,
971
            sw->buf + pos,
972
            sw->hw->mix_buf + wpos,
973
            &isamp,
974
            &osamp
975
            );
976
        ret += isamp;
977
        swlim -= isamp;
978
        pos += isamp;
979
        live += osamp;
980
        wpos = (wpos + osamp) % hwsamples;
981
        total += osamp;
982
    }
983

    
984
    sw->total_hw_samples_mixed += total;
985
    sw->empty = sw->total_hw_samples_mixed == 0;
986

    
987
#ifdef DEBUG_OUT
988
    dolog (
989
        "%s: write size %d ret %d total sw %d\n",
990
        SW_NAME (sw),
991
        size >> sw->info.shift,
992
        ret,
993
        sw->total_hw_samples_mixed
994
        );
995
#endif
996

    
997
    return ret << sw->info.shift;
998
}
999

    
1000
#ifdef DEBUG_AUDIO
1001
static void audio_pcm_print_info (const char *cap, struct audio_pcm_info *info)
1002
{
1003
    dolog ("%s: bits %d, sign %d, freq %d, nchan %d\n",
1004
           cap, info->bits, info->sign, info->freq, info->nchannels);
1005
}
1006
#endif
1007

    
1008
#define DAC
1009
#include "audio_template.h"
1010
#undef DAC
1011
#include "audio_template.h"
1012

    
1013
int AUD_write (SWVoiceOut *sw, void *buf, int size)
1014
{
1015
    int bytes;
1016

    
1017
    if (!sw) {
1018
        /* XXX: Consider options */
1019
        return size;
1020
    }
1021

    
1022
    if (!sw->hw->enabled) {
1023
        dolog ("Writing to disabled voice %s\n", SW_NAME (sw));
1024
        return 0;
1025
    }
1026

    
1027
    bytes = sw->hw->pcm_ops->write (sw, buf, size);
1028
    return bytes;
1029
}
1030

    
1031
int AUD_read (SWVoiceIn *sw, void *buf, int size)
1032
{
1033
    int bytes;
1034

    
1035
    if (!sw) {
1036
        /* XXX: Consider options */
1037
        return size;
1038
    }
1039

    
1040
    if (!sw->hw->enabled) {
1041
        dolog ("Reading from disabled voice %s\n", SW_NAME (sw));
1042
        return 0;
1043
    }
1044

    
1045
    bytes = sw->hw->pcm_ops->read (sw, buf, size);
1046
    return bytes;
1047
}
1048

    
1049
int AUD_get_buffer_size_out (SWVoiceOut *sw)
1050
{
1051
    return sw->hw->samples << sw->hw->info.shift;
1052
}
1053

    
1054
void AUD_set_active_out (SWVoiceOut *sw, int on)
1055
{
1056
    HWVoiceOut *hw;
1057

    
1058
    if (!sw) {
1059
        return;
1060
    }
1061

    
1062
    hw = sw->hw;
1063
    if (sw->active != on) {
1064
        SWVoiceOut *temp_sw;
1065

    
1066
        if (on) {
1067
            int total;
1068

    
1069
            hw->pending_disable = 0;
1070
            if (!hw->enabled) {
1071
                hw->enabled = 1;
1072
                hw->pcm_ops->ctl_out (hw, VOICE_ENABLE);
1073
            }
1074

    
1075
            if (sw->empty) {
1076
                total = 0;
1077
            }
1078
        }
1079
        else {
1080
            if (hw->enabled) {
1081
                int nb_active = 0;
1082

    
1083
                for (temp_sw = hw->sw_head.lh_first; temp_sw;
1084
                     temp_sw = temp_sw->entries.le_next) {
1085
                    nb_active += temp_sw->active != 0;
1086
                }
1087

    
1088
                hw->pending_disable = nb_active == 1;
1089
            }
1090
        }
1091
        sw->active = on;
1092
    }
1093
}
1094

    
1095
void AUD_set_active_in (SWVoiceIn *sw, int on)
1096
{
1097
    HWVoiceIn *hw;
1098

    
1099
    if (!sw) {
1100
        return;
1101
    }
1102

    
1103
    hw = sw->hw;
1104
    if (sw->active != on) {
1105
        SWVoiceIn *temp_sw;
1106

    
1107
        if (on) {
1108
            if (!hw->enabled) {
1109
                hw->enabled = 1;
1110
                hw->pcm_ops->ctl_in (hw, VOICE_ENABLE);
1111
            }
1112
            sw->total_hw_samples_acquired = hw->total_samples_captured;
1113
        }
1114
        else {
1115
            if (hw->enabled) {
1116
                int nb_active = 0;
1117

    
1118
                for (temp_sw = hw->sw_head.lh_first; temp_sw;
1119
                     temp_sw = temp_sw->entries.le_next) {
1120
                    nb_active += temp_sw->active != 0;
1121
                }
1122

    
1123
                if (nb_active == 1) {
1124
                    hw->enabled = 0;
1125
                    hw->pcm_ops->ctl_in (hw, VOICE_DISABLE);
1126
                }
1127
            }
1128
        }
1129
        sw->active = on;
1130
    }
1131
}
1132

    
1133
static int audio_get_avail (SWVoiceIn *sw)
1134
{
1135
    int live;
1136

    
1137
    if (!sw) {
1138
        return 0;
1139
    }
1140

    
1141
    live = sw->hw->total_samples_captured - sw->total_hw_samples_acquired;
1142
    if (audio_bug (AUDIO_FUNC, live < 0 || live > sw->hw->samples)) {
1143
        dolog ("live=%d sw->hw->samples=%d\n", live, sw->hw->samples);
1144
        return 0;
1145
    }
1146

    
1147
    ldebug (
1148
        "%s: get_avail live %d ret %lld\n",
1149
        SW_NAME (sw),
1150
        live, (((int64_t) live << 32) / sw->ratio) << sw->info.shift
1151
        );
1152

    
1153
    return (((int64_t) live << 32) / sw->ratio) << sw->info.shift;
1154
}
1155

    
1156
static int audio_get_free (SWVoiceOut *sw)
1157
{
1158
    int live, dead;
1159

    
1160
    if (!sw) {
1161
        return 0;
1162
    }
1163

    
1164
    live = sw->total_hw_samples_mixed;
1165

    
1166
    if (audio_bug (AUDIO_FUNC, live < 0 || live > sw->hw->samples)) {
1167
        dolog ("live=%d sw->hw->samples=%d\n", live, sw->hw->samples);
1168
        return 0;
1169
    }
1170

    
1171
    dead = sw->hw->samples - live;
1172

    
1173
#ifdef DEBUG_OUT
1174
    dolog ("%s: get_free live %d dead %d ret %lld\n",
1175
           SW_NAME (sw),
1176
           live, dead, (((int64_t) dead << 32) / sw->ratio) << sw->info.shift);
1177
#endif
1178

    
1179
    return (((int64_t) dead << 32) / sw->ratio) << sw->info.shift;
1180
}
1181

    
1182
static void audio_run_out (AudioState *s)
1183
{
1184
    HWVoiceOut *hw = NULL;
1185
    SWVoiceOut *sw;
1186

    
1187
    while ((hw = audio_pcm_hw_find_any_enabled_out (s, hw))) {
1188
        int played;
1189
        int live, free, nb_live, cleanup_required;
1190

    
1191
        live = audio_pcm_hw_get_live_out2 (hw, &nb_live);
1192
        if (!nb_live) {
1193
            live = 0;
1194
        }
1195

    
1196
        if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
1197
            dolog ("live=%d hw->samples=%d\n", live, hw->samples);
1198
            continue;
1199
        }
1200

    
1201
        if (hw->pending_disable && !nb_live) {
1202
#ifdef DEBUG_OUT
1203
            dolog ("Disabling voice\n");
1204
#endif
1205
            hw->enabled = 0;
1206
            hw->pending_disable = 0;
1207
            hw->pcm_ops->ctl_out (hw, VOICE_DISABLE);
1208
            continue;
1209
        }
1210

    
1211
        if (!live) {
1212
            for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1213
                if (sw->active) {
1214
                    free = audio_get_free (sw);
1215
                    if (free > 0) {
1216
                        sw->callback.fn (sw->callback.opaque, free);
1217
                    }
1218
                }
1219
            }
1220
            continue;
1221
        }
1222

    
1223
        played = hw->pcm_ops->run_out (hw);
1224
        if (audio_bug (AUDIO_FUNC, hw->rpos >= hw->samples)) {
1225
            dolog ("hw->rpos=%d hw->samples=%d played=%d\n",
1226
                   hw->rpos, hw->samples, played);
1227
            hw->rpos = 0;
1228
        }
1229

    
1230
#ifdef DEBUG_OUT
1231
        dolog ("played=%d\n", played);
1232
#endif
1233

    
1234
        if (played) {
1235
            hw->ts_helper += played;
1236
        }
1237

    
1238
        cleanup_required = 0;
1239
        for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1240
            if (!sw->active && sw->empty) {
1241
                continue;
1242
            }
1243

    
1244
            if (audio_bug (AUDIO_FUNC, played > sw->total_hw_samples_mixed)) {
1245
                dolog ("played=%d sw->total_hw_samples_mixed=%d\n",
1246
                       played, sw->total_hw_samples_mixed);
1247
                played = sw->total_hw_samples_mixed;
1248
            }
1249

    
1250
            sw->total_hw_samples_mixed -= played;
1251

    
1252
            if (!sw->total_hw_samples_mixed) {
1253
                sw->empty = 1;
1254
                cleanup_required |= !sw->active && !sw->callback.fn;
1255
            }
1256

    
1257
            if (sw->active) {
1258
                free = audio_get_free (sw);
1259
                if (free > 0) {
1260
                    sw->callback.fn (sw->callback.opaque, free);
1261
                }
1262
            }
1263
        }
1264

    
1265
        if (cleanup_required) {
1266
        restart:
1267
            for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1268
                if (!sw->active && !sw->callback.fn) {
1269
#ifdef DEBUG_PLIVE
1270
                    dolog ("Finishing with old voice\n");
1271
#endif
1272
                    audio_close_out (s, sw);
1273
                    goto restart; /* play it safe */
1274
                }
1275
            }
1276
        }
1277
    }
1278
}
1279

    
1280
static void audio_run_in (AudioState *s)
1281
{
1282
    HWVoiceIn *hw = NULL;
1283

    
1284
    while ((hw = audio_pcm_hw_find_any_enabled_in (s, hw))) {
1285
        SWVoiceIn *sw;
1286
        int captured, min;
1287

    
1288
        captured = hw->pcm_ops->run_in (hw);
1289

    
1290
        min = audio_pcm_hw_find_min_in (hw);
1291
        hw->total_samples_captured += captured - min;
1292
        hw->ts_helper += captured;
1293

    
1294
        for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1295
            sw->total_hw_samples_acquired -= min;
1296

    
1297
            if (sw->active) {
1298
                int avail;
1299

    
1300
                avail = audio_get_avail (sw);
1301
                if (avail > 0) {
1302
                    sw->callback.fn (sw->callback.opaque, avail);
1303
                }
1304
            }
1305
        }
1306
    }
1307
}
1308

    
1309
static struct audio_option audio_options[] = {
1310
    /* DAC */
1311
    {"DAC_FIXED_SETTINGS", AUD_OPT_BOOL, &conf.fixed_out.enabled,
1312
     "Use fixed settings for host DAC", NULL, 0},
1313

    
1314
    {"DAC_FIXED_FREQ", AUD_OPT_INT, &conf.fixed_out.settings.freq,
1315
     "Frequency for fixed host DAC", NULL, 0},
1316

    
1317
    {"DAC_FIXED_FMT", AUD_OPT_FMT, &conf.fixed_out.settings.fmt,
1318
     "Format for fixed host DAC", NULL, 0},
1319

    
1320
    {"DAC_FIXED_CHANNELS", AUD_OPT_INT, &conf.fixed_out.settings.nchannels,
1321
     "Number of channels for fixed DAC (1 - mono, 2 - stereo)", NULL, 0},
1322

    
1323
    {"DAC_VOICES", AUD_OPT_INT, &conf.fixed_out.nb_voices,
1324
     "Number of voices for DAC", NULL, 0},
1325

    
1326
    /* ADC */
1327
    {"ADC_FIXED_SETTINGS", AUD_OPT_BOOL, &conf.fixed_in.enabled,
1328
     "Use fixed settings for host ADC", NULL, 0},
1329

    
1330
    {"ADC_FIXED_FREQ", AUD_OPT_INT, &conf.fixed_in.settings.freq,
1331
     "Frequency for fixed host ADC", NULL, 0},
1332

    
1333
    {"ADC_FIXED_FMT", AUD_OPT_FMT, &conf.fixed_in.settings.fmt,
1334
     "Format for fixed host ADC", NULL, 0},
1335

    
1336
    {"ADC_FIXED_CHANNELS", AUD_OPT_INT, &conf.fixed_in.settings.nchannels,
1337
     "Number of channels for fixed ADC (1 - mono, 2 - stereo)", NULL, 0},
1338

    
1339
    {"ADC_VOICES", AUD_OPT_INT, &conf.fixed_in.nb_voices,
1340
     "Number of voices for ADC", NULL, 0},
1341

    
1342
    /* Misc */
1343
    {"TIMER_PERIOD", AUD_OPT_INT, &conf.period.hz,
1344
     "Timer period in HZ (0 - use lowest possible)", NULL, 0},
1345

    
1346
    {"PLIVE", AUD_OPT_BOOL, &conf.plive,
1347
     "(undocumented)", NULL, 0},
1348

    
1349
    {NULL, 0, NULL, NULL, NULL, 0}
1350
};
1351

    
1352
void AUD_help (void)
1353
{
1354
    size_t i;
1355

    
1356
    audio_process_options ("AUDIO", audio_options);
1357
    for (i = 0; i < sizeof (drvtab) / sizeof (drvtab[0]); i++) {
1358
        struct audio_driver *d = drvtab[i];
1359
        if (d->options) {
1360
            audio_process_options (d->name, d->options);
1361
        }
1362
    }
1363

    
1364
    printf ("Audio options:\n");
1365
    audio_print_options ("AUDIO", audio_options);
1366
    printf ("\n");
1367

    
1368
    printf ("Available drivers:\n");
1369

    
1370
    for (i = 0; i < sizeof (drvtab) / sizeof (drvtab[0]); i++) {
1371
        struct audio_driver *d = drvtab[i];
1372

    
1373
        printf ("Name: %s\n", d->name);
1374
        printf ("Description: %s\n", d->descr);
1375

    
1376
        switch (d->max_voices_out) {
1377
        case 0:
1378
            printf ("Does not support DAC\n");
1379
            break;
1380
        case 1:
1381
            printf ("One DAC voice\n");
1382
            break;
1383
        case INT_MAX:
1384
            printf ("Theoretically supports many DAC voices\n");
1385
            break;
1386
        default:
1387
            printf ("Theoretically supports upto %d DAC voices\n",
1388
                     d->max_voices_out);
1389
            break;
1390
        }
1391

    
1392
        switch (d->max_voices_in) {
1393
        case 0:
1394
            printf ("Does not support ADC\n");
1395
            break;
1396
        case 1:
1397
            printf ("One ADC voice\n");
1398
            break;
1399
        case INT_MAX:
1400
            printf ("Theoretically supports many ADC voices\n");
1401
            break;
1402
        default:
1403
            printf ("Theoretically supports upto %d ADC voices\n",
1404
                     d->max_voices_in);
1405
            break;
1406
        }
1407

    
1408
        if (d->options) {
1409
            printf ("Options:\n");
1410
            audio_print_options (d->name, d->options);
1411
        }
1412
        else {
1413
            printf ("No options\n");
1414
        }
1415
        printf ("\n");
1416
    }
1417

    
1418
    printf (
1419
        "Options are settable through environment variables.\n"
1420
        "Example:\n"
1421
#ifdef _WIN32
1422
        "  set QEMU_AUDIO_DRV=wav\n"
1423
        "  set QEMU_WAV_PATH=c:/tune.wav\n"
1424
#else
1425
        "  export QEMU_AUDIO_DRV=wav\n"
1426
        "  export QEMU_WAV_PATH=$HOME/tune.wav\n"
1427
        "(for csh replace export with setenv in the above)\n"
1428
#endif
1429
        "  qemu ...\n\n"
1430
        );
1431
}
1432

    
1433
void audio_timer (void *opaque)
1434
{
1435
    AudioState *s = opaque;
1436

    
1437
    audio_run_out (s);
1438
    audio_run_in (s);
1439

    
1440
    qemu_mod_timer (s->ts, qemu_get_clock (vm_clock) + conf.period.ticks);
1441
}
1442

    
1443
static int audio_driver_init (AudioState *s, struct audio_driver *drv)
1444
{
1445
    if (drv->options) {
1446
        audio_process_options (drv->name, drv->options);
1447
    }
1448
    s->drv_opaque = drv->init ();
1449

    
1450
    if (s->drv_opaque) {
1451
        if (s->nb_hw_voices_out > drv->max_voices_out) {
1452
            if (!drv->max_voices_out) {
1453
                dolog ("`%s' does not support DAC\n", drv->name);
1454
            }
1455
            else {
1456
                dolog (
1457
                    "`%s' does not support %d multiple DAC voicess\n"
1458
                    "Resetting to %d\n",
1459
                    drv->name,
1460
                    s->nb_hw_voices_out,
1461
                    drv->max_voices_out
1462
                    );
1463
            }
1464
            s->nb_hw_voices_out = drv->max_voices_out;
1465
        }
1466

    
1467

    
1468
        if (!drv->voice_size_in && drv->max_voices_in) {
1469
            ldebug ("warning: No ADC voice size defined for `%s'\n",
1470
                    drv->name);
1471
            drv->max_voices_in = 0;
1472
        }
1473

    
1474
        if (!drv->voice_size_out && drv->max_voices_out) {
1475
            ldebug ("warning: No DAC voice size defined for `%s'\n",
1476
                    drv->name);
1477
        }
1478

    
1479
        if (drv->voice_size_in && !drv->max_voices_in) {
1480
            ldebug ("warning: `%s' ADC voice size %d, zero voices \n",
1481
                    drv->name, drv->voice_size_out);
1482
        }
1483

    
1484
        if (drv->voice_size_out && !drv->max_voices_out) {
1485
            ldebug ("warning: `%s' DAC voice size %d, zero voices \n",
1486
                    drv->name, drv->voice_size_in);
1487
        }
1488

    
1489
        if (s->nb_hw_voices_in > drv->max_voices_in) {
1490
            if (!drv->max_voices_in) {
1491
                ldebug ("`%s' does not support ADC\n", drv->name);
1492
            }
1493
            else {
1494
                dolog (
1495
                    "`%s' does not support %d multiple ADC voices\n"
1496
                    "Resetting to %d\n",
1497
                    drv->name,
1498
                    s->nb_hw_voices_in,
1499
                    drv->max_voices_in
1500
                    );
1501
            }
1502
            s->nb_hw_voices_in = drv->max_voices_in;
1503
        }
1504

    
1505
        LIST_INIT (&s->hw_head_out);
1506
        LIST_INIT (&s->hw_head_in);
1507
        s->drv = drv;
1508
        return 0;
1509
    }
1510
    else {
1511
        dolog ("Could not init `%s' audio driver\n", drv->name);
1512
        return -1;
1513
    }
1514
}
1515

    
1516
static void audio_vm_stop_handler (void *opaque, int reason)
1517
{
1518
    AudioState *s = opaque;
1519
    HWVoiceOut *hwo = NULL;
1520
    HWVoiceIn *hwi = NULL;
1521
    int op = reason ? VOICE_ENABLE : VOICE_DISABLE;
1522

    
1523
    while ((hwo = audio_pcm_hw_find_any_out (s, hwo))) {
1524
        if (!hwo->pcm_ops) {
1525
            continue;
1526
        }
1527

    
1528
        if (hwo->enabled != reason) {
1529
            hwo->pcm_ops->ctl_out (hwo, op);
1530
        }
1531
    }
1532

    
1533
    while ((hwi = audio_pcm_hw_find_any_in (s, hwi))) {
1534
        if (!hwi->pcm_ops) {
1535
            continue;
1536
        }
1537

    
1538
        if (hwi->enabled != reason) {
1539
            hwi->pcm_ops->ctl_in (hwi, op);
1540
        }
1541
    }
1542
}
1543

    
1544
static void audio_atexit (void)
1545
{
1546
    AudioState *s = &glob_audio_state;
1547
    HWVoiceOut *hwo = NULL;
1548
    HWVoiceIn *hwi = NULL;
1549

    
1550
    while ((hwo = audio_pcm_hw_find_any_out (s, hwo))) {
1551
        if (!hwo->pcm_ops) {
1552
            continue;
1553
        }
1554

    
1555
        if (hwo->enabled) {
1556
            hwo->pcm_ops->ctl_out (hwo, VOICE_DISABLE);
1557
        }
1558
        hwo->pcm_ops->fini_out (hwo);
1559
    }
1560

    
1561
    while ((hwi = audio_pcm_hw_find_any_in (s, hwi))) {
1562
        if (!hwi->pcm_ops) {
1563
            continue;
1564
        }
1565

    
1566
        if (hwi->enabled) {
1567
            hwi->pcm_ops->ctl_in (hwi, VOICE_DISABLE);
1568
        }
1569
        hwi->pcm_ops->fini_in (hwi);
1570
    }
1571

    
1572
    if (s->drv) {
1573
        s->drv->fini (s->drv_opaque);
1574
    }
1575
}
1576

    
1577
static void audio_save (QEMUFile *f, void *opaque)
1578
{
1579
    (void) f;
1580
    (void) opaque;
1581
}
1582

    
1583
static int audio_load (QEMUFile *f, void *opaque, int version_id)
1584
{
1585
    (void) f;
1586
    (void) opaque;
1587

    
1588
    if (version_id != 1) {
1589
        return -EINVAL;
1590
    }
1591

    
1592
    return 0;
1593
}
1594

    
1595
void AUD_register_card (AudioState *s, const char *name, QEMUSoundCard *card)
1596
{
1597
    card->audio = s;
1598
    card->name = qemu_strdup (name);
1599
    memset (&card->entries, 0, sizeof (card->entries));
1600
    LIST_INSERT_HEAD (&s->card_head, card, entries);
1601
}
1602

    
1603
void AUD_remove_card (QEMUSoundCard *card)
1604
{
1605
    LIST_REMOVE (card, entries);
1606
    card->audio = NULL;
1607
    qemu_free (card->name);
1608
}
1609

    
1610
AudioState *AUD_init (void)
1611
{
1612
    size_t i;
1613
    int done = 0;
1614
    const char *drvname;
1615
    AudioState *s = &glob_audio_state;
1616

    
1617
    audio_process_options ("AUDIO", audio_options);
1618

    
1619
    s->nb_hw_voices_out = conf.fixed_out.nb_voices;
1620
    s->nb_hw_voices_in = conf.fixed_in.nb_voices;
1621

    
1622
    if (s->nb_hw_voices_out <= 0) {
1623
        dolog ("Bogus number of DAC voices %d\n",
1624
               s->nb_hw_voices_out);
1625
        s->nb_hw_voices_out = 1;
1626
    }
1627

    
1628
    if (s->nb_hw_voices_in <= 0) {
1629
        dolog ("Bogus number of ADC voices %d\n",
1630
               s->nb_hw_voices_in);
1631
        s->nb_hw_voices_in = 1;
1632
    }
1633

    
1634
    {
1635
        int def;
1636
        drvname = audio_get_conf_str ("QEMU_AUDIO_DRV", NULL, &def);
1637
    }
1638

    
1639
    s->ts = qemu_new_timer (vm_clock, audio_timer, s);
1640
    if (!s->ts) {
1641
        dolog ("Could not create audio timer\n");
1642
        return NULL;
1643
    }
1644

    
1645
    if (drvname) {
1646
        int found = 0;
1647

    
1648
        for (i = 0; i < sizeof (drvtab) / sizeof (drvtab[0]); i++) {
1649
            if (!strcmp (drvname, drvtab[i]->name)) {
1650
                done = !audio_driver_init (s, drvtab[i]);
1651
                found = 1;
1652
                break;
1653
            }
1654
        }
1655

    
1656
        if (!found) {
1657
            dolog ("Unknown audio driver `%s'\n", drvname);
1658
            dolog ("Run with -audio-help to list available drivers\n");
1659
        }
1660
    }
1661

    
1662
    if (!done) {
1663
        for (i = 0; !done && i < sizeof (drvtab) / sizeof (drvtab[0]); i++) {
1664
            if (drvtab[i]->can_be_default) {
1665
                done = !audio_driver_init (s, drvtab[i]);
1666
            }
1667
        }
1668
    }
1669

    
1670
    if (!done) {
1671
        done = !audio_driver_init (s, &no_audio_driver);
1672
        if (!done) {
1673
            dolog ("Could not initialize audio subsystem\n");
1674
        }
1675
        else {
1676
            dolog ("warning: Using timer based audio emulation\n");
1677
        }
1678
    }
1679

    
1680
    if (done) {
1681
        if (conf.period.hz <= 0) {
1682
            if (conf.period.hz < 0) {
1683
                dolog ("warning: Timer period is negative - %d "
1684
                       "treating as zero\n",
1685
                       conf.period.hz);
1686
            }
1687
            conf.period.ticks = 1;
1688
        }
1689
        else {
1690
            conf.period.ticks = ticks_per_sec / conf.period.hz;
1691
        }
1692

    
1693
        qemu_add_vm_stop_handler (audio_vm_stop_handler, NULL);
1694
    }
1695
    else {
1696
        qemu_del_timer (s->ts);
1697
        return NULL;
1698
    }
1699

    
1700
    LIST_INIT (&s->card_head);
1701
    register_savevm ("audio", 0, 1, audio_save, audio_load, s);
1702
    atexit (audio_atexit);
1703
    qemu_mod_timer (s->ts, qemu_get_clock (vm_clock) + conf.period.ticks);
1704
    return s;
1705
}