~pmdj/ubuntu/trusty/qemu/2.9+applesmc+fadtv3

« back to all changes in this revision

Viewing changes to audio/sdlaudio.c

  • Committer: Phil Dennis-Jordan
  • Date: 2017-07-21 08:03:43 UTC
  • mfrom: (1.1.1)
  • Revision ID: phil@philjordan.eu-20170721080343-2yr2vdj7713czahv
New upstream release 2.9.0.

Show diffs side-by-side

added added

removed removed

Lines of Context:
38
38
#define AUDIO_CAP "sdl"
39
39
#include "audio_int.h"
40
40
 
 
41
#define USE_SEMAPHORE (SDL_MAJOR_VERSION < 2)
 
42
 
41
43
typedef struct SDLVoiceOut {
42
44
    HWVoiceOut hw;
43
45
    int live;
 
46
#if USE_SEMAPHORE
44
47
    int rpos;
 
48
#endif
45
49
    int decr;
46
50
} SDLVoiceOut;
47
51
 
53
57
 
54
58
static struct SDLAudioState {
55
59
    int exit;
 
60
#if USE_SEMAPHORE
56
61
    SDL_mutex *mutex;
57
62
    SDL_sem *sem;
 
63
#endif
58
64
    int initialized;
59
65
    bool driver_created;
60
66
} glob_sdl;
73
79
 
74
80
static int sdl_lock (SDLAudioState *s, const char *forfn)
75
81
{
 
82
#if USE_SEMAPHORE
76
83
    if (SDL_LockMutex (s->mutex)) {
77
84
        sdl_logerr ("SDL_LockMutex for %s failed\n", forfn);
78
85
        return -1;
79
86
    }
 
87
#else
 
88
    SDL_LockAudio();
 
89
#endif
 
90
 
80
91
    return 0;
81
92
}
82
93
 
83
94
static int sdl_unlock (SDLAudioState *s, const char *forfn)
84
95
{
 
96
#if USE_SEMAPHORE
85
97
    if (SDL_UnlockMutex (s->mutex)) {
86
98
        sdl_logerr ("SDL_UnlockMutex for %s failed\n", forfn);
87
99
        return -1;
88
100
    }
 
101
#else
 
102
    SDL_UnlockAudio();
 
103
#endif
 
104
 
89
105
    return 0;
90
106
}
91
107
 
92
108
static int sdl_post (SDLAudioState *s, const char *forfn)
93
109
{
 
110
#if USE_SEMAPHORE
94
111
    if (SDL_SemPost (s->sem)) {
95
112
        sdl_logerr ("SDL_SemPost for %s failed\n", forfn);
96
113
        return -1;
97
114
    }
 
115
#endif
 
116
 
98
117
    return 0;
99
118
}
100
119
 
 
120
#if USE_SEMAPHORE
101
121
static int sdl_wait (SDLAudioState *s, const char *forfn)
102
122
{
103
123
    if (SDL_SemWait (s->sem)) {
106
126
    }
107
127
    return 0;
108
128
}
 
129
#endif
109
130
 
110
131
static int sdl_unlock_and_post (SDLAudioState *s, const char *forfn)
111
132
{
246
267
        int to_mix, decr;
247
268
 
248
269
        /* dolog ("in callback samples=%d\n", samples); */
 
270
#if USE_SEMAPHORE
249
271
        sdl_wait (s, "sdl_callback");
250
272
        if (s->exit) {
251
273
            return;
264
286
        if (!sdl->live) {
265
287
            goto again;
266
288
        }
 
289
#else
 
290
        if (s->exit || !sdl->live) {
 
291
            break;
 
292
        }
 
293
#endif
267
294
 
268
295
        /* dolog ("in callback live=%d\n", live); */
269
296
        to_mix = audio_MIN (samples, sdl->live);
274
301
 
275
302
            /* dolog ("in callback to_mix %d, chunk %d\n", to_mix, chunk); */
276
303
            hw->clip (buf, src, chunk);
 
304
#if USE_SEMAPHORE
277
305
            sdl->rpos = (sdl->rpos + chunk) % hw->samples;
 
306
#else
 
307
            hw->rpos = (hw->rpos + chunk) % hw->samples;
 
308
#endif
278
309
            to_mix -= chunk;
279
310
            buf += chunk << hw->info.shift;
280
311
        }
282
313
        sdl->live -= decr;
283
314
        sdl->decr += decr;
284
315
 
 
316
#if USE_SEMAPHORE
285
317
    again:
286
318
        if (sdl_unlock (s, "sdl_callback")) {
287
319
            return;
288
320
        }
 
321
#endif
289
322
    }
290
323
    /* dolog ("done len=%d\n", len); */
 
324
 
 
325
#if (SDL_MAJOR_VERSION >= 2)
 
326
    /* SDL2 does not clear the remaining buffer for us, so do it on our own */
 
327
    if (samples) {
 
328
        memset(buf, 0, samples << hw->info.shift);
 
329
    }
 
330
#endif
291
331
}
292
332
 
293
333
static int sdl_write_out (SWVoiceOut *sw, void *buf, int len)
315
355
    decr = audio_MIN (sdl->decr, live);
316
356
    sdl->decr -= decr;
317
357
 
 
358
#if USE_SEMAPHORE
318
359
    sdl->live = live - decr;
319
360
    hw->rpos = sdl->rpos;
 
361
#else
 
362
    sdl->live = live;
 
363
#endif
320
364
 
321
365
    if (sdl->live > 0) {
322
366
        sdl_unlock_and_post (s, "sdl_run_out");
405
449
        return NULL;
406
450
    }
407
451
 
 
452
#if USE_SEMAPHORE
408
453
    s->mutex = SDL_CreateMutex ();
409
454
    if (!s->mutex) {
410
455
        sdl_logerr ("Failed to create SDL mutex\n");
419
464
        SDL_QuitSubSystem (SDL_INIT_AUDIO);
420
465
        return NULL;
421
466
    }
 
467
#endif
422
468
 
423
469
    s->driver_created = true;
424
470
    return s;
428
474
{
429
475
    SDLAudioState *s = opaque;
430
476
    sdl_close (s);
 
477
#if USE_SEMAPHORE
431
478
    SDL_DestroySemaphore (s->sem);
432
479
    SDL_DestroyMutex (s->mutex);
 
480
#endif
433
481
    SDL_QuitSubSystem (SDL_INIT_AUDIO);
434
482
    s->driver_created = false;
435
483
}