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"
28 #define AUDIO_CAP "esd"
29 #include "audio_int.h"
30 #include "audio_pt_int.h"
64 static void GCC_FMT_ATTR (2, 3) qesd_logerr (int err
, const char *fmt
, ...)
69 AUD_vlog (AUDIO_CAP
, fmt
, ap
);
72 AUD_log (AUDIO_CAP
, "Reason: %s\n", strerror (err
));
76 static void *qesd_thread_out (void *arg
)
78 ESDVoiceOut
*esd
= arg
;
79 HWVoiceOut
*hw
= &esd
->hw
;
82 threshold
= conf
.divisor
? hw
->samples
/ conf
.divisor
: 0;
84 if (audio_pt_lock (&esd
->pt
, AUDIO_FUNC
)) {
89 int decr
, to_mix
, rpos
;
96 if (esd
->live
> threshold
) {
100 if (audio_pt_wait (&esd
->pt
, AUDIO_FUNC
)) {
105 decr
= to_mix
= esd
->live
;
108 if (audio_pt_unlock (&esd
->pt
, AUDIO_FUNC
)) {
114 int chunk
= audio_MIN (to_mix
, hw
->samples
- rpos
);
115 struct st_sample
*src
= hw
->mix_buf
+ rpos
;
117 hw
->clip (esd
->pcm_buf
, src
, chunk
);
120 written
= write (esd
->fd
, esd
->pcm_buf
, chunk
<< hw
->info
.shift
);
122 if (errno
== EINTR
|| errno
== EAGAIN
) {
125 qesd_logerr (errno
, "write failed\n");
129 if (written
!= chunk
<< hw
->info
.shift
) {
130 int wsamples
= written
>> hw
->info
.shift
;
131 int wbytes
= wsamples
<< hw
->info
.shift
;
132 if (wbytes
!= written
) {
133 dolog ("warning: Misaligned write %d (requested %zd), "
135 wbytes
, written
, hw
->info
.align
+ 1);
138 rpos
= (rpos
+ wsamples
) % hw
->samples
;
142 rpos
= (rpos
+ chunk
) % hw
->samples
;
146 if (audio_pt_lock (&esd
->pt
, AUDIO_FUNC
)) {
156 audio_pt_unlock (&esd
->pt
, AUDIO_FUNC
);
160 static int qesd_run_out (HWVoiceOut
*hw
, int live
)
163 ESDVoiceOut
*esd
= (ESDVoiceOut
*) hw
;
165 if (audio_pt_lock (&esd
->pt
, AUDIO_FUNC
)) {
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
, struct audsettings
*as
)
189 ESDVoiceOut
*esd
= (ESDVoiceOut
*) hw
;
190 struct audsettings obt_as
= *as
;
191 int esdfmt
= ESD_STREAM
| ESD_PLAY
;
193 esdfmt
|= (as
->nchannels
== 2) ? ESD_STEREO
: ESD_MONO
;
198 obt_as
.fmt
= AUD_FMT_U8
;
203 dolog ("Will use 16 instead of 32 bit samples\n");
208 esdfmt
|= ESD_BITS16
;
209 obt_as
.fmt
= AUD_FMT_S16
;
213 dolog ("Internal logic error: Bad audio format %d\n", as
->fmt
);
217 obt_as
.endianness
= AUDIO_HOST_ENDIANNESS
;
219 audio_pcm_init_info (&hw
->info
, &obt_as
);
221 hw
->samples
= conf
.samples
;
222 esd
->pcm_buf
= audio_calloc (AUDIO_FUNC
, hw
->samples
, 1 << hw
->info
.shift
);
224 dolog ("Could not allocate buffer (%d bytes)\n",
225 hw
->samples
<< hw
->info
.shift
);
229 esd
->fd
= esd_play_stream (esdfmt
, as
->freq
, conf
.dac_host
, NULL
);
231 qesd_logerr (errno
, "esd_play_stream failed\n");
235 if (audio_pt_init (&esd
->pt
, qesd_thread_out
, esd
, AUDIO_CAP
, AUDIO_FUNC
)) {
242 if (close (esd
->fd
)) {
243 qesd_logerr (errno
, "%s: close on esd socket(%d) failed\n",
244 AUDIO_FUNC
, esd
->fd
);
249 qemu_free (esd
->pcm_buf
);
254 static void qesd_fini_out (HWVoiceOut
*hw
)
257 ESDVoiceOut
*esd
= (ESDVoiceOut
*) hw
;
259 audio_pt_lock (&esd
->pt
, AUDIO_FUNC
);
261 audio_pt_unlock_and_signal (&esd
->pt
, AUDIO_FUNC
);
262 audio_pt_join (&esd
->pt
, &ret
, AUDIO_FUNC
);
265 if (close (esd
->fd
)) {
266 qesd_logerr (errno
, "failed to close esd socket\n");
271 audio_pt_fini (&esd
->pt
, AUDIO_FUNC
);
273 qemu_free (esd
->pcm_buf
);
277 static int qesd_ctl_out (HWVoiceOut
*hw
, int cmd
, ...)
285 static void *qesd_thread_in (void *arg
)
287 ESDVoiceIn
*esd
= arg
;
288 HWVoiceIn
*hw
= &esd
->hw
;
291 threshold
= conf
.divisor
? hw
->samples
/ conf
.divisor
: 0;
293 if (audio_pt_lock (&esd
->pt
, AUDIO_FUNC
)) {
298 int incr
, to_grab
, wpos
;
305 if (esd
->dead
> threshold
) {
309 if (audio_pt_wait (&esd
->pt
, AUDIO_FUNC
)) {
314 incr
= to_grab
= esd
->dead
;
317 if (audio_pt_unlock (&esd
->pt
, AUDIO_FUNC
)) {
323 int chunk
= audio_MIN (to_grab
, hw
->samples
- wpos
);
324 void *buf
= advance (esd
->pcm_buf
, wpos
);
327 nread
= read (esd
->fd
, buf
, chunk
<< hw
->info
.shift
);
329 if (errno
== EINTR
|| errno
== EAGAIN
) {
332 qesd_logerr (errno
, "read failed\n");
336 if (nread
!= chunk
<< hw
->info
.shift
) {
337 int rsamples
= nread
>> hw
->info
.shift
;
338 int rbytes
= rsamples
<< hw
->info
.shift
;
339 if (rbytes
!= nread
) {
340 dolog ("warning: Misaligned write %d (requested %zd), "
342 rbytes
, nread
, hw
->info
.align
+ 1);
345 wpos
= (wpos
+ rsamples
) % hw
->samples
;
349 hw
->conv (hw
->conv_buf
+ wpos
, buf
, nread
>> hw
->info
.shift
,
351 wpos
= (wpos
+ chunk
) % hw
->samples
;
355 if (audio_pt_lock (&esd
->pt
, AUDIO_FUNC
)) {
365 audio_pt_unlock (&esd
->pt
, AUDIO_FUNC
);
369 static int qesd_run_in (HWVoiceIn
*hw
)
371 int live
, incr
, dead
;
372 ESDVoiceIn
*esd
= (ESDVoiceIn
*) hw
;
374 if (audio_pt_lock (&esd
->pt
, AUDIO_FUNC
)) {
378 live
= audio_pcm_hw_get_live_in (hw
);
379 dead
= hw
->samples
- live
;
380 incr
= audio_MIN (dead
, esd
->incr
);
382 esd
->dead
= dead
- incr
;
383 hw
->wpos
= esd
->wpos
;
385 audio_pt_unlock_and_signal (&esd
->pt
, AUDIO_FUNC
);
388 audio_pt_unlock (&esd
->pt
, AUDIO_FUNC
);
393 static int qesd_read (SWVoiceIn
*sw
, void *buf
, int len
)
395 return audio_pcm_sw_read (sw
, buf
, len
);
398 static int qesd_init_in (HWVoiceIn
*hw
, struct audsettings
*as
)
400 ESDVoiceIn
*esd
= (ESDVoiceIn
*) hw
;
401 struct audsettings obt_as
= *as
;
402 int esdfmt
= ESD_STREAM
| ESD_RECORD
;
404 esdfmt
|= (as
->nchannels
== 2) ? ESD_STEREO
: ESD_MONO
;
409 obt_as
.fmt
= AUD_FMT_U8
;
414 esdfmt
|= ESD_BITS16
;
415 obt_as
.fmt
= AUD_FMT_S16
;
420 dolog ("Will use 16 instead of 32 bit samples\n");
421 esdfmt
|= ESD_BITS16
;
422 obt_as
.fmt
= AUD_FMT_S16
;
425 obt_as
.endianness
= AUDIO_HOST_ENDIANNESS
;
427 audio_pcm_init_info (&hw
->info
, &obt_as
);
429 hw
->samples
= conf
.samples
;
430 esd
->pcm_buf
= audio_calloc (AUDIO_FUNC
, hw
->samples
, 1 << hw
->info
.shift
);
432 dolog ("Could not allocate buffer (%d bytes)\n",
433 hw
->samples
<< hw
->info
.shift
);
437 esd
->fd
= esd_record_stream (esdfmt
, as
->freq
, conf
.adc_host
, NULL
);
439 qesd_logerr (errno
, "esd_record_stream failed\n");
443 if (audio_pt_init (&esd
->pt
, qesd_thread_in
, esd
, AUDIO_CAP
, AUDIO_FUNC
)) {
450 if (close (esd
->fd
)) {
451 qesd_logerr (errno
, "%s: close on esd socket(%d) failed\n",
452 AUDIO_FUNC
, esd
->fd
);
457 qemu_free (esd
->pcm_buf
);
462 static void qesd_fini_in (HWVoiceIn
*hw
)
465 ESDVoiceIn
*esd
= (ESDVoiceIn
*) hw
;
467 audio_pt_lock (&esd
->pt
, AUDIO_FUNC
);
469 audio_pt_unlock_and_signal (&esd
->pt
, AUDIO_FUNC
);
470 audio_pt_join (&esd
->pt
, &ret
, AUDIO_FUNC
);
473 if (close (esd
->fd
)) {
474 qesd_logerr (errno
, "failed to close esd socket\n");
479 audio_pt_fini (&esd
->pt
, AUDIO_FUNC
);
481 qemu_free (esd
->pcm_buf
);
485 static int qesd_ctl_in (HWVoiceIn
*hw
, int cmd
, ...)
493 static void *qesd_audio_init (void)
498 static void qesd_audio_fini (void *opaque
)
504 struct audio_option qesd_options
[] = {
508 .valp
= &conf
.samples
,
509 .descr
= "buffer size in samples"
514 .valp
= &conf
.divisor
,
515 .descr
= "threshold divisor"
520 .valp
= &conf
.dac_host
,
521 .descr
= "playback host"
526 .valp
= &conf
.adc_host
,
527 .descr
= "capture host"
529 { /* End of list */ }
532 static struct audio_pcm_ops qesd_pcm_ops
= {
533 .init_out
= qesd_init_out
,
534 .fini_out
= qesd_fini_out
,
535 .run_out
= qesd_run_out
,
537 .ctl_out
= qesd_ctl_out
,
539 .init_in
= qesd_init_in
,
540 .fini_in
= qesd_fini_in
,
541 .run_in
= qesd_run_in
,
543 .ctl_in
= qesd_ctl_in
,
546 struct audio_driver esd_audio_driver
= {
548 .descr
= "http://en.wikipedia.org/wiki/Esound",
549 .options
= qesd_options
,
550 .init
= qesd_audio_init
,
551 .fini
= qesd_audio_fini
,
552 .pcm_ops
= &qesd_pcm_ops
,
554 .max_voices_out
= INT_MAX
,
555 .max_voices_in
= INT_MAX
,
556 .voice_size_out
= sizeof (ESDVoiceOut
),
557 .voice_size_in
= sizeof (ESDVoiceIn
)