src/audio/fusionsound/SDL_fsaudio.c
author Ryan C. Gordon
Sun, 31 Mar 2013 13:05:40 -0400
changeset 7038 7f22b9ba218f
parent 6885 700f1b25f77f
child 7191 75360622e65f
permissions -rw-r--r--
Changed audio subsystem's OpenDevice interface to return -1 on error.

This lets us 'return SDL_SetError("whatever");' on one line.

Fixes Bugzilla #1778.
slouken@2947
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@6885
     3
  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
slouken@2947
     4
slouken@5535
     5
  This software is provided 'as-is', without any express or implied
slouken@5535
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     7
  arising from the use of this software.
slouken@2947
     8
slouken@5535
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
    10
  including commercial applications, and to alter it and redistribute it
slouken@5535
    11
  freely, subject to the following restrictions:
slouken@2947
    12
slouken@5535
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@5535
    14
     claim that you wrote the original software. If you use this software
slouken@5535
    15
     in a product, an acknowledgment in the product documentation would be
slouken@5535
    16
     appreciated but is not required.
slouken@5535
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@5535
    18
     misrepresented as being the original software.
slouken@5535
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@2947
    20
*/
slouken@2947
    21
#include "SDL_config.h"
slouken@2947
    22
slouken@6044
    23
#if SDL_AUDIO_DRIVER_FUSIONSOUND
slouken@6044
    24
slouken@2947
    25
/* Allow access to a raw mixing buffer */
slouken@2947
    26
slouken@2947
    27
#ifdef HAVE_SIGNAL_H
slouken@2947
    28
#include <signal.h>
slouken@2947
    29
#endif
slouken@2947
    30
#include <unistd.h>
slouken@2947
    31
slouken@2947
    32
#include "SDL_timer.h"
slouken@2947
    33
#include "SDL_audio.h"
slouken@2947
    34
#include "../SDL_audiomem.h"
slouken@2947
    35
#include "../SDL_audio_c.h"
slouken@2947
    36
#include "SDL_fsaudio.h"
slouken@2947
    37
couriersud@3230
    38
#include <fusionsound/fusionsound_version.h>
couriersud@3230
    39
slouken@2947
    40
//#define SDL_AUDIO_DRIVER_FUSIONSOUND_DYNAMIC "libfusionsound.so"
slouken@2947
    41
icculus@6046
    42
#ifdef SDL_AUDIO_DRIVER_FUSIONSOUND_DYNAMIC
slouken@2947
    43
#include "SDL_name.h"
slouken@2947
    44
#include "SDL_loadso.h"
slouken@2947
    45
#else
slouken@2947
    46
#define SDL_NAME(X)	X
slouken@2947
    47
#endif
slouken@2947
    48
couriersud@3230
    49
#if (FUSIONSOUND_MAJOR_VERSION == 1) && (FUSIONSOUND_MINOR_VERSION < 1)
couriersud@3230
    50
typedef DFBResult DirectResult;
couriersud@3230
    51
#endif
couriersud@3230
    52
slouken@2947
    53
/* Buffers to use - more than 2 gives a lot of latency */
slouken@2947
    54
#define FUSION_BUFFERS				(2)
slouken@2947
    55
icculus@6046
    56
#ifdef SDL_AUDIO_DRIVER_FUSIONSOUND_DYNAMIC
slouken@2947
    57
slouken@2947
    58
static const char *fs_library = SDL_AUDIO_DRIVER_FUSIONSOUND_DYNAMIC;
slouken@2947
    59
static void *fs_handle = NULL;
slouken@2947
    60
couriersud@3230
    61
static DirectResult (*SDL_NAME(FusionSoundInit)) (int *argc, char *(*argv[]));
couriersud@3230
    62
static DirectResult (*SDL_NAME(FusionSoundCreate)) (IFusionSound **
slouken@2947
    63
                                                   ret_interface);
slouken@2947
    64
slouken@2947
    65
#define SDL_FS_SYM(x) { #x, (void **) (char *) &SDL_NAME(x) }
slouken@2947
    66
static struct
slouken@2947
    67
{
slouken@2947
    68
    const char *name;
slouken@2947
    69
    void **func;
slouken@2947
    70
} fs_functions[] = {
slouken@2947
    71
/* *INDENT-OFF* */
slouken@2947
    72
    SDL_FS_SYM(FusionSoundInit),
slouken@2947
    73
    SDL_FS_SYM(FusionSoundCreate),
slouken@2947
    74
/* *INDENT-ON* */
slouken@2947
    75
};
slouken@2947
    76
slouken@2947
    77
#undef SDL_FS_SYM
slouken@2947
    78
slouken@2947
    79
static void
slouken@2947
    80
UnloadFusionSoundLibrary()
slouken@2947
    81
{
slouken@2947
    82
    if (fs_handle != NULL) {
slouken@2947
    83
        SDL_UnloadObject(fs_handle);
slouken@2947
    84
        fs_handle = NULL;
slouken@2947
    85
    }
slouken@2947
    86
}
slouken@2947
    87
slouken@2947
    88
static int
slouken@2947
    89
LoadFusionSoundLibrary(void)
slouken@2947
    90
{
slouken@2947
    91
    int i, retval = -1;
slouken@2947
    92
slouken@2947
    93
    if (fs_handle == NULL) {
slouken@2947
    94
        fs_handle = SDL_LoadObject(fs_library);
slouken@2947
    95
        if (fs_handle != NULL) {
slouken@2947
    96
            retval = 0;
slouken@2947
    97
            for (i = 0; i < SDL_arraysize(fs_functions); ++i) {
slouken@2947
    98
                *fs_functions[i].func =
slouken@2947
    99
                    SDL_LoadFunction(fs_handle, fs_functions[i].name);
slouken@2947
   100
                if (!*fs_functions[i].func) {
slouken@2947
   101
                    retval = -1;
slouken@2947
   102
                    UnloadFusionSoundLibrary();
slouken@2947
   103
                    break;
slouken@2947
   104
                }
slouken@2947
   105
            }
slouken@2947
   106
        }
slouken@2947
   107
    }
slouken@2947
   108
slouken@2947
   109
    return retval;
slouken@2947
   110
}
slouken@2947
   111
slouken@2947
   112
#else
slouken@2947
   113
slouken@2947
   114
static void
slouken@2947
   115
UnloadFusionSoundLibrary()
slouken@2947
   116
{
slouken@2947
   117
    return;
slouken@2947
   118
}
slouken@2947
   119
slouken@2947
   120
static int
slouken@2947
   121
LoadFusionSoundLibrary(void)
slouken@2947
   122
{
slouken@2947
   123
    return 0;
slouken@2947
   124
}
slouken@2947
   125
slouken@2947
   126
#endif /* SDL_AUDIO_DRIVER_FUSIONSOUND_DYNAMIC */
slouken@2947
   127
slouken@2947
   128
/* This function waits until it is possible to write a full sound buffer */
slouken@2947
   129
static void
slouken@2947
   130
SDL_FS_WaitDevice(_THIS)
slouken@2947
   131
{
slouken@2947
   132
    this->hidden->stream->Wait(this->hidden->stream,
slouken@2947
   133
                               this->hidden->mixsamples);
slouken@2947
   134
}
slouken@2947
   135
slouken@2947
   136
static void
slouken@2947
   137
SDL_FS_PlayDevice(_THIS)
slouken@2947
   138
{
slouken@2947
   139
    DirectResult ret;
slouken@2947
   140
slouken@2947
   141
    ret = this->hidden->stream->Write(this->hidden->stream,
slouken@2947
   142
                                      this->hidden->mixbuf,
slouken@2947
   143
                                      this->hidden->mixsamples);
slouken@2947
   144
    /* If we couldn't write, assume fatal error for now */
slouken@2947
   145
    if (ret) {
slouken@2947
   146
        this->enabled = 0;
slouken@2947
   147
    }
slouken@2947
   148
#ifdef DEBUG_AUDIO
slouken@2947
   149
    fprintf(stderr, "Wrote %d bytes of audio data\n", this->hidden->mixlen);
slouken@2947
   150
#endif
slouken@2947
   151
}
slouken@2947
   152
slouken@2947
   153
static void
slouken@2947
   154
SDL_FS_WaitDone(_THIS)
slouken@2947
   155
{
slouken@2947
   156
    this->hidden->stream->Wait(this->hidden->stream,
slouken@2947
   157
                               this->hidden->mixsamples * FUSION_BUFFERS);
slouken@2947
   158
}
slouken@2947
   159
slouken@2947
   160
slouken@2947
   161
static Uint8 *
slouken@2947
   162
SDL_FS_GetDeviceBuf(_THIS)
slouken@2947
   163
{
slouken@2947
   164
    return (this->hidden->mixbuf);
slouken@2947
   165
}
slouken@2947
   166
slouken@2947
   167
slouken@2947
   168
static void
slouken@2947
   169
SDL_FS_CloseDevice(_THIS)
slouken@2947
   170
{
slouken@2947
   171
    if (this->hidden != NULL) {
slouken@2947
   172
        if (this->hidden->mixbuf != NULL) {
slouken@2947
   173
            SDL_FreeAudioMem(this->hidden->mixbuf);
slouken@2947
   174
            this->hidden->mixbuf = NULL;
slouken@2947
   175
        }
slouken@2947
   176
        if (this->hidden->stream) {
slouken@2947
   177
            this->hidden->stream->Release(this->hidden->stream);
slouken@2947
   178
            this->hidden->stream = NULL;
slouken@2947
   179
        }
slouken@2947
   180
        if (this->hidden->fs) {
slouken@2947
   181
            this->hidden->fs->Release(this->hidden->fs);
slouken@2947
   182
            this->hidden->fs = NULL;
slouken@2947
   183
        }
slouken@2947
   184
        SDL_free(this->hidden);
slouken@2947
   185
        this->hidden = NULL;
slouken@2947
   186
    }
slouken@2947
   187
}
slouken@2947
   188
slouken@2947
   189
slouken@2947
   190
static int
slouken@2947
   191
SDL_FS_OpenDevice(_THIS, const char *devname, int iscapture)
slouken@2947
   192
{
slouken@2947
   193
    int bytes;
slouken@2947
   194
    SDL_AudioFormat test_format = 0, format = 0;
slouken@2947
   195
    FSSampleFormat fs_format;
slouken@2947
   196
    FSStreamDescription desc;
slouken@2947
   197
    DirectResult ret;
slouken@2947
   198
slouken@2947
   199
    /* Initialize all variables that we clean on shutdown */
slouken@2947
   200
    this->hidden = (struct SDL_PrivateAudioData *)
slouken@2947
   201
        SDL_malloc((sizeof *this->hidden));
slouken@2947
   202
    if (this->hidden == NULL) {
icculus@7038
   203
        return SDL_OutOfMemory();
slouken@2947
   204
    }
slouken@2947
   205
    SDL_memset(this->hidden, 0, (sizeof *this->hidden));
slouken@2947
   206
slouken@2947
   207
    /* Try for a closest match on audio format */
slouken@2947
   208
    for (test_format = SDL_FirstAudioFormat(this->spec.format);
slouken@2947
   209
         !format && test_format;) {
slouken@2947
   210
#ifdef DEBUG_AUDIO
slouken@2947
   211
        fprintf(stderr, "Trying format 0x%4.4x\n", test_format);
slouken@2947
   212
#endif
slouken@2947
   213
        switch (test_format) {
slouken@2947
   214
        case AUDIO_U8:
slouken@2947
   215
            fs_format = FSSF_U8;
slouken@2947
   216
            bytes = 1;
slouken@2947
   217
            format = 1;
slouken@2947
   218
            break;
slouken@2947
   219
        case AUDIO_S16SYS:
slouken@2947
   220
            fs_format = FSSF_S16;
slouken@2947
   221
            bytes = 2;
slouken@2947
   222
            format = 1;
slouken@2947
   223
            break;
slouken@2947
   224
        case AUDIO_S32SYS:
slouken@2947
   225
            fs_format = FSSF_S32;
slouken@2947
   226
            bytes = 4;
slouken@2947
   227
            format = 1;
slouken@2947
   228
            break;
slouken@2947
   229
        case AUDIO_F32SYS:
slouken@2947
   230
            fs_format = FSSF_FLOAT;
slouken@2947
   231
            bytes = 4;
slouken@2947
   232
            format = 1;
slouken@2947
   233
            break;
slouken@2947
   234
        default:
slouken@2947
   235
            format = 0;
slouken@2947
   236
            break;
slouken@2947
   237
        }
slouken@2947
   238
        if (!format) {
slouken@2947
   239
            test_format = SDL_NextAudioFormat();
slouken@2947
   240
        }
slouken@2947
   241
    }
slouken@2947
   242
slouken@2947
   243
    if (format == 0) {
slouken@2947
   244
        SDL_FS_CloseDevice(this);
icculus@7038
   245
        return SDL_SetError("Couldn't find any hardware audio formats");
slouken@2947
   246
    }
slouken@2947
   247
    this->spec.format = test_format;
slouken@2947
   248
slouken@2947
   249
    /* Retrieve the main sound interface. */
slouken@2947
   250
    ret = SDL_NAME(FusionSoundCreate) (&this->hidden->fs);
slouken@2947
   251
    if (ret) {
slouken@2947
   252
        SDL_FS_CloseDevice(this);
icculus@7038
   253
        return SDL_SetError("Unable to initialize FusionSound: %d", ret);
slouken@2947
   254
    }
slouken@2947
   255
slouken@2947
   256
    this->hidden->mixsamples = this->spec.size / bytes / this->spec.channels;
slouken@2947
   257
slouken@2947
   258
    /* Fill stream description. */
slouken@2947
   259
    desc.flags = FSSDF_SAMPLERATE | FSSDF_BUFFERSIZE |
slouken@2947
   260
        FSSDF_CHANNELS | FSSDF_SAMPLEFORMAT | FSSDF_PREBUFFER;
slouken@2947
   261
    desc.samplerate = this->spec.freq;
slouken@2947
   262
    desc.buffersize = this->spec.size * FUSION_BUFFERS;
slouken@2947
   263
    desc.channels = this->spec.channels;
slouken@2947
   264
    desc.prebuffer = 10;
slouken@2947
   265
    desc.sampleformat = fs_format;
slouken@2947
   266
slouken@2947
   267
    ret =
slouken@2947
   268
        this->hidden->fs->CreateStream(this->hidden->fs, &desc,
slouken@2947
   269
                                       &this->hidden->stream);
slouken@2947
   270
    if (ret) {
slouken@2947
   271
        SDL_FS_CloseDevice(this);
icculus@7038
   272
        return SDL_SetError("Unable to create FusionSoundStream: %d", ret);
slouken@2947
   273
    }
slouken@2947
   274
slouken@2947
   275
    /* See what we got */
slouken@2947
   276
    desc.flags = FSSDF_SAMPLERATE | FSSDF_BUFFERSIZE |
slouken@2947
   277
        FSSDF_CHANNELS | FSSDF_SAMPLEFORMAT;
slouken@2947
   278
    ret = this->hidden->stream->GetDescription(this->hidden->stream, &desc);
slouken@2947
   279
slouken@2947
   280
    this->spec.freq = desc.samplerate;
slouken@2947
   281
    this->spec.size =
slouken@2947
   282
        desc.buffersize / FUSION_BUFFERS * bytes * desc.channels;
slouken@2947
   283
    this->spec.channels = desc.channels;
slouken@2947
   284
slouken@2947
   285
    /* Calculate the final parameters for this audio specification */
slouken@2947
   286
    SDL_CalculateAudioSpec(&this->spec);
slouken@2947
   287
slouken@2947
   288
    /* Allocate mixing buffer */
slouken@2947
   289
    this->hidden->mixlen = this->spec.size;
slouken@2947
   290
    this->hidden->mixbuf = (Uint8 *) SDL_AllocAudioMem(this->hidden->mixlen);
slouken@2947
   291
    if (this->hidden->mixbuf == NULL) {
slouken@2947
   292
        SDL_FS_CloseDevice(this);
icculus@7038
   293
        return SDL_OutOfMemory();
slouken@2947
   294
    }
slouken@2947
   295
    SDL_memset(this->hidden->mixbuf, this->spec.silence, this->spec.size);
slouken@2947
   296
slouken@2947
   297
    /* We're ready to rock and roll. :-) */
icculus@7038
   298
    return 0;
slouken@2947
   299
}
slouken@2947
   300
slouken@2947
   301
slouken@2947
   302
static void
slouken@2947
   303
SDL_FS_Deinitialize(void)
slouken@2947
   304
{
slouken@2947
   305
    UnloadFusionSoundLibrary();
slouken@2947
   306
}
slouken@2947
   307
slouken@2947
   308
slouken@2947
   309
static int
slouken@2947
   310
SDL_FS_Init(SDL_AudioDriverImpl * impl)
slouken@2947
   311
{
slouken@2947
   312
    if (LoadFusionSoundLibrary() < 0) {
slouken@2947
   313
        return 0;
slouken@2947
   314
    } else {
slouken@2947
   315
        DirectResult ret;
slouken@2947
   316
slouken@2947
   317
        ret = SDL_NAME(FusionSoundInit) (NULL, NULL);
slouken@2947
   318
        if (ret) {
slouken@2947
   319
            UnloadFusionSoundLibrary();
slouken@2947
   320
            SDL_SetError
slouken@2947
   321
                ("FusionSound: SDL_FS_init failed (FusionSoundInit: %d)",
slouken@2947
   322
                 ret);
slouken@2947
   323
            return 0;
slouken@2947
   324
        }
slouken@2947
   325
    }
slouken@2947
   326
slouken@2947
   327
    /* Set the function pointers */
slouken@2947
   328
    impl->OpenDevice = SDL_FS_OpenDevice;
slouken@2947
   329
    impl->PlayDevice = SDL_FS_PlayDevice;
slouken@2947
   330
    impl->WaitDevice = SDL_FS_WaitDevice;
slouken@2947
   331
    impl->GetDeviceBuf = SDL_FS_GetDeviceBuf;
slouken@2947
   332
    impl->CloseDevice = SDL_FS_CloseDevice;
slouken@2947
   333
    impl->WaitDone = SDL_FS_WaitDone;
slouken@2947
   334
    impl->Deinitialize = SDL_FS_Deinitialize;
slouken@2947
   335
    impl->OnlyHasDefaultOutputDevice = 1;
slouken@2947
   336
icculus@3699
   337
    return 1;   /* this audio target is available. */
slouken@2947
   338
}
slouken@2947
   339
slouken@2947
   340
slouken@2947
   341
AudioBootStrap FUSIONSOUND_bootstrap = {
icculus@5594
   342
    "fusionsound", "FusionSound", SDL_FS_Init, 0
slouken@2947
   343
};
slouken@2947
   344
slouken@6044
   345
#endif /* SDL_AUDIO_DRIVER_FUSIONSOUND */
slouken@6044
   346
slouken@2947
   347
/* vi: set ts=4 sw=4 expandtab: */