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/SDL_thread.h>
28 #define AUDIO_CAP "sdl"
29 #include "audio/audio.h"
30 #include "audio/sdlaudio.h"
32 #define QC_SDL_SAMPLES "QEMU_SDL_SAMPLES"
34 #define errstr() SDL_GetError ()
42 struct SDLAudioState
{
48 typedef struct SDLAudioState SDLAudioState
;
50 static void sdl_hw_run (HWVoice
*hw
)
55 static int sdl_lock (SDLAudioState
*s
)
57 if (SDL_LockMutex (s
->mutex
)) {
58 dolog ("SDL_LockMutex failed\nReason: %s\n", errstr ());
64 static int sdl_unlock (SDLAudioState
*s
)
66 if (SDL_UnlockMutex (s
->mutex
)) {
67 dolog ("SDL_UnlockMutex failed\nReason: %s\n", errstr ());
73 static int sdl_post (SDLAudioState
*s
)
75 if (SDL_SemPost (s
->sem
)) {
76 dolog ("SDL_SemPost failed\nReason: %s\n", errstr ());
82 static int sdl_wait (SDLAudioState
*s
)
84 if (SDL_SemWait (s
->sem
)) {
85 dolog ("SDL_SemWait failed\nReason: %s\n", errstr ());
91 static int sdl_unlock_and_post (SDLAudioState
*s
)
99 static int sdl_hw_write (SWVoice
*sw
, void *buf
, int len
)
102 SDLAudioState
*s
= &glob_sdl
;
104 ret
= pcm_hw_write (sw
, buf
, len
);
105 sdl_unlock_and_post (s
);
109 static int AUD_to_sdlfmt (audfmt_e fmt
, int *shift
)
113 case AUD_FMT_S8
: return AUDIO_S8
;
114 case AUD_FMT_U8
: return AUDIO_U8
;
115 case AUD_FMT_S16
: *shift
= 1; return AUDIO_S16LSB
;
116 case AUD_FMT_U16
: *shift
= 1; return AUDIO_U16LSB
;
118 dolog ("Internal logic error: Bad audio format %d\nAborting\n", fmt
);
123 static int sdl_to_audfmt (int fmt
)
126 case AUDIO_S8
: return AUD_FMT_S8
;
127 case AUDIO_U8
: return AUD_FMT_U8
;
128 case AUDIO_S16LSB
: return AUD_FMT_S16
;
129 case AUDIO_U16LSB
: return AUD_FMT_U16
;
131 dolog ("Internal logic error: Unrecognized SDL audio format %d\n"
137 static int sdl_open (SDL_AudioSpec
*req
, SDL_AudioSpec
*obt
)
141 status
= SDL_OpenAudio (req
, obt
);
143 dolog ("SDL_OpenAudio failed\nReason: %s\n", errstr ());
148 static void sdl_close (SDLAudioState
*s
)
150 if (s
->initialized
) {
153 sdl_unlock_and_post (s
);
160 static void sdl_callback (void *opaque
, Uint8
*buf
, int len
)
162 SDLVoice
*sdl
= opaque
;
163 SDLAudioState
*s
= &glob_sdl
;
164 HWVoice
*hw
= &sdl
->hw
;
165 int samples
= len
>> hw
->shift
;
172 int to_mix
, live
, decr
;
174 /* dolog ("in callback samples=%d\n", samples); */
181 live
= pcm_hw_get_live (hw
);
185 /* dolog ("in callback live=%d\n", live); */
186 to_mix
= audio_MIN (samples
, live
);
189 int chunk
= audio_MIN (to_mix
, hw
->samples
- hw
->rpos
);
190 st_sample_t
*src
= hw
->mix_buf
+ hw
->rpos
;
192 /* dolog ("in callback to_mix %d, chunk %d\n", to_mix, chunk); */
193 hw
->clip (buf
, src
, chunk
);
194 memset (src
, 0, chunk
* sizeof (st_sample_t
));
195 hw
->rpos
= (hw
->rpos
+ chunk
) % hw
->samples
;
197 buf
+= chunk
<< hw
->shift
;
200 pcm_hw_dec_live (hw
, decr
);
205 /* dolog ("done len=%d\n", len); */
208 static void sdl_hw_fini (HWVoice
*hw
)
210 ldebug ("sdl_hw_fini %d fixed=%d\n",
211 glob_sdl
.initialized
, audio_conf
.fixed_format
);
212 sdl_close (&glob_sdl
);
215 static int sdl_hw_init (HWVoice
*hw
, int freq
, int nchannels
, audfmt_e fmt
)
217 SDLVoice
*sdl
= (SDLVoice
*) hw
;
218 SDLAudioState
*s
= &glob_sdl
;
219 SDL_AudioSpec req
, obt
;
222 ldebug ("sdl_hw_init %d freq=%d fixed=%d\n",
223 s
->initialized
, freq
, audio_conf
.fixed_format
);
225 if (nchannels
!= 2) {
226 dolog ("Bogus channel count %d\n", nchannels
);
231 req
.format
= AUD_to_sdlfmt (fmt
, &shift
);
232 req
.channels
= nchannels
;
233 req
.samples
= conf
.nb_samples
;
234 shift
<<= nchannels
== 2;
236 req
.callback
= sdl_callback
;
239 if (sdl_open (&req
, &obt
))
243 hw
->fmt
= sdl_to_audfmt (obt
.format
);
244 hw
->nchannels
= obt
.channels
;
245 hw
->bufsize
= obt
.samples
<< shift
;
253 static int sdl_hw_ctl (HWVoice
*hw
, int cmd
, ...)
269 static void *sdl_audio_init (void)
271 SDLAudioState
*s
= &glob_sdl
;
272 conf
.nb_samples
= audio_get_conf_int (QC_SDL_SAMPLES
, conf
.nb_samples
);
274 if (SDL_InitSubSystem (SDL_INIT_AUDIO
)) {
275 dolog ("SDL failed to initialize audio subsystem\nReason: %s\n",
280 s
->mutex
= SDL_CreateMutex ();
282 dolog ("Failed to create SDL mutex\nReason: %s\n", errstr ());
283 SDL_QuitSubSystem (SDL_INIT_AUDIO
);
287 s
->sem
= SDL_CreateSemaphore (0);
289 dolog ("Failed to create SDL semaphore\nReason: %s\n", errstr ());
290 SDL_DestroyMutex (s
->mutex
);
291 SDL_QuitSubSystem (SDL_INIT_AUDIO
);
298 static void sdl_audio_fini (void *opaque
)
300 SDLAudioState
*s
= opaque
;
302 SDL_DestroySemaphore (s
->sem
);
303 SDL_DestroyMutex (s
->mutex
);
304 SDL_QuitSubSystem (SDL_INIT_AUDIO
);
307 struct pcm_ops sdl_pcm_ops
= {
315 struct audio_output_driver sdl_output_driver
= {