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"
65 static void GCC_FMT_ATTR (2, 3) qesd_logerr (int err
, const char *fmt
, ...)
70 AUD_vlog (AUDIO_CAP
, fmt
, ap
);
73 AUD_log (AUDIO_CAP
, "Reason: %s\n", strerror (err
));
77 static void *qesd_thread_out (void *arg
)
79 ESDVoiceOut
*esd
= arg
;
80 HWVoiceOut
*hw
= &esd
->hw
;
83 threshold
= conf
.divisor
? hw
->samples
/ conf
.divisor
: 0;
85 if (audio_pt_lock (&esd
->pt
, AUDIO_FUNC
)) {
90 int decr
, to_mix
, rpos
;
97 if (esd
->live
> threshold
) {
101 if (audio_pt_wait (&esd
->pt
, AUDIO_FUNC
)) {
106 decr
= to_mix
= esd
->live
;
109 if (audio_pt_unlock (&esd
->pt
, AUDIO_FUNC
)) {
115 int chunk
= audio_MIN (to_mix
, hw
->samples
- rpos
);
116 struct st_sample
*src
= hw
->mix_buf
+ rpos
;
118 hw
->clip (esd
->pcm_buf
, src
, chunk
);
121 written
= write (esd
->fd
, esd
->pcm_buf
, chunk
<< hw
->info
.shift
);
123 if (errno
== EINTR
|| errno
== EAGAIN
) {
126 qesd_logerr (errno
, "write failed\n");
130 if (written
!= chunk
<< hw
->info
.shift
) {
131 int wsamples
= written
>> hw
->info
.shift
;
132 int wbytes
= wsamples
<< hw
->info
.shift
;
133 if (wbytes
!= written
) {
134 dolog ("warning: Misaligned write %d (requested %zd), "
136 wbytes
, written
, hw
->info
.align
+ 1);
139 rpos
= (rpos
+ wsamples
) % hw
->samples
;
143 rpos
= (rpos
+ chunk
) % hw
->samples
;
147 if (audio_pt_lock (&esd
->pt
, AUDIO_FUNC
)) {
157 audio_pt_unlock (&esd
->pt
, AUDIO_FUNC
);
161 static int qesd_run_out (HWVoiceOut
*hw
, int live
)
164 ESDVoiceOut
*esd
= (ESDVoiceOut
*) hw
;
166 if (audio_pt_lock (&esd
->pt
, AUDIO_FUNC
)) {
170 decr
= audio_MIN (live
, esd
->decr
);
172 esd
->live
= live
- decr
;
173 hw
->rpos
= esd
->rpos
;
175 audio_pt_unlock_and_signal (&esd
->pt
, AUDIO_FUNC
);
178 audio_pt_unlock (&esd
->pt
, AUDIO_FUNC
);
183 static int qesd_write (SWVoiceOut
*sw
, void *buf
, int len
)
185 return audio_pcm_sw_write (sw
, buf
, len
);
188 static int qesd_init_out (HWVoiceOut
*hw
, struct audsettings
*as
)
190 ESDVoiceOut
*esd
= (ESDVoiceOut
*) hw
;
191 struct audsettings obt_as
= *as
;
192 int esdfmt
= ESD_STREAM
| ESD_PLAY
;
194 sigset_t set
, old_set
;
198 esdfmt
|= (as
->nchannels
== 2) ? ESD_STEREO
: ESD_MONO
;
203 obt_as
.fmt
= AUD_FMT_U8
;
208 dolog ("Will use 16 instead of 32 bit samples\n");
213 esdfmt
|= ESD_BITS16
;
214 obt_as
.fmt
= AUD_FMT_S16
;
218 dolog ("Internal logic error: Bad audio format %d\n", as
->fmt
);
222 obt_as
.endianness
= AUDIO_HOST_ENDIANNESS
;
224 audio_pcm_init_info (&hw
->info
, &obt_as
);
226 hw
->samples
= conf
.samples
;
227 esd
->pcm_buf
= audio_calloc (AUDIO_FUNC
, hw
->samples
, 1 << hw
->info
.shift
);
229 dolog ("Could not allocate buffer (%d bytes)\n",
230 hw
->samples
<< hw
->info
.shift
);
235 err
= pthread_sigmask (SIG_BLOCK
, &set
, &old_set
);
237 qesd_logerr (err
, "pthread_sigmask failed\n");
241 esd
->fd
= esd_play_stream (esdfmt
, as
->freq
, conf
.dac_host
, NULL
);
243 qesd_logerr (errno
, "esd_play_stream failed\n");
247 if (audio_pt_init (&esd
->pt
, qesd_thread_out
, esd
, AUDIO_CAP
, AUDIO_FUNC
)) {
251 err
= pthread_sigmask (SIG_SETMASK
, &old_set
, NULL
);
253 qesd_logerr (err
, "pthread_sigmask(restore) failed\n");
259 if (close (esd
->fd
)) {
260 qesd_logerr (errno
, "%s: close on esd socket(%d) failed\n",
261 AUDIO_FUNC
, esd
->fd
);
266 err
= pthread_sigmask (SIG_SETMASK
, &old_set
, NULL
);
268 qesd_logerr (err
, "pthread_sigmask(restore) failed\n");
272 qemu_free (esd
->pcm_buf
);
277 static void qesd_fini_out (HWVoiceOut
*hw
)
280 ESDVoiceOut
*esd
= (ESDVoiceOut
*) hw
;
282 audio_pt_lock (&esd
->pt
, AUDIO_FUNC
);
284 audio_pt_unlock_and_signal (&esd
->pt
, AUDIO_FUNC
);
285 audio_pt_join (&esd
->pt
, &ret
, AUDIO_FUNC
);
288 if (close (esd
->fd
)) {
289 qesd_logerr (errno
, "failed to close esd socket\n");
294 audio_pt_fini (&esd
->pt
, AUDIO_FUNC
);
296 qemu_free (esd
->pcm_buf
);
300 static int qesd_ctl_out (HWVoiceOut
*hw
, int cmd
, ...)
308 static void *qesd_thread_in (void *arg
)
310 ESDVoiceIn
*esd
= arg
;
311 HWVoiceIn
*hw
= &esd
->hw
;
314 threshold
= conf
.divisor
? hw
->samples
/ conf
.divisor
: 0;
316 if (audio_pt_lock (&esd
->pt
, AUDIO_FUNC
)) {
321 int incr
, to_grab
, wpos
;
328 if (esd
->dead
> threshold
) {
332 if (audio_pt_wait (&esd
->pt
, AUDIO_FUNC
)) {
337 incr
= to_grab
= esd
->dead
;
340 if (audio_pt_unlock (&esd
->pt
, AUDIO_FUNC
)) {
346 int chunk
= audio_MIN (to_grab
, hw
->samples
- wpos
);
347 void *buf
= advance (esd
->pcm_buf
, wpos
);
350 nread
= read (esd
->fd
, buf
, chunk
<< hw
->info
.shift
);
352 if (errno
== EINTR
|| errno
== EAGAIN
) {
355 qesd_logerr (errno
, "read failed\n");
359 if (nread
!= chunk
<< hw
->info
.shift
) {
360 int rsamples
= nread
>> hw
->info
.shift
;
361 int rbytes
= rsamples
<< hw
->info
.shift
;
362 if (rbytes
!= nread
) {
363 dolog ("warning: Misaligned write %d (requested %zd), "
365 rbytes
, nread
, hw
->info
.align
+ 1);
368 wpos
= (wpos
+ rsamples
) % hw
->samples
;
372 hw
->conv (hw
->conv_buf
+ wpos
, buf
, nread
>> hw
->info
.shift
,
374 wpos
= (wpos
+ chunk
) % hw
->samples
;
378 if (audio_pt_lock (&esd
->pt
, AUDIO_FUNC
)) {
388 audio_pt_unlock (&esd
->pt
, AUDIO_FUNC
);
392 static int qesd_run_in (HWVoiceIn
*hw
)
394 int live
, incr
, dead
;
395 ESDVoiceIn
*esd
= (ESDVoiceIn
*) hw
;
397 if (audio_pt_lock (&esd
->pt
, AUDIO_FUNC
)) {
401 live
= audio_pcm_hw_get_live_in (hw
);
402 dead
= hw
->samples
- live
;
403 incr
= audio_MIN (dead
, esd
->incr
);
405 esd
->dead
= dead
- incr
;
406 hw
->wpos
= esd
->wpos
;
408 audio_pt_unlock_and_signal (&esd
->pt
, AUDIO_FUNC
);
411 audio_pt_unlock (&esd
->pt
, AUDIO_FUNC
);
416 static int qesd_read (SWVoiceIn
*sw
, void *buf
, int len
)
418 return audio_pcm_sw_read (sw
, buf
, len
);
421 static int qesd_init_in (HWVoiceIn
*hw
, struct audsettings
*as
)
423 ESDVoiceIn
*esd
= (ESDVoiceIn
*) hw
;
424 struct audsettings obt_as
= *as
;
425 int esdfmt
= ESD_STREAM
| ESD_RECORD
;
427 sigset_t set
, old_set
;
431 esdfmt
|= (as
->nchannels
== 2) ? ESD_STEREO
: ESD_MONO
;
436 obt_as
.fmt
= AUD_FMT_U8
;
441 esdfmt
|= ESD_BITS16
;
442 obt_as
.fmt
= AUD_FMT_S16
;
447 dolog ("Will use 16 instead of 32 bit samples\n");
448 esdfmt
|= ESD_BITS16
;
449 obt_as
.fmt
= AUD_FMT_S16
;
452 obt_as
.endianness
= AUDIO_HOST_ENDIANNESS
;
454 audio_pcm_init_info (&hw
->info
, &obt_as
);
456 hw
->samples
= conf
.samples
;
457 esd
->pcm_buf
= audio_calloc (AUDIO_FUNC
, hw
->samples
, 1 << hw
->info
.shift
);
459 dolog ("Could not allocate buffer (%d bytes)\n",
460 hw
->samples
<< hw
->info
.shift
);
466 err
= pthread_sigmask (SIG_BLOCK
, &set
, &old_set
);
468 qesd_logerr (err
, "pthread_sigmask failed\n");
472 esd
->fd
= esd_record_stream (esdfmt
, as
->freq
, conf
.adc_host
, NULL
);
474 qesd_logerr (errno
, "esd_record_stream failed\n");
478 if (audio_pt_init (&esd
->pt
, qesd_thread_in
, esd
, AUDIO_CAP
, AUDIO_FUNC
)) {
482 err
= pthread_sigmask (SIG_SETMASK
, &old_set
, NULL
);
484 qesd_logerr (err
, "pthread_sigmask(restore) failed\n");
490 if (close (esd
->fd
)) {
491 qesd_logerr (errno
, "%s: close on esd socket(%d) failed\n",
492 AUDIO_FUNC
, esd
->fd
);
497 err
= pthread_sigmask (SIG_SETMASK
, &old_set
, NULL
);
499 qesd_logerr (err
, "pthread_sigmask(restore) failed\n");
503 qemu_free (esd
->pcm_buf
);
508 static void qesd_fini_in (HWVoiceIn
*hw
)
511 ESDVoiceIn
*esd
= (ESDVoiceIn
*) hw
;
513 audio_pt_lock (&esd
->pt
, AUDIO_FUNC
);
515 audio_pt_unlock_and_signal (&esd
->pt
, AUDIO_FUNC
);
516 audio_pt_join (&esd
->pt
, &ret
, AUDIO_FUNC
);
519 if (close (esd
->fd
)) {
520 qesd_logerr (errno
, "failed to close esd socket\n");
525 audio_pt_fini (&esd
->pt
, AUDIO_FUNC
);
527 qemu_free (esd
->pcm_buf
);
531 static int qesd_ctl_in (HWVoiceIn
*hw
, int cmd
, ...)
539 static void *qesd_audio_init (void)
544 static void qesd_audio_fini (void *opaque
)
550 struct audio_option qesd_options
[] = {
554 .valp
= &conf
.samples
,
555 .descr
= "buffer size in samples"
560 .valp
= &conf
.divisor
,
561 .descr
= "threshold divisor"
566 .valp
= &conf
.dac_host
,
567 .descr
= "playback host"
572 .valp
= &conf
.adc_host
,
573 .descr
= "capture host"
575 { /* End of list */ }
578 static struct audio_pcm_ops qesd_pcm_ops
= {
579 .init_out
= qesd_init_out
,
580 .fini_out
= qesd_fini_out
,
581 .run_out
= qesd_run_out
,
583 .ctl_out
= qesd_ctl_out
,
585 .init_in
= qesd_init_in
,
586 .fini_in
= qesd_fini_in
,
587 .run_in
= qesd_run_in
,
589 .ctl_in
= qesd_ctl_in
,
592 struct audio_driver esd_audio_driver
= {
594 .descr
= "http://en.wikipedia.org/wiki/Esound",
595 .options
= qesd_options
,
596 .init
= qesd_audio_init
,
597 .fini
= qesd_audio_fini
,
598 .pcm_ops
= &qesd_pcm_ops
,
600 .max_voices_out
= INT_MAX
,
601 .max_voices_in
= INT_MAX
,
602 .voice_size_out
= sizeof (ESDVoiceOut
),
603 .voice_size_in
= sizeof (ESDVoiceIn
)