2 * QEMU ESD audio driver
4 * Copyright (c) 2006 Frederick Reeve (brushed up by malc)
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
25 #include "qemu-common.h"
29 #define AUDIO_CAP "esd"
30 #include "audio_int.h"
31 #include "audio_pt_int.h"
67 static void GCC_FMT_ATTR (2, 3) qesd_logerr (int err, const char *fmt, ...)
72 AUD_vlog (AUDIO_CAP, fmt, ap);
75 AUD_log (AUDIO_CAP, "Reason: %s\n", strerror (err));
79 static void *qesd_thread_out (void *arg)
81 ESDVoiceOut *esd = arg;
82 HWVoiceOut *hw = &esd->hw;
85 threshold = conf.divisor ? hw->samples / conf.divisor : 0;
87 if (audio_pt_lock (&esd->pt, AUDIO_FUNC)) {
92 int decr, to_mix, rpos;
99 if (esd->live > threshold) {
103 if (audio_pt_wait (&esd->pt, AUDIO_FUNC)) {
108 decr = to_mix = esd->live;
111 if (audio_pt_unlock (&esd->pt, AUDIO_FUNC)) {
117 int chunk = audio_MIN (to_mix, hw->samples - rpos);
118 struct st_sample *src = hw->mix_buf + rpos;
120 hw->clip (esd->pcm_buf, src, chunk);
123 written = write (esd->fd, esd->pcm_buf, chunk << hw->info.shift);
125 if (errno == EINTR || errno == EAGAIN) {
128 qesd_logerr (errno, "write failed\n");
132 if (written != chunk << hw->info.shift) {
133 int wsamples = written >> hw->info.shift;
134 int wbytes = wsamples << hw->info.shift;
135 if (wbytes != written) {
136 dolog ("warning: Misaligned write %d (requested %d), "
138 wbytes, written, hw->info.align + 1);
141 rpos = (rpos + wsamples) % hw->samples;
145 rpos = (rpos + chunk) % hw->samples;
149 if (audio_pt_lock (&esd->pt, AUDIO_FUNC)) {
159 audio_pt_unlock (&esd->pt, AUDIO_FUNC);
163 static int qesd_run_out (HWVoiceOut *hw)
166 ESDVoiceOut *esd = (ESDVoiceOut *) hw;
168 if (audio_pt_lock (&esd->pt, AUDIO_FUNC)) {
172 live = audio_pcm_hw_get_live_out (hw);
173 decr = audio_MIN (live, esd->decr);
175 esd->live = live - decr;
176 hw->rpos = esd->rpos;
178 audio_pt_unlock_and_signal (&esd->pt, AUDIO_FUNC);
181 audio_pt_unlock (&esd->pt, AUDIO_FUNC);
186 static int qesd_write (SWVoiceOut *sw, void *buf, int len)
188 return audio_pcm_sw_write (sw, buf, len);
191 static int qesd_init_out (HWVoiceOut *hw, struct audsettings *as)
193 ESDVoiceOut *esd = (ESDVoiceOut *) hw;
194 struct audsettings obt_as = *as;
195 int esdfmt = ESD_STREAM | ESD_PLAY;
197 sigset_t set, old_set;
201 esdfmt |= (as->nchannels == 2) ? ESD_STEREO : ESD_MONO;
206 obt_as.fmt = AUD_FMT_U8;
211 dolog ("Will use 16 instead of 32 bit samples\n");
216 esdfmt |= ESD_BITS16;
217 obt_as.fmt = AUD_FMT_S16;
221 dolog ("Internal logic error: Bad audio format %d\n", as->fmt);
225 obt_as.endianness = AUDIO_HOST_ENDIANNESS;
227 audio_pcm_init_info (&hw->info, &obt_as);
229 hw->samples = conf.samples;
230 esd->pcm_buf = audio_calloc (AUDIO_FUNC, hw->samples, 1 << hw->info.shift);
232 dolog ("Could not allocate buffer (%d bytes)\n",
233 hw->samples << hw->info.shift);
238 err = pthread_sigmask (SIG_BLOCK, &set, &old_set);
240 qesd_logerr (err, "pthread_sigmask failed\n");
244 esd->fd = esd_play_stream (esdfmt, as->freq, conf.dac_host, NULL);
246 qesd_logerr (errno, "esd_play_stream failed\n");
250 if (audio_pt_init (&esd->pt, qesd_thread_out, esd, AUDIO_CAP, AUDIO_FUNC)) {
254 err = pthread_sigmask (SIG_SETMASK, &old_set, NULL);
256 qesd_logerr (err, "pthread_sigmask(restore) failed\n");
262 if (close (esd->fd)) {
263 qesd_logerr (errno, "%s: close on esd socket(%d) failed\n",
264 AUDIO_FUNC, esd->fd);
269 err = pthread_sigmask (SIG_SETMASK, &old_set, NULL);
271 qesd_logerr (err, "pthread_sigmask(restore) failed\n");
275 qemu_free (esd->pcm_buf);
280 static void qesd_fini_out (HWVoiceOut *hw)
283 ESDVoiceOut *esd = (ESDVoiceOut *) hw;
285 audio_pt_lock (&esd->pt, AUDIO_FUNC);
287 audio_pt_unlock_and_signal (&esd->pt, AUDIO_FUNC);
288 audio_pt_join (&esd->pt, &ret, AUDIO_FUNC);
291 if (close (esd->fd)) {
292 qesd_logerr (errno, "failed to close esd socket\n");
297 audio_pt_fini (&esd->pt, AUDIO_FUNC);
299 qemu_free (esd->pcm_buf);
303 static int qesd_ctl_out (HWVoiceOut *hw, int cmd, ...)
311 static void *qesd_thread_in (void *arg)
313 ESDVoiceIn *esd = arg;
314 HWVoiceIn *hw = &esd->hw;
317 threshold = conf.divisor ? hw->samples / conf.divisor : 0;
319 if (audio_pt_lock (&esd->pt, AUDIO_FUNC)) {
324 int incr, to_grab, wpos;
331 if (esd->dead > threshold) {
335 if (audio_pt_wait (&esd->pt, AUDIO_FUNC)) {
340 incr = to_grab = esd->dead;
343 if (audio_pt_unlock (&esd->pt, AUDIO_FUNC)) {
349 int chunk = audio_MIN (to_grab, hw->samples - wpos);
350 void *buf = advance (esd->pcm_buf, wpos);
353 nread = read (esd->fd, buf, chunk << hw->info.shift);
355 if (errno == EINTR || errno == EAGAIN) {
358 qesd_logerr (errno, "read failed\n");
362 if (nread != chunk << hw->info.shift) {
363 int rsamples = nread >> hw->info.shift;
364 int rbytes = rsamples << hw->info.shift;
365 if (rbytes != nread) {
366 dolog ("warning: Misaligned write %d (requested %d), "
368 rbytes, nread, hw->info.align + 1);
371 wpos = (wpos + rsamples) % hw->samples;
375 hw->conv (hw->conv_buf + wpos, buf, nread >> hw->info.shift,
377 wpos = (wpos + chunk) % hw->samples;
381 if (audio_pt_lock (&esd->pt, AUDIO_FUNC)) {
391 audio_pt_unlock (&esd->pt, AUDIO_FUNC);
395 static int qesd_run_in (HWVoiceIn *hw)
397 int live, incr, dead;
398 ESDVoiceIn *esd = (ESDVoiceIn *) hw;
400 if (audio_pt_lock (&esd->pt, AUDIO_FUNC)) {
404 live = audio_pcm_hw_get_live_in (hw);
405 dead = hw->samples - live;
406 incr = audio_MIN (dead, esd->incr);
408 esd->dead = dead - incr;
409 hw->wpos = esd->wpos;
411 audio_pt_unlock_and_signal (&esd->pt, AUDIO_FUNC);
414 audio_pt_unlock (&esd->pt, AUDIO_FUNC);
419 static int qesd_read (SWVoiceIn *sw, void *buf, int len)
421 return audio_pcm_sw_read (sw, buf, len);
424 static int qesd_init_in (HWVoiceIn *hw, struct audsettings *as)
426 ESDVoiceIn *esd = (ESDVoiceIn *) hw;
427 struct audsettings obt_as = *as;
428 int esdfmt = ESD_STREAM | ESD_RECORD;
430 sigset_t set, old_set;
434 esdfmt |= (as->nchannels == 2) ? ESD_STEREO : ESD_MONO;
439 obt_as.fmt = AUD_FMT_U8;
444 esdfmt |= ESD_BITS16;
445 obt_as.fmt = AUD_FMT_S16;
450 dolog ("Will use 16 instead of 32 bit samples\n");
451 esdfmt |= ESD_BITS16;
452 obt_as.fmt = AUD_FMT_S16;
455 obt_as.endianness = AUDIO_HOST_ENDIANNESS;
457 audio_pcm_init_info (&hw->info, &obt_as);
459 hw->samples = conf.samples;
460 esd->pcm_buf = audio_calloc (AUDIO_FUNC, hw->samples, 1 << hw->info.shift);
462 dolog ("Could not allocate buffer (%d bytes)\n",
463 hw->samples << hw->info.shift);
469 err = pthread_sigmask (SIG_BLOCK, &set, &old_set);
471 qesd_logerr (err, "pthread_sigmask failed\n");
475 esd->fd = esd_record_stream (esdfmt, as->freq, conf.adc_host, NULL);
477 qesd_logerr (errno, "esd_record_stream failed\n");
481 if (audio_pt_init (&esd->pt, qesd_thread_in, esd, AUDIO_CAP, AUDIO_FUNC)) {
485 err = pthread_sigmask (SIG_SETMASK, &old_set, NULL);
487 qesd_logerr (err, "pthread_sigmask(restore) failed\n");
493 if (close (esd->fd)) {
494 qesd_logerr (errno, "%s: close on esd socket(%d) failed\n",
495 AUDIO_FUNC, esd->fd);
500 err = pthread_sigmask (SIG_SETMASK, &old_set, NULL);
502 qesd_logerr (err, "pthread_sigmask(restore) failed\n");
506 qemu_free (esd->pcm_buf);
511 static void qesd_fini_in (HWVoiceIn *hw)
514 ESDVoiceIn *esd = (ESDVoiceIn *) hw;
516 audio_pt_lock (&esd->pt, AUDIO_FUNC);
518 audio_pt_unlock_and_signal (&esd->pt, AUDIO_FUNC);
519 audio_pt_join (&esd->pt, &ret, AUDIO_FUNC);
522 if (close (esd->fd)) {
523 qesd_logerr (errno, "failed to close esd socket\n");
528 audio_pt_fini (&esd->pt, AUDIO_FUNC);
530 qemu_free (esd->pcm_buf);
534 static int qesd_ctl_in (HWVoiceIn *hw, int cmd, ...)
542 static void *qesd_audio_init (void)
547 static void qesd_audio_fini (void *opaque)
553 struct audio_option qesd_options[] = {
554 {"SAMPLES", AUD_OPT_INT, &conf.samples,
555 "buffer size in samples", NULL, 0},
557 {"DIVISOR", AUD_OPT_INT, &conf.divisor,
558 "threshold divisor", NULL, 0},
560 {"DAC_HOST", AUD_OPT_STR, &conf.dac_host,
561 "playback host", NULL, 0},
563 {"ADC_HOST", AUD_OPT_STR, &conf.adc_host,
564 "capture host", NULL, 0},
566 {NULL, 0, NULL, NULL, NULL, 0}
569 static struct audio_pcm_ops qesd_pcm_ops = {
583 struct audio_driver esd_audio_driver = {
585 .descr = "http://en.wikipedia.org/wiki/Esound",
586 .options = qesd_options,
587 .init = qesd_audio_init,
588 .fini = qesd_audio_fini,
589 .pcm_ops = &qesd_pcm_ops,
591 .max_voices_out = INT_MAX,
592 .max_voices_in = INT_MAX,
593 .voice_size_out = sizeof (ESDVoiceOut),
594 .voice_size_in = sizeof (ESDVoiceIn)