~ubuntu-branches/ubuntu/hardy/kvm/hardy-backports

« back to all changes in this revision

Viewing changes to qemu/audio/alsaaudio.c

  • Committer: Bazaar Package Importer
  • Author(s): Soren Hansen
  • Date: 2008-02-26 13:10:57 UTC
  • mfrom: (1.1.18 upstream)
  • Revision ID: james.westby@ubuntu.com-20080226131057-s67x6l89mtjw1x9b
Tags: 1:62+dfsg-0ubuntu1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
86
86
};
87
87
 
88
88
struct alsa_params_req {
89
 
    unsigned int freq;
90
 
    audfmt_e fmt;
91
 
    unsigned int nchannels;
 
89
    int freq;
 
90
    snd_pcm_format_t fmt;
 
91
    int nchannels;
92
92
    unsigned int buffer_size;
93
93
    unsigned int period_size;
94
94
};
96
96
struct alsa_params_obt {
97
97
    int freq;
98
98
    audfmt_e fmt;
 
99
    int endianness;
99
100
    int nchannels;
100
101
    snd_pcm_uframes_t samples;
101
102
};
143
144
    return audio_pcm_sw_write (sw, buf, len);
144
145
}
145
146
 
146
 
static int aud_to_alsafmt (audfmt_e fmt)
 
147
static snd_pcm_format_t aud_to_alsafmt (audfmt_e fmt)
147
148
{
148
149
    switch (fmt) {
149
150
    case AUD_FMT_S8:
173
174
    }
174
175
}
175
176
 
176
 
static int alsa_to_audfmt (int alsafmt, audfmt_e *fmt, int *endianness)
 
177
static int alsa_to_audfmt (snd_pcm_format_t alsafmt, audfmt_e *fmt,
 
178
                           int *endianness)
177
179
{
178
180
    switch (alsafmt) {
179
181
    case SND_PCM_FORMAT_S8:
234
236
    return 0;
235
237
}
236
238
 
237
 
#if defined DEBUG_MISMATCHES || defined DEBUG
238
239
static void alsa_dump_info (struct alsa_params_req *req,
239
240
                            struct alsa_params_obt *obt)
240
241
{
248
249
           req->buffer_size, req->period_size);
249
250
    dolog ("obtained: samples %ld\n", obt->samples);
250
251
}
251
 
#endif
252
252
 
253
253
static void alsa_set_threshold (snd_pcm_t *handle, snd_pcm_uframes_t threshold)
254
254
{
291
291
    unsigned int period_size, buffer_size;
292
292
    snd_pcm_uframes_t obt_buffer_size;
293
293
    const char *typ = in ? "ADC" : "DAC";
 
294
    snd_pcm_format_t obtfmt;
294
295
 
295
296
    freq = req->freq;
296
297
    period_size = req->period_size;
327
328
    }
328
329
 
329
330
    err = snd_pcm_hw_params_set_format (handle, hw_params, req->fmt);
330
 
    if (err < 0) {
 
331
    if (err < 0 && conf.verbose) {
331
332
        alsa_logerr2 (err, typ, "Failed to set format %d\n", req->fmt);
332
 
        goto err;
333
333
    }
334
334
 
335
335
    err = snd_pcm_hw_params_set_rate_near (handle, hw_params, &freq, 0);
494
494
        goto err;
495
495
    }
496
496
 
 
497
    err = snd_pcm_hw_params_get_format (hw_params, &obtfmt);
 
498
    if (err < 0) {
 
499
        alsa_logerr2 (err, typ, "Failed to get format\n");
 
500
        goto err;
 
501
    }
 
502
 
 
503
    if (alsa_to_audfmt (obtfmt, &obt->fmt, &obt->endianness)) {
 
504
        dolog ("Invalid format was returned %d\n", obtfmt);
 
505
        goto err;
 
506
    }
 
507
 
497
508
    err = snd_pcm_prepare (handle);
498
509
    if (err < 0) {
499
510
        alsa_logerr2 (err, typ, "Could not prepare handle %p\n", handle);
504
515
        snd_pcm_uframes_t threshold;
505
516
        int bytes_per_sec;
506
517
 
507
 
        bytes_per_sec = freq
508
 
            << (nchannels == 2)
509
 
            << (req->fmt == AUD_FMT_S16 || req->fmt == AUD_FMT_U16);
 
518
        bytes_per_sec = freq << (nchannels == 2);
 
519
 
 
520
        switch (obt->fmt) {
 
521
        case AUD_FMT_S8:
 
522
        case AUD_FMT_U8:
 
523
            break;
 
524
 
 
525
        case AUD_FMT_S16:
 
526
        case AUD_FMT_U16:
 
527
            bytes_per_sec <<= 1;
 
528
            break;
 
529
 
 
530
        case AUD_FMT_S32:
 
531
        case AUD_FMT_U32:
 
532
            bytes_per_sec <<= 2;
 
533
            break;
 
534
        }
510
535
 
511
536
        threshold = (conf.threshold * bytes_per_sec) / 1000;
512
537
        alsa_set_threshold (handle, threshold);
513
538
    }
514
539
 
515
 
    obt->fmt = req->fmt;
516
540
    obt->nchannels = nchannels;
517
541
    obt->freq = freq;
518
542
    obt->samples = obt_buffer_size;
 
543
 
519
544
    *handlep = handle;
520
545
 
521
 
#if defined DEBUG_MISMATCHES || defined DEBUG
522
 
    if (obt->fmt != req->fmt ||
523
 
        obt->nchannels != req->nchannels ||
524
 
        obt->freq != req->freq) {
525
 
        dolog ("Audio paramters mismatch for %s\n", typ);
 
546
    if (conf.verbose &&
 
547
        (obt->fmt != req->fmt ||
 
548
         obt->nchannels != req->nchannels ||
 
549
         obt->freq != req->freq)) {
 
550
        dolog ("Audio paramters for %s\n", typ);
526
551
        alsa_dump_info (req, obt);
527
552
    }
528
 
#endif
529
553
 
530
554
#ifdef DEBUG
531
555
    alsa_dump_info (req, obt);
665
689
    ALSAVoiceOut *alsa = (ALSAVoiceOut *) hw;
666
690
    struct alsa_params_req req;
667
691
    struct alsa_params_obt obt;
668
 
    audfmt_e effective_fmt;
669
 
    int endianness;
670
 
    int err;
671
692
    snd_pcm_t *handle;
672
693
    audsettings_t obt_as;
673
694
 
681
702
        return -1;
682
703
    }
683
704
 
684
 
    err = alsa_to_audfmt (obt.fmt, &effective_fmt, &endianness);
685
 
    if (err) {
686
 
        alsa_anal_close (&handle);
687
 
        return -1;
688
 
    }
689
 
 
690
705
    obt_as.freq = obt.freq;
691
706
    obt_as.nchannels = obt.nchannels;
692
 
    obt_as.fmt = effective_fmt;
693
 
    obt_as.endianness = endianness;
 
707
    obt_as.fmt = obt.fmt;
 
708
    obt_as.endianness = obt.endianness;
694
709
 
695
710
    audio_pcm_init_info (&hw->info, &obt_as);
696
711
    hw->samples = obt.samples;
751
766
    ALSAVoiceIn *alsa = (ALSAVoiceIn *) hw;
752
767
    struct alsa_params_req req;
753
768
    struct alsa_params_obt obt;
754
 
    int endianness;
755
 
    int err;
756
 
    audfmt_e effective_fmt;
757
769
    snd_pcm_t *handle;
758
770
    audsettings_t obt_as;
759
771
 
767
779
        return -1;
768
780
    }
769
781
 
770
 
    err = alsa_to_audfmt (obt.fmt, &effective_fmt, &endianness);
771
 
    if (err) {
772
 
        alsa_anal_close (&handle);
773
 
        return -1;
774
 
    }
775
 
 
776
782
    obt_as.freq = obt.freq;
777
783
    obt_as.nchannels = obt.nchannels;
778
 
    obt_as.fmt = effective_fmt;
779
 
    obt_as.endianness = endianness;
 
784
    obt_as.fmt = obt.fmt;
 
785
    obt_as.endianness = obt.endianness;
780
786
 
781
787
    audio_pcm_init_info (&hw->info, &obt_as);
782
788
    hw->samples = obt.samples;