2 * QEMU SDL audio driver
4 * Copyright (c) 2004-2005 Vassili Karpov (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/osdep.h"
27 #include <SDL_thread.h>
28 #include "qemu/module.h"
29 #include "qapi/error.h"
34 #define _POSIX_PTHREAD_SEMANTICS 1
35 #elif defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__DragonFly__)
40 #define AUDIO_CAP "sdl"
41 #include "audio_int.h"
43 typedef struct SDLVoiceOut
{
48 SDL_AudioDeviceID devid
;
51 typedef struct SDLVoiceIn
{
56 SDL_AudioDeviceID devid
;
59 static void G_GNUC_PRINTF (1, 2) sdl_logerr (const char *fmt
, ...)
64 AUD_vlog (AUDIO_CAP
, fmt
, ap
);
67 AUD_log (AUDIO_CAP
, "Reason: %s\n", SDL_GetError ());
70 static int aud_to_sdlfmt (AudioFormat fmt
)
79 case AUDIO_FORMAT_S16
:
82 case AUDIO_FORMAT_U16
:
85 case AUDIO_FORMAT_S32
:
88 /* no unsigned 32-bit support in SDL */
90 case AUDIO_FORMAT_F32
:
94 dolog ("Internal logic error: Bad audio format %d\n", fmt
);
102 static int sdl_to_audfmt(int sdlfmt
, AudioFormat
*fmt
, int *endianness
)
107 *fmt
= AUDIO_FORMAT_S8
;
112 *fmt
= AUDIO_FORMAT_U8
;
117 *fmt
= AUDIO_FORMAT_S16
;
122 *fmt
= AUDIO_FORMAT_U16
;
127 *fmt
= AUDIO_FORMAT_S16
;
132 *fmt
= AUDIO_FORMAT_U16
;
137 *fmt
= AUDIO_FORMAT_S32
;
142 *fmt
= AUDIO_FORMAT_S32
;
147 *fmt
= AUDIO_FORMAT_F32
;
152 *fmt
= AUDIO_FORMAT_F32
;
156 dolog ("Unrecognized SDL audio format %d\n", sdlfmt
);
163 static SDL_AudioDeviceID
sdl_open(SDL_AudioSpec
*req
, SDL_AudioSpec
*obt
,
166 SDL_AudioDeviceID devid
;
171 /* Make sure potential threads created by SDL don't hog signals. */
172 err
= sigfillset (&new);
174 dolog ("sdl_open: sigfillset failed: %s\n", strerror (errno
));
177 err
= pthread_sigmask (SIG_BLOCK
, &new, &old
);
179 dolog ("sdl_open: pthread_sigmask failed: %s\n", strerror (err
));
184 devid
= SDL_OpenAudioDevice(NULL
, rec
, req
, obt
, 0);
186 sdl_logerr("SDL_OpenAudioDevice for %s failed\n",
187 rec
? "recording" : "playback");
191 err
= pthread_sigmask (SIG_SETMASK
, &old
, NULL
);
193 dolog ("sdl_open: pthread_sigmask (restore) failed: %s\n",
195 /* We have failed to restore original signal mask, all bets are off,
196 so exit the process */
203 static void sdl_close_out(SDLVoiceOut
*sdl
)
205 if (sdl
->initialized
) {
206 SDL_LockAudioDevice(sdl
->devid
);
208 SDL_UnlockAudioDevice(sdl
->devid
);
209 SDL_PauseAudioDevice(sdl
->devid
, 1);
210 sdl
->initialized
= 0;
213 SDL_CloseAudioDevice(sdl
->devid
);
218 static void sdl_callback_out(void *opaque
, Uint8
*buf
, int len
)
220 SDLVoiceOut
*sdl
= opaque
;
221 HWVoiceOut
*hw
= &sdl
->hw
;
225 /* dolog("callback_out: len=%d avail=%zu\n", len, hw->pending_emul); */
227 while (hw
->pending_emul
&& len
) {
228 size_t write_len
, start
;
230 start
= audio_ring_posb(hw
->pos_emul
, hw
->pending_emul
,
232 assert(start
< hw
->size_emul
);
234 write_len
= MIN(MIN(hw
->pending_emul
, len
),
235 hw
->size_emul
- start
);
237 memcpy(buf
, hw
->buf_emul
+ start
, write_len
);
238 hw
->pending_emul
-= write_len
;
244 /* clear remaining buffer that we couldn't fill with data */
246 audio_pcm_info_clear_buf(&hw
->info
, buf
,
247 len
/ hw
->info
.bytes_per_frame
);
251 static void sdl_close_in(SDLVoiceIn
*sdl
)
253 if (sdl
->initialized
) {
254 SDL_LockAudioDevice(sdl
->devid
);
256 SDL_UnlockAudioDevice(sdl
->devid
);
257 SDL_PauseAudioDevice(sdl
->devid
, 1);
258 sdl
->initialized
= 0;
261 SDL_CloseAudioDevice(sdl
->devid
);
266 static void sdl_callback_in(void *opaque
, Uint8
*buf
, int len
)
268 SDLVoiceIn
*sdl
= opaque
;
269 HWVoiceIn
*hw
= &sdl
->hw
;
275 /* dolog("callback_in: len=%d pending=%zu\n", len, hw->pending_emul); */
277 while (hw
->pending_emul
< hw
->size_emul
&& len
) {
278 size_t read_len
= MIN(len
, MIN(hw
->size_emul
- hw
->pos_emul
,
279 hw
->size_emul
- hw
->pending_emul
));
281 memcpy(hw
->buf_emul
+ hw
->pos_emul
, buf
, read_len
);
283 hw
->pending_emul
+= read_len
;
284 hw
->pos_emul
= (hw
->pos_emul
+ read_len
) % hw
->size_emul
;
290 #define SDL_WRAPPER_FUNC(name, ret_type, args_decl, args, dir) \
291 static ret_type glue(sdl_, name)args_decl \
294 glue(SDLVoice, dir) *sdl = (glue(SDLVoice, dir) *)hw; \
296 SDL_LockAudioDevice(sdl->devid); \
297 ret = glue(audio_generic_, name)args; \
298 SDL_UnlockAudioDevice(sdl->devid); \
303 #define SDL_WRAPPER_VOID_FUNC(name, args_decl, args, dir) \
304 static void glue(sdl_, name)args_decl \
306 glue(SDLVoice, dir) *sdl = (glue(SDLVoice, dir) *)hw; \
308 SDL_LockAudioDevice(sdl->devid); \
309 glue(audio_generic_, name)args; \
310 SDL_UnlockAudioDevice(sdl->devid); \
313 SDL_WRAPPER_FUNC(buffer_get_free
, size_t, (HWVoiceOut
*hw
), (hw
), Out
)
314 SDL_WRAPPER_FUNC(get_buffer_out
, void *, (HWVoiceOut
*hw
, size_t *size
),
316 SDL_WRAPPER_FUNC(put_buffer_out
, size_t,
317 (HWVoiceOut
*hw
, void *buf
, size_t size
), (hw
, buf
, size
), Out
)
318 SDL_WRAPPER_FUNC(write
, size_t,
319 (HWVoiceOut
*hw
, void *buf
, size_t size
), (hw
, buf
, size
), Out
)
320 SDL_WRAPPER_FUNC(read
, size_t, (HWVoiceIn
*hw
, void *buf
, size_t size
),
322 SDL_WRAPPER_FUNC(get_buffer_in
, void *, (HWVoiceIn
*hw
, size_t *size
),
324 SDL_WRAPPER_VOID_FUNC(put_buffer_in
, (HWVoiceIn
*hw
, void *buf
, size_t size
),
326 #undef SDL_WRAPPER_FUNC
327 #undef SDL_WRAPPER_VOID_FUNC
329 static void sdl_fini_out(HWVoiceOut
*hw
)
331 SDLVoiceOut
*sdl
= (SDLVoiceOut
*)hw
;
336 static int sdl_init_out(HWVoiceOut
*hw
, struct audsettings
*as
,
339 SDLVoiceOut
*sdl
= (SDLVoiceOut
*)hw
;
340 SDL_AudioSpec req
, obt
;
343 AudioFormat effective_fmt
;
344 Audiodev
*dev
= drv_opaque
;
345 AudiodevSdlPerDirectionOptions
*spdo
= dev
->u
.sdl
.out
;
346 struct audsettings obt_as
;
349 req
.format
= aud_to_sdlfmt (as
->fmt
);
350 req
.channels
= as
->nchannels
;
351 /* SDL samples are QEMU frames */
352 req
.samples
= audio_buffer_frames(
353 qapi_AudiodevSdlPerDirectionOptions_base(spdo
), as
, 11610);
354 req
.callback
= sdl_callback_out
;
358 sdl
->devid
= sdl_open(&req
, &obt
, 0);
363 err
= sdl_to_audfmt(obt
.format
, &effective_fmt
, &endianness
);
369 obt_as
.freq
= obt
.freq
;
370 obt_as
.nchannels
= obt
.channels
;
371 obt_as
.fmt
= effective_fmt
;
372 obt_as
.endianness
= endianness
;
374 audio_pcm_init_info (&hw
->info
, &obt_as
);
375 hw
->samples
= (spdo
->has_buffer_count
? spdo
->buffer_count
: 4) *
378 sdl
->initialized
= 1;
383 static void sdl_enable_out(HWVoiceOut
*hw
, bool enable
)
385 SDLVoiceOut
*sdl
= (SDLVoiceOut
*)hw
;
387 SDL_PauseAudioDevice(sdl
->devid
, !enable
);
390 static void sdl_fini_in(HWVoiceIn
*hw
)
392 SDLVoiceIn
*sdl
= (SDLVoiceIn
*)hw
;
397 static int sdl_init_in(HWVoiceIn
*hw
, audsettings
*as
, void *drv_opaque
)
399 SDLVoiceIn
*sdl
= (SDLVoiceIn
*)hw
;
400 SDL_AudioSpec req
, obt
;
403 AudioFormat effective_fmt
;
404 Audiodev
*dev
= drv_opaque
;
405 AudiodevSdlPerDirectionOptions
*spdo
= dev
->u
.sdl
.in
;
406 struct audsettings obt_as
;
409 req
.format
= aud_to_sdlfmt(as
->fmt
);
410 req
.channels
= as
->nchannels
;
411 /* SDL samples are QEMU frames */
412 req
.samples
= audio_buffer_frames(
413 qapi_AudiodevSdlPerDirectionOptions_base(spdo
), as
, 11610);
414 req
.callback
= sdl_callback_in
;
418 sdl
->devid
= sdl_open(&req
, &obt
, 1);
423 err
= sdl_to_audfmt(obt
.format
, &effective_fmt
, &endianness
);
429 obt_as
.freq
= obt
.freq
;
430 obt_as
.nchannels
= obt
.channels
;
431 obt_as
.fmt
= effective_fmt
;
432 obt_as
.endianness
= endianness
;
434 audio_pcm_init_info(&hw
->info
, &obt_as
);
435 hw
->samples
= (spdo
->has_buffer_count
? spdo
->buffer_count
: 4) *
437 hw
->size_emul
= hw
->samples
* hw
->info
.bytes_per_frame
;
438 hw
->buf_emul
= g_malloc(hw
->size_emul
);
439 hw
->pos_emul
= hw
->pending_emul
= 0;
441 sdl
->initialized
= 1;
446 static void sdl_enable_in(HWVoiceIn
*hw
, bool enable
)
448 SDLVoiceIn
*sdl
= (SDLVoiceIn
*)hw
;
450 SDL_PauseAudioDevice(sdl
->devid
, !enable
);
453 static void *sdl_audio_init(Audiodev
*dev
, Error
**errp
)
455 if (SDL_InitSubSystem (SDL_INIT_AUDIO
)) {
456 error_setg(errp
, "SDL failed to initialize audio subsystem");
463 static void sdl_audio_fini (void *opaque
)
465 SDL_QuitSubSystem (SDL_INIT_AUDIO
);
468 static struct audio_pcm_ops sdl_pcm_ops
= {
469 .init_out
= sdl_init_out
,
470 .fini_out
= sdl_fini_out
,
471 /* wrapper for audio_generic_write */
473 /* wrapper for audio_generic_buffer_get_free */
474 .buffer_get_free
= sdl_buffer_get_free
,
475 /* wrapper for audio_generic_get_buffer_out */
476 .get_buffer_out
= sdl_get_buffer_out
,
477 /* wrapper for audio_generic_put_buffer_out */
478 .put_buffer_out
= sdl_put_buffer_out
,
479 .enable_out
= sdl_enable_out
,
480 .init_in
= sdl_init_in
,
481 .fini_in
= sdl_fini_in
,
482 /* wrapper for audio_generic_read */
484 /* wrapper for audio_generic_get_buffer_in */
485 .get_buffer_in
= sdl_get_buffer_in
,
486 /* wrapper for audio_generic_put_buffer_in */
487 .put_buffer_in
= sdl_put_buffer_in
,
488 .enable_in
= sdl_enable_in
,
491 static struct audio_driver sdl_audio_driver
= {
493 .descr
= "SDL http://www.libsdl.org",
494 .init
= sdl_audio_init
,
495 .fini
= sdl_audio_fini
,
496 .pcm_ops
= &sdl_pcm_ops
,
497 .max_voices_out
= INT_MAX
,
498 .max_voices_in
= INT_MAX
,
499 .voice_size_out
= sizeof(SDLVoiceOut
),
500 .voice_size_in
= sizeof(SDLVoiceIn
),
503 static void register_audio_sdl(void)
505 audio_driver_register(&sdl_audio_driver
);
507 type_init(register_audio_sdl
);