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