sdlaudio: replace legacy functions with modern ones
[qemu/ar7.git] / audio / sdlaudio.c
blob47968c502027620ce1c9997f400eab675e26b5c0
1 /*
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
22 * THE SOFTWARE.
25 #include "qemu/osdep.h"
26 #include <SDL.h>
27 #include <SDL_thread.h>
28 #include "qemu/module.h"
29 #include "audio.h"
31 #ifndef _WIN32
32 #ifdef __sun__
33 #define _POSIX_PTHREAD_SEMANTICS 1
34 #elif defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__DragonFly__)
35 #include <pthread.h>
36 #endif
37 #endif
39 #define AUDIO_CAP "sdl"
40 #include "audio_int.h"
42 typedef struct SDLVoiceOut {
43 HWVoiceOut hw;
44 int exit;
45 int initialized;
46 Audiodev *dev;
47 SDL_AudioDeviceID devid;
48 } SDLVoiceOut;
50 static void GCC_FMT_ATTR (1, 2) sdl_logerr (const char *fmt, ...)
52 va_list ap;
54 va_start (ap, fmt);
55 AUD_vlog (AUDIO_CAP, fmt, ap);
56 va_end (ap);
58 AUD_log (AUDIO_CAP, "Reason: %s\n", SDL_GetError ());
61 static int aud_to_sdlfmt (AudioFormat fmt)
63 switch (fmt) {
64 case AUDIO_FORMAT_S8:
65 return AUDIO_S8;
67 case AUDIO_FORMAT_U8:
68 return AUDIO_U8;
70 case AUDIO_FORMAT_S16:
71 return AUDIO_S16LSB;
73 case AUDIO_FORMAT_U16:
74 return AUDIO_U16LSB;
76 case AUDIO_FORMAT_S32:
77 return AUDIO_S32LSB;
79 /* no unsigned 32-bit support in SDL */
81 case AUDIO_FORMAT_F32:
82 return AUDIO_F32LSB;
84 default:
85 dolog ("Internal logic error: Bad audio format %d\n", fmt);
86 #ifdef DEBUG_AUDIO
87 abort ();
88 #endif
89 return AUDIO_U8;
93 static int sdl_to_audfmt(int sdlfmt, AudioFormat *fmt, int *endianness)
95 switch (sdlfmt) {
96 case AUDIO_S8:
97 *endianness = 0;
98 *fmt = AUDIO_FORMAT_S8;
99 break;
101 case AUDIO_U8:
102 *endianness = 0;
103 *fmt = AUDIO_FORMAT_U8;
104 break;
106 case AUDIO_S16LSB:
107 *endianness = 0;
108 *fmt = AUDIO_FORMAT_S16;
109 break;
111 case AUDIO_U16LSB:
112 *endianness = 0;
113 *fmt = AUDIO_FORMAT_U16;
114 break;
116 case AUDIO_S16MSB:
117 *endianness = 1;
118 *fmt = AUDIO_FORMAT_S16;
119 break;
121 case AUDIO_U16MSB:
122 *endianness = 1;
123 *fmt = AUDIO_FORMAT_U16;
124 break;
126 case AUDIO_S32LSB:
127 *endianness = 0;
128 *fmt = AUDIO_FORMAT_S32;
129 break;
131 case AUDIO_S32MSB:
132 *endianness = 1;
133 *fmt = AUDIO_FORMAT_S32;
134 break;
136 case AUDIO_F32LSB:
137 *endianness = 0;
138 *fmt = AUDIO_FORMAT_F32;
139 break;
141 case AUDIO_F32MSB:
142 *endianness = 1;
143 *fmt = AUDIO_FORMAT_F32;
144 break;
146 default:
147 dolog ("Unrecognized SDL audio format %d\n", sdlfmt);
148 return -1;
151 return 0;
154 static SDL_AudioDeviceID sdl_open(SDL_AudioSpec *req, SDL_AudioSpec *obt,
155 int rec)
157 SDL_AudioDeviceID devid;
158 #ifndef _WIN32
159 int err;
160 sigset_t new, old;
162 /* Make sure potential threads created by SDL don't hog signals. */
163 err = sigfillset (&new);
164 if (err) {
165 dolog ("sdl_open: sigfillset failed: %s\n", strerror (errno));
166 return 0;
168 err = pthread_sigmask (SIG_BLOCK, &new, &old);
169 if (err) {
170 dolog ("sdl_open: pthread_sigmask failed: %s\n", strerror (err));
171 return 0;
173 #endif
175 devid = SDL_OpenAudioDevice(NULL, rec, req, obt, 0);
176 if (!devid) {
177 sdl_logerr("SDL_OpenAudioDevice for %s failed\n",
178 rec ? "recording" : "playback");
181 #ifndef _WIN32
182 err = pthread_sigmask (SIG_SETMASK, &old, NULL);
183 if (err) {
184 dolog ("sdl_open: pthread_sigmask (restore) failed: %s\n",
185 strerror (errno));
186 /* We have failed to restore original signal mask, all bets are off,
187 so exit the process */
188 exit (EXIT_FAILURE);
190 #endif
191 return devid;
194 static void sdl_close_out(SDLVoiceOut *sdl)
196 if (sdl->initialized) {
197 SDL_LockAudioDevice(sdl->devid);
198 sdl->exit = 1;
199 SDL_UnlockAudioDevice(sdl->devid);
200 SDL_PauseAudioDevice(sdl->devid, 1);
201 sdl->initialized = 0;
203 if (sdl->devid) {
204 SDL_CloseAudioDevice(sdl->devid);
205 sdl->devid = 0;
209 static void sdl_callback_out(void *opaque, Uint8 *buf, int len)
211 SDLVoiceOut *sdl = opaque;
212 HWVoiceOut *hw = &sdl->hw;
214 if (!sdl->exit) {
216 /* dolog("callback_out: len=%d avail=%zu\n", len, hw->pending_emul); */
218 while (hw->pending_emul && len) {
219 size_t write_len;
220 ssize_t start = (ssize_t)hw->pos_emul - hw->pending_emul;
221 if (start < 0) {
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;
231 len -= write_len;
232 buf += write_len;
236 /* clear remaining buffer that we couldn't fill with data */
237 if (len) {
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 \
246 ret_type ret; \
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); \
253 return ret; \
256 SDL_WRAPPER_FUNC(get_buffer_out, void *, (HWVoiceOut *hw, size_t *size),
257 (hw, size), Out)
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;
268 sdl_close_out(sdl);
271 static int sdl_init_out(HWVoiceOut *hw, struct audsettings *as,
272 void *drv_opaque)
274 SDLVoiceOut *sdl = (SDLVoiceOut *)hw;
275 SDL_AudioSpec req, obt;
276 int endianness;
277 int err;
278 AudioFormat effective_fmt;
279 Audiodev *dev = drv_opaque;
280 AudiodevSdlPerDirectionOptions *spdo = dev->u.sdl.out;
281 struct audsettings obt_as;
283 req.freq = as->freq;
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;
293 req.userdata = sdl;
295 sdl->dev = dev;
296 sdl->devid = sdl_open(&req, &obt, 0);
297 if (!sdl->devid) {
298 return -1;
301 err = sdl_to_audfmt(obt.format, &effective_fmt, &endianness);
302 if (err) {
303 sdl_close_out(sdl);
304 return -1;
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) *
314 obt.samples;
316 sdl->initialized = 1;
317 sdl->exit = 0;
318 return 0;
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");
332 return NULL;
335 return dev;
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 */
347 .write = sdl_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 = {
356 .name = "sdl",
357 .descr = "SDL http://www.libsdl.org",
358 .init = sdl_audio_init,
359 .fini = sdl_audio_fini,
360 .pcm_ops = &sdl_pcm_ops,
361 .can_be_default = 1,
362 .max_voices_out = 1,
363 .max_voices_in = 0,
364 .voice_size_out = sizeof (SDLVoiceOut),
365 .voice_size_in = 0
368 static void register_audio_sdl(void)
370 audio_driver_register(&sdl_audio_driver);
372 type_init(register_audio_sdl);