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"
33 #define _POSIX_PTHREAD_SEMANTICS 1
34 #elif defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__DragonFly__)
39 #define AUDIO_CAP "sdl"
40 #include "audio_int.h"
42 typedef struct SDLVoiceOut
{
46 static struct SDLAudioState
{
52 typedef struct SDLAudioState SDLAudioState
;
54 static void GCC_FMT_ATTR (1, 2) sdl_logerr (const char *fmt
, ...)
59 AUD_vlog (AUDIO_CAP
, fmt
, ap
);
62 AUD_log (AUDIO_CAP
, "Reason: %s\n", SDL_GetError ());
65 static int aud_to_sdlfmt (AudioFormat fmt
)
74 case AUDIO_FORMAT_S16
:
77 case AUDIO_FORMAT_U16
:
80 case AUDIO_FORMAT_S32
:
83 /* no unsigned 32-bit support in SDL */
85 case AUDIO_FORMAT_F32
:
89 dolog ("Internal logic error: Bad audio format %d\n", fmt
);
97 static int sdl_to_audfmt(int sdlfmt
, AudioFormat
*fmt
, int *endianness
)
102 *fmt
= AUDIO_FORMAT_S8
;
107 *fmt
= AUDIO_FORMAT_U8
;
112 *fmt
= AUDIO_FORMAT_S16
;
117 *fmt
= AUDIO_FORMAT_U16
;
122 *fmt
= AUDIO_FORMAT_S16
;
127 *fmt
= AUDIO_FORMAT_U16
;
132 *fmt
= AUDIO_FORMAT_S32
;
137 *fmt
= AUDIO_FORMAT_S32
;
142 *fmt
= AUDIO_FORMAT_F32
;
147 *fmt
= AUDIO_FORMAT_F32
;
151 dolog ("Unrecognized SDL audio format %d\n", sdlfmt
);
158 static int sdl_open (SDL_AudioSpec
*req
, SDL_AudioSpec
*obt
)
165 /* Make sure potential threads created by SDL don't hog signals. */
166 err
= sigfillset (&new);
168 dolog ("sdl_open: sigfillset failed: %s\n", strerror (errno
));
171 err
= pthread_sigmask (SIG_BLOCK
, &new, &old
);
173 dolog ("sdl_open: pthread_sigmask failed: %s\n", strerror (err
));
178 status
= SDL_OpenAudio (req
, obt
);
180 sdl_logerr ("SDL_OpenAudio failed\n");
184 err
= pthread_sigmask (SIG_SETMASK
, &old
, NULL
);
186 dolog ("sdl_open: pthread_sigmask (restore) failed: %s\n",
188 /* We have failed to restore original signal mask, all bets are off,
189 so exit the process */
196 static void sdl_close (SDLAudioState
*s
)
198 if (s
->initialized
) {
208 static void sdl_callback (void *opaque
, Uint8
*buf
, int len
)
210 SDLVoiceOut
*sdl
= opaque
;
211 SDLAudioState
*s
= &glob_sdl
;
212 HWVoiceOut
*hw
= &sdl
->hw
;
218 /* dolog ("in callback samples=%zu live=%zu\n", samples, sdl->live); */
220 while (hw
->pending_emul
&& len
) {
222 ssize_t start
= ((ssize_t
) hw
->pos_emul
) - hw
->pending_emul
;
224 start
+= hw
->size_emul
;
226 assert(start
>= 0 && start
< hw
->size_emul
);
228 write_len
= MIN(MIN(hw
->pending_emul
, len
),
229 hw
->size_emul
- start
);
231 memcpy(buf
, hw
->buf_emul
+ start
, write_len
);
232 hw
->pending_emul
-= write_len
;
237 /* clear remaining buffer that we couldn't fill with data */
243 #define SDL_WRAPPER_FUNC(name, ret_type, args_decl, args, fail, unlock) \
244 static ret_type glue(sdl_, name)args_decl \
250 ret = glue(audio_generic_, name)args; \
256 SDL_WRAPPER_FUNC(get_buffer_out
, void *, (HWVoiceOut
*hw
, size_t *size
),
257 (hw
, size
), *size
= 0, sdl_unlock
)
258 SDL_WRAPPER_FUNC(put_buffer_out
, size_t,
259 (HWVoiceOut
*hw
, void *buf
, size_t size
), (hw
, buf
, size
),
260 /*nothing*/, sdl_unlock_and_post
)
261 SDL_WRAPPER_FUNC(write
, size_t,
262 (HWVoiceOut
*hw
, void *buf
, size_t size
), (hw
, buf
, size
),
263 /*nothing*/, sdl_unlock_and_post
)
265 #undef SDL_WRAPPER_FUNC
267 static void sdl_fini_out (HWVoiceOut
*hw
)
271 sdl_close (&glob_sdl
);
274 static int sdl_init_out(HWVoiceOut
*hw
, struct audsettings
*as
,
277 SDLVoiceOut
*sdl
= (SDLVoiceOut
*) hw
;
278 SDLAudioState
*s
= &glob_sdl
;
279 SDL_AudioSpec req
, obt
;
282 AudioFormat effective_fmt
;
283 struct audsettings obt_as
;
286 req
.format
= aud_to_sdlfmt (as
->fmt
);
287 req
.channels
= as
->nchannels
;
288 req
.samples
= audio_buffer_samples(s
->dev
->u
.sdl
.out
, as
, 11610);
289 req
.callback
= sdl_callback
;
292 if (sdl_open (&req
, &obt
)) {
296 err
= sdl_to_audfmt(obt
.format
, &effective_fmt
, &endianness
);
302 obt_as
.freq
= obt
.freq
;
303 obt_as
.nchannels
= obt
.channels
;
304 obt_as
.fmt
= effective_fmt
;
305 obt_as
.endianness
= endianness
;
307 audio_pcm_init_info (&hw
->info
, &obt_as
);
308 hw
->samples
= obt
.samples
;
316 static void sdl_enable_out(HWVoiceOut
*hw
, bool enable
)
318 SDL_PauseAudio(!enable
);
321 static void *sdl_audio_init(Audiodev
*dev
)
323 SDLAudioState
*s
= &glob_sdl
;
324 if (s
->driver_created
) {
325 sdl_logerr("Can't create multiple sdl backends\n");
329 if (SDL_InitSubSystem (SDL_INIT_AUDIO
)) {
330 sdl_logerr ("SDL failed to initialize audio subsystem\n");
334 s
->driver_created
= true;
339 static void sdl_audio_fini (void *opaque
)
341 SDLAudioState
*s
= opaque
;
343 SDL_QuitSubSystem (SDL_INIT_AUDIO
);
344 s
->driver_created
= false;
348 static struct audio_pcm_ops sdl_pcm_ops
= {
349 .init_out
= sdl_init_out
,
350 .fini_out
= sdl_fini_out
,
351 /* wrapper for audio_generic_write */
353 /* wrapper for audio_generic_get_buffer_out */
354 .get_buffer_out
= sdl_get_buffer_out
,
355 /* wrapper for audio_generic_put_buffer_out */
356 .put_buffer_out
= sdl_put_buffer_out
,
357 .enable_out
= sdl_enable_out
,
360 static struct audio_driver sdl_audio_driver
= {
362 .descr
= "SDL http://www.libsdl.org",
363 .init
= sdl_audio_init
,
364 .fini
= sdl_audio_fini
,
365 .pcm_ops
= &sdl_pcm_ops
,
369 .voice_size_out
= sizeof (SDLVoiceOut
),
373 static void register_audio_sdl(void)
375 audio_driver_register(&sdl_audio_driver
);
377 type_init(register_audio_sdl
);