src/audio/fusionsound/SDL_fsaudio.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 02 Jan 2016 10:10:34 -0800
changeset 9998 f67cf37e9cd4
parent 9619 b94b6d0bff0f
child 10255 9530fc07da6c
permissions -rw-r--r--
Updated copyright to 2016
slouken@2947
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@9998
     3
  Copyright (C) 1997-2016 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
*/
icculus@8093
    21
#include "../../SDL_internal.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@7191
    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@7191
    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@7191
    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) {
icculus@9394
   146
        SDL_OpenedAudioDeviceDisconnected(this);
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@7719
   172
        SDL_FreeAudioMem(this->hidden->mixbuf);
slouken@7719
   173
        this->hidden->mixbuf = NULL;
slouken@2947
   174
        if (this->hidden->stream) {
slouken@2947
   175
            this->hidden->stream->Release(this->hidden->stream);
slouken@2947
   176
            this->hidden->stream = NULL;
slouken@2947
   177
        }
slouken@2947
   178
        if (this->hidden->fs) {
slouken@2947
   179
            this->hidden->fs->Release(this->hidden->fs);
slouken@2947
   180
            this->hidden->fs = NULL;
slouken@2947
   181
        }
slouken@2947
   182
        SDL_free(this->hidden);
slouken@2947
   183
        this->hidden = NULL;
slouken@2947
   184
    }
slouken@2947
   185
}
slouken@2947
   186
slouken@2947
   187
slouken@2947
   188
static int
icculus@9394
   189
SDL_FS_OpenDevice(_THIS, void *handle, const char *devname, int iscapture)
slouken@2947
   190
{
slouken@2947
   191
    int bytes;
slouken@2947
   192
    SDL_AudioFormat test_format = 0, format = 0;
slouken@2947
   193
    FSSampleFormat fs_format;
slouken@2947
   194
    FSStreamDescription desc;
slouken@2947
   195
    DirectResult ret;
slouken@2947
   196
slouken@2947
   197
    /* Initialize all variables that we clean on shutdown */
slouken@2947
   198
    this->hidden = (struct SDL_PrivateAudioData *)
slouken@2947
   199
        SDL_malloc((sizeof *this->hidden));
slouken@2947
   200
    if (this->hidden == NULL) {
icculus@7038
   201
        return SDL_OutOfMemory();
slouken@2947
   202
    }
slouken@2947
   203
    SDL_memset(this->hidden, 0, (sizeof *this->hidden));
slouken@2947
   204
slouken@2947
   205
    /* Try for a closest match on audio format */
slouken@2947
   206
    for (test_format = SDL_FirstAudioFormat(this->spec.format);
slouken@2947
   207
         !format && test_format;) {
slouken@2947
   208
#ifdef DEBUG_AUDIO
slouken@2947
   209
        fprintf(stderr, "Trying format 0x%4.4x\n", test_format);
slouken@2947
   210
#endif
slouken@2947
   211
        switch (test_format) {
slouken@2947
   212
        case AUDIO_U8:
slouken@2947
   213
            fs_format = FSSF_U8;
slouken@2947
   214
            bytes = 1;
slouken@2947
   215
            format = 1;
slouken@2947
   216
            break;
slouken@2947
   217
        case AUDIO_S16SYS:
slouken@2947
   218
            fs_format = FSSF_S16;
slouken@2947
   219
            bytes = 2;
slouken@2947
   220
            format = 1;
slouken@2947
   221
            break;
slouken@2947
   222
        case AUDIO_S32SYS:
slouken@2947
   223
            fs_format = FSSF_S32;
slouken@2947
   224
            bytes = 4;
slouken@2947
   225
            format = 1;
slouken@2947
   226
            break;
slouken@2947
   227
        case AUDIO_F32SYS:
slouken@2947
   228
            fs_format = FSSF_FLOAT;
slouken@2947
   229
            bytes = 4;
slouken@2947
   230
            format = 1;
slouken@2947
   231
            break;
slouken@2947
   232
        default:
slouken@2947
   233
            format = 0;
slouken@2947
   234
            break;
slouken@2947
   235
        }
slouken@2947
   236
        if (!format) {
slouken@2947
   237
            test_format = SDL_NextAudioFormat();
slouken@2947
   238
        }
slouken@2947
   239
    }
slouken@2947
   240
slouken@2947
   241
    if (format == 0) {
slouken@2947
   242
        SDL_FS_CloseDevice(this);
icculus@7038
   243
        return SDL_SetError("Couldn't find any hardware audio formats");
slouken@2947
   244
    }
slouken@2947
   245
    this->spec.format = test_format;
slouken@2947
   246
slouken@2947
   247
    /* Retrieve the main sound interface. */
slouken@2947
   248
    ret = SDL_NAME(FusionSoundCreate) (&this->hidden->fs);
slouken@2947
   249
    if (ret) {
slouken@2947
   250
        SDL_FS_CloseDevice(this);
icculus@7038
   251
        return SDL_SetError("Unable to initialize FusionSound: %d", ret);
slouken@2947
   252
    }
slouken@2947
   253
slouken@2947
   254
    this->hidden->mixsamples = this->spec.size / bytes / this->spec.channels;
slouken@2947
   255
slouken@2947
   256
    /* Fill stream description. */
slouken@2947
   257
    desc.flags = FSSDF_SAMPLERATE | FSSDF_BUFFERSIZE |
slouken@2947
   258
        FSSDF_CHANNELS | FSSDF_SAMPLEFORMAT | FSSDF_PREBUFFER;
slouken@2947
   259
    desc.samplerate = this->spec.freq;
slouken@2947
   260
    desc.buffersize = this->spec.size * FUSION_BUFFERS;
slouken@2947
   261
    desc.channels = this->spec.channels;
slouken@2947
   262
    desc.prebuffer = 10;
slouken@2947
   263
    desc.sampleformat = fs_format;
slouken@2947
   264
slouken@2947
   265
    ret =
slouken@2947
   266
        this->hidden->fs->CreateStream(this->hidden->fs, &desc,
slouken@2947
   267
                                       &this->hidden->stream);
slouken@2947
   268
    if (ret) {
slouken@2947
   269
        SDL_FS_CloseDevice(this);
icculus@7038
   270
        return SDL_SetError("Unable to create FusionSoundStream: %d", ret);
slouken@2947
   271
    }
slouken@2947
   272
slouken@2947
   273
    /* See what we got */
slouken@2947
   274
    desc.flags = FSSDF_SAMPLERATE | FSSDF_BUFFERSIZE |
slouken@2947
   275
        FSSDF_CHANNELS | FSSDF_SAMPLEFORMAT;
slouken@2947
   276
    ret = this->hidden->stream->GetDescription(this->hidden->stream, &desc);
slouken@2947
   277
slouken@2947
   278
    this->spec.freq = desc.samplerate;
slouken@2947
   279
    this->spec.size =
slouken@2947
   280
        desc.buffersize / FUSION_BUFFERS * bytes * desc.channels;
slouken@2947
   281
    this->spec.channels = desc.channels;
slouken@2947
   282
slouken@2947
   283
    /* Calculate the final parameters for this audio specification */
slouken@2947
   284
    SDL_CalculateAudioSpec(&this->spec);
slouken@2947
   285
slouken@2947
   286
    /* Allocate mixing buffer */
slouken@2947
   287
    this->hidden->mixlen = this->spec.size;
slouken@2947
   288
    this->hidden->mixbuf = (Uint8 *) SDL_AllocAudioMem(this->hidden->mixlen);
slouken@2947
   289
    if (this->hidden->mixbuf == NULL) {
slouken@2947
   290
        SDL_FS_CloseDevice(this);
icculus@7038
   291
        return SDL_OutOfMemory();
slouken@2947
   292
    }
slouken@2947
   293
    SDL_memset(this->hidden->mixbuf, this->spec.silence, this->spec.size);
slouken@2947
   294
slouken@2947
   295
    /* We're ready to rock and roll. :-) */
icculus@7038
   296
    return 0;
slouken@2947
   297
}
slouken@2947
   298
slouken@2947
   299
slouken@2947
   300
static void
slouken@2947
   301
SDL_FS_Deinitialize(void)
slouken@2947
   302
{
slouken@2947
   303
    UnloadFusionSoundLibrary();
slouken@2947
   304
}
slouken@2947
   305
slouken@2947
   306
slouken@2947
   307
static int
slouken@2947
   308
SDL_FS_Init(SDL_AudioDriverImpl * impl)
slouken@2947
   309
{
slouken@2947
   310
    if (LoadFusionSoundLibrary() < 0) {
slouken@2947
   311
        return 0;
slouken@2947
   312
    } else {
slouken@2947
   313
        DirectResult ret;
slouken@2947
   314
slouken@2947
   315
        ret = SDL_NAME(FusionSoundInit) (NULL, NULL);
slouken@2947
   316
        if (ret) {
slouken@2947
   317
            UnloadFusionSoundLibrary();
slouken@2947
   318
            SDL_SetError
slouken@2947
   319
                ("FusionSound: SDL_FS_init failed (FusionSoundInit: %d)",
slouken@2947
   320
                 ret);
slouken@2947
   321
            return 0;
slouken@2947
   322
        }
slouken@2947
   323
    }
slouken@2947
   324
slouken@2947
   325
    /* Set the function pointers */
slouken@2947
   326
    impl->OpenDevice = SDL_FS_OpenDevice;
slouken@2947
   327
    impl->PlayDevice = SDL_FS_PlayDevice;
slouken@2947
   328
    impl->WaitDevice = SDL_FS_WaitDevice;
slouken@2947
   329
    impl->GetDeviceBuf = SDL_FS_GetDeviceBuf;
slouken@2947
   330
    impl->CloseDevice = SDL_FS_CloseDevice;
slouken@2947
   331
    impl->WaitDone = SDL_FS_WaitDone;
slouken@2947
   332
    impl->Deinitialize = SDL_FS_Deinitialize;
slouken@2947
   333
    impl->OnlyHasDefaultOutputDevice = 1;
slouken@2947
   334
icculus@3699
   335
    return 1;   /* this audio target is available. */
slouken@2947
   336
}
slouken@2947
   337
slouken@2947
   338
slouken@2947
   339
AudioBootStrap FUSIONSOUND_bootstrap = {
icculus@5594
   340
    "fusionsound", "FusionSound", SDL_FS_Init, 0
slouken@2947
   341
};
slouken@2947
   342
slouken@6044
   343
#endif /* SDL_AUDIO_DRIVER_FUSIONSOUND */
slouken@6044
   344
slouken@2947
   345
/* vi: set ts=4 sw=4 expandtab: */