src/audio/fusionsound/SDL_fsaudio.c
author Sam Lantinga
Fri, 08 Apr 2011 13:03:26 -0700
changeset 5535 96594ac5fd1a
parent 5262 b530ef003506
child 5594 e741303e08a8
permissions -rw-r--r--
SDL 1.3 is now under the zlib license.
slouken@2947
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@5535
     3
  Copyright (C) 1997-2011 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@2947
    23
/* Allow access to a raw mixing buffer */
slouken@2947
    24
slouken@2947
    25
#ifdef HAVE_SIGNAL_H
slouken@2947
    26
#include <signal.h>
slouken@2947
    27
#endif
slouken@2947
    28
#include <unistd.h>
slouken@2947
    29
slouken@2947
    30
#include "SDL_timer.h"
slouken@2947
    31
#include "SDL_audio.h"
slouken@2947
    32
#include "../SDL_audiomem.h"
slouken@2947
    33
#include "../SDL_audio_c.h"
slouken@2947
    34
#include "SDL_fsaudio.h"
slouken@2947
    35
couriersud@3230
    36
#include <fusionsound/fusionsound_version.h>
couriersud@3230
    37
slouken@2947
    38
//#define SDL_AUDIO_DRIVER_FUSIONSOUND_DYNAMIC "libfusionsound.so"
slouken@2947
    39
slouken@2947
    40
#ifdef SDL_AUDIO_DRIVER_FUSIONSOUND_DYNAMIC
slouken@2947
    41
#include "SDL_name.h"
slouken@2947
    42
#include "SDL_loadso.h"
slouken@2947
    43
#else
slouken@2947
    44
#define SDL_NAME(X)	X
slouken@2947
    45
#endif
slouken@2947
    46
couriersud@3230
    47
#if (FUSIONSOUND_MAJOR_VERSION == 1) && (FUSIONSOUND_MINOR_VERSION < 1)
couriersud@3230
    48
typedef DFBResult DirectResult;
couriersud@3230
    49
#endif
couriersud@3230
    50
slouken@2947
    51
/* The tag name used by fusionsoundc audio */
slouken@2947
    52
#define SDL_FS_DRIVER_NAME         "fusionsound"
slouken@2947
    53
/* Buffers to use - more than 2 gives a lot of latency */
slouken@2947
    54
#define FUSION_BUFFERS				(2)
slouken@2947
    55
slouken@2947
    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) {
slouken@2947
   203
        SDL_OutOfMemory();
slouken@2947
   204
        return 0;
slouken@2947
   205
    }
slouken@2947
   206
    SDL_memset(this->hidden, 0, (sizeof *this->hidden));
slouken@2947
   207
slouken@2947
   208
    /* Try for a closest match on audio format */
slouken@2947
   209
    for (test_format = SDL_FirstAudioFormat(this->spec.format);
slouken@2947
   210
         !format && test_format;) {
slouken@2947
   211
#ifdef DEBUG_AUDIO
slouken@2947
   212
        fprintf(stderr, "Trying format 0x%4.4x\n", test_format);
slouken@2947
   213
#endif
slouken@2947
   214
        switch (test_format) {
slouken@2947
   215
        case AUDIO_U8:
slouken@2947
   216
            fs_format = FSSF_U8;
slouken@2947
   217
            bytes = 1;
slouken@2947
   218
            format = 1;
slouken@2947
   219
            break;
slouken@2947
   220
        case AUDIO_S16SYS:
slouken@2947
   221
            fs_format = FSSF_S16;
slouken@2947
   222
            bytes = 2;
slouken@2947
   223
            format = 1;
slouken@2947
   224
            break;
slouken@2947
   225
        case AUDIO_S32SYS:
slouken@2947
   226
            fs_format = FSSF_S32;
slouken@2947
   227
            bytes = 4;
slouken@2947
   228
            format = 1;
slouken@2947
   229
            break;
slouken@2947
   230
        case AUDIO_F32SYS:
slouken@2947
   231
            fs_format = FSSF_FLOAT;
slouken@2947
   232
            bytes = 4;
slouken@2947
   233
            format = 1;
slouken@2947
   234
            break;
slouken@2947
   235
        default:
slouken@2947
   236
            format = 0;
slouken@2947
   237
            break;
slouken@2947
   238
        }
slouken@2947
   239
        if (!format) {
slouken@2947
   240
            test_format = SDL_NextAudioFormat();
slouken@2947
   241
        }
slouken@2947
   242
    }
slouken@2947
   243
slouken@2947
   244
    if (format == 0) {
slouken@2947
   245
        SDL_FS_CloseDevice(this);
slouken@2947
   246
        SDL_SetError("Couldn't find any hardware audio formats");
slouken@2947
   247
        return 0;
slouken@2947
   248
    }
slouken@2947
   249
    this->spec.format = test_format;
slouken@2947
   250
slouken@2947
   251
    /* Retrieve the main sound interface. */
slouken@2947
   252
    ret = SDL_NAME(FusionSoundCreate) (&this->hidden->fs);
slouken@2947
   253
    if (ret) {
slouken@2947
   254
        SDL_FS_CloseDevice(this);
slouken@2947
   255
        SDL_SetError("Unable to initialize FusionSound: %d", ret);
slouken@2947
   256
        return 0;
slouken@2947
   257
    }
slouken@2947
   258
slouken@2947
   259
    this->hidden->mixsamples = this->spec.size / bytes / this->spec.channels;
slouken@2947
   260
slouken@2947
   261
    /* Fill stream description. */
slouken@2947
   262
    desc.flags = FSSDF_SAMPLERATE | FSSDF_BUFFERSIZE |
slouken@2947
   263
        FSSDF_CHANNELS | FSSDF_SAMPLEFORMAT | FSSDF_PREBUFFER;
slouken@2947
   264
    desc.samplerate = this->spec.freq;
slouken@2947
   265
    desc.buffersize = this->spec.size * FUSION_BUFFERS;
slouken@2947
   266
    desc.channels = this->spec.channels;
slouken@2947
   267
    desc.prebuffer = 10;
slouken@2947
   268
    desc.sampleformat = fs_format;
slouken@2947
   269
slouken@2947
   270
    ret =
slouken@2947
   271
        this->hidden->fs->CreateStream(this->hidden->fs, &desc,
slouken@2947
   272
                                       &this->hidden->stream);
slouken@2947
   273
    if (ret) {
slouken@2947
   274
        SDL_FS_CloseDevice(this);
slouken@2947
   275
        SDL_SetError("Unable to create FusionSoundStream: %d", ret);
slouken@2947
   276
        return 0;
slouken@2947
   277
    }
slouken@2947
   278
slouken@2947
   279
    /* See what we got */
slouken@2947
   280
    desc.flags = FSSDF_SAMPLERATE | FSSDF_BUFFERSIZE |
slouken@2947
   281
        FSSDF_CHANNELS | FSSDF_SAMPLEFORMAT;
slouken@2947
   282
    ret = this->hidden->stream->GetDescription(this->hidden->stream, &desc);
slouken@2947
   283
slouken@2947
   284
    this->spec.freq = desc.samplerate;
slouken@2947
   285
    this->spec.size =
slouken@2947
   286
        desc.buffersize / FUSION_BUFFERS * bytes * desc.channels;
slouken@2947
   287
    this->spec.channels = desc.channels;
slouken@2947
   288
slouken@2947
   289
    /* Calculate the final parameters for this audio specification */
slouken@2947
   290
    SDL_CalculateAudioSpec(&this->spec);
slouken@2947
   291
slouken@2947
   292
    /* Allocate mixing buffer */
slouken@2947
   293
    this->hidden->mixlen = this->spec.size;
slouken@2947
   294
    this->hidden->mixbuf = (Uint8 *) SDL_AllocAudioMem(this->hidden->mixlen);
slouken@2947
   295
    if (this->hidden->mixbuf == NULL) {
slouken@2947
   296
        SDL_FS_CloseDevice(this);
slouken@2947
   297
        SDL_OutOfMemory();
slouken@2947
   298
        return 0;
slouken@2947
   299
    }
slouken@2947
   300
    SDL_memset(this->hidden->mixbuf, this->spec.silence, this->spec.size);
slouken@2947
   301
slouken@2947
   302
    /* We're ready to rock and roll. :-) */
slouken@2947
   303
    return 1;
slouken@2947
   304
}
slouken@2947
   305
slouken@2947
   306
slouken@2947
   307
static void
slouken@2947
   308
SDL_FS_Deinitialize(void)
slouken@2947
   309
{
slouken@2947
   310
    UnloadFusionSoundLibrary();
slouken@2947
   311
}
slouken@2947
   312
slouken@2947
   313
slouken@2947
   314
static int
slouken@2947
   315
SDL_FS_Init(SDL_AudioDriverImpl * impl)
slouken@2947
   316
{
slouken@2947
   317
    if (LoadFusionSoundLibrary() < 0) {
slouken@2947
   318
        return 0;
slouken@2947
   319
    } else {
slouken@2947
   320
        DirectResult ret;
slouken@2947
   321
slouken@2947
   322
        ret = SDL_NAME(FusionSoundInit) (NULL, NULL);
slouken@2947
   323
        if (ret) {
slouken@2947
   324
            UnloadFusionSoundLibrary();
slouken@2947
   325
            SDL_SetError
slouken@2947
   326
                ("FusionSound: SDL_FS_init failed (FusionSoundInit: %d)",
slouken@2947
   327
                 ret);
slouken@2947
   328
            return 0;
slouken@2947
   329
        }
slouken@2947
   330
    }
slouken@2947
   331
slouken@2947
   332
    /* Set the function pointers */
slouken@2947
   333
    impl->OpenDevice = SDL_FS_OpenDevice;
slouken@2947
   334
    impl->PlayDevice = SDL_FS_PlayDevice;
slouken@2947
   335
    impl->WaitDevice = SDL_FS_WaitDevice;
slouken@2947
   336
    impl->GetDeviceBuf = SDL_FS_GetDeviceBuf;
slouken@2947
   337
    impl->CloseDevice = SDL_FS_CloseDevice;
slouken@2947
   338
    impl->WaitDone = SDL_FS_WaitDone;
slouken@2947
   339
    impl->Deinitialize = SDL_FS_Deinitialize;
slouken@2947
   340
    impl->OnlyHasDefaultOutputDevice = 1;
slouken@2947
   341
icculus@3699
   342
    return 1;   /* this audio target is available. */
slouken@2947
   343
}
slouken@2947
   344
slouken@2947
   345
slouken@2947
   346
AudioBootStrap FUSIONSOUND_bootstrap = {
slouken@2947
   347
    SDL_FS_DRIVER_NAME, "FusionSound", SDL_FS_Init, 0
slouken@2947
   348
};
slouken@2947
   349
slouken@2947
   350
/* vi: set ts=4 sw=4 expandtab: */