Home | History | Annotate | Download | only in audio
      1 /*
      2  * QEMU Audio subsystem header
      3  *
      4  * Copyright (c) 2005 Vassili Karpov (malc)
      5  *
      6  * Permission is hereby granted, free of charge, to any person obtaining a copy
      7  * of this software and associated documentation files (the "Software"), to deal
      8  * in the Software without restriction, including without limitation the rights
      9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     10  * copies of the Software, and to permit persons to whom the Software is
     11  * furnished to do so, subject to the following conditions:
     12  *
     13  * The above copyright notice and this permission notice shall be included in
     14  * all copies or substantial portions of the Software.
     15  *
     16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
     19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
     21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
     22  * THE SOFTWARE.
     23  */
     24 
     25 #ifdef DAC
     26 #define NAME "playback"
     27 #define HWBUF hw->mix_buf
     28 #define TYPE out
     29 #define HW HWVoiceOut
     30 #define SW SWVoiceOut
     31 #else
     32 #define NAME "capture"
     33 #define TYPE in
     34 #define HW HWVoiceIn
     35 #define SW SWVoiceIn
     36 #define HWBUF hw->conv_buf
     37 #endif
     38 
     39 static void glue (audio_init_nb_voices_, TYPE) (struct audio_driver *drv)
     40 {
     41     AudioState *s = &glob_audio_state;
     42     int max_voices = glue (drv->max_voices_, TYPE);
     43     int voice_size = glue (drv->voice_size_, TYPE);
     44 
     45     if (glue (s->nb_hw_voices_, TYPE) > max_voices) {
     46         if (!max_voices) {
     47 #ifdef DAC
     48             dolog ("Driver `%s' does not support " NAME "\n", drv->name);
     49 #endif
     50         }
     51         else {
     52             dolog ("Driver `%s' does not support %d " NAME " voices, max %d\n",
     53                    drv->name,
     54                    glue (s->nb_hw_voices_, TYPE),
     55                    max_voices);
     56         }
     57         glue (s->nb_hw_voices_, TYPE) = max_voices;
     58     }
     59 
     60     if (audio_bug (AUDIO_FUNC, !voice_size && max_voices)) {
     61         dolog ("drv=`%s' voice_size=0 max_voices=%d\n",
     62                drv->name, max_voices);
     63         glue (s->nb_hw_voices_, TYPE) = 0;
     64     }
     65 
     66     if (audio_bug (AUDIO_FUNC, voice_size && !max_voices)) {
     67         dolog ("drv=`%s' voice_size=%d max_voices=0\n",
     68                drv->name, voice_size);
     69     }
     70 }
     71 
     72 static void glue (audio_pcm_hw_free_resources_, TYPE) (HW *hw)
     73 {
     74     if (HWBUF) {
     75         qemu_free (HWBUF);
     76     }
     77 
     78     HWBUF = NULL;
     79 }
     80 
     81 static int glue (audio_pcm_hw_alloc_resources_, TYPE) (HW *hw)
     82 {
     83     HWBUF = audio_calloc (AUDIO_FUNC, hw->samples, sizeof (struct st_sample));
     84     if (!HWBUF) {
     85         dolog ("Could not allocate " NAME " buffer (%d samples)\n",
     86                hw->samples);
     87         return -1;
     88     }
     89 
     90     return 0;
     91 }
     92 
     93 static void glue (audio_pcm_sw_free_resources_, TYPE) (SW *sw)
     94 {
     95     if (sw->buf) {
     96         qemu_free (sw->buf);
     97     }
     98 
     99     if (sw->rate) {
    100         st_rate_stop (sw->rate);
    101     }
    102 
    103     sw->buf = NULL;
    104     sw->rate = NULL;
    105 }
    106 
    107 static int glue (audio_pcm_sw_alloc_resources_, TYPE) (SW *sw)
    108 {
    109     int samples;
    110 
    111 #ifdef DAC
    112     samples = sw->hw->samples;
    113 #else
    114     samples = ((int64_t) sw->hw->samples << 32) / sw->ratio;
    115 #endif
    116 
    117     sw->buf = audio_calloc (AUDIO_FUNC, samples, sizeof (struct st_sample));
    118     if (!sw->buf) {
    119         dolog ("Could not allocate buffer for `%s' (%d samples)\n",
    120                SW_NAME (sw), samples);
    121         return -1;
    122     }
    123 
    124 #ifdef DAC
    125     sw->rate = st_rate_start (sw->info.freq, sw->hw->info.freq);
    126 #else
    127     sw->rate = st_rate_start (sw->hw->info.freq, sw->info.freq);
    128 #endif
    129     if (!sw->rate) {
    130         qemu_free (sw->buf);
    131         sw->buf = NULL;
    132         return -1;
    133     }
    134     return 0;
    135 }
    136 
    137 static int glue (audio_pcm_sw_init_, TYPE) (
    138     SW *sw,
    139     HW *hw,
    140     const char *name,
    141     struct audsettings *as
    142     )
    143 {
    144     int err;
    145 
    146     audio_pcm_init_info (&sw->info, as);
    147     sw->hw = hw;
    148     sw->active = 0;
    149 #ifdef DAC
    150     sw->ratio = ((int64_t) sw->hw->info.freq << 32) / sw->info.freq;
    151     sw->total_hw_samples_mixed = 0;
    152     sw->empty = 1;
    153 #else
    154     sw->ratio = ((int64_t) sw->info.freq << 32) / sw->hw->info.freq;
    155 #endif
    156 
    157 #ifdef DAC
    158     sw->conv = mixeng_conv
    159 #else
    160     sw->clip = mixeng_clip
    161 #endif
    162         [sw->info.nchannels == 2]
    163         [sw->info.sign]
    164         [sw->info.swap_endianness]
    165         [audio_bits_to_index (sw->info.bits)];
    166 
    167     sw->name = qemu_strdup (name);
    168     err = glue (audio_pcm_sw_alloc_resources_, TYPE) (sw);
    169     if (err) {
    170         qemu_free (sw->name);
    171         sw->name = NULL;
    172     }
    173     return err;
    174 }
    175 
    176 static void glue (audio_pcm_sw_fini_, TYPE) (SW *sw)
    177 {
    178     glue (audio_pcm_sw_free_resources_, TYPE) (sw);
    179     if (sw->name) {
    180         qemu_free (sw->name);
    181         sw->name = NULL;
    182     }
    183 }
    184 
    185 static void glue (audio_pcm_hw_add_sw_, TYPE) (HW *hw, SW *sw)
    186 {
    187     QLIST_INSERT_HEAD (&hw->sw_head, sw, entries);
    188 }
    189 
    190 static void glue (audio_pcm_hw_del_sw_, TYPE) (SW *sw)
    191 {
    192     QLIST_REMOVE (sw, entries);
    193 }
    194 
    195 static void glue (audio_pcm_hw_gc_, TYPE) (HW **hwp)
    196 {
    197     AudioState *s = &glob_audio_state;
    198     HW *hw = *hwp;
    199 
    200     if (!hw->sw_head.lh_first) {
    201 #ifdef DAC
    202         audio_detach_capture (hw);
    203 #endif
    204         QLIST_REMOVE (hw, entries);
    205         glue (s->nb_hw_voices_, TYPE) += 1;
    206         glue (audio_pcm_hw_free_resources_ ,TYPE) (hw);
    207         BEGIN_NOSIGALRM
    208         glue (hw->pcm_ops->fini_, TYPE) (hw);
    209         END_NOSIGALRM
    210         qemu_free (hw);
    211         *hwp = NULL;
    212     }
    213 }
    214 
    215 static HW *glue (audio_pcm_hw_find_any_, TYPE) (HW *hw)
    216 {
    217     AudioState *s = &glob_audio_state;
    218     return hw ? hw->entries.le_next : glue (s->hw_head_, TYPE).lh_first;
    219 }
    220 
    221 static HW *glue (audio_pcm_hw_find_any_enabled_, TYPE) (HW *hw)
    222 {
    223     while ((hw = glue (audio_pcm_hw_find_any_, TYPE) (hw))) {
    224         if (hw->enabled) {
    225             return hw;
    226         }
    227     }
    228     return NULL;
    229 }
    230 
    231 static HW *glue (audio_pcm_hw_find_specific_, TYPE) (
    232     HW *hw,
    233     struct audsettings *as
    234     )
    235 {
    236     while ((hw = glue (audio_pcm_hw_find_any_, TYPE) (hw))) {
    237         if (audio_pcm_info_eq (&hw->info, as)) {
    238             return hw;
    239         }
    240     }
    241     return NULL;
    242 }
    243 
    244 static HW *glue (audio_pcm_hw_add_new_, TYPE) (struct audsettings *as)
    245 {
    246     HW *hw;
    247     AudioState *s = &glob_audio_state;
    248     struct audio_driver *drv = s->drv;
    249     int err;
    250 
    251     if (!glue (s->nb_hw_voices_, TYPE)) {
    252         return NULL;
    253     }
    254 
    255     if (audio_bug (AUDIO_FUNC, !drv)) {
    256         dolog ("No host audio driver\n");
    257         return NULL;
    258     }
    259 
    260     if (audio_bug (AUDIO_FUNC, !drv->pcm_ops)) {
    261         dolog ("Host audio driver without pcm_ops\n");
    262         return NULL;
    263     }
    264 
    265     hw = audio_calloc (AUDIO_FUNC, 1, glue (drv->voice_size_, TYPE));
    266     if (!hw) {
    267         dolog ("Can not allocate voice `%s' size %d\n",
    268                drv->name, glue (drv->voice_size_, TYPE));
    269         return NULL;
    270     }
    271 
    272     hw->pcm_ops = drv->pcm_ops;
    273     QLIST_INIT (&hw->sw_head);
    274 #ifdef DAC
    275     QLIST_INIT (&hw->cap_head);
    276 #endif
    277     BEGIN_NOSIGALRM
    278     err = glue (hw->pcm_ops->init_, TYPE) (hw, as);
    279     END_NOSIGALRM
    280     if (err)
    281         goto err0;
    282 
    283     if (audio_bug (AUDIO_FUNC, hw->samples <= 0)) {
    284         dolog ("hw->samples=%d\n", hw->samples);
    285         goto err1;
    286     }
    287 
    288 #ifdef DAC
    289     hw->clip = mixeng_clip
    290 #else
    291     hw->conv = mixeng_conv
    292 #endif
    293         [hw->info.nchannels == 2]
    294         [hw->info.sign]
    295         [hw->info.swap_endianness]
    296         [audio_bits_to_index (hw->info.bits)];
    297 
    298     if (glue (audio_pcm_hw_alloc_resources_, TYPE) (hw)) {
    299         goto err1;
    300     }
    301 
    302     QLIST_INSERT_HEAD (&s->glue (hw_head_, TYPE), hw, entries);
    303     glue (s->nb_hw_voices_, TYPE) -= 1;
    304 #ifdef DAC
    305     audio_attach_capture (hw);
    306 #endif
    307     return hw;
    308 
    309  err1:
    310     BEGIN_NOSIGALRM
    311     glue (hw->pcm_ops->fini_, TYPE) (hw);
    312     END_NOSIGALRM
    313  err0:
    314     qemu_free (hw);
    315     return NULL;
    316 }
    317 
    318 static HW *glue (audio_pcm_hw_add_, TYPE) (struct audsettings *as)
    319 {
    320     HW *hw;
    321 
    322     if (glue (conf.fixed_, TYPE).enabled && glue (conf.fixed_, TYPE).greedy) {
    323         hw = glue (audio_pcm_hw_add_new_, TYPE) (as);
    324         if (hw) {
    325             return hw;
    326         }
    327     }
    328 
    329     hw = glue (audio_pcm_hw_find_specific_, TYPE) (NULL, as);
    330     if (hw) {
    331         return hw;
    332     }
    333 
    334     hw = glue (audio_pcm_hw_add_new_, TYPE) (as);
    335     if (hw) {
    336         return hw;
    337     }
    338 
    339     return glue (audio_pcm_hw_find_any_, TYPE) (NULL);
    340 }
    341 
    342 static SW *glue (audio_pcm_create_voice_pair_, TYPE) (
    343     const char *sw_name,
    344     struct audsettings *as
    345     )
    346 {
    347     SW *sw;
    348     HW *hw;
    349     struct audsettings hw_as;
    350 
    351     if (glue (conf.fixed_, TYPE).enabled) {
    352         hw_as = glue (conf.fixed_, TYPE).settings;
    353     }
    354     else {
    355         hw_as = *as;
    356     }
    357 
    358     sw = audio_calloc (AUDIO_FUNC, 1, sizeof (*sw));
    359     if (!sw) {
    360         dolog ("Could not allocate soft voice `%s' (%zu bytes)\n",
    361                sw_name ? sw_name : "unknown", sizeof (*sw));
    362         goto err1;
    363     }
    364 
    365     hw = glue (audio_pcm_hw_add_, TYPE) (&hw_as);
    366     if (!hw) {
    367         goto err2;
    368     }
    369 
    370     glue (audio_pcm_hw_add_sw_, TYPE) (hw, sw);
    371 
    372     if (glue (audio_pcm_sw_init_, TYPE) (sw, hw, sw_name, as)) {
    373         goto err3;
    374     }
    375 
    376     return sw;
    377 
    378 err3:
    379     glue (audio_pcm_hw_del_sw_, TYPE) (sw);
    380     glue (audio_pcm_hw_gc_, TYPE) (&hw);
    381 err2:
    382     qemu_free (sw);
    383 err1:
    384     return NULL;
    385 }
    386 
    387 static void glue (audio_close_, TYPE) (SW *sw)
    388 {
    389     glue (audio_pcm_sw_fini_, TYPE) (sw);
    390     glue (audio_pcm_hw_del_sw_, TYPE) (sw);
    391     glue (audio_pcm_hw_gc_, TYPE) (&sw->hw);
    392     qemu_free (sw);
    393 }
    394 
    395 void glue (AUD_close_, TYPE) (QEMUSoundCard *card, SW *sw)
    396 {
    397     if (sw) {
    398         if (audio_bug (AUDIO_FUNC, !card)) {
    399             dolog ("card=%p\n", card);
    400             return;
    401         }
    402 
    403         glue (audio_close_, TYPE) (sw);
    404     }
    405 }
    406 
    407 SW *glue (AUD_open_, TYPE) (
    408     QEMUSoundCard *card,
    409     SW *sw,
    410     const char *name,
    411     void *callback_opaque ,
    412     audio_callback_fn callback_fn,
    413     struct audsettings *as
    414     )
    415 {
    416     AudioState *s = &glob_audio_state;
    417 #ifdef DAC
    418     int live = 0;
    419     SW *old_sw = NULL;
    420 #endif
    421 
    422     ldebug ("open %s, freq %d, nchannels %d, fmt %d\n",
    423             name, as->freq, as->nchannels, as->fmt);
    424 
    425     if (audio_bug (AUDIO_FUNC, !card || !name || !callback_fn || !as)) {
    426         dolog ("card=%p name=%p callback_fn=%p as=%p\n",
    427                card, name, callback_fn, as);
    428         goto fail;
    429     }
    430 
    431     if (audio_bug (AUDIO_FUNC, audio_validate_settings (as))) {
    432         audio_print_settings (as);
    433         goto fail;
    434     }
    435 
    436     if (audio_bug (AUDIO_FUNC, !s->drv)) {
    437         dolog ("Can not open `%s' (no host audio driver)\n", name);
    438         goto fail;
    439     }
    440 
    441     if (sw && audio_pcm_info_eq (&sw->info, as)) {
    442         return sw;
    443     }
    444 
    445 #ifdef DAC
    446     if (conf.plive && sw && (!sw->active && !sw->empty)) {
    447         live = sw->total_hw_samples_mixed;
    448 
    449 #ifdef DEBUG_PLIVE
    450         dolog ("Replacing voice %s with %d live samples\n", SW_NAME (sw), live);
    451         dolog ("Old %s freq %d, bits %d, channels %d\n",
    452                SW_NAME (sw), sw->info.freq, sw->info.bits, sw->info.nchannels);
    453         dolog ("New %s freq %d, bits %d, channels %d\n",
    454                name,
    455                as->freq,
    456                (as->fmt == AUD_FMT_S16 || as->fmt == AUD_FMT_U16) ? 16 : 8,
    457                as->nchannels);
    458 #endif
    459 
    460         if (live) {
    461             old_sw = sw;
    462             old_sw->callback.fn = NULL;
    463             sw = NULL;
    464         }
    465     }
    466 #endif
    467 
    468     if (!glue (conf.fixed_, TYPE).enabled && sw) {
    469         glue (AUD_close_, TYPE) (card, sw);
    470         sw = NULL;
    471     }
    472 
    473     if (sw) {
    474         HW *hw = sw->hw;
    475 
    476         if (!hw) {
    477             dolog ("Internal logic error voice `%s' has no hardware store\n",
    478                    SW_NAME (sw));
    479             goto fail;
    480         }
    481 
    482         glue (audio_pcm_sw_fini_, TYPE) (sw);
    483         if (glue (audio_pcm_sw_init_, TYPE) (sw, hw, name, as)) {
    484             goto fail;
    485         }
    486     }
    487     else {
    488         sw = glue (audio_pcm_create_voice_pair_, TYPE) (name, as);
    489         if (!sw) {
    490             dolog ("Failed to create voice `%s'\n", name);
    491             return NULL;
    492         }
    493     }
    494 
    495     sw->card = card;
    496     sw->vol = nominal_volume;
    497     sw->callback.fn = callback_fn;
    498     sw->callback.opaque = callback_opaque;
    499 #ifdef DAC
    500     sw->empty = 1;
    501 #endif
    502 
    503 #ifdef DAC
    504     if (live) {
    505         int mixed =
    506             (live << old_sw->info.shift)
    507             * old_sw->info.bytes_per_second
    508             / sw->info.bytes_per_second;
    509 
    510 #ifdef DEBUG_PLIVE
    511         dolog ("Silence will be mixed %d\n", mixed);
    512 #endif
    513         sw->total_hw_samples_mixed += mixed;
    514     }
    515 #endif
    516 
    517 #ifdef DEBUG_AUDIO
    518     dolog ("%s\n", name);
    519     audio_pcm_print_info ("hw", &sw->hw->info);
    520     audio_pcm_print_info ("sw", &sw->info);
    521 #endif
    522 
    523     return sw;
    524 
    525  fail:
    526     glue (AUD_close_, TYPE) (card, sw);
    527     return NULL;
    528 }
    529 
    530 int glue (AUD_is_active_, TYPE) (SW *sw)
    531 {
    532     return sw ? sw->active : 0;
    533 }
    534 
    535 void glue (AUD_init_time_stamp_, TYPE) (SW *sw, QEMUAudioTimeStamp *ts)
    536 {
    537     if (!sw) {
    538         return;
    539     }
    540 
    541     ts->old_ts = sw->hw->ts_helper;
    542 }
    543 
    544 uint64_t glue (AUD_get_elapsed_usec_, TYPE) (SW *sw, QEMUAudioTimeStamp *ts)
    545 {
    546     uint64_t delta, cur_ts, old_ts;
    547 
    548     if (!sw) {
    549         return 0;
    550     }
    551 
    552     cur_ts = sw->hw->ts_helper;
    553     old_ts = ts->old_ts;
    554     /* dolog ("cur %" PRId64 " old %" PRId64 "\n", cur_ts, old_ts); */
    555 
    556     if (cur_ts >= old_ts) {
    557         delta = cur_ts - old_ts;
    558     }
    559     else {
    560         delta = UINT64_MAX - old_ts + cur_ts;
    561     }
    562 
    563     if (!delta) {
    564         return 0;
    565     }
    566 
    567     return muldiv64 (delta, sw->hw->info.freq, 1000000);
    568 }
    569 
    570 #undef TYPE
    571 #undef HW
    572 #undef SW
    573 #undef HWBUF
    574 #undef NAME
    575