2 * QEMU SDL audio output driver
4 * Copyright (c) 2004 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 #include "audio/audio_int.h"
30 typedef struct SDLVoice
{
34 #define dolog(...) AUD_log ("sdl", __VA_ARGS__)
36 #define ldebug(...) dolog (__VA_ARGS__)
41 #define QC_SDL_SAMPLES "QEMU_SDL_SAMPLES"
43 #define errstr() SDL_GetError ()
51 struct SDLAudioState
{
57 typedef struct SDLAudioState SDLAudioState
;
59 static void sdl_hw_run (HWVoice
*hw
)
64 static int sdl_lock (SDLAudioState
*s
)
66 if (SDL_LockMutex (s
->mutex
)) {
67 dolog ("SDL_LockMutex failed\nReason: %s\n", errstr ());
73 static int sdl_unlock (SDLAudioState
*s
)
75 if (SDL_UnlockMutex (s
->mutex
)) {
76 dolog ("SDL_UnlockMutex failed\nReason: %s\n", errstr ());
82 static int sdl_post (SDLAudioState
*s
)
84 if (SDL_SemPost (s
->sem
)) {
85 dolog ("SDL_SemPost failed\nReason: %s\n", errstr ());
91 static int sdl_wait (SDLAudioState
*s
)
93 if (SDL_SemWait (s
->sem
)) {
94 dolog ("SDL_SemWait failed\nReason: %s\n", errstr ());
100 static int sdl_unlock_and_post (SDLAudioState
*s
)
108 static int sdl_hw_write (SWVoice
*sw
, void *buf
, int len
)
111 SDLAudioState
*s
= &glob_sdl
;
113 ret
= pcm_hw_write (sw
, buf
, len
);
114 sdl_unlock_and_post (s
);
118 static int AUD_to_sdlfmt (audfmt_e fmt
, int *shift
)
122 case AUD_FMT_S8
: return AUDIO_S8
;
123 case AUD_FMT_U8
: return AUDIO_U8
;
124 case AUD_FMT_S16
: *shift
= 1; return AUDIO_S16LSB
;
125 case AUD_FMT_U16
: *shift
= 1; return AUDIO_U16LSB
;
127 dolog ("Internal logic error: Bad audio format %d\nAborting\n", fmt
);
132 static int sdl_to_audfmt (int fmt
)
135 case AUDIO_S8
: return AUD_FMT_S8
;
136 case AUDIO_U8
: return AUD_FMT_U8
;
137 case AUDIO_S16LSB
: return AUD_FMT_S16
;
138 case AUDIO_U16LSB
: return AUD_FMT_U16
;
140 dolog ("Internal logic error: Unrecognized SDL audio format %d\n"
146 static int sdl_open (SDL_AudioSpec
*req
, SDL_AudioSpec
*obt
)
150 status
= SDL_OpenAudio (req
, obt
);
152 dolog ("SDL_OpenAudio failed\nReason: %s\n", errstr ());
157 static void sdl_close (SDLAudioState
*s
)
159 if (s
->initialized
) {
162 sdl_unlock_and_post (s
);
169 static void sdl_callback (void *opaque
, Uint8
*buf
, int len
)
171 SDLVoice
*sdl
= opaque
;
172 SDLAudioState
*s
= &glob_sdl
;
173 HWVoice
*hw
= &sdl
->hw
;
174 int samples
= len
>> hw
->shift
;
181 int to_mix
, live
, decr
;
183 /* dolog ("in callback samples=%d\n", samples); */
190 live
= pcm_hw_get_live (hw
);
194 /* dolog ("in callback live=%d\n", live); */
195 to_mix
= audio_MIN (samples
, live
);
198 int chunk
= audio_MIN (to_mix
, hw
->samples
- hw
->rpos
);
199 st_sample_t
*src
= hw
->mix_buf
+ hw
->rpos
;
201 /* dolog ("in callback to_mix %d, chunk %d\n", to_mix, chunk); */
202 hw
->clip (buf
, src
, chunk
);
203 memset (src
, 0, chunk
* sizeof (st_sample_t
));
204 hw
->rpos
= (hw
->rpos
+ chunk
) % hw
->samples
;
206 buf
+= chunk
<< hw
->shift
;
209 pcm_hw_dec_live (hw
, decr
);
214 /* dolog ("done len=%d\n", len); */
217 static void sdl_hw_fini (HWVoice
*hw
)
219 ldebug ("sdl_hw_fini %d fixed=%d\n",
220 glob_sdl
.initialized
, audio_state
.fixed_format
);
221 sdl_close (&glob_sdl
);
224 static int sdl_hw_init (HWVoice
*hw
, int freq
, int nchannels
, audfmt_e fmt
)
226 SDLVoice
*sdl
= (SDLVoice
*) hw
;
227 SDLAudioState
*s
= &glob_sdl
;
228 SDL_AudioSpec req
, obt
;
231 ldebug ("sdl_hw_init %d freq=%d fixed=%d\n",
232 s
->initialized
, freq
, audio_state
.fixed_format
);
234 if (nchannels
!= 2) {
235 dolog ("Bogus channel count %d\n", nchannels
);
240 req
.format
= AUD_to_sdlfmt (fmt
, &shift
);
241 req
.channels
= nchannels
;
242 req
.samples
= conf
.nb_samples
;
243 shift
<<= nchannels
== 2;
245 req
.callback
= sdl_callback
;
248 if (sdl_open (&req
, &obt
))
252 hw
->fmt
= sdl_to_audfmt (obt
.format
);
253 hw
->nchannels
= obt
.channels
;
254 hw
->bufsize
= obt
.samples
<< shift
;
262 static int sdl_hw_ctl (HWVoice
*hw
, int cmd
, ...)
278 static void *sdl_audio_init (void)
280 SDLAudioState
*s
= &glob_sdl
;
281 conf
.nb_samples
= audio_get_conf_int (QC_SDL_SAMPLES
, conf
.nb_samples
);
283 if (SDL_InitSubSystem (SDL_INIT_AUDIO
)) {
284 dolog ("SDL failed to initialize audio subsystem\nReason: %s\n",
289 s
->mutex
= SDL_CreateMutex ();
291 dolog ("Failed to create SDL mutex\nReason: %s\n", errstr ());
292 SDL_QuitSubSystem (SDL_INIT_AUDIO
);
296 s
->sem
= SDL_CreateSemaphore (0);
298 dolog ("Failed to create SDL semaphore\nReason: %s\n", errstr ());
299 SDL_DestroyMutex (s
->mutex
);
300 SDL_QuitSubSystem (SDL_INIT_AUDIO
);
307 static void sdl_audio_fini (void *opaque
)
309 SDLAudioState
*s
= opaque
;
311 SDL_DestroySemaphore (s
->sem
);
312 SDL_DestroyMutex (s
->mutex
);
313 SDL_QuitSubSystem (SDL_INIT_AUDIO
);
316 struct pcm_ops sdl_pcm_ops
= {
324 struct audio_output_driver sdl_output_driver
= {