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;
88
int decr, to_mix, rpos;
95
if (esd->live > threshold) {
99
if (audio_pt_wait (&esd->pt, AUDIO_FUNC)) {
104
decr = to_mix = esd->live;
107
if (audio_pt_unlock (&esd->pt, AUDIO_FUNC)) {
113
int chunk = audio_MIN (to_mix, hw->samples - rpos);
114
st_sample_t *src = hw->mix_buf + rpos;
116
hw->clip (esd->pcm_buf, src, chunk);
119
written = write (esd->fd, esd->pcm_buf, chunk << hw->info.shift);
121
if (errno == EINTR || errno == EAGAIN) {
124
qesd_logerr (errno, "write failed\n");
128
if (written != chunk << hw->info.shift) {
129
int wsamples = written >> hw->info.shift;
130
int wbytes = wsamples << hw->info.shift;
131
if (wbytes != written) {
132
dolog ("warning: Misaligned write %d (requested %d), "
134
wbytes, written, hw->info.align + 1);
137
rpos = (rpos + wsamples) % hw->samples;
141
rpos = (rpos + chunk) % hw->samples;
145
if (audio_pt_lock (&esd->pt, AUDIO_FUNC)) {
155
audio_pt_unlock (&esd->pt, AUDIO_FUNC);
159
static int qesd_run_out (HWVoiceOut *hw)
162
ESDVoiceOut *esd = (ESDVoiceOut *) hw;
164
if (audio_pt_lock (&esd->pt, AUDIO_FUNC)) {
168
live = audio_pcm_hw_get_live_out (hw);
169
decr = audio_MIN (live, esd->decr);
171
esd->live = live - decr;
172
hw->rpos = esd->rpos;
174
audio_pt_unlock_and_signal (&esd->pt, AUDIO_FUNC);
177
audio_pt_unlock (&esd->pt, AUDIO_FUNC);
182
static int qesd_write (SWVoiceOut *sw, void *buf, int len)
184
return audio_pcm_sw_write (sw, buf, len);
187
static int qesd_init_out (HWVoiceOut *hw, audsettings_t *as)
189
ESDVoiceOut *esd = (ESDVoiceOut *) hw;
190
audsettings_t obt_as = *as;
191
int esdfmt = ESD_STREAM | ESD_PLAY;
193
sigset_t set, old_set;
197
esdfmt |= (as->nchannels == 2) ? ESD_STEREO : ESD_MONO;
202
obt_as.fmt = AUD_FMT_U8;
207
dolog ("Will use 16 instead of 32 bit samples\n");
212
esdfmt |= ESD_BITS16;
213
obt_as.fmt = AUD_FMT_S16;
217
dolog ("Internal logic error: Bad audio format %d\n", as->fmt);
224
obt_as.endianness = 0;
226
audio_pcm_init_info (&hw->info, &obt_as);
228
hw->samples = conf.samples;
229
esd->pcm_buf = audio_calloc (AUDIO_FUNC, hw->samples, 1 << hw->info.shift);
231
dolog ("Could not allocate buffer (%d bytes)\n",
232
hw->samples << hw->info.shift);
237
err = pthread_sigmask (SIG_BLOCK, &set, &old_set);
239
qesd_logerr (err, "pthread_sigmask failed\n");
243
esd->fd = esd_play_stream (esdfmt, as->freq, conf.dac_host, NULL);
245
qesd_logerr (errno, "esd_play_stream failed\n");
249
if (audio_pt_init (&esd->pt, qesd_thread_out, esd, AUDIO_CAP, AUDIO_FUNC)) {
253
err = pthread_sigmask (SIG_SETMASK, &old_set, NULL);
255
qesd_logerr (err, "pthread_sigmask(restore) failed\n");
261
if (close (esd->fd)) {
262
qesd_logerr (errno, "%s: close on esd socket(%d) failed\n",
263
AUDIO_FUNC, esd->fd);
268
err = pthread_sigmask (SIG_SETMASK, &old_set, NULL);
270
qesd_logerr (err, "pthread_sigmask(restore) failed\n");
274
qemu_free (esd->pcm_buf);
279
static void qesd_fini_out (HWVoiceOut *hw)
282
ESDVoiceOut *esd = (ESDVoiceOut *) hw;
284
audio_pt_lock (&esd->pt, AUDIO_FUNC);
286
audio_pt_unlock_and_signal (&esd->pt, AUDIO_FUNC);
287
audio_pt_join (&esd->pt, &ret, AUDIO_FUNC);
290
if (close (esd->fd)) {
291
qesd_logerr (errno, "failed to close esd socket\n");
296
audio_pt_fini (&esd->pt, AUDIO_FUNC);
298
qemu_free (esd->pcm_buf);
302
static int qesd_ctl_out (HWVoiceOut *hw, int cmd, ...)
310
static void *qesd_thread_in (void *arg)
312
ESDVoiceIn *esd = arg;
313
HWVoiceIn *hw = &esd->hw;
316
threshold = conf.divisor ? hw->samples / conf.divisor : 0;
319
int incr, to_grab, wpos;
326
if (esd->dead > threshold) {
330
if (audio_pt_wait (&esd->pt, AUDIO_FUNC)) {
335
incr = to_grab = esd->dead;
338
if (audio_pt_unlock (&esd->pt, AUDIO_FUNC)) {
344
int chunk = audio_MIN (to_grab, hw->samples - wpos);
345
void *buf = advance (esd->pcm_buf, wpos);
348
nread = read (esd->fd, buf, chunk << hw->info.shift);
350
if (errno == EINTR || errno == EAGAIN) {
353
qesd_logerr (errno, "read failed\n");
357
if (nread != chunk << hw->info.shift) {
358
int rsamples = nread >> hw->info.shift;
359
int rbytes = rsamples << hw->info.shift;
360
if (rbytes != nread) {
361
dolog ("warning: Misaligned write %d (requested %d), "
363
rbytes, nread, hw->info.align + 1);
366
wpos = (wpos + rsamples) % hw->samples;
370
hw->conv (hw->conv_buf + wpos, buf, nread >> hw->info.shift,
372
wpos = (wpos + chunk) % hw->samples;
376
if (audio_pt_lock (&esd->pt, AUDIO_FUNC)) {
386
audio_pt_unlock (&esd->pt, AUDIO_FUNC);
390
static int qesd_run_in (HWVoiceIn *hw)
392
int live, incr, dead;
393
ESDVoiceIn *esd = (ESDVoiceIn *) hw;
395
if (audio_pt_lock (&esd->pt, AUDIO_FUNC)) {
399
live = audio_pcm_hw_get_live_in (hw);
400
dead = hw->samples - live;
401
incr = audio_MIN (dead, esd->incr);
403
esd->dead = dead - incr;
404
hw->wpos = esd->wpos;
406
audio_pt_unlock_and_signal (&esd->pt, AUDIO_FUNC);
409
audio_pt_unlock (&esd->pt, AUDIO_FUNC);
414
static int qesd_read (SWVoiceIn *sw, void *buf, int len)
416
return audio_pcm_sw_read (sw, buf, len);
419
static int qesd_init_in (HWVoiceIn *hw, audsettings_t *as)
421
ESDVoiceIn *esd = (ESDVoiceIn *) hw;
422
audsettings_t obt_as = *as;
423
int esdfmt = ESD_STREAM | ESD_RECORD;
425
sigset_t set, old_set;
429
esdfmt |= (as->nchannels == 2) ? ESD_STEREO : ESD_MONO;
434
obt_as.fmt = AUD_FMT_U8;
439
esdfmt |= ESD_BITS16;
440
obt_as.fmt = AUD_FMT_S16;
445
dolog ("Will use 16 instead of 32 bit samples\n");
446
esdfmt |= ESD_BITS16;
447
obt_as.fmt = AUD_FMT_S16;
450
obt_as.endianness = 0;
452
audio_pcm_init_info (&hw->info, &obt_as);
454
hw->samples = conf.samples;
455
esd->pcm_buf = audio_calloc (AUDIO_FUNC, hw->samples, 1 << hw->info.shift);
457
dolog ("Could not allocate buffer (%d bytes)\n",
458
hw->samples << hw->info.shift);
464
err = pthread_sigmask (SIG_BLOCK, &set, &old_set);
466
qesd_logerr (err, "pthread_sigmask failed\n");
470
esd->fd = esd_record_stream (esdfmt, as->freq, conf.adc_host, NULL);
472
qesd_logerr (errno, "esd_record_stream failed\n");
476
if (audio_pt_init (&esd->pt, qesd_thread_in, esd, AUDIO_CAP, AUDIO_FUNC)) {
480
err = pthread_sigmask (SIG_SETMASK, &old_set, NULL);
482
qesd_logerr (err, "pthread_sigmask(restore) failed\n");
488
if (close (esd->fd)) {
489
qesd_logerr (errno, "%s: close on esd socket(%d) failed\n",
490
AUDIO_FUNC, esd->fd);
495
err = pthread_sigmask (SIG_SETMASK, &old_set, NULL);
497
qesd_logerr (err, "pthread_sigmask(restore) failed\n");
501
qemu_free (esd->pcm_buf);
506
static void qesd_fini_in (HWVoiceIn *hw)
509
ESDVoiceIn *esd = (ESDVoiceIn *) hw;
511
audio_pt_lock (&esd->pt, AUDIO_FUNC);
513
audio_pt_unlock_and_signal (&esd->pt, AUDIO_FUNC);
514
audio_pt_join (&esd->pt, &ret, AUDIO_FUNC);
517
if (close (esd->fd)) {
518
qesd_logerr (errno, "failed to close esd socket\n");
523
audio_pt_fini (&esd->pt, AUDIO_FUNC);
525
qemu_free (esd->pcm_buf);
529
static int qesd_ctl_in (HWVoiceIn *hw, int cmd, ...)
537
static void *qesd_audio_init (void)
542
static void qesd_audio_fini (void *opaque)
548
struct audio_option qesd_options[] = {
549
{"SAMPLES", AUD_OPT_INT, &conf.samples,
550
"buffer size in samples", NULL, 0},
552
{"DIVISOR", AUD_OPT_INT, &conf.divisor,
553
"threshold divisor", NULL, 0},
555
{"DAC_HOST", AUD_OPT_STR, &conf.dac_host,
556
"playback host", NULL, 0},
558
{"ADC_HOST", AUD_OPT_STR, &conf.adc_host,
559
"capture host", NULL, 0},
561
{NULL, 0, NULL, NULL, NULL, 0}
564
struct audio_pcm_ops qesd_pcm_ops = {
578
struct audio_driver esd_audio_driver = {
579
INIT_FIELD (name = ) "esd",
580
INIT_FIELD (descr = )
581
"http://en.wikipedia.org/wiki/Esound",
582
INIT_FIELD (options = ) qesd_options,
583
INIT_FIELD (init = ) qesd_audio_init,
584
INIT_FIELD (fini = ) qesd_audio_fini,
585
INIT_FIELD (pcm_ops = ) &qesd_pcm_ops,
586
INIT_FIELD (can_be_default = ) 0,
587
INIT_FIELD (max_voices_out = ) INT_MAX,
588
INIT_FIELD (max_voices_in = ) INT_MAX,
589
INIT_FIELD (voice_size_out = ) sizeof (ESDVoiceOut),
590
INIT_FIELD (voice_size_in = ) sizeof (ESDVoiceIn)