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 <SDL_thread.h>
26 #include "qemu-common.h"
31 #define _POSIX_PTHREAD_SEMANTICS 1
36 #define AUDIO_CAP "sdl"
37 #include "audio_int.h"
39 typedef struct SDLVoiceOut {
52 struct SDLAudioState {
58 typedef struct SDLAudioState SDLAudioState;
60 static void GCC_FMT_ATTR (1, 2) sdl_logerr (const char *fmt, ...)
65 AUD_vlog (AUDIO_CAP, fmt, ap);
68 AUD_log (AUDIO_CAP, "Reason: %s\n", SDL_GetError ());
71 static int sdl_lock (SDLAudioState *s, const char *forfn)
73 if (SDL_LockMutex (s->mutex)) {
74 sdl_logerr ("SDL_LockMutex for %s failed\n", forfn);
80 static int sdl_unlock (SDLAudioState *s, const char *forfn)
82 if (SDL_UnlockMutex (s->mutex)) {
83 sdl_logerr ("SDL_UnlockMutex for %s failed\n", forfn);
89 static int sdl_post (SDLAudioState *s, const char *forfn)
91 if (SDL_SemPost (s->sem)) {
92 sdl_logerr ("SDL_SemPost for %s failed\n", forfn);
98 static int sdl_wait (SDLAudioState *s, const char *forfn)
100 if (SDL_SemWait (s->sem)) {
101 sdl_logerr ("SDL_SemWait for %s failed\n", forfn);
107 static int sdl_unlock_and_post (SDLAudioState *s, const char *forfn)
109 if (sdl_unlock (s, forfn)) {
113 return sdl_post (s, forfn);
116 static int aud_to_sdlfmt (audfmt_e fmt, int *shift)
136 dolog ("Internal logic error: Bad audio format %d\n", fmt);
144 static int sdl_to_audfmt (int sdlfmt, audfmt_e *fmt, int *endianess)
178 dolog ("Unrecognized SDL audio format %d\n", sdlfmt);
185 static int sdl_open (SDL_AudioSpec *req, SDL_AudioSpec *obt)
191 /* Make sure potential threads created by SDL don't hog signals. */
193 pthread_sigmask (SIG_BLOCK, &new, &old);
196 status = SDL_OpenAudio (req, obt);
198 sdl_logerr ("SDL_OpenAudio failed\n");
202 pthread_sigmask (SIG_SETMASK, &old, 0);
207 static void sdl_close (SDLAudioState *s)
209 if (s->initialized) {
210 sdl_lock (s, "sdl_close");
212 sdl_unlock_and_post (s, "sdl_close");
219 static void sdl_callback (void *opaque, Uint8 *buf, int len)
221 SDLVoiceOut *sdl = opaque;
222 SDLAudioState *s = &glob_sdl;
223 HWVoiceOut *hw = &sdl->hw;
224 int samples = len >> hw->info.shift;
233 /* dolog ("in callback samples=%d\n", samples); */
234 sdl_wait (s, "sdl_callback");
239 if (sdl_lock (s, "sdl_callback")) {
243 if (audio_bug (AUDIO_FUNC, sdl->live < 0 || sdl->live > hw->samples)) {
244 dolog ("sdl->live=%d hw->samples=%d\n",
245 sdl->live, hw->samples);
253 /* dolog ("in callback live=%d\n", live); */
254 to_mix = audio_MIN (samples, sdl->live);
257 int chunk = audio_MIN (to_mix, hw->samples - hw->rpos);
258 st_sample_t *src = hw->mix_buf + hw->rpos;
260 /* dolog ("in callback to_mix %d, chunk %d\n", to_mix, chunk); */
261 hw->clip (buf, src, chunk);
262 sdl->rpos = (sdl->rpos + chunk) % hw->samples;
264 buf += chunk << hw->info.shift;
271 if (sdl_unlock (s, "sdl_callback")) {
275 /* dolog ("done len=%d\n", len); */
278 static int sdl_write_out (SWVoiceOut *sw, void *buf, int len)
280 return audio_pcm_sw_write (sw, buf, len);
283 static int sdl_run_out (HWVoiceOut *hw)
286 SDLVoiceOut *sdl = (SDLVoiceOut *) hw;
287 SDLAudioState *s = &glob_sdl;
289 if (sdl_lock (s, "sdl_callback")) {
293 live = audio_pcm_hw_get_live_out (hw);
295 if (sdl->decr > live) {
296 ldebug ("sdl->decr %d live %d sdl->live %d\n",
302 decr = audio_MIN (sdl->decr, live);
305 sdl->live = live - decr;
306 hw->rpos = sdl->rpos;
309 sdl_unlock_and_post (s, "sdl_callback");
312 sdl_unlock (s, "sdl_callback");
317 static void sdl_fini_out (HWVoiceOut *hw)
321 sdl_close (&glob_sdl);
324 static int sdl_init_out (HWVoiceOut *hw, audsettings_t *as)
326 SDLVoiceOut *sdl = (SDLVoiceOut *) hw;
327 SDLAudioState *s = &glob_sdl;
328 SDL_AudioSpec req, obt;
332 audfmt_e effective_fmt;
333 audsettings_t obt_as;
335 shift <<= as->nchannels == 2;
338 req.format = aud_to_sdlfmt (as->fmt, &shift);
339 req.channels = as->nchannels;
340 req.samples = conf.nb_samples;
341 req.callback = sdl_callback;
344 if (sdl_open (&req, &obt)) {
348 err = sdl_to_audfmt (obt.format, &effective_fmt, &endianess);
354 obt_as.freq = obt.freq;
355 obt_as.nchannels = obt.channels;
356 obt_as.fmt = effective_fmt;
357 obt_as.endianness = endianess;
359 audio_pcm_init_info (&hw->info, &obt_as);
360 hw->samples = obt.samples;
368 static int sdl_ctl_out (HWVoiceOut *hw, int cmd, ...)
384 static void *sdl_audio_init (void)
386 SDLAudioState *s = &glob_sdl;
388 if (SDL_InitSubSystem (SDL_INIT_AUDIO)) {
389 sdl_logerr ("SDL failed to initialize audio subsystem\n");
393 s->mutex = SDL_CreateMutex ();
395 sdl_logerr ("Failed to create SDL mutex\n");
396 SDL_QuitSubSystem (SDL_INIT_AUDIO);
400 s->sem = SDL_CreateSemaphore (0);
402 sdl_logerr ("Failed to create SDL semaphore\n");
403 SDL_DestroyMutex (s->mutex);
404 SDL_QuitSubSystem (SDL_INIT_AUDIO);
411 static void sdl_audio_fini (void *opaque)
413 SDLAudioState *s = opaque;
415 SDL_DestroySemaphore (s->sem);
416 SDL_DestroyMutex (s->mutex);
417 SDL_QuitSubSystem (SDL_INIT_AUDIO);
420 static struct audio_option sdl_options[] = {
421 {"SAMPLES", AUD_OPT_INT, &conf.nb_samples,
422 "Size of SDL buffer in samples", NULL, 0},
423 {NULL, 0, NULL, NULL, NULL, 0}
426 static struct audio_pcm_ops sdl_pcm_ops = {
440 struct audio_driver sdl_audio_driver = {
441 INIT_FIELD (name = ) "sdl",
442 INIT_FIELD (descr = ) "SDL http://www.libsdl.org",
443 INIT_FIELD (options = ) sdl_options,
444 INIT_FIELD (init = ) sdl_audio_init,
445 INIT_FIELD (fini = ) sdl_audio_fini,
446 INIT_FIELD (pcm_ops = ) &sdl_pcm_ops,
447 INIT_FIELD (can_be_default = ) 1,
448 INIT_FIELD (max_voices_out = ) 1,
449 INIT_FIELD (max_voices_in = ) 0,
450 INIT_FIELD (voice_size_out = ) sizeof (SDLVoiceOut),
451 INIT_FIELD (voice_size_in = ) 0