root / audio / sdlaudio.c @ 977d5710
History | View | Annotate | Download (7.8 kB)
1 | 85571bc7 | bellard | /*
|
---|---|---|---|
2 | 85571bc7 | bellard | * QEMU SDL audio output driver
|
3 | 85571bc7 | bellard | *
|
4 | 85571bc7 | bellard | * Copyright (c) 2004 Vassili Karpov (malc)
|
5 | 85571bc7 | bellard | *
|
6 | 85571bc7 | bellard | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
7 | 85571bc7 | bellard | * of this software and associated documentation files (the "Software"), to deal
|
8 | 85571bc7 | bellard | * in the Software without restriction, including without limitation the rights
|
9 | 85571bc7 | bellard | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10 | 85571bc7 | bellard | * copies of the Software, and to permit persons to whom the Software is
|
11 | 85571bc7 | bellard | * furnished to do so, subject to the following conditions:
|
12 | 85571bc7 | bellard | *
|
13 | 85571bc7 | bellard | * The above copyright notice and this permission notice shall be included in
|
14 | 85571bc7 | bellard | * all copies or substantial portions of the Software.
|
15 | 85571bc7 | bellard | *
|
16 | 85571bc7 | bellard | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17 | 85571bc7 | bellard | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18 | 85571bc7 | bellard | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
19 | 85571bc7 | bellard | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20 | 85571bc7 | bellard | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21 | 85571bc7 | bellard | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22 | 85571bc7 | bellard | * THE SOFTWARE.
|
23 | 85571bc7 | bellard | */
|
24 | 9f059eca | bellard | #include <SDL.h> |
25 | 9f059eca | bellard | #include <SDL_thread.h> |
26 | 85571bc7 | bellard | #include "vl.h" |
27 | 85571bc7 | bellard | |
28 | fb065187 | bellard | #include "audio/audio_int.h" |
29 | fb065187 | bellard | |
30 | fb065187 | bellard | typedef struct SDLVoice { |
31 | fb065187 | bellard | HWVoice hw; |
32 | fb065187 | bellard | } SDLVoice; |
33 | fb065187 | bellard | |
34 | fb065187 | bellard | #define dolog(...) AUD_log ("sdl", __VA_ARGS__) |
35 | fb065187 | bellard | #ifdef DEBUG
|
36 | fb065187 | bellard | #define ldebug(...) dolog (__VA_ARGS__)
|
37 | fb065187 | bellard | #else
|
38 | fb065187 | bellard | #define ldebug(...)
|
39 | fb065187 | bellard | #endif
|
40 | 85571bc7 | bellard | |
41 | 85571bc7 | bellard | #define QC_SDL_SAMPLES "QEMU_SDL_SAMPLES" |
42 | 85571bc7 | bellard | |
43 | 85571bc7 | bellard | #define errstr() SDL_GetError ()
|
44 | 85571bc7 | bellard | |
45 | 85571bc7 | bellard | static struct { |
46 | 85571bc7 | bellard | int nb_samples;
|
47 | 85571bc7 | bellard | } conf = { |
48 | 85571bc7 | bellard | 1024
|
49 | 85571bc7 | bellard | }; |
50 | 85571bc7 | bellard | |
51 | 85571bc7 | bellard | struct SDLAudioState {
|
52 | 85571bc7 | bellard | int exit;
|
53 | 85571bc7 | bellard | SDL_mutex *mutex; |
54 | 85571bc7 | bellard | SDL_sem *sem; |
55 | 85571bc7 | bellard | int initialized;
|
56 | 85571bc7 | bellard | } glob_sdl; |
57 | 85571bc7 | bellard | typedef struct SDLAudioState SDLAudioState; |
58 | 85571bc7 | bellard | |
59 | 85571bc7 | bellard | static void sdl_hw_run (HWVoice *hw) |
60 | 85571bc7 | bellard | { |
61 | 85571bc7 | bellard | (void) hw;
|
62 | 85571bc7 | bellard | } |
63 | 85571bc7 | bellard | |
64 | 85571bc7 | bellard | static int sdl_lock (SDLAudioState *s) |
65 | 85571bc7 | bellard | { |
66 | 85571bc7 | bellard | if (SDL_LockMutex (s->mutex)) {
|
67 | 85571bc7 | bellard | dolog ("SDL_LockMutex failed\nReason: %s\n", errstr ());
|
68 | 85571bc7 | bellard | return -1; |
69 | 85571bc7 | bellard | } |
70 | 85571bc7 | bellard | return 0; |
71 | 85571bc7 | bellard | } |
72 | 85571bc7 | bellard | |
73 | 85571bc7 | bellard | static int sdl_unlock (SDLAudioState *s) |
74 | 85571bc7 | bellard | { |
75 | 85571bc7 | bellard | if (SDL_UnlockMutex (s->mutex)) {
|
76 | 85571bc7 | bellard | dolog ("SDL_UnlockMutex failed\nReason: %s\n", errstr ());
|
77 | 85571bc7 | bellard | return -1; |
78 | 85571bc7 | bellard | } |
79 | 85571bc7 | bellard | return 0; |
80 | 85571bc7 | bellard | } |
81 | 85571bc7 | bellard | |
82 | 85571bc7 | bellard | static int sdl_post (SDLAudioState *s) |
83 | 85571bc7 | bellard | { |
84 | 85571bc7 | bellard | if (SDL_SemPost (s->sem)) {
|
85 | 85571bc7 | bellard | dolog ("SDL_SemPost failed\nReason: %s\n", errstr ());
|
86 | 85571bc7 | bellard | return -1; |
87 | 85571bc7 | bellard | } |
88 | 85571bc7 | bellard | return 0; |
89 | 85571bc7 | bellard | } |
90 | 85571bc7 | bellard | |
91 | 85571bc7 | bellard | static int sdl_wait (SDLAudioState *s) |
92 | 85571bc7 | bellard | { |
93 | 85571bc7 | bellard | if (SDL_SemWait (s->sem)) {
|
94 | 85571bc7 | bellard | dolog ("SDL_SemWait failed\nReason: %s\n", errstr ());
|
95 | 85571bc7 | bellard | return -1; |
96 | 85571bc7 | bellard | } |
97 | 85571bc7 | bellard | return 0; |
98 | 85571bc7 | bellard | } |
99 | 85571bc7 | bellard | |
100 | 85571bc7 | bellard | static int sdl_unlock_and_post (SDLAudioState *s) |
101 | 85571bc7 | bellard | { |
102 | 85571bc7 | bellard | if (sdl_unlock (s))
|
103 | 85571bc7 | bellard | return -1; |
104 | 85571bc7 | bellard | |
105 | 85571bc7 | bellard | return sdl_post (s);
|
106 | 85571bc7 | bellard | } |
107 | 85571bc7 | bellard | |
108 | 85571bc7 | bellard | static int sdl_hw_write (SWVoice *sw, void *buf, int len) |
109 | 85571bc7 | bellard | { |
110 | 85571bc7 | bellard | int ret;
|
111 | 85571bc7 | bellard | SDLAudioState *s = &glob_sdl; |
112 | 85571bc7 | bellard | sdl_lock (s); |
113 | 85571bc7 | bellard | ret = pcm_hw_write (sw, buf, len); |
114 | 85571bc7 | bellard | sdl_unlock_and_post (s); |
115 | 85571bc7 | bellard | return ret;
|
116 | 85571bc7 | bellard | } |
117 | 85571bc7 | bellard | |
118 | 85571bc7 | bellard | static int AUD_to_sdlfmt (audfmt_e fmt, int *shift) |
119 | 85571bc7 | bellard | { |
120 | 85571bc7 | bellard | *shift = 0;
|
121 | 85571bc7 | bellard | switch (fmt) {
|
122 | 85571bc7 | bellard | case AUD_FMT_S8: return AUDIO_S8; |
123 | 85571bc7 | bellard | case AUD_FMT_U8: return AUDIO_U8; |
124 | 85571bc7 | bellard | case AUD_FMT_S16: *shift = 1; return AUDIO_S16LSB; |
125 | 85571bc7 | bellard | case AUD_FMT_U16: *shift = 1; return AUDIO_U16LSB; |
126 | 85571bc7 | bellard | default:
|
127 | 85571bc7 | bellard | dolog ("Internal logic error: Bad audio format %d\nAborting\n", fmt);
|
128 | 85571bc7 | bellard | exit (EXIT_FAILURE); |
129 | 85571bc7 | bellard | } |
130 | 85571bc7 | bellard | } |
131 | 85571bc7 | bellard | |
132 | 85571bc7 | bellard | static int sdl_to_audfmt (int fmt) |
133 | 85571bc7 | bellard | { |
134 | 85571bc7 | bellard | switch (fmt) {
|
135 | 85571bc7 | bellard | case AUDIO_S8: return AUD_FMT_S8; |
136 | 85571bc7 | bellard | case AUDIO_U8: return AUD_FMT_U8; |
137 | 85571bc7 | bellard | case AUDIO_S16LSB: return AUD_FMT_S16; |
138 | 85571bc7 | bellard | case AUDIO_U16LSB: return AUD_FMT_U16; |
139 | 85571bc7 | bellard | default:
|
140 | 85571bc7 | bellard | dolog ("Internal logic error: Unrecognized SDL audio format %d\n"
|
141 | 85571bc7 | bellard | "Aborting\n", fmt);
|
142 | 85571bc7 | bellard | exit (EXIT_FAILURE); |
143 | 85571bc7 | bellard | } |
144 | 85571bc7 | bellard | } |
145 | 85571bc7 | bellard | |
146 | 85571bc7 | bellard | static int sdl_open (SDL_AudioSpec *req, SDL_AudioSpec *obt) |
147 | 85571bc7 | bellard | { |
148 | 85571bc7 | bellard | int status;
|
149 | 85571bc7 | bellard | |
150 | 85571bc7 | bellard | status = SDL_OpenAudio (req, obt); |
151 | 85571bc7 | bellard | if (status) {
|
152 | 85571bc7 | bellard | dolog ("SDL_OpenAudio failed\nReason: %s\n", errstr ());
|
153 | 85571bc7 | bellard | } |
154 | 85571bc7 | bellard | return status;
|
155 | 85571bc7 | bellard | } |
156 | 85571bc7 | bellard | |
157 | 85571bc7 | bellard | static void sdl_close (SDLAudioState *s) |
158 | 85571bc7 | bellard | { |
159 | 85571bc7 | bellard | if (s->initialized) {
|
160 | 85571bc7 | bellard | sdl_lock (s); |
161 | 85571bc7 | bellard | s->exit = 1;
|
162 | 85571bc7 | bellard | sdl_unlock_and_post (s); |
163 | 85571bc7 | bellard | SDL_PauseAudio (1);
|
164 | 85571bc7 | bellard | SDL_CloseAudio (); |
165 | 85571bc7 | bellard | s->initialized = 0;
|
166 | 85571bc7 | bellard | } |
167 | 85571bc7 | bellard | } |
168 | 85571bc7 | bellard | |
169 | 85571bc7 | bellard | static void sdl_callback (void *opaque, Uint8 *buf, int len) |
170 | 85571bc7 | bellard | { |
171 | 85571bc7 | bellard | SDLVoice *sdl = opaque; |
172 | 85571bc7 | bellard | SDLAudioState *s = &glob_sdl; |
173 | 85571bc7 | bellard | HWVoice *hw = &sdl->hw; |
174 | 85571bc7 | bellard | int samples = len >> hw->shift;
|
175 | 85571bc7 | bellard | |
176 | 85571bc7 | bellard | if (s->exit) {
|
177 | 85571bc7 | bellard | return;
|
178 | 85571bc7 | bellard | } |
179 | 85571bc7 | bellard | |
180 | 85571bc7 | bellard | while (samples) {
|
181 | 85571bc7 | bellard | int to_mix, live, decr;
|
182 | 85571bc7 | bellard | |
183 | 85571bc7 | bellard | /* dolog ("in callback samples=%d\n", samples); */
|
184 | 85571bc7 | bellard | sdl_wait (s); |
185 | 85571bc7 | bellard | if (s->exit) {
|
186 | 85571bc7 | bellard | return;
|
187 | 85571bc7 | bellard | } |
188 | 85571bc7 | bellard | |
189 | 85571bc7 | bellard | sdl_lock (s); |
190 | 85571bc7 | bellard | live = pcm_hw_get_live (hw); |
191 | 85571bc7 | bellard | if (live <= 0) |
192 | 85571bc7 | bellard | goto again;
|
193 | 85571bc7 | bellard | |
194 | 85571bc7 | bellard | /* dolog ("in callback live=%d\n", live); */
|
195 | 85571bc7 | bellard | to_mix = audio_MIN (samples, live); |
196 | 85571bc7 | bellard | decr = to_mix; |
197 | 85571bc7 | bellard | while (to_mix) {
|
198 | 85571bc7 | bellard | int chunk = audio_MIN (to_mix, hw->samples - hw->rpos);
|
199 | 85571bc7 | bellard | st_sample_t *src = hw->mix_buf + hw->rpos; |
200 | 85571bc7 | bellard | |
201 | 85571bc7 | bellard | /* dolog ("in callback to_mix %d, chunk %d\n", to_mix, chunk); */
|
202 | 85571bc7 | bellard | hw->clip (buf, src, chunk); |
203 | 85571bc7 | bellard | memset (src, 0, chunk * sizeof (st_sample_t)); |
204 | 85571bc7 | bellard | hw->rpos = (hw->rpos + chunk) % hw->samples; |
205 | 85571bc7 | bellard | to_mix -= chunk; |
206 | 85571bc7 | bellard | buf += chunk << hw->shift; |
207 | 85571bc7 | bellard | } |
208 | 85571bc7 | bellard | samples -= decr; |
209 | 85571bc7 | bellard | pcm_hw_dec_live (hw, decr); |
210 | 85571bc7 | bellard | |
211 | 85571bc7 | bellard | again:
|
212 | 85571bc7 | bellard | sdl_unlock (s); |
213 | 85571bc7 | bellard | } |
214 | 85571bc7 | bellard | /* dolog ("done len=%d\n", len); */
|
215 | 85571bc7 | bellard | } |
216 | 85571bc7 | bellard | |
217 | 85571bc7 | bellard | static void sdl_hw_fini (HWVoice *hw) |
218 | 85571bc7 | bellard | { |
219 | 85571bc7 | bellard | ldebug ("sdl_hw_fini %d fixed=%d\n",
|
220 | 85571bc7 | bellard | glob_sdl.initialized, audio_conf.fixed_format); |
221 | 85571bc7 | bellard | sdl_close (&glob_sdl); |
222 | 85571bc7 | bellard | } |
223 | 85571bc7 | bellard | |
224 | 85571bc7 | bellard | static int sdl_hw_init (HWVoice *hw, int freq, int nchannels, audfmt_e fmt) |
225 | 85571bc7 | bellard | { |
226 | 85571bc7 | bellard | SDLVoice *sdl = (SDLVoice *) hw; |
227 | 85571bc7 | bellard | SDLAudioState *s = &glob_sdl; |
228 | 85571bc7 | bellard | SDL_AudioSpec req, obt; |
229 | 85571bc7 | bellard | int shift;
|
230 | 85571bc7 | bellard | |
231 | 85571bc7 | bellard | ldebug ("sdl_hw_init %d freq=%d fixed=%d\n",
|
232 | 85571bc7 | bellard | s->initialized, freq, audio_conf.fixed_format); |
233 | 85571bc7 | bellard | |
234 | 85571bc7 | bellard | if (nchannels != 2) { |
235 | 85571bc7 | bellard | dolog ("Bogus channel count %d\n", nchannels);
|
236 | 85571bc7 | bellard | return -1; |
237 | 85571bc7 | bellard | } |
238 | 85571bc7 | bellard | |
239 | 85571bc7 | bellard | req.freq = freq; |
240 | 85571bc7 | bellard | req.format = AUD_to_sdlfmt (fmt, &shift); |
241 | 85571bc7 | bellard | req.channels = nchannels; |
242 | 85571bc7 | bellard | req.samples = conf.nb_samples; |
243 | 85571bc7 | bellard | shift <<= nchannels == 2;
|
244 | 85571bc7 | bellard | |
245 | 85571bc7 | bellard | req.callback = sdl_callback; |
246 | 85571bc7 | bellard | req.userdata = sdl; |
247 | 85571bc7 | bellard | |
248 | 85571bc7 | bellard | if (sdl_open (&req, &obt))
|
249 | 85571bc7 | bellard | return -1; |
250 | 85571bc7 | bellard | |
251 | 85571bc7 | bellard | hw->freq = obt.freq; |
252 | 85571bc7 | bellard | hw->fmt = sdl_to_audfmt (obt.format); |
253 | 85571bc7 | bellard | hw->nchannels = obt.channels; |
254 | 85571bc7 | bellard | hw->bufsize = obt.samples << shift; |
255 | 85571bc7 | bellard | |
256 | 85571bc7 | bellard | s->initialized = 1;
|
257 | 85571bc7 | bellard | s->exit = 0;
|
258 | 85571bc7 | bellard | SDL_PauseAudio (0);
|
259 | 85571bc7 | bellard | return 0; |
260 | 85571bc7 | bellard | } |
261 | 85571bc7 | bellard | |
262 | 85571bc7 | bellard | static int sdl_hw_ctl (HWVoice *hw, int cmd, ...) |
263 | 85571bc7 | bellard | { |
264 | 85571bc7 | bellard | (void) hw;
|
265 | 85571bc7 | bellard | |
266 | 85571bc7 | bellard | switch (cmd) {
|
267 | 85571bc7 | bellard | case VOICE_ENABLE:
|
268 | 85571bc7 | bellard | SDL_PauseAudio (0);
|
269 | 85571bc7 | bellard | break;
|
270 | 85571bc7 | bellard | |
271 | 85571bc7 | bellard | case VOICE_DISABLE:
|
272 | 85571bc7 | bellard | SDL_PauseAudio (1);
|
273 | 85571bc7 | bellard | break;
|
274 | 85571bc7 | bellard | } |
275 | 85571bc7 | bellard | return 0; |
276 | 85571bc7 | bellard | } |
277 | 85571bc7 | bellard | |
278 | 85571bc7 | bellard | static void *sdl_audio_init (void) |
279 | 85571bc7 | bellard | { |
280 | 85571bc7 | bellard | SDLAudioState *s = &glob_sdl; |
281 | 85571bc7 | bellard | conf.nb_samples = audio_get_conf_int (QC_SDL_SAMPLES, conf.nb_samples); |
282 | 85571bc7 | bellard | |
283 | 85571bc7 | bellard | if (SDL_InitSubSystem (SDL_INIT_AUDIO)) {
|
284 | 85571bc7 | bellard | dolog ("SDL failed to initialize audio subsystem\nReason: %s\n",
|
285 | 85571bc7 | bellard | errstr ()); |
286 | 85571bc7 | bellard | return NULL; |
287 | 85571bc7 | bellard | } |
288 | 85571bc7 | bellard | |
289 | 85571bc7 | bellard | s->mutex = SDL_CreateMutex (); |
290 | 85571bc7 | bellard | if (!s->mutex) {
|
291 | 85571bc7 | bellard | dolog ("Failed to create SDL mutex\nReason: %s\n", errstr ());
|
292 | 85571bc7 | bellard | SDL_QuitSubSystem (SDL_INIT_AUDIO); |
293 | 85571bc7 | bellard | return NULL; |
294 | 85571bc7 | bellard | } |
295 | 85571bc7 | bellard | |
296 | 85571bc7 | bellard | s->sem = SDL_CreateSemaphore (0);
|
297 | 85571bc7 | bellard | if (!s->sem) {
|
298 | 85571bc7 | bellard | dolog ("Failed to create SDL semaphore\nReason: %s\n", errstr ());
|
299 | 85571bc7 | bellard | SDL_DestroyMutex (s->mutex); |
300 | 85571bc7 | bellard | SDL_QuitSubSystem (SDL_INIT_AUDIO); |
301 | 85571bc7 | bellard | return NULL; |
302 | 85571bc7 | bellard | } |
303 | 85571bc7 | bellard | |
304 | 85571bc7 | bellard | return s;
|
305 | 85571bc7 | bellard | } |
306 | 85571bc7 | bellard | |
307 | 85571bc7 | bellard | static void sdl_audio_fini (void *opaque) |
308 | 85571bc7 | bellard | { |
309 | 85571bc7 | bellard | SDLAudioState *s = opaque; |
310 | 85571bc7 | bellard | sdl_close (s); |
311 | 85571bc7 | bellard | SDL_DestroySemaphore (s->sem); |
312 | 85571bc7 | bellard | SDL_DestroyMutex (s->mutex); |
313 | 85571bc7 | bellard | SDL_QuitSubSystem (SDL_INIT_AUDIO); |
314 | 85571bc7 | bellard | } |
315 | 85571bc7 | bellard | |
316 | 85571bc7 | bellard | struct pcm_ops sdl_pcm_ops = {
|
317 | 85571bc7 | bellard | sdl_hw_init, |
318 | 85571bc7 | bellard | sdl_hw_fini, |
319 | 85571bc7 | bellard | sdl_hw_run, |
320 | 85571bc7 | bellard | sdl_hw_write, |
321 | 85571bc7 | bellard | sdl_hw_ctl |
322 | 85571bc7 | bellard | }; |
323 | 85571bc7 | bellard | |
324 | 85571bc7 | bellard | struct audio_output_driver sdl_output_driver = {
|
325 | 85571bc7 | bellard | "sdl",
|
326 | 85571bc7 | bellard | sdl_audio_init, |
327 | 85571bc7 | bellard | sdl_audio_fini, |
328 | 85571bc7 | bellard | &sdl_pcm_ops, |
329 | 85571bc7 | bellard | 1,
|
330 | 85571bc7 | bellard | 1,
|
331 | 85571bc7 | bellard | sizeof (SDLVoice)
|
332 | 85571bc7 | bellard | }; |