00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #ifdef DAC
00026 #define NAME "playback"
00027 #define HWBUF hw->mix_buf
00028 #define TYPE out
00029 #define HW HWVoiceOut
00030 #define SW SWVoiceOut
00031 #else
00032 #define NAME "capture"
00033 #define TYPE in
00034 #define HW HWVoiceIn
00035 #define SW SWVoiceIn
00036 #define HWBUF hw->conv_buf
00037 #endif
00038
00039 static void glue (audio_init_nb_voices_, TYPE) (
00040 AudioState *s,
00041 struct audio_driver *drv
00042 )
00043 {
00044 int max_voices = glue (drv->max_voices_, TYPE);
00045 int voice_size = glue (drv->voice_size_, TYPE);
00046
00047 if (glue (s->nb_hw_voices_, TYPE) > max_voices) {
00048 if (!max_voices) {
00049 #ifdef DAC
00050 dolog ("Driver `%s' does not support " NAME "\n", drv->name);
00051 #endif
00052 }
00053 else {
00054 dolog ("Driver `%s' does not support %d " NAME " voices, max %d\n",
00055 drv->name,
00056 glue (s->nb_hw_voices_, TYPE),
00057 max_voices);
00058 }
00059 glue (s->nb_hw_voices_, TYPE) = max_voices;
00060 }
00061
00062 if (audio_bug (AUDIO_FUNC, !voice_size && max_voices)) {
00063 dolog ("drv=`%s' voice_size=0 max_voices=%d\n",
00064 drv->name, max_voices);
00065 glue (s->nb_hw_voices_, TYPE) = 0;
00066 }
00067
00068 if (audio_bug (AUDIO_FUNC, voice_size && !max_voices)) {
00069 dolog ("drv=`%s' voice_size=%d max_voices=0\n",
00070 drv->name, voice_size);
00071 }
00072 }
00073
00074 static void glue (audio_pcm_hw_free_resources_, TYPE) (HW *hw)
00075 {
00076 if (HWBUF) {
00077 qemu_free (HWBUF);
00078 }
00079
00080 HWBUF = NULL;
00081 }
00082
00083 static int glue (audio_pcm_hw_alloc_resources_, TYPE) (HW *hw)
00084 {
00085 HWBUF = audio_calloc (AUDIO_FUNC, hw->samples, sizeof (struct st_sample));
00086 if (!HWBUF) {
00087 dolog ("Could not allocate " NAME " buffer (%d samples)\n",
00088 hw->samples);
00089 return -1;
00090 }
00091
00092 return 0;
00093 }
00094
00095 static void glue (audio_pcm_sw_free_resources_, TYPE) (SW *sw)
00096 {
00097 if (sw->buf) {
00098 qemu_free (sw->buf);
00099 }
00100
00101 if (sw->rate) {
00102 st_rate_stop (sw->rate);
00103 }
00104
00105 sw->buf = NULL;
00106 sw->rate = NULL;
00107 }
00108
00109 static int glue (audio_pcm_sw_alloc_resources_, TYPE) (SW *sw)
00110 {
00111 int samples;
00112
00113 #ifdef DAC
00114 samples = sw->hw->samples;
00115 #else
00116 samples = ((int64_t) sw->hw->samples << 32) / sw->ratio;
00117 #endif
00118
00119 sw->buf = audio_calloc (AUDIO_FUNC, samples, sizeof (struct st_sample));
00120 if (!sw->buf) {
00121 dolog ("Could not allocate buffer for `%s' (%d samples)\n",
00122 SW_NAME (sw), samples);
00123 return -1;
00124 }
00125
00126 #ifdef DAC
00127 sw->rate = st_rate_start (sw->info.freq, sw->hw->info.freq);
00128 #else
00129 sw->rate = st_rate_start (sw->hw->info.freq, sw->info.freq);
00130 #endif
00131 if (!sw->rate) {
00132 qemu_free (sw->buf);
00133 sw->buf = NULL;
00134 return -1;
00135 }
00136 return 0;
00137 }
00138
00139 static int glue (audio_pcm_sw_init_, TYPE) (
00140 SW *sw,
00141 HW *hw,
00142 const char *name,
00143 struct audsettings *as
00144 )
00145 {
00146 int err;
00147
00148 audio_pcm_init_info (&sw->info, as);
00149 sw->hw = hw;
00150 sw->active = 0;
00151 #ifdef DAC
00152 sw->ratio = ((int64_t) sw->hw->info.freq << 32) / sw->info.freq;
00153 sw->total_hw_samples_mixed = 0;
00154 sw->empty = 1;
00155 #else
00156 sw->ratio = ((int64_t) sw->info.freq << 32) / sw->hw->info.freq;
00157 #endif
00158
00159 #ifdef DAC
00160 sw->conv = mixeng_conv
00161 #else
00162 sw->clip = mixeng_clip
00163 #endif
00164 [sw->info.nchannels == 2]
00165 [sw->info.sign]
00166 [sw->info.swap_endianness]
00167 [audio_bits_to_index (sw->info.bits)];
00168
00169 sw->name = qemu_strdup (name);
00170 err = glue (audio_pcm_sw_alloc_resources_, TYPE) (sw);
00171 if (err) {
00172 qemu_free (sw->name);
00173 sw->name = NULL;
00174 }
00175 return err;
00176 }
00177
00178 static void glue (audio_pcm_sw_fini_, TYPE) (SW *sw)
00179 {
00180 glue (audio_pcm_sw_free_resources_, TYPE) (sw);
00181 if (sw->name) {
00182 qemu_free (sw->name);
00183 sw->name = NULL;
00184 }
00185 }
00186
00187 static void glue (audio_pcm_hw_add_sw_, TYPE) (HW *hw, SW *sw)
00188 {
00189 LIST_INSERT_HEAD (&hw->sw_head, sw, entries);
00190 }
00191
00192 static void glue (audio_pcm_hw_del_sw_, TYPE) (SW *sw)
00193 {
00194 LIST_REMOVE (sw, entries);
00195 }
00196
00197 static void glue (audio_pcm_hw_gc_, TYPE) (AudioState *s, HW **hwp)
00198 {
00199 HW *hw = *hwp;
00200
00201 if (!hw->sw_head.lh_first) {
00202 #ifdef DAC
00203 audio_detach_capture (hw);
00204 #endif
00205 LIST_REMOVE (hw, entries);
00206 glue (s->nb_hw_voices_, TYPE) += 1;
00207 glue (audio_pcm_hw_free_resources_ ,TYPE) (hw);
00208 glue (hw->pcm_ops->fini_, TYPE) (hw);
00209 qemu_free (hw);
00210 *hwp = NULL;
00211 }
00212 }
00213
00214 static HW *glue (audio_pcm_hw_find_any_, TYPE) (AudioState *s, HW *hw)
00215 {
00216 return hw ? hw->entries.le_next : s->glue (hw_head_, TYPE).lh_first;
00217 }
00218
00219 static HW *glue (audio_pcm_hw_find_any_enabled_, TYPE) (AudioState *s, HW *hw)
00220 {
00221 while ((hw = glue (audio_pcm_hw_find_any_, TYPE) (s, hw))) {
00222 if (hw->enabled) {
00223 return hw;
00224 }
00225 }
00226 return NULL;
00227 }
00228
00229 static HW *glue (audio_pcm_hw_find_specific_, TYPE) (
00230 AudioState *s,
00231 HW *hw,
00232 struct audsettings *as
00233 )
00234 {
00235 while ((hw = glue (audio_pcm_hw_find_any_, TYPE) (s, hw))) {
00236 if (audio_pcm_info_eq (&hw->info, as)) {
00237 return hw;
00238 }
00239 }
00240 return NULL;
00241 }
00242
00243 static HW *glue (audio_pcm_hw_add_new_, TYPE) (AudioState *s,
00244 struct audsettings *as)
00245 {
00246 HW *hw;
00247 struct audio_driver *drv = s->drv;
00248
00249 if (!glue (s->nb_hw_voices_, TYPE)) {
00250 return NULL;
00251 }
00252
00253 if (audio_bug (AUDIO_FUNC, !drv)) {
00254 dolog ("No host audio driver\n");
00255 return NULL;
00256 }
00257
00258 if (audio_bug (AUDIO_FUNC, !drv->pcm_ops)) {
00259 dolog ("Host audio driver without pcm_ops\n");
00260 return NULL;
00261 }
00262
00263 hw = audio_calloc (AUDIO_FUNC, 1, glue (drv->voice_size_, TYPE));
00264 if (!hw) {
00265 dolog ("Can not allocate voice `%s' size %d\n",
00266 drv->name, glue (drv->voice_size_, TYPE));
00267 return NULL;
00268 }
00269
00270 hw->pcm_ops = drv->pcm_ops;
00271 LIST_INIT (&hw->sw_head);
00272 #ifdef DAC
00273 LIST_INIT (&hw->cap_head);
00274 #endif
00275 if (glue (hw->pcm_ops->init_, TYPE) (hw, as)) {
00276 goto err0;
00277 }
00278
00279 if (audio_bug (AUDIO_FUNC, hw->samples <= 0)) {
00280 dolog ("hw->samples=%d\n", hw->samples);
00281 goto err1;
00282 }
00283
00284 #ifdef DAC
00285 hw->clip = mixeng_clip
00286 #else
00287 hw->conv = mixeng_conv
00288 #endif
00289 [hw->info.nchannels == 2]
00290 [hw->info.sign]
00291 [hw->info.swap_endianness]
00292 [audio_bits_to_index (hw->info.bits)];
00293
00294 if (glue (audio_pcm_hw_alloc_resources_, TYPE) (hw)) {
00295 goto err1;
00296 }
00297
00298 LIST_INSERT_HEAD (&s->glue (hw_head_, TYPE), hw, entries);
00299 glue (s->nb_hw_voices_, TYPE) -= 1;
00300 #ifdef DAC
00301 audio_attach_capture (s, hw);
00302 #endif
00303 return hw;
00304
00305 err1:
00306 glue (hw->pcm_ops->fini_, TYPE) (hw);
00307 err0:
00308 qemu_free (hw);
00309 return NULL;
00310 }
00311
00312 static HW *glue (audio_pcm_hw_add_, TYPE) (AudioState *s,
00313 struct audsettings *as)
00314 {
00315 HW *hw;
00316
00317 if (glue (conf.fixed_, TYPE).enabled && glue (conf.fixed_, TYPE).greedy) {
00318 hw = glue (audio_pcm_hw_add_new_, TYPE) (s, as);
00319 if (hw) {
00320 return hw;
00321 }
00322 }
00323
00324 hw = glue (audio_pcm_hw_find_specific_, TYPE) (s, NULL, as);
00325 if (hw) {
00326 return hw;
00327 }
00328
00329 hw = glue (audio_pcm_hw_add_new_, TYPE) (s, as);
00330 if (hw) {
00331 return hw;
00332 }
00333
00334 return glue (audio_pcm_hw_find_any_, TYPE) (s, NULL);
00335 }
00336
00337 static SW *glue (audio_pcm_create_voice_pair_, TYPE) (
00338 AudioState *s,
00339 const char *sw_name,
00340 struct audsettings *as
00341 )
00342 {
00343 SW *sw;
00344 HW *hw;
00345 struct audsettings hw_as;
00346
00347 if (glue (conf.fixed_, TYPE).enabled) {
00348 hw_as = glue (conf.fixed_, TYPE).settings;
00349 }
00350 else {
00351 hw_as = *as;
00352 }
00353
00354 sw = audio_calloc (AUDIO_FUNC, 1, sizeof (*sw));
00355 if (!sw) {
00356 dolog ("Could not allocate soft voice `%s' (%zu bytes)\n",
00357 sw_name ? sw_name : "unknown", sizeof (*sw));
00358 goto err1;
00359 }
00360
00361 hw = glue (audio_pcm_hw_add_, TYPE) (s, &hw_as);
00362 if (!hw) {
00363 goto err2;
00364 }
00365
00366 glue (audio_pcm_hw_add_sw_, TYPE) (hw, sw);
00367
00368 if (glue (audio_pcm_sw_init_, TYPE) (sw, hw, sw_name, as)) {
00369 goto err3;
00370 }
00371
00372 return sw;
00373
00374 err3:
00375 glue (audio_pcm_hw_del_sw_, TYPE) (sw);
00376 glue (audio_pcm_hw_gc_, TYPE) (s, &hw);
00377 err2:
00378 qemu_free (sw);
00379 err1:
00380 return NULL;
00381 }
00382
00383 static void glue (audio_close_, TYPE) (AudioState *s, SW *sw)
00384 {
00385 glue (audio_pcm_sw_fini_, TYPE) (sw);
00386 glue (audio_pcm_hw_del_sw_, TYPE) (sw);
00387 glue (audio_pcm_hw_gc_, TYPE) (s, &sw->hw);
00388 qemu_free (sw);
00389 }
00390
00391 void glue (AUD_close_, TYPE) (QEMUSoundCard *card, SW *sw)
00392 {
00393 if (sw) {
00394 if (audio_bug (AUDIO_FUNC, !card || !card->audio)) {
00395 dolog ("card=%p card->audio=%p\n",
00396 card, card ? card->audio : NULL);
00397 return;
00398 }
00399
00400 glue (audio_close_, TYPE) (card->audio, sw);
00401 }
00402 }
00403
00404 SW *glue (AUD_open_, TYPE) (
00405 QEMUSoundCard *card,
00406 SW *sw,
00407 const char *name,
00408 void *callback_opaque ,
00409 audio_callback_fn_t callback_fn,
00410 struct audsettings *as
00411 )
00412 {
00413 AudioState *s;
00414 #ifdef DAC
00415 int live = 0;
00416 SW *old_sw = NULL;
00417 #endif
00418
00419 ldebug ("open %s, freq %d, nchannels %d, fmt %d\n",
00420 name, as->freq, as->nchannels, as->fmt);
00421
00422 if (audio_bug (AUDIO_FUNC,
00423 !card || !card->audio || !name || !callback_fn || !as)) {
00424 dolog ("card=%p card->audio=%p name=%p callback_fn=%p as=%p\n",
00425 card, card ? card->audio : NULL, name, callback_fn, as);
00426 goto fail;
00427 }
00428
00429 s = card->audio;
00430
00431 if (audio_bug (AUDIO_FUNC, audio_validate_settings (as))) {
00432 audio_print_settings (as);
00433 goto fail;
00434 }
00435
00436 if (audio_bug (AUDIO_FUNC, !s->drv)) {
00437 dolog ("Can not open `%s' (no host audio driver)\n", name);
00438 goto fail;
00439 }
00440
00441 if (sw && audio_pcm_info_eq (&sw->info, as)) {
00442 return sw;
00443 }
00444
00445 #ifdef DAC
00446 if (conf.plive && sw && (!sw->active && !sw->empty)) {
00447 live = sw->total_hw_samples_mixed;
00448
00449 #ifdef DEBUG_PLIVE
00450 dolog ("Replacing voice %s with %d live samples\n", SW_NAME (sw), live);
00451 dolog ("Old %s freq %d, bits %d, channels %d\n",
00452 SW_NAME (sw), sw->info.freq, sw->info.bits, sw->info.nchannels);
00453 dolog ("New %s freq %d, bits %d, channels %d\n",
00454 name,
00455 freq,
00456 (fmt == AUD_FMT_S16 || fmt == AUD_FMT_U16) ? 16 : 8,
00457 nchannels);
00458 #endif
00459
00460 if (live) {
00461 old_sw = sw;
00462 old_sw->callback.fn = NULL;
00463 sw = NULL;
00464 }
00465 }
00466 #endif
00467
00468 if (!glue (conf.fixed_, TYPE).enabled && sw) {
00469 glue (AUD_close_, TYPE) (card, sw);
00470 sw = NULL;
00471 }
00472
00473 if (sw) {
00474 HW *hw = sw->hw;
00475
00476 if (!hw) {
00477 dolog ("Internal logic error voice `%s' has no hardware store\n",
00478 SW_NAME (sw));
00479 goto fail;
00480 }
00481
00482 glue (audio_pcm_sw_fini_, TYPE) (sw);
00483 if (glue (audio_pcm_sw_init_, TYPE) (sw, hw, name, as)) {
00484 goto fail;
00485 }
00486 }
00487 else {
00488 sw = glue (audio_pcm_create_voice_pair_, TYPE) (s, name, as);
00489 if (!sw) {
00490 dolog ("Failed to create voice `%s'\n", name);
00491 return NULL;
00492 }
00493 }
00494
00495 if (sw) {
00496 sw->vol = nominal_volume;
00497 sw->callback.fn = callback_fn;
00498 sw->callback.opaque = callback_opaque;
00499
00500 #ifdef DAC
00501 if (live) {
00502 int mixed =
00503 (live << old_sw->info.shift)
00504 * old_sw->info.bytes_per_second
00505 / sw->info.bytes_per_second;
00506
00507 #ifdef DEBUG_PLIVE
00508 dolog ("Silence will be mixed %d\n", mixed);
00509 #endif
00510 sw->total_hw_samples_mixed += mixed;
00511 }
00512 #endif
00513
00514 #ifdef DEBUG_AUDIO
00515 dolog ("%s\n", name);
00516 audio_pcm_print_info ("hw", &sw->hw->info);
00517 audio_pcm_print_info ("sw", &sw->info);
00518 #endif
00519 }
00520
00521 return sw;
00522
00523 fail:
00524 glue (AUD_close_, TYPE) (card, sw);
00525 return NULL;
00526 }
00527
00528 int glue (AUD_is_active_, TYPE) (SW *sw)
00529 {
00530 return sw ? sw->active : 0;
00531 }
00532
00533 void glue (AUD_init_time_stamp_, TYPE) (SW *sw, QEMUAudioTimeStamp *ts)
00534 {
00535 if (!sw) {
00536 return;
00537 }
00538
00539 ts->old_ts = sw->hw->ts_helper;
00540 }
00541
00542 uint64_t glue (AUD_get_elapsed_usec_, TYPE) (SW *sw, QEMUAudioTimeStamp *ts)
00543 {
00544 uint64_t delta, cur_ts, old_ts;
00545
00546 if (!sw) {
00547 return 0;
00548 }
00549
00550 cur_ts = sw->hw->ts_helper;
00551 old_ts = ts->old_ts;
00552
00553
00554 if (cur_ts >= old_ts) {
00555 delta = cur_ts - old_ts;
00556 }
00557 else {
00558 delta = UINT64_MAX - old_ts + cur_ts;
00559 }
00560
00561 if (!delta) {
00562 return 0;
00563 }
00564
00565 return (delta * sw->hw->info.freq) / 1000000;
00566 }
00567
00568 #undef TYPE
00569 #undef HW
00570 #undef SW
00571 #undef HWBUF
00572 #undef NAME