bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 1 | /* |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 2 | * QEMU SDL audio driver |
| 3 | * |
| 4 | * Copyright (c) 2004-2005 Vassili Karpov (malc) |
| 5 | * |
bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 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: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in |
| 14 | * all copies or substantial portions of the Software. |
| 15 | * |
| 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. |
| 23 | */ |
Markus Armbruster | 0b8fa32 | 2019-05-23 16:35:07 +0200 | [diff] [blame] | 24 | |
Peter Maydell | 6086a56 | 2016-01-18 17:33:52 +0000 | [diff] [blame] | 25 | #include "qemu/osdep.h" |
bellard | 9f059ec | 2004-11-14 18:59:52 +0000 | [diff] [blame] | 26 | #include <SDL.h> |
| 27 | #include <SDL_thread.h> |
Markus Armbruster | 0b8fa32 | 2019-05-23 16:35:07 +0200 | [diff] [blame] | 28 | #include "qemu/module.h" |
pbrook | 87ecb68 | 2007-11-17 17:14:51 +0000 | [diff] [blame] | 29 | #include "audio.h" |
bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 30 | |
ths | e784ba7 | 2007-07-11 23:23:15 +0000 | [diff] [blame] | 31 | #ifndef _WIN32 |
| 32 | #ifdef __sun__ |
| 33 | #define _POSIX_PTHREAD_SEMANTICS 1 |
blueswir1 | c5e9723 | 2009-03-07 20:06:23 +0000 | [diff] [blame] | 34 | #elif defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__DragonFly__) |
blueswir1 | 9b4c14c | 2008-10-25 11:19:14 +0000 | [diff] [blame] | 35 | #include <pthread.h> |
ths | e784ba7 | 2007-07-11 23:23:15 +0000 | [diff] [blame] | 36 | #endif |
ths | e784ba7 | 2007-07-11 23:23:15 +0000 | [diff] [blame] | 37 | #endif |
| 38 | |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 39 | #define AUDIO_CAP "sdl" |
| 40 | #include "audio_int.h" |
bellard | fb06518 | 2004-11-09 23:09:44 +0000 | [diff] [blame] | 41 | |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 42 | typedef struct SDLVoiceOut { |
| 43 | HWVoiceOut hw; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 44 | } SDLVoiceOut; |
bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 45 | |
blueswir1 | b1d8e52 | 2008-10-26 13:43:07 +0000 | [diff] [blame] | 46 | static struct SDLAudioState { |
bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 47 | int exit; |
bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 48 | int initialized; |
Kővágó, Zoltán | 81ebb07 | 2015-06-03 23:03:55 +0200 | [diff] [blame] | 49 | bool driver_created; |
Kővágó, Zoltán | 57dea55 | 2019-03-08 23:34:22 +0100 | [diff] [blame] | 50 | Audiodev *dev; |
bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 51 | } glob_sdl; |
| 52 | typedef struct SDLAudioState SDLAudioState; |
| 53 | |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 54 | static void GCC_FMT_ATTR (1, 2) sdl_logerr (const char *fmt, ...) |
bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 55 | { |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 56 | va_list ap; |
| 57 | |
| 58 | va_start (ap, fmt); |
| 59 | AUD_vlog (AUDIO_CAP, fmt, ap); |
| 60 | va_end (ap); |
| 61 | |
| 62 | AUD_log (AUDIO_CAP, "Reason: %s\n", SDL_GetError ()); |
bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 63 | } |
| 64 | |
Kővágó, Zoltán | 85bc585 | 2019-03-08 23:34:13 +0100 | [diff] [blame] | 65 | static int aud_to_sdlfmt (AudioFormat fmt) |
bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 66 | { |
bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 67 | switch (fmt) { |
Kővágó, Zoltán | 85bc585 | 2019-03-08 23:34:13 +0100 | [diff] [blame] | 68 | case AUDIO_FORMAT_S8: |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 69 | return AUDIO_S8; |
| 70 | |
Kővágó, Zoltán | 85bc585 | 2019-03-08 23:34:13 +0100 | [diff] [blame] | 71 | case AUDIO_FORMAT_U8: |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 72 | return AUDIO_U8; |
| 73 | |
Kővágó, Zoltán | 85bc585 | 2019-03-08 23:34:13 +0100 | [diff] [blame] | 74 | case AUDIO_FORMAT_S16: |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 75 | return AUDIO_S16LSB; |
| 76 | |
Kővágó, Zoltán | 85bc585 | 2019-03-08 23:34:13 +0100 | [diff] [blame] | 77 | case AUDIO_FORMAT_U16: |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 78 | return AUDIO_U16LSB; |
| 79 | |
Kővágó, Zoltán | ed2a4a7 | 2020-02-02 20:38:07 +0100 | [diff] [blame] | 80 | case AUDIO_FORMAT_S32: |
| 81 | return AUDIO_S32LSB; |
| 82 | |
| 83 | /* no unsigned 32-bit support in SDL */ |
| 84 | |
| 85 | case AUDIO_FORMAT_F32: |
| 86 | return AUDIO_F32LSB; |
| 87 | |
bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 88 | default: |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 89 | dolog ("Internal logic error: Bad audio format %d\n", fmt); |
| 90 | #ifdef DEBUG_AUDIO |
| 91 | abort (); |
| 92 | #endif |
| 93 | return AUDIO_U8; |
bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 94 | } |
| 95 | } |
| 96 | |
Kővágó, Zoltán | 85bc585 | 2019-03-08 23:34:13 +0100 | [diff] [blame] | 97 | static int sdl_to_audfmt(int sdlfmt, AudioFormat *fmt, int *endianness) |
bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 98 | { |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 99 | switch (sdlfmt) { |
| 100 | case AUDIO_S8: |
Stefan Weil | 4ff9786 | 2011-03-13 15:44:02 +0100 | [diff] [blame] | 101 | *endianness = 0; |
Kővágó, Zoltán | 85bc585 | 2019-03-08 23:34:13 +0100 | [diff] [blame] | 102 | *fmt = AUDIO_FORMAT_S8; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 103 | break; |
| 104 | |
| 105 | case AUDIO_U8: |
Stefan Weil | 4ff9786 | 2011-03-13 15:44:02 +0100 | [diff] [blame] | 106 | *endianness = 0; |
Kővágó, Zoltán | 85bc585 | 2019-03-08 23:34:13 +0100 | [diff] [blame] | 107 | *fmt = AUDIO_FORMAT_U8; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 108 | break; |
| 109 | |
| 110 | case AUDIO_S16LSB: |
Stefan Weil | 4ff9786 | 2011-03-13 15:44:02 +0100 | [diff] [blame] | 111 | *endianness = 0; |
Kővágó, Zoltán | 85bc585 | 2019-03-08 23:34:13 +0100 | [diff] [blame] | 112 | *fmt = AUDIO_FORMAT_S16; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 113 | break; |
| 114 | |
| 115 | case AUDIO_U16LSB: |
Stefan Weil | 4ff9786 | 2011-03-13 15:44:02 +0100 | [diff] [blame] | 116 | *endianness = 0; |
Kővágó, Zoltán | 85bc585 | 2019-03-08 23:34:13 +0100 | [diff] [blame] | 117 | *fmt = AUDIO_FORMAT_U16; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 118 | break; |
| 119 | |
| 120 | case AUDIO_S16MSB: |
Stefan Weil | 4ff9786 | 2011-03-13 15:44:02 +0100 | [diff] [blame] | 121 | *endianness = 1; |
Kővágó, Zoltán | 85bc585 | 2019-03-08 23:34:13 +0100 | [diff] [blame] | 122 | *fmt = AUDIO_FORMAT_S16; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 123 | break; |
| 124 | |
| 125 | case AUDIO_U16MSB: |
Stefan Weil | 4ff9786 | 2011-03-13 15:44:02 +0100 | [diff] [blame] | 126 | *endianness = 1; |
Kővágó, Zoltán | 85bc585 | 2019-03-08 23:34:13 +0100 | [diff] [blame] | 127 | *fmt = AUDIO_FORMAT_U16; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 128 | break; |
| 129 | |
Kővágó, Zoltán | ed2a4a7 | 2020-02-02 20:38:07 +0100 | [diff] [blame] | 130 | case AUDIO_S32LSB: |
| 131 | *endianness = 0; |
| 132 | *fmt = AUDIO_FORMAT_S32; |
| 133 | break; |
| 134 | |
| 135 | case AUDIO_S32MSB: |
| 136 | *endianness = 1; |
| 137 | *fmt = AUDIO_FORMAT_S32; |
| 138 | break; |
| 139 | |
| 140 | case AUDIO_F32LSB: |
| 141 | *endianness = 0; |
| 142 | *fmt = AUDIO_FORMAT_F32; |
| 143 | break; |
| 144 | |
| 145 | case AUDIO_F32MSB: |
| 146 | *endianness = 1; |
| 147 | *fmt = AUDIO_FORMAT_F32; |
| 148 | break; |
| 149 | |
bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 150 | default: |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 151 | dolog ("Unrecognized SDL audio format %d\n", sdlfmt); |
| 152 | return -1; |
bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 153 | } |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 154 | |
| 155 | return 0; |
bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | static int sdl_open (SDL_AudioSpec *req, SDL_AudioSpec *obt) |
| 159 | { |
| 160 | int status; |
ths | e784ba7 | 2007-07-11 23:23:15 +0000 | [diff] [blame] | 161 | #ifndef _WIN32 |
malc | d087bb3 | 2010-08-06 13:09:41 +0400 | [diff] [blame] | 162 | int err; |
ths | e784ba7 | 2007-07-11 23:23:15 +0000 | [diff] [blame] | 163 | sigset_t new, old; |
| 164 | |
| 165 | /* Make sure potential threads created by SDL don't hog signals. */ |
malc | d087bb3 | 2010-08-06 13:09:41 +0400 | [diff] [blame] | 166 | err = sigfillset (&new); |
| 167 | if (err) { |
| 168 | dolog ("sdl_open: sigfillset failed: %s\n", strerror (errno)); |
malc | 60592ed | 2010-08-07 20:03:05 +0400 | [diff] [blame] | 169 | return -1; |
malc | d087bb3 | 2010-08-06 13:09:41 +0400 | [diff] [blame] | 170 | } |
| 171 | err = pthread_sigmask (SIG_BLOCK, &new, &old); |
| 172 | if (err) { |
| 173 | dolog ("sdl_open: pthread_sigmask failed: %s\n", strerror (err)); |
| 174 | return -1; |
| 175 | } |
ths | e784ba7 | 2007-07-11 23:23:15 +0000 | [diff] [blame] | 176 | #endif |
bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 177 | |
| 178 | status = SDL_OpenAudio (req, obt); |
| 179 | if (status) { |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 180 | sdl_logerr ("SDL_OpenAudio failed\n"); |
bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 181 | } |
ths | e784ba7 | 2007-07-11 23:23:15 +0000 | [diff] [blame] | 182 | |
| 183 | #ifndef _WIN32 |
malc | d087bb3 | 2010-08-06 13:09:41 +0400 | [diff] [blame] | 184 | err = pthread_sigmask (SIG_SETMASK, &old, NULL); |
| 185 | if (err) { |
| 186 | dolog ("sdl_open: pthread_sigmask (restore) failed: %s\n", |
| 187 | strerror (errno)); |
| 188 | /* We have failed to restore original signal mask, all bets are off, |
| 189 | so exit the process */ |
| 190 | exit (EXIT_FAILURE); |
| 191 | } |
ths | e784ba7 | 2007-07-11 23:23:15 +0000 | [diff] [blame] | 192 | #endif |
bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 193 | return status; |
| 194 | } |
| 195 | |
| 196 | static void sdl_close (SDLAudioState *s) |
| 197 | { |
| 198 | if (s->initialized) { |
Thomas Huth | 8a7816c | 2019-02-05 04:08:20 +0100 | [diff] [blame] | 199 | SDL_LockAudio(); |
bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 200 | s->exit = 1; |
Thomas Huth | 8a7816c | 2019-02-05 04:08:20 +0100 | [diff] [blame] | 201 | SDL_UnlockAudio(); |
bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 202 | SDL_PauseAudio (1); |
| 203 | SDL_CloseAudio (); |
| 204 | s->initialized = 0; |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | static void sdl_callback (void *opaque, Uint8 *buf, int len) |
| 209 | { |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 210 | SDLVoiceOut *sdl = opaque; |
bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 211 | SDLAudioState *s = &glob_sdl; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 212 | HWVoiceOut *hw = &sdl->hw; |
bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 213 | |
Kővágó, Zoltán | ff71876 | 2019-09-19 23:24:16 +0200 | [diff] [blame] | 214 | if (s->exit) { |
bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 215 | return; |
| 216 | } |
| 217 | |
Kővágó, Zoltán | 7520462 | 2019-08-19 01:06:58 +0200 | [diff] [blame] | 218 | /* dolog ("in callback samples=%zu live=%zu\n", samples, sdl->live); */ |
bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 219 | |
Kővágó, Zoltán | ff71876 | 2019-09-19 23:24:16 +0200 | [diff] [blame] | 220 | while (hw->pending_emul && len) { |
| 221 | size_t write_len; |
| 222 | ssize_t start = ((ssize_t) hw->pos_emul) - hw->pending_emul; |
| 223 | if (start < 0) { |
| 224 | start += hw->size_emul; |
| 225 | } |
| 226 | assert(start >= 0 && start < hw->size_emul); |
bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 227 | |
Kővágó, Zoltán | ff71876 | 2019-09-19 23:24:16 +0200 | [diff] [blame] | 228 | write_len = MIN(MIN(hw->pending_emul, len), |
| 229 | hw->size_emul - start); |
| 230 | |
| 231 | memcpy(buf, hw->buf_emul + start, write_len); |
| 232 | hw->pending_emul -= write_len; |
| 233 | len -= write_len; |
| 234 | buf += write_len; |
malc | 4839abe | 2009-09-30 16:25:55 +0400 | [diff] [blame] | 235 | } |
Thomas Huth | 9399ef1 | 2019-02-05 04:08:21 +0100 | [diff] [blame] | 236 | |
Kővágó, Zoltán | ff71876 | 2019-09-19 23:24:16 +0200 | [diff] [blame] | 237 | /* clear remaining buffer that we couldn't fill with data */ |
| 238 | if (len) { |
| 239 | memset(buf, 0, len); |
Thomas Huth | bcf1977 | 2017-01-31 09:46:38 +0100 | [diff] [blame] | 240 | } |
bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 241 | } |
| 242 | |
Kővágó, Zoltán | ff71876 | 2019-09-19 23:24:16 +0200 | [diff] [blame] | 243 | #define SDL_WRAPPER_FUNC(name, ret_type, args_decl, args, fail, unlock) \ |
| 244 | static ret_type glue(sdl_, name)args_decl \ |
| 245 | { \ |
| 246 | ret_type ret; \ |
| 247 | \ |
| 248 | SDL_LockAudio(); \ |
| 249 | \ |
| 250 | ret = glue(audio_generic_, name)args; \ |
| 251 | \ |
| 252 | SDL_UnlockAudio(); \ |
| 253 | return ret; \ |
malc | ff54149 | 2010-01-17 00:25:29 +0300 | [diff] [blame] | 254 | } |
| 255 | |
Kővágó, Zoltán | ff71876 | 2019-09-19 23:24:16 +0200 | [diff] [blame] | 256 | SDL_WRAPPER_FUNC(get_buffer_out, void *, (HWVoiceOut *hw, size_t *size), |
| 257 | (hw, size), *size = 0, sdl_unlock) |
Volker Rümelin | fdc8c5f | 2020-01-23 08:49:39 +0100 | [diff] [blame] | 258 | SDL_WRAPPER_FUNC(put_buffer_out, size_t, |
Kővágó, Zoltán | ff71876 | 2019-09-19 23:24:16 +0200 | [diff] [blame] | 259 | (HWVoiceOut *hw, void *buf, size_t size), (hw, buf, size), |
| 260 | /*nothing*/, sdl_unlock_and_post) |
| 261 | SDL_WRAPPER_FUNC(write, size_t, |
| 262 | (HWVoiceOut *hw, void *buf, size_t size), (hw, buf, size), |
| 263 | /*nothing*/, sdl_unlock_and_post) |
malc | ff54149 | 2010-01-17 00:25:29 +0300 | [diff] [blame] | 264 | |
Kővágó, Zoltán | ff71876 | 2019-09-19 23:24:16 +0200 | [diff] [blame] | 265 | #undef SDL_WRAPPER_FUNC |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 266 | |
| 267 | static void sdl_fini_out (HWVoiceOut *hw) |
| 268 | { |
| 269 | (void) hw; |
| 270 | |
bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 271 | sdl_close (&glob_sdl); |
| 272 | } |
| 273 | |
Kővágó, Zoltán | 5706db1 | 2015-06-03 23:03:47 +0200 | [diff] [blame] | 274 | static int sdl_init_out(HWVoiceOut *hw, struct audsettings *as, |
| 275 | void *drv_opaque) |
bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 276 | { |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 277 | SDLVoiceOut *sdl = (SDLVoiceOut *) hw; |
bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 278 | SDLAudioState *s = &glob_sdl; |
| 279 | SDL_AudioSpec req, obt; |
Stefan Weil | 4ff9786 | 2011-03-13 15:44:02 +0100 | [diff] [blame] | 280 | int endianness; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 281 | int err; |
Kővágó, Zoltán | 85bc585 | 2019-03-08 23:34:13 +0100 | [diff] [blame] | 282 | AudioFormat effective_fmt; |
malc | 1ea879e | 2008-12-03 22:48:44 +0000 | [diff] [blame] | 283 | struct audsettings obt_as; |
bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 284 | |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 285 | req.freq = as->freq; |
Serge Ziryukin | 6c557ab | 2010-04-22 14:14:24 +0300 | [diff] [blame] | 286 | req.format = aud_to_sdlfmt (as->fmt); |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 287 | req.channels = as->nchannels; |
Kővágó, Zoltán | 57dea55 | 2019-03-08 23:34:22 +0100 | [diff] [blame] | 288 | req.samples = audio_buffer_samples(s->dev->u.sdl.out, as, 11610); |
bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 289 | req.callback = sdl_callback; |
| 290 | req.userdata = sdl; |
| 291 | |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 292 | if (sdl_open (&req, &obt)) { |
bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 293 | return -1; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 294 | } |
bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 295 | |
Stefan Weil | 4ff9786 | 2011-03-13 15:44:02 +0100 | [diff] [blame] | 296 | err = sdl_to_audfmt(obt.format, &effective_fmt, &endianness); |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 297 | if (err) { |
| 298 | sdl_close (s); |
| 299 | return -1; |
| 300 | } |
| 301 | |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 302 | obt_as.freq = obt.freq; |
| 303 | obt_as.nchannels = obt.channels; |
| 304 | obt_as.fmt = effective_fmt; |
Stefan Weil | 4ff9786 | 2011-03-13 15:44:02 +0100 | [diff] [blame] | 305 | obt_as.endianness = endianness; |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 306 | |
bellard | d929eba | 2006-07-04 21:47:22 +0000 | [diff] [blame] | 307 | audio_pcm_init_info (&hw->info, &obt_as); |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 308 | hw->samples = obt.samples; |
bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 309 | |
| 310 | s->initialized = 1; |
| 311 | s->exit = 0; |
| 312 | SDL_PauseAudio (0); |
| 313 | return 0; |
| 314 | } |
| 315 | |
Kővágó, Zoltán | 571a8c5 | 2019-09-19 23:24:22 +0200 | [diff] [blame] | 316 | static void sdl_enable_out(HWVoiceOut *hw, bool enable) |
bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 317 | { |
Kővágó, Zoltán | 571a8c5 | 2019-09-19 23:24:22 +0200 | [diff] [blame] | 318 | SDL_PauseAudio(!enable); |
bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 319 | } |
| 320 | |
Kővágó, Zoltán | 7183022 | 2019-03-08 23:34:15 +0100 | [diff] [blame] | 321 | static void *sdl_audio_init(Audiodev *dev) |
bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 322 | { |
| 323 | SDLAudioState *s = &glob_sdl; |
Kővágó, Zoltán | 81ebb07 | 2015-06-03 23:03:55 +0200 | [diff] [blame] | 324 | if (s->driver_created) { |
| 325 | sdl_logerr("Can't create multiple sdl backends\n"); |
| 326 | return NULL; |
| 327 | } |
bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 328 | |
| 329 | if (SDL_InitSubSystem (SDL_INIT_AUDIO)) { |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 330 | sdl_logerr ("SDL failed to initialize audio subsystem\n"); |
bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 331 | return NULL; |
| 332 | } |
| 333 | |
Kővágó, Zoltán | 81ebb07 | 2015-06-03 23:03:55 +0200 | [diff] [blame] | 334 | s->driver_created = true; |
Kővágó, Zoltán | 57dea55 | 2019-03-08 23:34:22 +0100 | [diff] [blame] | 335 | s->dev = dev; |
bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 336 | return s; |
| 337 | } |
| 338 | |
| 339 | static void sdl_audio_fini (void *opaque) |
| 340 | { |
| 341 | SDLAudioState *s = opaque; |
| 342 | sdl_close (s); |
bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 343 | SDL_QuitSubSystem (SDL_INIT_AUDIO); |
Kővágó, Zoltán | 81ebb07 | 2015-06-03 23:03:55 +0200 | [diff] [blame] | 344 | s->driver_created = false; |
Kővágó, Zoltán | 57dea55 | 2019-03-08 23:34:22 +0100 | [diff] [blame] | 345 | s->dev = NULL; |
bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 346 | } |
| 347 | |
blueswir1 | 35f4b58 | 2008-10-06 18:08:30 +0000 | [diff] [blame] | 348 | static struct audio_pcm_ops sdl_pcm_ops = { |
Juan Quintela | 1dd3e4d | 2009-08-11 02:31:15 +0200 | [diff] [blame] | 349 | .init_out = sdl_init_out, |
| 350 | .fini_out = sdl_fini_out, |
Volker Rümelin | fdc8c5f | 2020-01-23 08:49:39 +0100 | [diff] [blame] | 351 | /* wrapper for audio_generic_write */ |
Kővágó, Zoltán | ff71876 | 2019-09-19 23:24:16 +0200 | [diff] [blame] | 352 | .write = sdl_write, |
Volker Rümelin | fdc8c5f | 2020-01-23 08:49:39 +0100 | [diff] [blame] | 353 | /* wrapper for audio_generic_get_buffer_out */ |
Kővágó, Zoltán | ff71876 | 2019-09-19 23:24:16 +0200 | [diff] [blame] | 354 | .get_buffer_out = sdl_get_buffer_out, |
Volker Rümelin | fdc8c5f | 2020-01-23 08:49:39 +0100 | [diff] [blame] | 355 | /* wrapper for audio_generic_put_buffer_out */ |
| 356 | .put_buffer_out = sdl_put_buffer_out, |
Kővágó, Zoltán | 571a8c5 | 2019-09-19 23:24:22 +0200 | [diff] [blame] | 357 | .enable_out = sdl_enable_out, |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 358 | }; |
| 359 | |
Gerd Hoffmann | d3893a3 | 2018-03-06 08:40:47 +0100 | [diff] [blame] | 360 | static struct audio_driver sdl_audio_driver = { |
Juan Quintela | bee37f3 | 2009-08-11 02:31:14 +0200 | [diff] [blame] | 361 | .name = "sdl", |
| 362 | .descr = "SDL http://www.libsdl.org", |
Juan Quintela | bee37f3 | 2009-08-11 02:31:14 +0200 | [diff] [blame] | 363 | .init = sdl_audio_init, |
| 364 | .fini = sdl_audio_fini, |
| 365 | .pcm_ops = &sdl_pcm_ops, |
| 366 | .can_be_default = 1, |
| 367 | .max_voices_out = 1, |
| 368 | .max_voices_in = 0, |
| 369 | .voice_size_out = sizeof (SDLVoiceOut), |
| 370 | .voice_size_in = 0 |
bellard | 85571bc | 2004-11-07 18:04:02 +0000 | [diff] [blame] | 371 | }; |
Gerd Hoffmann | d3893a3 | 2018-03-06 08:40:47 +0100 | [diff] [blame] | 372 | |
| 373 | static void register_audio_sdl(void) |
| 374 | { |
| 375 | audio_driver_register(&sdl_audio_driver); |
| 376 | } |
| 377 | type_init(register_audio_sdl); |