src/audio/sndio/SDL_sndioaudio.c
author Ryan C. Gordon
Sun, 03 Jan 2016 20:52:44 -0500
changeset 10004 8f2f519d1e61
parent 9998 f67cf37e9cd4
child 10255 9530fc07da6c
permissions -rw-r--r--
CMake: Don't make a libSDL2.so symlink on Mac OS X (do .dylib instead).
icculus@7367
     1
/*
icculus@7367
     2
  Simple DirectMedia Layer
slouken@9998
     3
  Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
icculus@7367
     4
icculus@7367
     5
  This software is provided 'as-is', without any express or implied
icculus@7367
     6
  warranty.  In no event will the authors be held liable for any damages
icculus@7367
     7
  arising from the use of this software.
icculus@7367
     8
icculus@7367
     9
  Permission is granted to anyone to use this software for any purpose,
icculus@7367
    10
  including commercial applications, and to alter it and redistribute it
icculus@7367
    11
  freely, subject to the following restrictions:
icculus@7367
    12
icculus@7367
    13
  1. The origin of this software must not be misrepresented; you must not
icculus@7367
    14
     claim that you wrote the original software. If you use this software
icculus@7367
    15
     in a product, an acknowledgment in the product documentation would be
icculus@7367
    16
     appreciated but is not required.
icculus@7367
    17
  2. Altered source versions must be plainly marked as such, and must not be
icculus@7367
    18
     misrepresented as being the original software.
icculus@7367
    19
  3. This notice may not be removed or altered from any source distribution.
icculus@7367
    20
*/
icculus@7367
    21
icculus@8093
    22
#include "../../SDL_internal.h"
icculus@7367
    23
icculus@7367
    24
#if SDL_AUDIO_DRIVER_SNDIO
icculus@7367
    25
icculus@7367
    26
/* OpenBSD sndio target */
icculus@7367
    27
icculus@7367
    28
#if HAVE_STDIO_H
icculus@7367
    29
#include <stdio.h>
icculus@7367
    30
#endif
icculus@7367
    31
icculus@7367
    32
#ifdef HAVE_SIGNAL_H
icculus@7367
    33
#include <signal.h>
icculus@7367
    34
#endif
icculus@7367
    35
icculus@7367
    36
#include <unistd.h>
icculus@7367
    37
icculus@7367
    38
#include "SDL_audio.h"
icculus@7367
    39
#include "../SDL_audiomem.h"
icculus@7367
    40
#include "../SDL_audio_c.h"
icculus@7367
    41
#include "SDL_sndioaudio.h"
icculus@7367
    42
icculus@7367
    43
#ifdef SDL_AUDIO_DRIVER_SNDIO_DYNAMIC
icculus@7367
    44
#include "SDL_loadso.h"
icculus@7367
    45
#endif
icculus@7367
    46
icculus@7367
    47
static struct sio_hdl * (*SNDIO_sio_open)(const char *, unsigned int, int);
icculus@7367
    48
static void (*SNDIO_sio_close)(struct sio_hdl *);
icculus@7367
    49
static int (*SNDIO_sio_setpar)(struct sio_hdl *, struct sio_par *);
icculus@7367
    50
static int (*SNDIO_sio_getpar)(struct sio_hdl *, struct sio_par *);
icculus@7367
    51
static int (*SNDIO_sio_start)(struct sio_hdl *);
icculus@7367
    52
static int (*SNDIO_sio_stop)(struct sio_hdl *);
icculus@7367
    53
static size_t (*SNDIO_sio_read)(struct sio_hdl *, void *, size_t);
icculus@7367
    54
static size_t (*SNDIO_sio_write)(struct sio_hdl *, const void *, size_t);
icculus@7367
    55
static void (*SNDIO_sio_initpar)(struct sio_par *);
icculus@7367
    56
icculus@7367
    57
#ifdef SDL_AUDIO_DRIVER_SNDIO_DYNAMIC
icculus@7367
    58
static const char *sndio_library = SDL_AUDIO_DRIVER_SNDIO_DYNAMIC;
icculus@7367
    59
static void *sndio_handle = NULL;
icculus@7367
    60
icculus@7367
    61
static int
icculus@7367
    62
load_sndio_sym(const char *fn, void **addr)
icculus@7367
    63
{
icculus@7367
    64
    *addr = SDL_LoadFunction(sndio_handle, fn);
icculus@7367
    65
    if (*addr == NULL) {
icculus@7367
    66
        /* Don't call SDL_SetError(): SDL_LoadFunction already did. */
icculus@7367
    67
        return 0;
icculus@7367
    68
    }
icculus@7367
    69
icculus@7367
    70
    return 1;
icculus@7367
    71
}
icculus@7367
    72
icculus@7367
    73
/* cast funcs to char* first, to please GCC's strict aliasing rules. */
icculus@7367
    74
#define SDL_SNDIO_SYM(x) \
icculus@7367
    75
    if (!load_sndio_sym(#x, (void **) (char *) &SNDIO_##x)) return -1
icculus@7367
    76
#else
icculus@7367
    77
#define SDL_SNDIO_SYM(x) SNDIO_##x = x
icculus@7367
    78
#endif
icculus@7367
    79
icculus@7367
    80
static int
icculus@7367
    81
load_sndio_syms(void)
icculus@7367
    82
{
icculus@7367
    83
    SDL_SNDIO_SYM(sio_open);
icculus@7367
    84
    SDL_SNDIO_SYM(sio_close);
icculus@7367
    85
    SDL_SNDIO_SYM(sio_setpar);
icculus@7367
    86
    SDL_SNDIO_SYM(sio_getpar);
icculus@7367
    87
    SDL_SNDIO_SYM(sio_start);
icculus@7367
    88
    SDL_SNDIO_SYM(sio_stop);
icculus@7367
    89
    SDL_SNDIO_SYM(sio_read);
icculus@7367
    90
    SDL_SNDIO_SYM(sio_write);
icculus@7367
    91
    SDL_SNDIO_SYM(sio_initpar);
icculus@7367
    92
    return 0;
icculus@7367
    93
}
icculus@7367
    94
icculus@7367
    95
#undef SDL_SNDIO_SYM
icculus@7367
    96
icculus@7367
    97
#ifdef SDL_AUDIO_DRIVER_SNDIO_DYNAMIC
icculus@7367
    98
icculus@7367
    99
static void
icculus@7367
   100
UnloadSNDIOLibrary(void)
icculus@7367
   101
{
icculus@7367
   102
    if (sndio_handle != NULL) {
icculus@7367
   103
        SDL_UnloadObject(sndio_handle);
icculus@7367
   104
        sndio_handle = NULL;
icculus@7367
   105
    }
icculus@7367
   106
}
icculus@7367
   107
icculus@7367
   108
static int
icculus@7367
   109
LoadSNDIOLibrary(void)
icculus@7367
   110
{
icculus@7367
   111
    int retval = 0;
icculus@7367
   112
    if (sndio_handle == NULL) {
icculus@7367
   113
        sndio_handle = SDL_LoadObject(sndio_library);
icculus@7367
   114
        if (sndio_handle == NULL) {
icculus@7367
   115
            retval = -1;
icculus@7367
   116
            /* Don't call SDL_SetError(): SDL_LoadObject already did. */
icculus@7367
   117
        } else {
icculus@7367
   118
            retval = load_sndio_syms();
icculus@7367
   119
            if (retval < 0) {
icculus@7367
   120
                UnloadSNDIOLibrary();
icculus@7367
   121
            }
icculus@7367
   122
        }
icculus@7367
   123
    }
icculus@7367
   124
    return retval;
icculus@7367
   125
}
icculus@7367
   126
icculus@7367
   127
#else
icculus@7367
   128
icculus@7367
   129
static void
icculus@7367
   130
UnloadSNDIOLibrary(void)
icculus@7367
   131
{
icculus@7367
   132
}
icculus@7367
   133
icculus@7367
   134
static int
icculus@7367
   135
LoadSNDIOLibrary(void)
icculus@7367
   136
{
icculus@7367
   137
    load_sndio_syms();
icculus@7367
   138
    return 0;
icculus@7367
   139
}
icculus@7367
   140
icculus@7367
   141
#endif /* SDL_AUDIO_DRIVER_SNDIO_DYNAMIC */
icculus@7367
   142
icculus@7367
   143
icculus@7367
   144
icculus@7367
   145
icculus@7367
   146
static void
icculus@7367
   147
SNDIO_WaitDevice(_THIS)
icculus@7367
   148
{
icculus@7377
   149
    /* no-op; SNDIO_sio_write() blocks if necessary. */
icculus@7367
   150
}
icculus@7367
   151
icculus@7367
   152
static void
icculus@7367
   153
SNDIO_PlayDevice(_THIS)
icculus@7367
   154
{
icculus@7377
   155
    const int written = SNDIO_sio_write(this->hidden->dev,
icculus@7377
   156
                                        this->hidden->mixbuf,
icculus@7377
   157
                                        this->hidden->mixlen);
icculus@7367
   158
icculus@7367
   159
    /* If we couldn't write, assume fatal error for now */
icculus@7367
   160
    if ( written == 0 ) {
icculus@9394
   161
        SDL_OpenedAudioDeviceDisconnected(this);
icculus@7367
   162
    }
icculus@7367
   163
#ifdef DEBUG_AUDIO
icculus@7367
   164
    fprintf(stderr, "Wrote %d bytes of audio data\n", written);
icculus@7367
   165
#endif
icculus@7367
   166
}
icculus@7367
   167
icculus@7367
   168
static Uint8 *
icculus@7367
   169
SNDIO_GetDeviceBuf(_THIS)
icculus@7367
   170
{
icculus@7367
   171
    return this->hidden->mixbuf;
icculus@7367
   172
}
icculus@7367
   173
icculus@7367
   174
static void
icculus@7367
   175
SNDIO_WaitDone(_THIS)
icculus@7367
   176
{
icculus@7377
   177
    SNDIO_sio_stop(this->hidden->dev);
icculus@7367
   178
}
icculus@7367
   179
icculus@7367
   180
static void
icculus@7367
   181
SNDIO_CloseDevice(_THIS)
icculus@7367
   182
{
icculus@7367
   183
    if (this->hidden != NULL) {
slouken@7719
   184
        SDL_FreeAudioMem(this->hidden->mixbuf);
slouken@7719
   185
        this->hidden->mixbuf = NULL;
icculus@7367
   186
        if ( this->hidden->dev != NULL ) {
icculus@7377
   187
            SNDIO_sio_close(this->hidden->dev);
icculus@7367
   188
            this->hidden->dev = NULL;
icculus@7367
   189
        }
icculus@7367
   190
        SDL_free(this->hidden);
icculus@7367
   191
        this->hidden = NULL;
icculus@7367
   192
    }
icculus@7367
   193
}
icculus@7367
   194
icculus@7367
   195
static int
icculus@9394
   196
SNDIO_OpenDevice(_THIS, void *handle, const char *devname, int iscapture)
icculus@7367
   197
{
icculus@7367
   198
    SDL_AudioFormat test_format = SDL_FirstAudioFormat(this->spec.format);
icculus@7367
   199
    struct sio_par par;
icculus@7367
   200
    int status;
icculus@7367
   201
icculus@7367
   202
    this->hidden = (struct SDL_PrivateAudioData *)
icculus@7367
   203
        SDL_malloc(sizeof(*this->hidden));
icculus@7367
   204
    if (this->hidden == NULL) {
icculus@7367
   205
        return SDL_OutOfMemory();
icculus@7367
   206
    }
icculus@7367
   207
    SDL_memset(this->hidden, 0, sizeof(*this->hidden));
icculus@7367
   208
icculus@7367
   209
    this->hidden->mixlen = this->spec.size;
icculus@7367
   210
icculus@7367
   211
    /* !!! FIXME: SIO_DEVANY can be a specific device... */
slouken@8867
   212
    if ((this->hidden->dev = SNDIO_sio_open(SIO_DEVANY, SIO_PLAY, 0)) == NULL) {
icculus@7367
   213
        SNDIO_CloseDevice(this);
icculus@7367
   214
        return SDL_SetError("sio_open() failed");
icculus@7367
   215
    }
icculus@7367
   216
icculus@7377
   217
    SNDIO_sio_initpar(&par);
icculus@7367
   218
icculus@7367
   219
    par.rate = this->spec.freq;
icculus@7367
   220
    par.pchan = this->spec.channels;
icculus@7367
   221
    par.round = this->spec.samples;
icculus@7367
   222
    par.appbufsz = par.round * 2;
icculus@7367
   223
icculus@7367
   224
    /* Try for a closest match on audio format */
icculus@7367
   225
    status = -1;
icculus@7367
   226
    while (test_format && (status < 0)) {
icculus@7367
   227
        if (!SDL_AUDIO_ISFLOAT(test_format)) {
icculus@7367
   228
            par.le = SDL_AUDIO_ISLITTLEENDIAN(test_format) ? 1 : 0;
icculus@7367
   229
            par.sig = SDL_AUDIO_ISSIGNED(test_format) ? 1 : 0;
icculus@7367
   230
            par.bits = SDL_AUDIO_BITSIZE(test_format);
icculus@7367
   231
slouken@8867
   232
            if (SNDIO_sio_setpar(this->hidden->dev, &par) == 0) {
slouken@8867
   233
                continue;
slouken@8867
   234
            }
slouken@8867
   235
            if (SNDIO_sio_getpar(this->hidden->dev, &par) == 0) {
slouken@8867
   236
                SNDIO_CloseDevice(this);
slouken@8867
   237
                return SDL_SetError("sio_getpar() failed");
slouken@8867
   238
            }
slouken@8867
   239
            if (par.bps != SIO_BPS(par.bits)) {
slouken@8867
   240
                continue;
slouken@8867
   241
            }
slouken@8867
   242
            if ((par.bits == 8 * par.bps) || (par.msb)) {
icculus@7367
   243
                status = 0;
icculus@7367
   244
                break;
icculus@7367
   245
            }
icculus@7367
   246
        }
icculus@7367
   247
        test_format = SDL_NextAudioFormat();
icculus@7367
   248
    }
icculus@7367
   249
icculus@7367
   250
    if (status < 0) {
icculus@7367
   251
        SNDIO_CloseDevice(this);
icculus@7367
   252
        return SDL_SetError("sndio: Couldn't find any hardware audio formats");
icculus@7367
   253
    }
icculus@7367
   254
slouken@8867
   255
    if ((par.bps == 4) && (par.sig) && (par.le))
icculus@7367
   256
        this->spec.format = AUDIO_S32LSB;
slouken@8867
   257
    else if ((par.bps == 4) && (par.sig) && (!par.le))
icculus@7367
   258
        this->spec.format = AUDIO_S32MSB;
slouken@8867
   259
    else if ((par.bps == 2) && (par.sig) && (par.le))
icculus@7367
   260
        this->spec.format = AUDIO_S16LSB;
slouken@8867
   261
    else if ((par.bps == 2) && (par.sig) && (!par.le))
icculus@7367
   262
        this->spec.format = AUDIO_S16MSB;
slouken@8867
   263
    else if ((par.bps == 2) && (!par.sig) && (par.le))
icculus@7367
   264
        this->spec.format = AUDIO_U16LSB;
slouken@8867
   265
    else if ((par.bps == 2) && (!par.sig) && (!par.le))
icculus@7367
   266
        this->spec.format = AUDIO_U16MSB;
slouken@8867
   267
    else if ((par.bps == 1) && (par.sig))
icculus@7367
   268
        this->spec.format = AUDIO_S8;
slouken@8867
   269
    else if ((par.bps == 1) && (!par.sig))
icculus@7367
   270
        this->spec.format = AUDIO_U8;
icculus@7367
   271
    else {
icculus@7367
   272
        SNDIO_CloseDevice(this);
icculus@7367
   273
        return SDL_SetError("sndio: Got unsupported hardware audio format.");
icculus@7367
   274
    }
icculus@7367
   275
icculus@7367
   276
    this->spec.freq = par.rate;
icculus@7367
   277
    this->spec.channels = par.pchan;
icculus@7367
   278
    this->spec.samples = par.round;
icculus@7367
   279
icculus@7367
   280
    /* Calculate the final parameters for this audio specification */
icculus@7367
   281
    SDL_CalculateAudioSpec(&this->spec);
icculus@7367
   282
icculus@7367
   283
    /* Allocate mixing buffer */
icculus@7367
   284
    this->hidden->mixlen = this->spec.size;
icculus@7367
   285
    this->hidden->mixbuf = (Uint8 *) SDL_AllocAudioMem(this->hidden->mixlen);
icculus@7367
   286
    if (this->hidden->mixbuf == NULL) {
icculus@7367
   287
        SNDIO_CloseDevice(this);
icculus@7367
   288
        return SDL_OutOfMemory();
icculus@7367
   289
    }
icculus@7367
   290
    SDL_memset(this->hidden->mixbuf, this->spec.silence, this->hidden->mixlen);
icculus@7367
   291
icculus@7377
   292
    if (!SNDIO_sio_start(this->hidden->dev)) {
icculus@7367
   293
        return SDL_SetError("sio_start() failed");
icculus@7367
   294
    }
icculus@7367
   295
icculus@7367
   296
    /* We're ready to rock and roll. :-) */
icculus@7367
   297
    return 0;
icculus@7367
   298
}
icculus@7367
   299
icculus@7367
   300
static void
icculus@7367
   301
SNDIO_Deinitialize(void)
icculus@7367
   302
{
icculus@7367
   303
    UnloadSNDIOLibrary();
icculus@7367
   304
}
icculus@7367
   305
icculus@7367
   306
static int
icculus@7367
   307
SNDIO_Init(SDL_AudioDriverImpl * impl)
icculus@7367
   308
{
icculus@7367
   309
    if (LoadSNDIOLibrary() < 0) {
icculus@7367
   310
        return 0;
icculus@7367
   311
    }
icculus@7367
   312
icculus@7367
   313
    /* Set the function pointers */
icculus@7367
   314
    impl->OpenDevice = SNDIO_OpenDevice;
icculus@7367
   315
    impl->WaitDevice = SNDIO_WaitDevice;
icculus@7367
   316
    impl->PlayDevice = SNDIO_PlayDevice;
icculus@7367
   317
    impl->GetDeviceBuf = SNDIO_GetDeviceBuf;
icculus@7367
   318
    impl->WaitDone = SNDIO_WaitDone;
icculus@7367
   319
    impl->CloseDevice = SNDIO_CloseDevice;
icculus@7367
   320
    impl->Deinitialize = SNDIO_Deinitialize;
icculus@7367
   321
    impl->OnlyHasDefaultOutputDevice = 1;  /* !!! FIXME: sndio can handle multiple devices. */
icculus@7367
   322
icculus@7367
   323
    return 1;   /* this audio target is available. */
icculus@7367
   324
}
icculus@7367
   325
icculus@7367
   326
AudioBootStrap SNDIO_bootstrap = {
icculus@7367
   327
    "sndio", "OpenBSD sndio", SNDIO_Init, 0
icculus@7367
   328
};
icculus@7367
   329
icculus@7367
   330
#endif /* SDL_AUDIO_DRIVER_SNDIO */
icculus@7367
   331
icculus@7367
   332
/* vi: set ts=4 sw=4 expandtab: */