185571bc7Sbellard /* 21d14ffa9Sbellard * QEMU SDL audio driver 385571bc7Sbellard * 41d14ffa9Sbellard * Copyright (c) 2004-2005 Vassili Karpov (malc) 585571bc7Sbellard * 685571bc7Sbellard * Permission is hereby granted, free of charge, to any person obtaining a copy 785571bc7Sbellard * of this software and associated documentation files (the "Software"), to deal 885571bc7Sbellard * in the Software without restriction, including without limitation the rights 985571bc7Sbellard * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 1085571bc7Sbellard * copies of the Software, and to permit persons to whom the Software is 1185571bc7Sbellard * furnished to do so, subject to the following conditions: 1285571bc7Sbellard * 1385571bc7Sbellard * The above copyright notice and this permission notice shall be included in 1485571bc7Sbellard * all copies or substantial portions of the Software. 1585571bc7Sbellard * 1685571bc7Sbellard * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 1785571bc7Sbellard * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 1885571bc7Sbellard * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 1985571bc7Sbellard * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 2085571bc7Sbellard * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 2185571bc7Sbellard * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 2285571bc7Sbellard * THE SOFTWARE. 2385571bc7Sbellard */ 249f059ecaSbellard #include <SDL.h> 259f059ecaSbellard #include <SDL_thread.h> 2685571bc7Sbellard #include "vl.h" 2785571bc7Sbellard 281d14ffa9Sbellard #define AUDIO_CAP "sdl" 291d14ffa9Sbellard #include "audio_int.h" 30fb065187Sbellard 311d14ffa9Sbellard typedef struct SDLVoiceOut { 321d14ffa9Sbellard HWVoiceOut hw; 331d14ffa9Sbellard int live; 341d14ffa9Sbellard int rpos; 351d14ffa9Sbellard int decr; 361d14ffa9Sbellard } SDLVoiceOut; 3785571bc7Sbellard 3885571bc7Sbellard static struct { 3985571bc7Sbellard int nb_samples; 4085571bc7Sbellard } conf = { 4185571bc7Sbellard 1024 4285571bc7Sbellard }; 4385571bc7Sbellard 4485571bc7Sbellard struct SDLAudioState { 4585571bc7Sbellard int exit; 4685571bc7Sbellard SDL_mutex *mutex; 4785571bc7Sbellard SDL_sem *sem; 4885571bc7Sbellard int initialized; 4985571bc7Sbellard } glob_sdl; 5085571bc7Sbellard typedef struct SDLAudioState SDLAudioState; 5185571bc7Sbellard 521d14ffa9Sbellard static void GCC_FMT_ATTR (1, 2) sdl_logerr (const char *fmt, ...) 5385571bc7Sbellard { 541d14ffa9Sbellard va_list ap; 551d14ffa9Sbellard 561d14ffa9Sbellard va_start (ap, fmt); 571d14ffa9Sbellard AUD_vlog (AUDIO_CAP, fmt, ap); 581d14ffa9Sbellard va_end (ap); 591d14ffa9Sbellard 601d14ffa9Sbellard AUD_log (AUDIO_CAP, "Reason: %s\n", SDL_GetError ()); 6185571bc7Sbellard } 6285571bc7Sbellard 631d14ffa9Sbellard static int sdl_lock (SDLAudioState *s, const char *forfn) 6485571bc7Sbellard { 6585571bc7Sbellard if (SDL_LockMutex (s->mutex)) { 661d14ffa9Sbellard sdl_logerr ("SDL_LockMutex for %s failed\n", forfn); 6785571bc7Sbellard return -1; 6885571bc7Sbellard } 6985571bc7Sbellard return 0; 7085571bc7Sbellard } 7185571bc7Sbellard 721d14ffa9Sbellard static int sdl_unlock (SDLAudioState *s, const char *forfn) 7385571bc7Sbellard { 7485571bc7Sbellard if (SDL_UnlockMutex (s->mutex)) { 751d14ffa9Sbellard sdl_logerr ("SDL_UnlockMutex for %s failed\n", forfn); 7685571bc7Sbellard return -1; 7785571bc7Sbellard } 7885571bc7Sbellard return 0; 7985571bc7Sbellard } 8085571bc7Sbellard 811d14ffa9Sbellard static int sdl_post (SDLAudioState *s, const char *forfn) 8285571bc7Sbellard { 8385571bc7Sbellard if (SDL_SemPost (s->sem)) { 841d14ffa9Sbellard sdl_logerr ("SDL_SemPost for %s failed\n", forfn); 8585571bc7Sbellard return -1; 8685571bc7Sbellard } 8785571bc7Sbellard return 0; 8885571bc7Sbellard } 8985571bc7Sbellard 901d14ffa9Sbellard static int sdl_wait (SDLAudioState *s, const char *forfn) 9185571bc7Sbellard { 9285571bc7Sbellard if (SDL_SemWait (s->sem)) { 931d14ffa9Sbellard sdl_logerr ("SDL_SemWait for %s failed\n", forfn); 9485571bc7Sbellard return -1; 9585571bc7Sbellard } 9685571bc7Sbellard return 0; 9785571bc7Sbellard } 9885571bc7Sbellard 991d14ffa9Sbellard static int sdl_unlock_and_post (SDLAudioState *s, const char *forfn) 10085571bc7Sbellard { 1011d14ffa9Sbellard if (sdl_unlock (s, forfn)) { 10285571bc7Sbellard return -1; 10385571bc7Sbellard } 10485571bc7Sbellard 1051d14ffa9Sbellard return sdl_post (s, forfn); 10685571bc7Sbellard } 10785571bc7Sbellard 1081d14ffa9Sbellard static int aud_to_sdlfmt (audfmt_e fmt, int *shift) 10985571bc7Sbellard { 1101d14ffa9Sbellard switch (fmt) { 1111d14ffa9Sbellard case AUD_FMT_S8: 11285571bc7Sbellard *shift = 0; 1131d14ffa9Sbellard return AUDIO_S8; 1141d14ffa9Sbellard 1151d14ffa9Sbellard case AUD_FMT_U8: 1161d14ffa9Sbellard *shift = 0; 1171d14ffa9Sbellard return AUDIO_U8; 1181d14ffa9Sbellard 1191d14ffa9Sbellard case AUD_FMT_S16: 1201d14ffa9Sbellard *shift = 1; 1211d14ffa9Sbellard return AUDIO_S16LSB; 1221d14ffa9Sbellard 1231d14ffa9Sbellard case AUD_FMT_U16: 1241d14ffa9Sbellard *shift = 1; 1251d14ffa9Sbellard return AUDIO_U16LSB; 1261d14ffa9Sbellard 12785571bc7Sbellard default: 1281d14ffa9Sbellard dolog ("Internal logic error: Bad audio format %d\n", fmt); 1291d14ffa9Sbellard #ifdef DEBUG_AUDIO 1301d14ffa9Sbellard abort (); 1311d14ffa9Sbellard #endif 1321d14ffa9Sbellard return AUDIO_U8; 13385571bc7Sbellard } 13485571bc7Sbellard } 13585571bc7Sbellard 1361d14ffa9Sbellard static int sdl_to_audfmt (int sdlfmt, audfmt_e *fmt, int *endianess) 13785571bc7Sbellard { 1381d14ffa9Sbellard switch (sdlfmt) { 1391d14ffa9Sbellard case AUDIO_S8: 1401d14ffa9Sbellard *endianess = 0; 1411d14ffa9Sbellard *fmt = AUD_FMT_S8; 1421d14ffa9Sbellard break; 1431d14ffa9Sbellard 1441d14ffa9Sbellard case AUDIO_U8: 1451d14ffa9Sbellard *endianess = 0; 1461d14ffa9Sbellard *fmt = AUD_FMT_U8; 1471d14ffa9Sbellard break; 1481d14ffa9Sbellard 1491d14ffa9Sbellard case AUDIO_S16LSB: 1501d14ffa9Sbellard *endianess = 0; 1511d14ffa9Sbellard *fmt = AUD_FMT_S16; 1521d14ffa9Sbellard break; 1531d14ffa9Sbellard 1541d14ffa9Sbellard case AUDIO_U16LSB: 1551d14ffa9Sbellard *endianess = 0; 1561d14ffa9Sbellard *fmt = AUD_FMT_U16; 1571d14ffa9Sbellard break; 1581d14ffa9Sbellard 1591d14ffa9Sbellard case AUDIO_S16MSB: 1601d14ffa9Sbellard *endianess = 1; 1611d14ffa9Sbellard *fmt = AUD_FMT_S16; 1621d14ffa9Sbellard break; 1631d14ffa9Sbellard 1641d14ffa9Sbellard case AUDIO_U16MSB: 1651d14ffa9Sbellard *endianess = 1; 1661d14ffa9Sbellard *fmt = AUD_FMT_U16; 1671d14ffa9Sbellard break; 1681d14ffa9Sbellard 16985571bc7Sbellard default: 1701d14ffa9Sbellard dolog ("Unrecognized SDL audio format %d\n", sdlfmt); 1711d14ffa9Sbellard return -1; 17285571bc7Sbellard } 1731d14ffa9Sbellard 1741d14ffa9Sbellard return 0; 17585571bc7Sbellard } 17685571bc7Sbellard 17785571bc7Sbellard static int sdl_open (SDL_AudioSpec *req, SDL_AudioSpec *obt) 17885571bc7Sbellard { 17985571bc7Sbellard int status; 18085571bc7Sbellard 18185571bc7Sbellard status = SDL_OpenAudio (req, obt); 18285571bc7Sbellard if (status) { 1831d14ffa9Sbellard sdl_logerr ("SDL_OpenAudio failed\n"); 18485571bc7Sbellard } 18585571bc7Sbellard return status; 18685571bc7Sbellard } 18785571bc7Sbellard 18885571bc7Sbellard static void sdl_close (SDLAudioState *s) 18985571bc7Sbellard { 19085571bc7Sbellard if (s->initialized) { 1911d14ffa9Sbellard sdl_lock (s, "sdl_close"); 19285571bc7Sbellard s->exit = 1; 1931d14ffa9Sbellard sdl_unlock_and_post (s, "sdl_close"); 19485571bc7Sbellard SDL_PauseAudio (1); 19585571bc7Sbellard SDL_CloseAudio (); 19685571bc7Sbellard s->initialized = 0; 19785571bc7Sbellard } 19885571bc7Sbellard } 19985571bc7Sbellard 20085571bc7Sbellard static void sdl_callback (void *opaque, Uint8 *buf, int len) 20185571bc7Sbellard { 2021d14ffa9Sbellard SDLVoiceOut *sdl = opaque; 20385571bc7Sbellard SDLAudioState *s = &glob_sdl; 2041d14ffa9Sbellard HWVoiceOut *hw = &sdl->hw; 2051d14ffa9Sbellard int samples = len >> hw->info.shift; 20685571bc7Sbellard 20785571bc7Sbellard if (s->exit) { 20885571bc7Sbellard return; 20985571bc7Sbellard } 21085571bc7Sbellard 21185571bc7Sbellard while (samples) { 2121d14ffa9Sbellard int to_mix, decr; 21385571bc7Sbellard 21485571bc7Sbellard /* dolog ("in callback samples=%d\n", samples); */ 2151d14ffa9Sbellard sdl_wait (s, "sdl_callback"); 21685571bc7Sbellard if (s->exit) { 21785571bc7Sbellard return; 21885571bc7Sbellard } 21985571bc7Sbellard 2201d14ffa9Sbellard if (sdl_lock (s, "sdl_callback")) { 2211d14ffa9Sbellard return; 2221d14ffa9Sbellard } 2231d14ffa9Sbellard 2241d14ffa9Sbellard if (audio_bug (AUDIO_FUNC, sdl->live < 0 || sdl->live > hw->samples)) { 2251d14ffa9Sbellard dolog ("sdl->live=%d hw->samples=%d\n", 2261d14ffa9Sbellard sdl->live, hw->samples); 2271d14ffa9Sbellard return; 2281d14ffa9Sbellard } 2291d14ffa9Sbellard 2301d14ffa9Sbellard if (!sdl->live) { 23185571bc7Sbellard goto again; 2321d14ffa9Sbellard } 23385571bc7Sbellard 23485571bc7Sbellard /* dolog ("in callback live=%d\n", live); */ 2351d14ffa9Sbellard to_mix = audio_MIN (samples, sdl->live); 23685571bc7Sbellard decr = to_mix; 23785571bc7Sbellard while (to_mix) { 23885571bc7Sbellard int chunk = audio_MIN (to_mix, hw->samples - hw->rpos); 23985571bc7Sbellard st_sample_t *src = hw->mix_buf + hw->rpos; 24085571bc7Sbellard 24185571bc7Sbellard /* dolog ("in callback to_mix %d, chunk %d\n", to_mix, chunk); */ 24285571bc7Sbellard hw->clip (buf, src, chunk); 2431d14ffa9Sbellard sdl->rpos = (sdl->rpos + chunk) % hw->samples; 24485571bc7Sbellard to_mix -= chunk; 2451d14ffa9Sbellard buf += chunk << hw->info.shift; 24685571bc7Sbellard } 24785571bc7Sbellard samples -= decr; 2481d14ffa9Sbellard sdl->live -= decr; 2491d14ffa9Sbellard sdl->decr += decr; 25085571bc7Sbellard 25185571bc7Sbellard again: 2521d14ffa9Sbellard if (sdl_unlock (s, "sdl_callback")) { 2531d14ffa9Sbellard return; 2541d14ffa9Sbellard } 25585571bc7Sbellard } 25685571bc7Sbellard /* dolog ("done len=%d\n", len); */ 25785571bc7Sbellard } 25885571bc7Sbellard 2591d14ffa9Sbellard static int sdl_write_out (SWVoiceOut *sw, void *buf, int len) 26085571bc7Sbellard { 2611d14ffa9Sbellard return audio_pcm_sw_write (sw, buf, len); 2621d14ffa9Sbellard } 2631d14ffa9Sbellard 2641d14ffa9Sbellard static int sdl_run_out (HWVoiceOut *hw) 2651d14ffa9Sbellard { 2661d14ffa9Sbellard int decr, live; 2671d14ffa9Sbellard SDLVoiceOut *sdl = (SDLVoiceOut *) hw; 2681d14ffa9Sbellard SDLAudioState *s = &glob_sdl; 2691d14ffa9Sbellard 2701d14ffa9Sbellard if (sdl_lock (s, "sdl_callback")) { 2711d14ffa9Sbellard return 0; 2721d14ffa9Sbellard } 2731d14ffa9Sbellard 2741d14ffa9Sbellard live = audio_pcm_hw_get_live_out (hw); 2751d14ffa9Sbellard 2761d14ffa9Sbellard if (sdl->decr > live) { 2771d14ffa9Sbellard ldebug ("sdl->decr %d live %d sdl->live %d\n", 2781d14ffa9Sbellard sdl->decr, 2791d14ffa9Sbellard live, 2801d14ffa9Sbellard sdl->live); 2811d14ffa9Sbellard } 2821d14ffa9Sbellard 2831d14ffa9Sbellard decr = audio_MIN (sdl->decr, live); 2841d14ffa9Sbellard sdl->decr -= decr; 2851d14ffa9Sbellard 2861d14ffa9Sbellard sdl->live = live - decr; 2871d14ffa9Sbellard hw->rpos = sdl->rpos; 2881d14ffa9Sbellard 2891d14ffa9Sbellard if (sdl->live > 0) { 2901d14ffa9Sbellard sdl_unlock_and_post (s, "sdl_callback"); 2911d14ffa9Sbellard } 2921d14ffa9Sbellard else { 2931d14ffa9Sbellard sdl_unlock (s, "sdl_callback"); 2941d14ffa9Sbellard } 2951d14ffa9Sbellard return decr; 2961d14ffa9Sbellard } 2971d14ffa9Sbellard 2981d14ffa9Sbellard static void sdl_fini_out (HWVoiceOut *hw) 2991d14ffa9Sbellard { 3001d14ffa9Sbellard (void) hw; 3011d14ffa9Sbellard 30285571bc7Sbellard sdl_close (&glob_sdl); 30385571bc7Sbellard } 30485571bc7Sbellard 305c0fe3827Sbellard static int sdl_init_out (HWVoiceOut *hw, audsettings_t *as) 30685571bc7Sbellard { 3071d14ffa9Sbellard SDLVoiceOut *sdl = (SDLVoiceOut *) hw; 30885571bc7Sbellard SDLAudioState *s = &glob_sdl; 30985571bc7Sbellard SDL_AudioSpec req, obt; 31085571bc7Sbellard int shift; 3111d14ffa9Sbellard int endianess; 3121d14ffa9Sbellard int err; 3131d14ffa9Sbellard audfmt_e effective_fmt; 314c0fe3827Sbellard audsettings_t obt_as; 31585571bc7Sbellard 316c0fe3827Sbellard shift <<= as->nchannels == 2; 31785571bc7Sbellard 318c0fe3827Sbellard req.freq = as->freq; 319c0fe3827Sbellard req.format = aud_to_sdlfmt (as->fmt, &shift); 320c0fe3827Sbellard req.channels = as->nchannels; 32185571bc7Sbellard req.samples = conf.nb_samples; 32285571bc7Sbellard req.callback = sdl_callback; 32385571bc7Sbellard req.userdata = sdl; 32485571bc7Sbellard 3251d14ffa9Sbellard if (sdl_open (&req, &obt)) { 32685571bc7Sbellard return -1; 3271d14ffa9Sbellard } 32885571bc7Sbellard 3291d14ffa9Sbellard err = sdl_to_audfmt (obt.format, &effective_fmt, &endianess); 3301d14ffa9Sbellard if (err) { 3311d14ffa9Sbellard sdl_close (s); 3321d14ffa9Sbellard return -1; 3331d14ffa9Sbellard } 3341d14ffa9Sbellard 335c0fe3827Sbellard obt_as.freq = obt.freq; 336c0fe3827Sbellard obt_as.nchannels = obt.channels; 337c0fe3827Sbellard obt_as.fmt = effective_fmt; 338*d929eba5Sbellard obt_as.endianness = endianess; 339c0fe3827Sbellard 340*d929eba5Sbellard audio_pcm_init_info (&hw->info, &obt_as); 341c0fe3827Sbellard hw->samples = obt.samples; 34285571bc7Sbellard 34385571bc7Sbellard s->initialized = 1; 34485571bc7Sbellard s->exit = 0; 34585571bc7Sbellard SDL_PauseAudio (0); 34685571bc7Sbellard return 0; 34785571bc7Sbellard } 34885571bc7Sbellard 3491d14ffa9Sbellard static int sdl_ctl_out (HWVoiceOut *hw, int cmd, ...) 35085571bc7Sbellard { 35185571bc7Sbellard (void) hw; 35285571bc7Sbellard 35385571bc7Sbellard switch (cmd) { 35485571bc7Sbellard case VOICE_ENABLE: 35585571bc7Sbellard SDL_PauseAudio (0); 35685571bc7Sbellard break; 35785571bc7Sbellard 35885571bc7Sbellard case VOICE_DISABLE: 35985571bc7Sbellard SDL_PauseAudio (1); 36085571bc7Sbellard break; 36185571bc7Sbellard } 36285571bc7Sbellard return 0; 36385571bc7Sbellard } 36485571bc7Sbellard 36585571bc7Sbellard static void *sdl_audio_init (void) 36685571bc7Sbellard { 36785571bc7Sbellard SDLAudioState *s = &glob_sdl; 36885571bc7Sbellard 36985571bc7Sbellard if (SDL_InitSubSystem (SDL_INIT_AUDIO)) { 3701d14ffa9Sbellard sdl_logerr ("SDL failed to initialize audio subsystem\n"); 37185571bc7Sbellard return NULL; 37285571bc7Sbellard } 37385571bc7Sbellard 37485571bc7Sbellard s->mutex = SDL_CreateMutex (); 37585571bc7Sbellard if (!s->mutex) { 3761d14ffa9Sbellard sdl_logerr ("Failed to create SDL mutex\n"); 37785571bc7Sbellard SDL_QuitSubSystem (SDL_INIT_AUDIO); 37885571bc7Sbellard return NULL; 37985571bc7Sbellard } 38085571bc7Sbellard 38185571bc7Sbellard s->sem = SDL_CreateSemaphore (0); 38285571bc7Sbellard if (!s->sem) { 3831d14ffa9Sbellard sdl_logerr ("Failed to create SDL semaphore\n"); 38485571bc7Sbellard SDL_DestroyMutex (s->mutex); 38585571bc7Sbellard SDL_QuitSubSystem (SDL_INIT_AUDIO); 38685571bc7Sbellard return NULL; 38785571bc7Sbellard } 38885571bc7Sbellard 38985571bc7Sbellard return s; 39085571bc7Sbellard } 39185571bc7Sbellard 39285571bc7Sbellard static void sdl_audio_fini (void *opaque) 39385571bc7Sbellard { 39485571bc7Sbellard SDLAudioState *s = opaque; 39585571bc7Sbellard sdl_close (s); 39685571bc7Sbellard SDL_DestroySemaphore (s->sem); 39785571bc7Sbellard SDL_DestroyMutex (s->mutex); 39885571bc7Sbellard SDL_QuitSubSystem (SDL_INIT_AUDIO); 39985571bc7Sbellard } 40085571bc7Sbellard 4011d14ffa9Sbellard static struct audio_option sdl_options[] = { 4021d14ffa9Sbellard {"SAMPLES", AUD_OPT_INT, &conf.nb_samples, 4031d14ffa9Sbellard "Size of SDL buffer in samples", NULL, 0}, 4041d14ffa9Sbellard {NULL, 0, NULL, NULL, NULL, 0} 40585571bc7Sbellard }; 40685571bc7Sbellard 4071d14ffa9Sbellard static struct audio_pcm_ops sdl_pcm_ops = { 4081d14ffa9Sbellard sdl_init_out, 4091d14ffa9Sbellard sdl_fini_out, 4101d14ffa9Sbellard sdl_run_out, 4111d14ffa9Sbellard sdl_write_out, 4121d14ffa9Sbellard sdl_ctl_out, 4131d14ffa9Sbellard 4141d14ffa9Sbellard NULL, 4151d14ffa9Sbellard NULL, 4161d14ffa9Sbellard NULL, 4171d14ffa9Sbellard NULL, 4181d14ffa9Sbellard NULL 4191d14ffa9Sbellard }; 4201d14ffa9Sbellard 4211d14ffa9Sbellard struct audio_driver sdl_audio_driver = { 4221d14ffa9Sbellard INIT_FIELD (name = ) "sdl", 4231d14ffa9Sbellard INIT_FIELD (descr = ) "SDL http://www.libsdl.org", 4241d14ffa9Sbellard INIT_FIELD (options = ) sdl_options, 4251d14ffa9Sbellard INIT_FIELD (init = ) sdl_audio_init, 4261d14ffa9Sbellard INIT_FIELD (fini = ) sdl_audio_fini, 4271d14ffa9Sbellard INIT_FIELD (pcm_ops = ) &sdl_pcm_ops, 4281d14ffa9Sbellard INIT_FIELD (can_be_default = ) 1, 4291d14ffa9Sbellard INIT_FIELD (max_voices_out = ) 1, 4301d14ffa9Sbellard INIT_FIELD (max_voices_in = ) 0, 4311d14ffa9Sbellard INIT_FIELD (voice_size_out = ) sizeof (SDLVoiceOut), 4321d14ffa9Sbellard INIT_FIELD (voice_size_in = ) 0 43385571bc7Sbellard }; 434