summaryrefslogtreecommitdiffstats
path: root/audio/sdlaudio.c
blob: 978686a0717932c440434d641b71b7a45b03b0d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/*
 * QEMU SDL audio output driver
 * 
 * Copyright (c) 2004 Vassili Karpov (malc)
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
#include <SDL.h>
#include <SDL_thread.h>
#include "vl.h"

#include "audio/audio_int.h"

typedef struct SDLVoice {
    HWVoice hw;
} SDLVoice;

#define dolog(...) AUD_log ("sdl", __VA_ARGS__)
#ifdef DEBUG
#define ldebug(...) dolog (__VA_ARGS__)
#else
#define ldebug(...)
#endif

#define QC_SDL_SAMPLES "QEMU_SDL_SAMPLES"

#define errstr() SDL_GetError ()

static struct {
    int nb_samples;
} conf = {
    1024
};

struct SDLAudioState {
    int exit;
    SDL_mutex *mutex;
    SDL_sem *sem;
    int initialized;
} glob_sdl;
typedef struct SDLAudioState SDLAudioState;

static void sdl_hw_run (HWVoice *hw)
{
    (void) hw;
}

static int sdl_lock (SDLAudioState *s)
{
    if (SDL_LockMutex (s->mutex)) {
        dolog ("SDL_LockMutex failed\nReason: %s\n", errstr ());
        return -1;
    }
    return 0;
}

static int sdl_unlock (SDLAudioState *s)
{
    if (SDL_UnlockMutex (s->mutex)) {
        dolog ("SDL_UnlockMutex failed\nReason: %s\n", errstr ());
        return -1;
    }
    return 0;
}

static int sdl_post (SDLAudioState *s)
{
    if (SDL_SemPost (s->sem)) {
        dolog ("SDL_SemPost failed\nReason: %s\n", errstr ());
        return -1;
    }
    return 0;
}

static int sdl_wait (SDLAudioState *s)
{
    if (SDL_SemWait (s->sem)) {
        dolog ("SDL_SemWait failed\nReason: %s\n", errstr ());
        return -1;
    }
    return 0;
}

static int sdl_unlock_and_post (SDLAudioState *s)
{
    if (sdl_unlock (s))
        return -1;

    return sdl_post (s);
}

static int sdl_hw_write (SWVoice *sw, void *buf, int len)
{
    int ret;
    SDLAudioState *s = &glob_sdl;
    sdl_lock (s);
    ret = pcm_hw_write (sw, buf, len);
    sdl_unlock_and_post (s);
    return ret;
}

static int AUD_to_sdlfmt (audfmt_e fmt, int *shift)
{
    *shift = 0;
    switch (fmt) {
    case AUD_FMT_S8: return AUDIO_S8;
    case AUD_FMT_U8: return AUDIO_U8;
    case AUD_FMT_S16: *shift = 1; return AUDIO_S16LSB;
    case AUD_FMT_U16: *shift = 1; return AUDIO_U16LSB;
    default:
        dolog ("Internal logic error: Bad audio format %d\nAborting\n", fmt);
        exit (EXIT_FAILURE);
    }
}

static int sdl_to_audfmt (int fmt)
{
    switch (fmt) {
    case AUDIO_S8: return AUD_FMT_S8;
    case AUDIO_U8: return AUD_FMT_U8;
    case AUDIO_S16LSB: return AUD_FMT_S16;
    case AUDIO_U16LSB: return AUD_FMT_U16;
    default:
        dolog ("Internal logic error: Unrecognized SDL audio format %d\n"
               "Aborting\n", fmt);
        exit (EXIT_FAILURE);
    }
}

static int sdl_open (SDL_AudioSpec *req, SDL_AudioSpec *obt)
{
    int status;

    status = SDL_OpenAudio (req, obt);
    if (status) {
        dolog ("SDL_OpenAudio failed\nReason: %s\n", errstr ());
    }
    return status;
}

static void sdl_close (SDLAudioState *s)
{
    if (s->initialized) {
        sdl_lock (s);
        s->exit = 1;
        sdl_unlock_and_post (s);
        SDL_PauseAudio (1);
        SDL_CloseAudio ();
        s->initialized = 0;
    }
}

static void sdl_callback (void *opaque, Uint8 *buf, int len)
{
    SDLVoice *sdl = opaque;
    SDLAudioState *s = &glob_sdl;
    HWVoice *hw = &sdl->hw;
    int samples = len >> hw->shift;

    if (s->exit) {
        return;
    }

    while (samples) {
        int to_mix, live, decr;

        /* dolog ("in callback samples=%d\n", samples); */
        sdl_wait (s);
        if (s->exit) {
            return;
        }

        sdl_lock (s);
        live = pcm_hw_get_live (hw);
        if (live <= 0)
            goto again;

        /* dolog ("in callback live=%d\n", live); */
        to_mix = audio_MIN (samples, live);
        decr = to_mix;
        while (to_mix) {
            int chunk = audio_MIN (to_mix, hw->samples - hw->rpos);
            st_sample_t *src = hw->mix_buf + hw->rpos;

            /* dolog ("in callback to_mix %d, chunk %d\n", to_mix, chunk); */
            hw->clip (buf, src, chunk);
            memset (src, 0, chunk * sizeof (st_sample_t));
            hw->rpos = (hw->rpos + chunk) % hw->samples;
            to_mix -= chunk;
            buf += chunk << hw->shift;
        }
        samples -= decr;
        pcm_hw_dec_live (hw, decr);

    again:
        sdl_unlock (s);
    }
    /* dolog ("done len=%d\n", len); */
}

static void sdl_hw_fini (HWVoice *hw)
{
    ldebug ("sdl_hw_fini %d fixed=%d\n",
             glob_sdl.initialized, audio_state.fixed_format);
    sdl_close (&glob_sdl);
}

static int sdl_hw_init (HWVoice *hw, int freq, int nchannels, audfmt_e fmt)
{
    SDLVoice *sdl = (SDLVoice *) hw;
    SDLAudioState *s = &glob_sdl;
    SDL_AudioSpec req, obt;
    int shift;

    ldebug ("sdl_hw_init %d freq=%d fixed=%d\n",
            s->initialized, freq, audio_state.fixed_format);

    if (nchannels != 2) {
        dolog ("Bogus channel count %d\n", nchannels);
        return -1;
    }

    req.freq = freq;
    req.format = AUD_to_sdlfmt (fmt, &shift);
    req.channels = nchannels;
    req.samples = conf.nb_samples;
    shift <<= nchannels == 2;

    req.callback = sdl_callback;
    req.userdata = sdl;

    if (sdl_open (&req, &obt))
        return -1;

    hw->freq = obt.freq;
    hw->fmt = sdl_to_audfmt (obt.format);
    hw->nchannels = obt.channels;
    hw->bufsize = obt.samples << shift;

    s->initialized = 1;
    s->exit = 0;
    SDL_PauseAudio (0);
    return 0;
}

static int sdl_hw_ctl (HWVoice *hw, int cmd, ...)
{
    (void) hw;

    switch (cmd) {
    case VOICE_ENABLE:
        SDL_PauseAudio (0);
        break;

    case VOICE_DISABLE:
        SDL_PauseAudio (1);
        break;
    }
    return 0;
}

static void *sdl_audio_init (void)
{
    SDLAudioState *s = &glob_sdl;
    conf.nb_samples = audio_get_conf_int (QC_SDL_SAMPLES, conf.nb_samples);

    if (SDL_InitSubSystem (SDL_INIT_AUDIO)) {
        dolog ("SDL failed to initialize audio subsystem\nReason: %s\n",
               errstr ());
        return NULL;
    }

    s->mutex = SDL_CreateMutex ();
    if (!s->mutex) {
        dolog ("Failed to create SDL mutex\nReason: %s\n", errstr ());
        SDL_QuitSubSystem (SDL_INIT_AUDIO);
        return NULL;
    }

    s->sem = SDL_CreateSemaphore (0);
    if (!s->sem) {
        dolog ("Failed to create SDL semaphore\nReason: %s\n", errstr ());
        SDL_DestroyMutex (s->mutex);
        SDL_QuitSubSystem (SDL_INIT_AUDIO);
        return NULL;
    }

    return s;
}

static void sdl_audio_fini (void *opaque)
{
    SDLAudioState *s = opaque;
    sdl_close (s);
    SDL_DestroySemaphore (s->sem);
    SDL_DestroyMutex (s->mutex);
    SDL_QuitSubSystem (SDL_INIT_AUDIO);
}

struct pcm_ops sdl_pcm_ops = {
    sdl_hw_init,
    sdl_hw_fini,
    sdl_hw_run,
    sdl_hw_write,
    sdl_hw_ctl
};

struct audio_output_driver sdl_output_driver = {
    "sdl",
    sdl_audio_init,
    sdl_audio_fini,
    &sdl_pcm_ops,
    1,
    1,
    sizeof (SDLVoice)
};
OpenPOWER on IntegriCloud