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
{
47 SDL_AudioDeviceID devid
;
50 static void GCC_FMT_ATTR (1, 2) sdl_logerr (const char *fmt
, ...)
55 AUD_vlog (AUDIO_CAP
, fmt
, ap
);
58 AUD_log (AUDIO_CAP
, "Reason: %s\n", SDL_GetError ());
61 static int aud_to_sdlfmt (AudioFormat fmt
)
70 case AUDIO_FORMAT_S16
:
73 case AUDIO_FORMAT_U16
:
76 case AUDIO_FORMAT_S32
:
79 /* no unsigned 32-bit support in SDL */
81 case AUDIO_FORMAT_F32
:
85 dolog ("Internal logic error: Bad audio format %d\n", fmt
);
93 static int sdl_to_audfmt(int sdlfmt
, AudioFormat
*fmt
, int *endianness
)
98 *fmt
= AUDIO_FORMAT_S8
;
103 *fmt
= AUDIO_FORMAT_U8
;
108 *fmt
= AUDIO_FORMAT_S16
;
113 *fmt
= AUDIO_FORMAT_U16
;
118 *fmt
= AUDIO_FORMAT_S16
;
123 *fmt
= AUDIO_FORMAT_U16
;
128 *fmt
= AUDIO_FORMAT_S32
;
133 *fmt
= AUDIO_FORMAT_S32
;
138 *fmt
= AUDIO_FORMAT_F32
;
143 *fmt
= AUDIO_FORMAT_F32
;
147 dolog ("Unrecognized SDL audio format %d\n", sdlfmt
);
154 static SDL_AudioDeviceID
sdl_open(SDL_AudioSpec
*req
, SDL_AudioSpec
*obt
,
157 SDL_AudioDeviceID devid
;
162 /* Make sure potential threads created by SDL don't hog signals. */
163 err
= sigfillset (&new);
165 dolog ("sdl_open: sigfillset failed: %s\n", strerror (errno
));
168 err
= pthread_sigmask (SIG_BLOCK
, &new, &old
);
170 dolog ("sdl_open: pthread_sigmask failed: %s\n", strerror (err
));
175 devid
= SDL_OpenAudioDevice(NULL
, rec
, req
, obt
, 0);
177 sdl_logerr("SDL_OpenAudioDevice for %s failed\n",
178 rec
? "recording" : "playback");
182 err
= pthread_sigmask (SIG_SETMASK
, &old
, NULL
);
184 dolog ("sdl_open: pthread_sigmask (restore) failed: %s\n",
186 /* We have failed to restore original signal mask, all bets are off,
187 so exit the process */
194 static void sdl_close_out(SDLVoiceOut
*sdl
)
196 if (sdl
->initialized
) {
197 SDL_LockAudioDevice(sdl
->devid
);
199 SDL_UnlockAudioDevice(sdl
->devid
);
200 SDL_PauseAudioDevice(sdl
->devid
, 1);
201 sdl
->initialized
= 0;
204 SDL_CloseAudioDevice(sdl
->devid
);
209 static void sdl_callback_out(void *opaque
, Uint8
*buf
, int len
)
211 SDLVoiceOut
*sdl
= opaque
;
212 HWVoiceOut
*hw
= &sdl
->hw
;
216 /* dolog("callback_out: len=%d avail=%zu\n", len, hw->pending_emul); */
218 while (hw
->pending_emul
&& len
) {
220 ssize_t start
= (ssize_t
)hw
->pos_emul
- hw
->pending_emul
;
222 start
+= hw
->size_emul
;
224 assert(start
>= 0 && start
< hw
->size_emul
);
226 write_len
= MIN(MIN(hw
->pending_emul
, len
),
227 hw
->size_emul
- start
);
229 memcpy(buf
, hw
->buf_emul
+ start
, write_len
);
230 hw
->pending_emul
-= write_len
;
236 /* clear remaining buffer that we couldn't fill with data */
238 audio_pcm_info_clear_buf(&hw
->info
, buf
,
239 len
/ hw
->info
.bytes_per_frame
);
243 #define SDL_WRAPPER_FUNC(name, ret_type, args_decl, args, dir) \
244 static ret_type glue(sdl_, name)args_decl \
247 glue(SDLVoice, dir) *sdl = (glue(SDLVoice, dir) *)hw; \
249 SDL_LockAudioDevice(sdl->devid); \
250 ret = glue(audio_generic_, name)args; \
251 SDL_UnlockAudioDevice(sdl->devid); \
256 SDL_WRAPPER_FUNC(get_buffer_out
, void *, (HWVoiceOut
*hw
, size_t *size
),
258 SDL_WRAPPER_FUNC(put_buffer_out
, size_t,
259 (HWVoiceOut
*hw
, void *buf
, size_t size
), (hw
, buf
, size
), Out
)
260 SDL_WRAPPER_FUNC(write
, size_t,
261 (HWVoiceOut
*hw
, void *buf
, size_t size
), (hw
, buf
, size
), Out
)
262 #undef SDL_WRAPPER_FUNC
264 static void sdl_fini_out(HWVoiceOut
*hw
)
266 SDLVoiceOut
*sdl
= (SDLVoiceOut
*)hw
;
271 static int sdl_init_out(HWVoiceOut
*hw
, struct audsettings
*as
,
274 SDLVoiceOut
*sdl
= (SDLVoiceOut
*)hw
;
275 SDL_AudioSpec req
, obt
;
278 AudioFormat effective_fmt
;
279 Audiodev
*dev
= drv_opaque
;
280 AudiodevSdlPerDirectionOptions
*spdo
= dev
->u
.sdl
.out
;
281 struct audsettings obt_as
;
284 req
.format
= aud_to_sdlfmt (as
->fmt
);
285 req
.channels
= as
->nchannels
;
287 * This is wrong. SDL samples are QEMU frames. The buffer size will be
288 * the requested buffer size multiplied by the number of channels.
290 req
.samples
= audio_buffer_samples(
291 qapi_AudiodevSdlPerDirectionOptions_base(spdo
), as
, 11610);
292 req
.callback
= sdl_callback_out
;
296 sdl
->devid
= sdl_open(&req
, &obt
, 0);
301 err
= sdl_to_audfmt(obt
.format
, &effective_fmt
, &endianness
);
307 obt_as
.freq
= obt
.freq
;
308 obt_as
.nchannels
= obt
.channels
;
309 obt_as
.fmt
= effective_fmt
;
310 obt_as
.endianness
= endianness
;
312 audio_pcm_init_info (&hw
->info
, &obt_as
);
313 hw
->samples
= (spdo
->has_buffer_count
? spdo
->buffer_count
: 4) *
316 sdl
->initialized
= 1;
321 static void sdl_enable_out(HWVoiceOut
*hw
, bool enable
)
323 SDLVoiceOut
*sdl
= (SDLVoiceOut
*)hw
;
325 SDL_PauseAudioDevice(sdl
->devid
, !enable
);
328 static void *sdl_audio_init(Audiodev
*dev
)
330 if (SDL_InitSubSystem (SDL_INIT_AUDIO
)) {
331 sdl_logerr ("SDL failed to initialize audio subsystem\n");
338 static void sdl_audio_fini (void *opaque
)
340 SDL_QuitSubSystem (SDL_INIT_AUDIO
);
343 static struct audio_pcm_ops sdl_pcm_ops
= {
344 .init_out
= sdl_init_out
,
345 .fini_out
= sdl_fini_out
,
346 /* wrapper for audio_generic_write */
348 /* wrapper for audio_generic_get_buffer_out */
349 .get_buffer_out
= sdl_get_buffer_out
,
350 /* wrapper for audio_generic_put_buffer_out */
351 .put_buffer_out
= sdl_put_buffer_out
,
352 .enable_out
= sdl_enable_out
,
355 static struct audio_driver sdl_audio_driver
= {
357 .descr
= "SDL http://www.libsdl.org",
358 .init
= sdl_audio_init
,
359 .fini
= sdl_audio_fini
,
360 .pcm_ops
= &sdl_pcm_ops
,
364 .voice_size_out
= sizeof (SDLVoiceOut
),
368 static void register_audio_sdl(void)
370 audio_driver_register(&sdl_audio_driver
);
372 type_init(register_audio_sdl
);