src/audio/sndio/SDL_sndioaudio.c
author Ryan C. Gordon
Tue, 24 Jan 2017 16:18:25 -0500
changeset 10850 c9dc0068b0e7
parent 10737 3406a0f8b041
child 11148 9bdb766ec7e5
permissions -rw-r--r--
configure: report libsamplerate support status.
icculus@7367
     1
/*
icculus@7367
     2
  Simple DirectMedia Layer
slouken@10737
     3
  Copyright (C) 1997-2017 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_audio_c.h"
icculus@7367
    40
#include "SDL_sndioaudio.h"
icculus@7367
    41
icculus@7367
    42
#ifdef SDL_AUDIO_DRIVER_SNDIO_DYNAMIC
icculus@7367
    43
#include "SDL_loadso.h"
icculus@7367
    44
#endif
icculus@7367
    45
icculus@7367
    46
static struct sio_hdl * (*SNDIO_sio_open)(const char *, unsigned int, int);
icculus@7367
    47
static void (*SNDIO_sio_close)(struct sio_hdl *);
icculus@7367
    48
static int (*SNDIO_sio_setpar)(struct sio_hdl *, struct sio_par *);
icculus@7367
    49
static int (*SNDIO_sio_getpar)(struct sio_hdl *, struct sio_par *);
icculus@7367
    50
static int (*SNDIO_sio_start)(struct sio_hdl *);
icculus@7367
    51
static int (*SNDIO_sio_stop)(struct sio_hdl *);
icculus@7367
    52
static size_t (*SNDIO_sio_read)(struct sio_hdl *, void *, size_t);
icculus@7367
    53
static size_t (*SNDIO_sio_write)(struct sio_hdl *, const void *, size_t);
icculus@7367
    54
static void (*SNDIO_sio_initpar)(struct sio_par *);
icculus@7367
    55
icculus@7367
    56
#ifdef SDL_AUDIO_DRIVER_SNDIO_DYNAMIC
icculus@7367
    57
static const char *sndio_library = SDL_AUDIO_DRIVER_SNDIO_DYNAMIC;
icculus@7367
    58
static void *sndio_handle = NULL;
icculus@7367
    59
icculus@7367
    60
static int
icculus@7367
    61
load_sndio_sym(const char *fn, void **addr)
icculus@7367
    62
{
icculus@7367
    63
    *addr = SDL_LoadFunction(sndio_handle, fn);
icculus@7367
    64
    if (*addr == NULL) {
icculus@7367
    65
        /* Don't call SDL_SetError(): SDL_LoadFunction already did. */
icculus@7367
    66
        return 0;
icculus@7367
    67
    }
icculus@7367
    68
icculus@7367
    69
    return 1;
icculus@7367
    70
}
icculus@7367
    71
icculus@7367
    72
/* cast funcs to char* first, to please GCC's strict aliasing rules. */
icculus@7367
    73
#define SDL_SNDIO_SYM(x) \
icculus@7367
    74
    if (!load_sndio_sym(#x, (void **) (char *) &SNDIO_##x)) return -1
icculus@7367
    75
#else
icculus@7367
    76
#define SDL_SNDIO_SYM(x) SNDIO_##x = x
icculus@7367
    77
#endif
icculus@7367
    78
icculus@7367
    79
static int
icculus@7367
    80
load_sndio_syms(void)
icculus@7367
    81
{
icculus@7367
    82
    SDL_SNDIO_SYM(sio_open);
icculus@7367
    83
    SDL_SNDIO_SYM(sio_close);
icculus@7367
    84
    SDL_SNDIO_SYM(sio_setpar);
icculus@7367
    85
    SDL_SNDIO_SYM(sio_getpar);
icculus@7367
    86
    SDL_SNDIO_SYM(sio_start);
icculus@7367
    87
    SDL_SNDIO_SYM(sio_stop);
icculus@7367
    88
    SDL_SNDIO_SYM(sio_read);
icculus@7367
    89
    SDL_SNDIO_SYM(sio_write);
icculus@7367
    90
    SDL_SNDIO_SYM(sio_initpar);
icculus@7367
    91
    return 0;
icculus@7367
    92
}
icculus@7367
    93
icculus@7367
    94
#undef SDL_SNDIO_SYM
icculus@7367
    95
icculus@7367
    96
#ifdef SDL_AUDIO_DRIVER_SNDIO_DYNAMIC
icculus@7367
    97
icculus@7367
    98
static void
icculus@7367
    99
UnloadSNDIOLibrary(void)
icculus@7367
   100
{
icculus@7367
   101
    if (sndio_handle != NULL) {
icculus@7367
   102
        SDL_UnloadObject(sndio_handle);
icculus@7367
   103
        sndio_handle = NULL;
icculus@7367
   104
    }
icculus@7367
   105
}
icculus@7367
   106
icculus@7367
   107
static int
icculus@7367
   108
LoadSNDIOLibrary(void)
icculus@7367
   109
{
icculus@7367
   110
    int retval = 0;
icculus@7367
   111
    if (sndio_handle == NULL) {
icculus@7367
   112
        sndio_handle = SDL_LoadObject(sndio_library);
icculus@7367
   113
        if (sndio_handle == NULL) {
icculus@7367
   114
            retval = -1;
icculus@7367
   115
            /* Don't call SDL_SetError(): SDL_LoadObject already did. */
icculus@7367
   116
        } else {
icculus@7367
   117
            retval = load_sndio_syms();
icculus@7367
   118
            if (retval < 0) {
icculus@7367
   119
                UnloadSNDIOLibrary();
icculus@7367
   120
            }
icculus@7367
   121
        }
icculus@7367
   122
    }
icculus@7367
   123
    return retval;
icculus@7367
   124
}
icculus@7367
   125
icculus@7367
   126
#else
icculus@7367
   127
icculus@7367
   128
static void
icculus@7367
   129
UnloadSNDIOLibrary(void)
icculus@7367
   130
{
icculus@7367
   131
}
icculus@7367
   132
icculus@7367
   133
static int
icculus@7367
   134
LoadSNDIOLibrary(void)
icculus@7367
   135
{
icculus@7367
   136
    load_sndio_syms();
icculus@7367
   137
    return 0;
icculus@7367
   138
}
icculus@7367
   139
icculus@7367
   140
#endif /* SDL_AUDIO_DRIVER_SNDIO_DYNAMIC */
icculus@7367
   141
icculus@7367
   142
icculus@7367
   143
icculus@7367
   144
icculus@7367
   145
static void
icculus@7367
   146
SNDIO_WaitDevice(_THIS)
icculus@7367
   147
{
icculus@7377
   148
    /* no-op; SNDIO_sio_write() blocks if necessary. */
icculus@7367
   149
}
icculus@7367
   150
icculus@7367
   151
static void
icculus@7367
   152
SNDIO_PlayDevice(_THIS)
icculus@7367
   153
{
icculus@7377
   154
    const int written = SNDIO_sio_write(this->hidden->dev,
icculus@7377
   155
                                        this->hidden->mixbuf,
icculus@7377
   156
                                        this->hidden->mixlen);
icculus@7367
   157
icculus@7367
   158
    /* If we couldn't write, assume fatal error for now */
icculus@7367
   159
    if ( written == 0 ) {
icculus@9394
   160
        SDL_OpenedAudioDeviceDisconnected(this);
icculus@7367
   161
    }
icculus@7367
   162
#ifdef DEBUG_AUDIO
icculus@7367
   163
    fprintf(stderr, "Wrote %d bytes of audio data\n", written);
icculus@7367
   164
#endif
icculus@7367
   165
}
icculus@7367
   166
icculus@7367
   167
static Uint8 *
icculus@7367
   168
SNDIO_GetDeviceBuf(_THIS)
icculus@7367
   169
{
icculus@7367
   170
    return this->hidden->mixbuf;
icculus@7367
   171
}
icculus@7367
   172
icculus@7367
   173
static void
icculus@7367
   174
SNDIO_CloseDevice(_THIS)
icculus@7367
   175
{
icculus@10255
   176
    if ( this->hidden->dev != NULL ) {
icculus@10473
   177
        SNDIO_sio_stop(this->hidden->dev);
icculus@10255
   178
        SNDIO_sio_close(this->hidden->dev);
icculus@7367
   179
    }
icculus@10256
   180
    SDL_free(this->hidden->mixbuf);
icculus@10255
   181
    SDL_free(this->hidden);
icculus@7367
   182
}
icculus@7367
   183
icculus@7367
   184
static int
icculus@9394
   185
SNDIO_OpenDevice(_THIS, void *handle, const char *devname, int iscapture)
icculus@7367
   186
{
icculus@7367
   187
    SDL_AudioFormat test_format = SDL_FirstAudioFormat(this->spec.format);
icculus@7367
   188
    struct sio_par par;
icculus@7367
   189
    int status;
icculus@7367
   190
icculus@7367
   191
    this->hidden = (struct SDL_PrivateAudioData *)
icculus@7367
   192
        SDL_malloc(sizeof(*this->hidden));
icculus@7367
   193
    if (this->hidden == NULL) {
icculus@7367
   194
        return SDL_OutOfMemory();
icculus@7367
   195
    }
icculus@10257
   196
    SDL_zerop(this->hidden);
icculus@7367
   197
icculus@7367
   198
    this->hidden->mixlen = this->spec.size;
icculus@7367
   199
icculus@7367
   200
    /* !!! FIXME: SIO_DEVANY can be a specific device... */
slouken@8867
   201
    if ((this->hidden->dev = SNDIO_sio_open(SIO_DEVANY, SIO_PLAY, 0)) == NULL) {
icculus@7367
   202
        return SDL_SetError("sio_open() failed");
icculus@7367
   203
    }
icculus@7367
   204
icculus@7377
   205
    SNDIO_sio_initpar(&par);
icculus@7367
   206
icculus@7367
   207
    par.rate = this->spec.freq;
icculus@7367
   208
    par.pchan = this->spec.channels;
icculus@7367
   209
    par.round = this->spec.samples;
icculus@7367
   210
    par.appbufsz = par.round * 2;
icculus@7367
   211
icculus@7367
   212
    /* Try for a closest match on audio format */
icculus@7367
   213
    status = -1;
icculus@7367
   214
    while (test_format && (status < 0)) {
icculus@7367
   215
        if (!SDL_AUDIO_ISFLOAT(test_format)) {
icculus@7367
   216
            par.le = SDL_AUDIO_ISLITTLEENDIAN(test_format) ? 1 : 0;
icculus@7367
   217
            par.sig = SDL_AUDIO_ISSIGNED(test_format) ? 1 : 0;
icculus@7367
   218
            par.bits = SDL_AUDIO_BITSIZE(test_format);
icculus@7367
   219
slouken@8867
   220
            if (SNDIO_sio_setpar(this->hidden->dev, &par) == 0) {
slouken@8867
   221
                continue;
slouken@8867
   222
            }
slouken@8867
   223
            if (SNDIO_sio_getpar(this->hidden->dev, &par) == 0) {
slouken@8867
   224
                return SDL_SetError("sio_getpar() failed");
slouken@8867
   225
            }
slouken@8867
   226
            if (par.bps != SIO_BPS(par.bits)) {
slouken@8867
   227
                continue;
slouken@8867
   228
            }
slouken@8867
   229
            if ((par.bits == 8 * par.bps) || (par.msb)) {
icculus@7367
   230
                status = 0;
icculus@7367
   231
                break;
icculus@7367
   232
            }
icculus@7367
   233
        }
icculus@7367
   234
        test_format = SDL_NextAudioFormat();
icculus@7367
   235
    }
icculus@7367
   236
icculus@7367
   237
    if (status < 0) {
icculus@7367
   238
        return SDL_SetError("sndio: Couldn't find any hardware audio formats");
icculus@7367
   239
    }
icculus@7367
   240
slouken@8867
   241
    if ((par.bps == 4) && (par.sig) && (par.le))
icculus@7367
   242
        this->spec.format = AUDIO_S32LSB;
slouken@8867
   243
    else if ((par.bps == 4) && (par.sig) && (!par.le))
icculus@7367
   244
        this->spec.format = AUDIO_S32MSB;
slouken@8867
   245
    else if ((par.bps == 2) && (par.sig) && (par.le))
icculus@7367
   246
        this->spec.format = AUDIO_S16LSB;
slouken@8867
   247
    else if ((par.bps == 2) && (par.sig) && (!par.le))
icculus@7367
   248
        this->spec.format = AUDIO_S16MSB;
slouken@8867
   249
    else if ((par.bps == 2) && (!par.sig) && (par.le))
icculus@7367
   250
        this->spec.format = AUDIO_U16LSB;
slouken@8867
   251
    else if ((par.bps == 2) && (!par.sig) && (!par.le))
icculus@7367
   252
        this->spec.format = AUDIO_U16MSB;
slouken@8867
   253
    else if ((par.bps == 1) && (par.sig))
icculus@7367
   254
        this->spec.format = AUDIO_S8;
slouken@8867
   255
    else if ((par.bps == 1) && (!par.sig))
icculus@7367
   256
        this->spec.format = AUDIO_U8;
icculus@7367
   257
    else {
icculus@7367
   258
        return SDL_SetError("sndio: Got unsupported hardware audio format.");
icculus@7367
   259
    }
icculus@7367
   260
icculus@7367
   261
    this->spec.freq = par.rate;
icculus@7367
   262
    this->spec.channels = par.pchan;
icculus@7367
   263
    this->spec.samples = par.round;
icculus@7367
   264
icculus@7367
   265
    /* Calculate the final parameters for this audio specification */
icculus@7367
   266
    SDL_CalculateAudioSpec(&this->spec);
icculus@7367
   267
icculus@7367
   268
    /* Allocate mixing buffer */
icculus@7367
   269
    this->hidden->mixlen = this->spec.size;
icculus@10256
   270
    this->hidden->mixbuf = (Uint8 *) SDL_malloc(this->hidden->mixlen);
icculus@7367
   271
    if (this->hidden->mixbuf == NULL) {
icculus@7367
   272
        return SDL_OutOfMemory();
icculus@7367
   273
    }
icculus@7367
   274
    SDL_memset(this->hidden->mixbuf, this->spec.silence, this->hidden->mixlen);
icculus@7367
   275
icculus@7377
   276
    if (!SNDIO_sio_start(this->hidden->dev)) {
icculus@7367
   277
        return SDL_SetError("sio_start() failed");
icculus@7367
   278
    }
icculus@7367
   279
icculus@7367
   280
    /* We're ready to rock and roll. :-) */
icculus@7367
   281
    return 0;
icculus@7367
   282
}
icculus@7367
   283
icculus@7367
   284
static void
icculus@7367
   285
SNDIO_Deinitialize(void)
icculus@7367
   286
{
icculus@7367
   287
    UnloadSNDIOLibrary();
icculus@7367
   288
}
icculus@7367
   289
icculus@7367
   290
static int
icculus@7367
   291
SNDIO_Init(SDL_AudioDriverImpl * impl)
icculus@7367
   292
{
icculus@7367
   293
    if (LoadSNDIOLibrary() < 0) {
icculus@7367
   294
        return 0;
icculus@7367
   295
    }
icculus@7367
   296
icculus@7367
   297
    /* Set the function pointers */
icculus@7367
   298
    impl->OpenDevice = SNDIO_OpenDevice;
icculus@7367
   299
    impl->WaitDevice = SNDIO_WaitDevice;
icculus@7367
   300
    impl->PlayDevice = SNDIO_PlayDevice;
icculus@7367
   301
    impl->GetDeviceBuf = SNDIO_GetDeviceBuf;
icculus@7367
   302
    impl->CloseDevice = SNDIO_CloseDevice;
icculus@7367
   303
    impl->Deinitialize = SNDIO_Deinitialize;
icculus@7367
   304
    impl->OnlyHasDefaultOutputDevice = 1;  /* !!! FIXME: sndio can handle multiple devices. */
icculus@7367
   305
icculus@7367
   306
    return 1;   /* this audio target is available. */
icculus@7367
   307
}
icculus@7367
   308
icculus@7367
   309
AudioBootStrap SNDIO_bootstrap = {
icculus@7367
   310
    "sndio", "OpenBSD sndio", SNDIO_Init, 0
icculus@7367
   311
};
icculus@7367
   312
icculus@7367
   313
#endif /* SDL_AUDIO_DRIVER_SNDIO */
icculus@7367
   314
icculus@7367
   315
/* vi: set ts=4 sw=4 expandtab: */