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>
28 #define AUDIO_CAP "sdl"
29 #include "audio_int.h"
31 typedef struct SDLVoiceOut
{
44 struct SDLAudioState
{
50 typedef struct SDLAudioState SDLAudioState
;
52 static void GCC_FMT_ATTR (1, 2) sdl_logerr (const char *fmt
, ...)
57 AUD_vlog (AUDIO_CAP
, fmt
, ap
);
60 AUD_log (AUDIO_CAP
, "Reason: %s\n", SDL_GetError ());
63 static int sdl_lock (SDLAudioState
*s
, const char *forfn
)
65 if (SDL_LockMutex (s
->mutex
)) {
66 sdl_logerr ("SDL_LockMutex for %s failed\n", forfn
);
72 static int sdl_unlock (SDLAudioState
*s
, const char *forfn
)
74 if (SDL_UnlockMutex (s
->mutex
)) {
75 sdl_logerr ("SDL_UnlockMutex for %s failed\n", forfn
);
81 static int sdl_post (SDLAudioState
*s
, const char *forfn
)
83 if (SDL_SemPost (s
->sem
)) {
84 sdl_logerr ("SDL_SemPost for %s failed\n", forfn
);
90 static int sdl_wait (SDLAudioState
*s
, const char *forfn
)
92 if (SDL_SemWait (s
->sem
)) {
93 sdl_logerr ("SDL_SemWait for %s failed\n", forfn
);
99 static int sdl_unlock_and_post (SDLAudioState
*s
, const char *forfn
)
101 if (sdl_unlock (s
, forfn
)) {
105 return sdl_post (s
, forfn
);
108 static int aud_to_sdlfmt (audfmt_e fmt
, int *shift
)
128 dolog ("Internal logic error: Bad audio format %d\n", fmt
);
136 static int sdl_to_audfmt (int sdlfmt
, audfmt_e
*fmt
, int *endianess
)
170 dolog ("Unrecognized SDL audio format %d\n", sdlfmt
);
177 static int sdl_open (SDL_AudioSpec
*req
, SDL_AudioSpec
*obt
)
181 status
= SDL_OpenAudio (req
, obt
);
183 sdl_logerr ("SDL_OpenAudio failed\n");
188 static void sdl_close (SDLAudioState
*s
)
190 if (s
->initialized
) {
191 sdl_lock (s
, "sdl_close");
193 sdl_unlock_and_post (s
, "sdl_close");
200 static void sdl_callback (void *opaque
, Uint8
*buf
, int len
)
202 SDLVoiceOut
*sdl
= opaque
;
203 SDLAudioState
*s
= &glob_sdl
;
204 HWVoiceOut
*hw
= &sdl
->hw
;
205 int samples
= len
>> hw
->info
.shift
;
214 /* dolog ("in callback samples=%d\n", samples); */
215 sdl_wait (s
, "sdl_callback");
220 if (sdl_lock (s
, "sdl_callback")) {
224 if (audio_bug (AUDIO_FUNC
, sdl
->live
< 0 || sdl
->live
> hw
->samples
)) {
225 dolog ("sdl->live=%d hw->samples=%d\n",
226 sdl
->live
, hw
->samples
);
234 /* dolog ("in callback live=%d\n", live); */
235 to_mix
= audio_MIN (samples
, sdl
->live
);
238 int chunk
= audio_MIN (to_mix
, hw
->samples
- hw
->rpos
);
239 st_sample_t
*src
= hw
->mix_buf
+ hw
->rpos
;
241 /* dolog ("in callback to_mix %d, chunk %d\n", to_mix, chunk); */
242 hw
->clip (buf
, src
, chunk
);
243 sdl
->rpos
= (sdl
->rpos
+ chunk
) % hw
->samples
;
245 buf
+= chunk
<< hw
->info
.shift
;
252 if (sdl_unlock (s
, "sdl_callback")) {
256 /* dolog ("done len=%d\n", len); */
259 static int sdl_write_out (SWVoiceOut
*sw
, void *buf
, int len
)
261 return audio_pcm_sw_write (sw
, buf
, len
);
264 static int sdl_run_out (HWVoiceOut
*hw
)
267 SDLVoiceOut
*sdl
= (SDLVoiceOut
*) hw
;
268 SDLAudioState
*s
= &glob_sdl
;
270 if (sdl_lock (s
, "sdl_callback")) {
274 live
= audio_pcm_hw_get_live_out (hw
);
276 if (sdl
->decr
> live
) {
277 ldebug ("sdl->decr %d live %d sdl->live %d\n",
283 decr
= audio_MIN (sdl
->decr
, live
);
286 sdl
->live
= live
- decr
;
287 hw
->rpos
= sdl
->rpos
;
290 sdl_unlock_and_post (s
, "sdl_callback");
293 sdl_unlock (s
, "sdl_callback");
298 static void sdl_fini_out (HWVoiceOut
*hw
)
302 sdl_close (&glob_sdl
);
305 static int sdl_init_out (HWVoiceOut
*hw
, audsettings_t
*as
)
307 SDLVoiceOut
*sdl
= (SDLVoiceOut
*) hw
;
308 SDLAudioState
*s
= &glob_sdl
;
309 SDL_AudioSpec req
, obt
;
313 audfmt_e effective_fmt
;
314 audsettings_t obt_as
;
316 shift
<<= as
->nchannels
== 2;
319 req
.format
= aud_to_sdlfmt (as
->fmt
, &shift
);
320 req
.channels
= as
->nchannels
;
321 req
.samples
= conf
.nb_samples
;
322 req
.callback
= sdl_callback
;
325 if (sdl_open (&req
, &obt
)) {
329 err
= sdl_to_audfmt (obt
.format
, &effective_fmt
, &endianess
);
335 obt_as
.freq
= obt
.freq
;
336 obt_as
.nchannels
= obt
.channels
;
337 obt_as
.fmt
= effective_fmt
;
338 obt_as
.endianness
= endianess
;
340 audio_pcm_init_info (&hw
->info
, &obt_as
);
341 hw
->samples
= obt
.samples
;
349 static int sdl_ctl_out (HWVoiceOut
*hw
, int cmd
, ...)
365 static void *sdl_audio_init (void)
367 SDLAudioState
*s
= &glob_sdl
;
369 if (SDL_InitSubSystem (SDL_INIT_AUDIO
)) {
370 sdl_logerr ("SDL failed to initialize audio subsystem\n");
374 s
->mutex
= SDL_CreateMutex ();
376 sdl_logerr ("Failed to create SDL mutex\n");
377 SDL_QuitSubSystem (SDL_INIT_AUDIO
);
381 s
->sem
= SDL_CreateSemaphore (0);
383 sdl_logerr ("Failed to create SDL semaphore\n");
384 SDL_DestroyMutex (s
->mutex
);
385 SDL_QuitSubSystem (SDL_INIT_AUDIO
);
392 static void sdl_audio_fini (void *opaque
)
394 SDLAudioState
*s
= opaque
;
396 SDL_DestroySemaphore (s
->sem
);
397 SDL_DestroyMutex (s
->mutex
);
398 SDL_QuitSubSystem (SDL_INIT_AUDIO
);
401 static struct audio_option sdl_options
[] = {
402 {"SAMPLES", AUD_OPT_INT
, &conf
.nb_samples
,
403 "Size of SDL buffer in samples", NULL
, 0},
404 {NULL
, 0, NULL
, NULL
, NULL
, 0}
407 static struct audio_pcm_ops sdl_pcm_ops
= {
421 struct audio_driver sdl_audio_driver
= {
422 INIT_FIELD (name
= ) "sdl",
423 INIT_FIELD (descr
= ) "SDL http://www.libsdl.org",
424 INIT_FIELD (options
= ) sdl_options
,
425 INIT_FIELD (init
= ) sdl_audio_init
,
426 INIT_FIELD (fini
= ) sdl_audio_fini
,
427 INIT_FIELD (pcm_ops
= ) &sdl_pcm_ops
,
428 INIT_FIELD (can_be_default
= ) 1,
429 INIT_FIELD (max_voices_out
= ) 1,
430 INIT_FIELD (max_voices_in
= ) 0,
431 INIT_FIELD (voice_size_out
= ) sizeof (SDLVoiceOut
),
432 INIT_FIELD (voice_size_in
= ) 0