src/audio/fusionsound/SDL_fsaudio.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 03 Jan 2018 10:03:25 -0800
changeset 11811 5d94cb6b24d3
parent 10737 3406a0f8b041
child 12503 806492103856
permissions -rw-r--r--
Updated copyright for 2018
slouken@2947
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@11811
     3
  Copyright (C) 1997-2018 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
icculus@10255
    25
/* !!! FIXME: why is this is SDL_FS_* instead of FUSIONSOUND_*? */
icculus@10255
    26
slouken@2947
    27
/* Allow access to a raw mixing buffer */
slouken@2947
    28
slouken@2947
    29
#ifdef HAVE_SIGNAL_H
slouken@2947
    30
#include <signal.h>
slouken@2947
    31
#endif
slouken@2947
    32
#include <unistd.h>
slouken@2947
    33
slouken@2947
    34
#include "SDL_timer.h"
slouken@2947
    35
#include "SDL_audio.h"
slouken@2947
    36
#include "../SDL_audio_c.h"
slouken@2947
    37
#include "SDL_fsaudio.h"
slouken@2947
    38
couriersud@3230
    39
#include <fusionsound/fusionsound_version.h>
couriersud@3230
    40
slouken@7191
    41
/* #define SDL_AUDIO_DRIVER_FUSIONSOUND_DYNAMIC "libfusionsound.so" */
slouken@2947
    42
icculus@6046
    43
#ifdef SDL_AUDIO_DRIVER_FUSIONSOUND_DYNAMIC
slouken@2947
    44
#include "SDL_name.h"
slouken@2947
    45
#include "SDL_loadso.h"
slouken@2947
    46
#else
slouken@7191
    47
#define SDL_NAME(X) X
slouken@2947
    48
#endif
slouken@2947
    49
couriersud@3230
    50
#if (FUSIONSOUND_MAJOR_VERSION == 1) && (FUSIONSOUND_MINOR_VERSION < 1)
couriersud@3230
    51
typedef DFBResult DirectResult;
couriersud@3230
    52
#endif
couriersud@3230
    53
slouken@2947
    54
/* Buffers to use - more than 2 gives a lot of latency */
slouken@7191
    55
#define FUSION_BUFFERS              (2)
slouken@2947
    56
icculus@6046
    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) {
icculus@9394
   147
        SDL_OpenedAudioDeviceDisconnected(this);
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
slouken@2947
   155
static Uint8 *
slouken@2947
   156
SDL_FS_GetDeviceBuf(_THIS)
slouken@2947
   157
{
slouken@2947
   158
    return (this->hidden->mixbuf);
slouken@2947
   159
}
slouken@2947
   160
slouken@2947
   161
slouken@2947
   162
static void
slouken@2947
   163
SDL_FS_CloseDevice(_THIS)
slouken@2947
   164
{
icculus@10255
   165
    if (this->hidden->stream) {
icculus@10255
   166
        this->hidden->stream->Release(this->hidden->stream);
slouken@2947
   167
    }
icculus@10255
   168
    if (this->hidden->fs) {
icculus@10255
   169
        this->hidden->fs->Release(this->hidden->fs);
icculus@10255
   170
    }
icculus@10256
   171
    SDL_free(this->hidden->mixbuf);
icculus@10255
   172
    SDL_free(this->hidden);
slouken@2947
   173
}
slouken@2947
   174
slouken@2947
   175
slouken@2947
   176
static int
icculus@9394
   177
SDL_FS_OpenDevice(_THIS, void *handle, const char *devname, int iscapture)
slouken@2947
   178
{
slouken@2947
   179
    int bytes;
slouken@2947
   180
    SDL_AudioFormat test_format = 0, format = 0;
slouken@2947
   181
    FSSampleFormat fs_format;
slouken@2947
   182
    FSStreamDescription desc;
slouken@2947
   183
    DirectResult ret;
slouken@2947
   184
slouken@2947
   185
    /* Initialize all variables that we clean on shutdown */
slouken@2947
   186
    this->hidden = (struct SDL_PrivateAudioData *)
slouken@2947
   187
        SDL_malloc((sizeof *this->hidden));
slouken@2947
   188
    if (this->hidden == NULL) {
icculus@7038
   189
        return SDL_OutOfMemory();
slouken@2947
   190
    }
icculus@10257
   191
    SDL_zerop(this->hidden);
slouken@2947
   192
slouken@2947
   193
    /* Try for a closest match on audio format */
slouken@2947
   194
    for (test_format = SDL_FirstAudioFormat(this->spec.format);
slouken@2947
   195
         !format && test_format;) {
slouken@2947
   196
#ifdef DEBUG_AUDIO
slouken@2947
   197
        fprintf(stderr, "Trying format 0x%4.4x\n", test_format);
slouken@2947
   198
#endif
slouken@2947
   199
        switch (test_format) {
slouken@2947
   200
        case AUDIO_U8:
slouken@2947
   201
            fs_format = FSSF_U8;
slouken@2947
   202
            bytes = 1;
slouken@2947
   203
            format = 1;
slouken@2947
   204
            break;
slouken@2947
   205
        case AUDIO_S16SYS:
slouken@2947
   206
            fs_format = FSSF_S16;
slouken@2947
   207
            bytes = 2;
slouken@2947
   208
            format = 1;
slouken@2947
   209
            break;
slouken@2947
   210
        case AUDIO_S32SYS:
slouken@2947
   211
            fs_format = FSSF_S32;
slouken@2947
   212
            bytes = 4;
slouken@2947
   213
            format = 1;
slouken@2947
   214
            break;
slouken@2947
   215
        case AUDIO_F32SYS:
slouken@2947
   216
            fs_format = FSSF_FLOAT;
slouken@2947
   217
            bytes = 4;
slouken@2947
   218
            format = 1;
slouken@2947
   219
            break;
slouken@2947
   220
        default:
slouken@2947
   221
            format = 0;
slouken@2947
   222
            break;
slouken@2947
   223
        }
slouken@2947
   224
        if (!format) {
slouken@2947
   225
            test_format = SDL_NextAudioFormat();
slouken@2947
   226
        }
slouken@2947
   227
    }
slouken@2947
   228
slouken@2947
   229
    if (format == 0) {
icculus@7038
   230
        return SDL_SetError("Couldn't find any hardware audio formats");
slouken@2947
   231
    }
slouken@2947
   232
    this->spec.format = test_format;
slouken@2947
   233
slouken@2947
   234
    /* Retrieve the main sound interface. */
slouken@2947
   235
    ret = SDL_NAME(FusionSoundCreate) (&this->hidden->fs);
slouken@2947
   236
    if (ret) {
icculus@7038
   237
        return SDL_SetError("Unable to initialize FusionSound: %d", ret);
slouken@2947
   238
    }
slouken@2947
   239
slouken@2947
   240
    this->hidden->mixsamples = this->spec.size / bytes / this->spec.channels;
slouken@2947
   241
slouken@2947
   242
    /* Fill stream description. */
slouken@2947
   243
    desc.flags = FSSDF_SAMPLERATE | FSSDF_BUFFERSIZE |
slouken@2947
   244
        FSSDF_CHANNELS | FSSDF_SAMPLEFORMAT | FSSDF_PREBUFFER;
slouken@2947
   245
    desc.samplerate = this->spec.freq;
slouken@2947
   246
    desc.buffersize = this->spec.size * FUSION_BUFFERS;
slouken@2947
   247
    desc.channels = this->spec.channels;
slouken@2947
   248
    desc.prebuffer = 10;
slouken@2947
   249
    desc.sampleformat = fs_format;
slouken@2947
   250
slouken@2947
   251
    ret =
slouken@2947
   252
        this->hidden->fs->CreateStream(this->hidden->fs, &desc,
slouken@2947
   253
                                       &this->hidden->stream);
slouken@2947
   254
    if (ret) {
icculus@7038
   255
        return SDL_SetError("Unable to create FusionSoundStream: %d", ret);
slouken@2947
   256
    }
slouken@2947
   257
slouken@2947
   258
    /* See what we got */
slouken@2947
   259
    desc.flags = FSSDF_SAMPLERATE | FSSDF_BUFFERSIZE |
slouken@2947
   260
        FSSDF_CHANNELS | FSSDF_SAMPLEFORMAT;
slouken@2947
   261
    ret = this->hidden->stream->GetDescription(this->hidden->stream, &desc);
slouken@2947
   262
slouken@2947
   263
    this->spec.freq = desc.samplerate;
slouken@2947
   264
    this->spec.size =
slouken@2947
   265
        desc.buffersize / FUSION_BUFFERS * bytes * desc.channels;
slouken@2947
   266
    this->spec.channels = desc.channels;
slouken@2947
   267
slouken@2947
   268
    /* Calculate the final parameters for this audio specification */
slouken@2947
   269
    SDL_CalculateAudioSpec(&this->spec);
slouken@2947
   270
slouken@2947
   271
    /* Allocate mixing buffer */
slouken@2947
   272
    this->hidden->mixlen = this->spec.size;
icculus@10256
   273
    this->hidden->mixbuf = (Uint8 *) SDL_malloc(this->hidden->mixlen);
slouken@2947
   274
    if (this->hidden->mixbuf == NULL) {
icculus@7038
   275
        return SDL_OutOfMemory();
slouken@2947
   276
    }
slouken@2947
   277
    SDL_memset(this->hidden->mixbuf, this->spec.silence, this->spec.size);
slouken@2947
   278
slouken@2947
   279
    /* We're ready to rock and roll. :-) */
icculus@7038
   280
    return 0;
slouken@2947
   281
}
slouken@2947
   282
slouken@2947
   283
slouken@2947
   284
static void
slouken@2947
   285
SDL_FS_Deinitialize(void)
slouken@2947
   286
{
slouken@2947
   287
    UnloadFusionSoundLibrary();
slouken@2947
   288
}
slouken@2947
   289
slouken@2947
   290
slouken@2947
   291
static int
slouken@2947
   292
SDL_FS_Init(SDL_AudioDriverImpl * impl)
slouken@2947
   293
{
slouken@2947
   294
    if (LoadFusionSoundLibrary() < 0) {
slouken@2947
   295
        return 0;
slouken@2947
   296
    } else {
slouken@2947
   297
        DirectResult ret;
slouken@2947
   298
slouken@2947
   299
        ret = SDL_NAME(FusionSoundInit) (NULL, NULL);
slouken@2947
   300
        if (ret) {
slouken@2947
   301
            UnloadFusionSoundLibrary();
slouken@2947
   302
            SDL_SetError
slouken@2947
   303
                ("FusionSound: SDL_FS_init failed (FusionSoundInit: %d)",
slouken@2947
   304
                 ret);
slouken@2947
   305
            return 0;
slouken@2947
   306
        }
slouken@2947
   307
    }
slouken@2947
   308
slouken@2947
   309
    /* Set the function pointers */
slouken@2947
   310
    impl->OpenDevice = SDL_FS_OpenDevice;
slouken@2947
   311
    impl->PlayDevice = SDL_FS_PlayDevice;
slouken@2947
   312
    impl->WaitDevice = SDL_FS_WaitDevice;
slouken@2947
   313
    impl->GetDeviceBuf = SDL_FS_GetDeviceBuf;
slouken@2947
   314
    impl->CloseDevice = SDL_FS_CloseDevice;
slouken@2947
   315
    impl->Deinitialize = SDL_FS_Deinitialize;
slouken@2947
   316
    impl->OnlyHasDefaultOutputDevice = 1;
slouken@2947
   317
icculus@3699
   318
    return 1;   /* this audio target is available. */
slouken@2947
   319
}
slouken@2947
   320
slouken@2947
   321
slouken@2947
   322
AudioBootStrap FUSIONSOUND_bootstrap = {
icculus@5594
   323
    "fusionsound", "FusionSound", SDL_FS_Init, 0
slouken@2947
   324
};
slouken@2947
   325
slouken@6044
   326
#endif /* SDL_AUDIO_DRIVER_FUSIONSOUND */
slouken@6044
   327
slouken@2947
   328
/* vi: set ts=4 sw=4 expandtab: */