src/audio/arts/SDL_artsaudio.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 31 Oct 2011 05:56:58 -0400
changeset 6044 35448a5ea044
parent 5594 e741303e08a8
child 6046 3b33b82cdbac
permissions -rw-r--r--
Lots of fixes importing SDL source wholesale into a new iOS project
slouken@0
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@5535
     3
  Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
slouken@0
     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@0
     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@0
    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@0
    20
*/
slouken@1402
    21
#include "SDL_config.h"
slouken@0
    22
slouken@6044
    23
#if SDL_AUDIO_DRIVER_ARTS
slouken@6044
    24
slouken@0
    25
/* Allow access to a raw mixing buffer */
slouken@0
    26
icculus@2155
    27
#ifdef HAVE_SIGNAL_H
icculus@2155
    28
#include <signal.h>
icculus@2155
    29
#endif
icculus@2155
    30
#include <unistd.h>
slouken@3068
    31
#include <errno.h>
icculus@2155
    32
slouken@1358
    33
#include "SDL_timer.h"
slouken@0
    34
#include "SDL_audio.h"
slouken@1361
    35
#include "../SDL_audiomem.h"
slouken@1361
    36
#include "../SDL_audio_c.h"
slouken@0
    37
#include "SDL_artsaudio.h"
slouken@0
    38
slouken@6044
    39
#if SDL_AUDIO_DRIVER_ARTS_DYNAMIC
slouken@294
    40
#include "SDL_name.h"
slouken@294
    41
#include "SDL_loadso.h"
slouken@294
    42
#else
slouken@294
    43
#define SDL_NAME(X)	X
slouken@294
    44
#endif
slouken@294
    45
slouken@6044
    46
#if SDL_AUDIO_DRIVER_ARTS_DYNAMIC
slouken@294
    47
slouken@1361
    48
static const char *arts_library = SDL_AUDIO_DRIVER_ARTS_DYNAMIC;
slouken@294
    49
static void *arts_handle = NULL;
slouken@294
    50
icculus@2049
    51
/* !!! FIXME: I hate this SDL_NAME clutter...it makes everything so messy! */
slouken@1895
    52
static int (*SDL_NAME(arts_init)) (void);
slouken@1895
    53
static void (*SDL_NAME(arts_free)) (void);
slouken@1895
    54
static arts_stream_t(*SDL_NAME(arts_play_stream)) (int rate, int bits,
slouken@1895
    55
                                                   int channels,
slouken@1895
    56
                                                   const char *name);
slouken@1895
    57
static int (*SDL_NAME(arts_stream_set)) (arts_stream_t s,
slouken@1895
    58
                                         arts_parameter_t param, int value);
slouken@1895
    59
static int (*SDL_NAME(arts_stream_get)) (arts_stream_t s,
slouken@1895
    60
                                         arts_parameter_t param);
slouken@1895
    61
static int (*SDL_NAME(arts_write)) (arts_stream_t s, const void *buffer,
slouken@1895
    62
                                    int count);
slouken@1895
    63
static void (*SDL_NAME(arts_close_stream)) (arts_stream_t s);
slouken@2141
    64
static int (*SDL_NAME(arts_suspended)) (void);
icculus@2049
    65
static const char *(*SDL_NAME(arts_error_text)) (int errorcode);
slouken@301
    66
icculus@2049
    67
#define SDL_ARTS_SYM(x) { #x, (void **) (char *) &SDL_NAME(x) }
slouken@1895
    68
static struct
slouken@1895
    69
{
slouken@1895
    70
    const char *name;
slouken@1895
    71
    void **func;
slouken@294
    72
} arts_functions[] = {
slouken@2141
    73
/* *INDENT-OFF* */
slouken@2141
    74
    SDL_ARTS_SYM(arts_init),
slouken@2141
    75
    SDL_ARTS_SYM(arts_free),
slouken@2141
    76
    SDL_ARTS_SYM(arts_play_stream),
slouken@2141
    77
    SDL_ARTS_SYM(arts_stream_set),
slouken@2141
    78
    SDL_ARTS_SYM(arts_stream_get),
slouken@2141
    79
    SDL_ARTS_SYM(arts_write),
slouken@2141
    80
    SDL_ARTS_SYM(arts_close_stream),
slouken@2141
    81
    SDL_ARTS_SYM(arts_suspended),
slouken@2141
    82
    SDL_ARTS_SYM(arts_error_text),
slouken@2141
    83
/* *INDENT-ON* */
icculus@2121
    84
};
slouken@2141
    85
icculus@2049
    86
#undef SDL_ARTS_SYM
slouken@294
    87
slouken@1895
    88
static void
slouken@1895
    89
UnloadARTSLibrary()
slouken@294
    90
{
icculus@2049
    91
    if (arts_handle != NULL) {
slouken@1895
    92
        SDL_UnloadObject(arts_handle);
slouken@1895
    93
        arts_handle = NULL;
slouken@1895
    94
    }
slouken@294
    95
}
slouken@294
    96
slouken@1895
    97
static int
slouken@1895
    98
LoadARTSLibrary(void)
slouken@294
    99
{
slouken@1895
   100
    int i, retval = -1;
slouken@294
   101
icculus@2049
   102
    if (arts_handle == NULL) {
icculus@2049
   103
        arts_handle = SDL_LoadObject(arts_library);
icculus@2049
   104
        if (arts_handle != NULL) {
icculus@2049
   105
            retval = 0;
icculus@2049
   106
            for (i = 0; i < SDL_arraysize(arts_functions); ++i) {
icculus@2049
   107
                *arts_functions[i].func =
icculus@2049
   108
                    SDL_LoadFunction(arts_handle, arts_functions[i].name);
icculus@2049
   109
                if (!*arts_functions[i].func) {
icculus@2049
   110
                    retval = -1;
icculus@2049
   111
                    UnloadARTSLibrary();
icculus@2049
   112
                    break;
icculus@2049
   113
                }
slouken@1895
   114
            }
slouken@1895
   115
        }
slouken@1895
   116
    }
icculus@2049
   117
slouken@1895
   118
    return retval;
slouken@294
   119
}
slouken@294
   120
slouken@294
   121
#else
slouken@294
   122
slouken@1895
   123
static void
slouken@1895
   124
UnloadARTSLibrary()
slouken@294
   125
{
slouken@1895
   126
    return;
slouken@294
   127
}
slouken@294
   128
slouken@1895
   129
static int
slouken@1895
   130
LoadARTSLibrary(void)
slouken@294
   131
{
slouken@1895
   132
    return 0;
slouken@294
   133
}
slouken@294
   134
slouken@1361
   135
#endif /* SDL_AUDIO_DRIVER_ARTS_DYNAMIC */
slouken@294
   136
slouken@0
   137
/* This function waits until it is possible to write a full sound buffer */
slouken@1895
   138
static void
icculus@2049
   139
ARTS_WaitDevice(_THIS)
slouken@0
   140
{
slouken@1895
   141
    Sint32 ticks;
slouken@0
   142
slouken@1895
   143
    /* Check to see if the thread-parent process is still alive */
slouken@1895
   144
    {
slouken@1895
   145
        static int cnt = 0;
icculus@2049
   146
        /* Note that this only works with thread implementations
slouken@1895
   147
           that use a different process id for each thread.
slouken@1895
   148
         */
icculus@2049
   149
        /* Check every 10 loops */
icculus@2049
   150
        if (this->hidden->parent && (((++cnt) % 10) == 0)) {
slouken@3068
   151
            if (kill(this->hidden->parent, 0) < 0 && errno == ESRCH) {
slouken@1895
   152
                this->enabled = 0;
slouken@1895
   153
            }
slouken@1895
   154
        }
slouken@1895
   155
    }
slouken@0
   156
slouken@1895
   157
    /* Use timer for general audio synchronization */
slouken@2060
   158
    ticks =
slouken@2060
   159
        ((Sint32) (this->hidden->next_frame - SDL_GetTicks())) - FUDGE_TICKS;
slouken@1895
   160
    if (ticks > 0) {
slouken@1895
   161
        SDL_Delay(ticks);
slouken@1895
   162
    }
slouken@0
   163
}
slouken@0
   164
slouken@1895
   165
static void
icculus@2049
   166
ARTS_PlayDevice(_THIS)
slouken@0
   167
{
slouken@1895
   168
    /* Write the audio data */
slouken@2060
   169
    int written = SDL_NAME(arts_write) (this->hidden->stream,
slouken@2060
   170
                                        this->hidden->mixbuf,
slouken@2060
   171
                                        this->hidden->mixlen);
slouken@0
   172
slouken@1895
   173
    /* If timer synchronization is enabled, set the next write frame */
icculus@2049
   174
    if (this->hidden->frame_ticks) {
icculus@2049
   175
        this->hidden->next_frame += this->hidden->frame_ticks;
slouken@1895
   176
    }
slouken@0
   177
slouken@1895
   178
    /* If we couldn't write, assume fatal error for now */
slouken@1895
   179
    if (written < 0) {
slouken@1895
   180
        this->enabled = 0;
slouken@1895
   181
    }
slouken@0
   182
#ifdef DEBUG_AUDIO
slouken@1895
   183
    fprintf(stderr, "Wrote %d bytes of audio data\n", written);
slouken@0
   184
#endif
slouken@0
   185
}
slouken@0
   186
icculus@2049
   187
static void
icculus@2049
   188
ARTS_WaitDone(_THIS)
slouken@0
   189
{
icculus@2049
   190
    /* !!! FIXME: camp here until buffer drains... SDL_Delay(???); */
slouken@0
   191
}
slouken@0
   192
icculus@2049
   193
icculus@2049
   194
static Uint8 *
icculus@2049
   195
ARTS_GetDeviceBuf(_THIS)
icculus@2049
   196
{
icculus@2049
   197
    return (this->hidden->mixbuf);
icculus@2049
   198
}
icculus@2049
   199
icculus@2049
   200
slouken@1895
   201
static void
icculus@2049
   202
ARTS_CloseDevice(_THIS)
slouken@0
   203
{
icculus@2049
   204
    if (this->hidden != NULL) {
icculus@2049
   205
        if (this->hidden->mixbuf != NULL) {
icculus@2049
   206
            SDL_FreeAudioMem(this->hidden->mixbuf);
icculus@2049
   207
            this->hidden->mixbuf = NULL;
icculus@2049
   208
        }
icculus@2049
   209
        if (this->hidden->stream) {
icculus@2049
   210
            SDL_NAME(arts_close_stream) (this->hidden->stream);
icculus@2049
   211
            this->hidden->stream = 0;
icculus@2049
   212
        }
icculus@2049
   213
        SDL_NAME(arts_free) ();
icculus@2049
   214
        SDL_free(this->hidden);
icculus@2049
   215
        this->hidden = NULL;
slouken@1895
   216
    }
slouken@0
   217
}
slouken@0
   218
icculus@2049
   219
slouken@1895
   220
static int
icculus@2049
   221
ARTS_OpenDevice(_THIS, const char *devname, int iscapture)
slouken@0
   222
{
icculus@2049
   223
    int rc = 0;
icculus@2049
   224
    int bits = 0, frag_spec = 0;
icculus@2049
   225
    SDL_AudioFormat test_format = 0, format = 0;
slouken@0
   226
icculus@2049
   227
    /* Initialize all variables that we clean on shutdown */
icculus@2049
   228
    this->hidden = (struct SDL_PrivateAudioData *)
slouken@2060
   229
        SDL_malloc((sizeof *this->hidden));
icculus@2049
   230
    if (this->hidden == NULL) {
icculus@2049
   231
        SDL_OutOfMemory();
icculus@2049
   232
        return 0;
icculus@2049
   233
    }
icculus@2049
   234
    SDL_memset(this->hidden, 0, (sizeof *this->hidden));
slouken@0
   235
slouken@1895
   236
    /* Try for a closest match on audio format */
icculus@2049
   237
    for (test_format = SDL_FirstAudioFormat(this->spec.format);
slouken@1895
   238
         !format && test_format;) {
slouken@0
   239
#ifdef DEBUG_AUDIO
slouken@1895
   240
        fprintf(stderr, "Trying format 0x%4.4x\n", test_format);
slouken@0
   241
#endif
slouken@1895
   242
        switch (test_format) {
slouken@1895
   243
        case AUDIO_U8:
slouken@1895
   244
            bits = 8;
slouken@1895
   245
            format = 1;
slouken@1895
   246
            break;
slouken@1895
   247
        case AUDIO_S16LSB:
slouken@1895
   248
            bits = 16;
slouken@1895
   249
            format = 1;
slouken@1895
   250
            break;
slouken@1895
   251
        default:
slouken@1895
   252
            format = 0;
slouken@1895
   253
            break;
slouken@1895
   254
        }
slouken@1895
   255
        if (!format) {
slouken@1895
   256
            test_format = SDL_NextAudioFormat();
slouken@1895
   257
        }
slouken@1895
   258
    }
slouken@1895
   259
    if (format == 0) {
icculus@2049
   260
        ARTS_CloseDevice(this);
slouken@1895
   261
        SDL_SetError("Couldn't find any hardware audio formats");
icculus@2049
   262
        return 0;
slouken@1895
   263
    }
icculus@2049
   264
    this->spec.format = test_format;
slouken@0
   265
icculus@2049
   266
    if ((rc = SDL_NAME(arts_init) ()) != 0) {
icculus@2049
   267
        ARTS_CloseDevice(this);
slouken@2060
   268
        SDL_SetError("Unable to initialize ARTS: %s",
slouken@2060
   269
                     SDL_NAME(arts_error_text) (rc));
icculus@2049
   270
        return 0;
slouken@1895
   271
    }
icculus@2121
   272
slouken@2141
   273
    if (!SDL_NAME(arts_suspended) ()) {
icculus@2121
   274
        ARTS_CloseDevice(this);
icculus@2121
   275
        SDL_SetError("ARTS can not open audio device");
icculus@2121
   276
        return 0;
icculus@2121
   277
    }
icculus@2121
   278
slouken@2060
   279
    this->hidden->stream = SDL_NAME(arts_play_stream) (this->spec.freq,
slouken@2060
   280
                                                       bits,
slouken@2060
   281
                                                       this->spec.channels,
slouken@2060
   282
                                                       "SDL");
slouken@0
   283
icculus@2121
   284
    /* Play nothing so we have at least one write (server bug workaround). */
icculus@2121
   285
    SDL_NAME(arts_write) (this->hidden->stream, "", 0);
icculus@2121
   286
slouken@1895
   287
    /* Calculate the final parameters for this audio specification */
icculus@2049
   288
    SDL_CalculateAudioSpec(&this->spec);
slouken@0
   289
slouken@1895
   290
    /* Determine the power of two of the fragment size */
icculus@2049
   291
    for (frag_spec = 0; (0x01 << frag_spec) < this->spec.size; ++frag_spec);
icculus@2049
   292
    if ((0x01 << frag_spec) != this->spec.size) {
icculus@2049
   293
        ARTS_CloseDevice(this);
slouken@1895
   294
        SDL_SetError("Fragment size must be a power of two");
icculus@2049
   295
        return 0;
slouken@1895
   296
    }
slouken@1895
   297
    frag_spec |= 0x00020000;    /* two fragments, for low latency */
slouken@0
   298
slouken@0
   299
#ifdef ARTS_P_PACKET_SETTINGS
icculus@2049
   300
    SDL_NAME(arts_stream_set) (this->hidden->stream,
slouken@2060
   301
                               ARTS_P_PACKET_SETTINGS, frag_spec);
slouken@0
   302
#else
icculus@2049
   303
    SDL_NAME(arts_stream_set) (this->hidden->stream, ARTS_P_PACKET_SIZE,
slouken@1895
   304
                               frag_spec & 0xffff);
icculus@2049
   305
    SDL_NAME(arts_stream_set) (this->hidden->stream, ARTS_P_PACKET_COUNT,
icculus@2049
   306
                               frag_spec >> 16);
slouken@0
   307
#endif
icculus@2049
   308
    this->spec.size = SDL_NAME(arts_stream_get) (this->hidden->stream,
icculus@2049
   309
                                                 ARTS_P_PACKET_SIZE);
slouken@0
   310
slouken@1895
   311
    /* Allocate mixing buffer */
icculus@2049
   312
    this->hidden->mixlen = this->spec.size;
icculus@2049
   313
    this->hidden->mixbuf = (Uint8 *) SDL_AllocAudioMem(this->hidden->mixlen);
icculus@2049
   314
    if (this->hidden->mixbuf == NULL) {
icculus@2049
   315
        ARTS_CloseDevice(this);
icculus@2049
   316
        SDL_OutOfMemory();
icculus@2049
   317
        return 0;
slouken@1895
   318
    }
icculus@2049
   319
    SDL_memset(this->hidden->mixbuf, this->spec.silence, this->spec.size);
slouken@0
   320
slouken@1895
   321
    /* Get the parent process id (we're the parent of the audio thread) */
icculus@2049
   322
    this->hidden->parent = getpid();
slouken@0
   323
slouken@1895
   324
    /* We're ready to rock and roll. :-) */
icculus@2049
   325
    return 1;
icculus@2049
   326
}
icculus@2049
   327
icculus@2049
   328
icculus@2049
   329
static void
icculus@2049
   330
ARTS_Deinitialize(void)
icculus@2049
   331
{
icculus@2049
   332
    UnloadARTSLibrary();
slouken@0
   333
}
slouken@1895
   334
icculus@2049
   335
icculus@2049
   336
static int
slouken@2060
   337
ARTS_Init(SDL_AudioDriverImpl * impl)
icculus@2049
   338
{
icculus@2049
   339
    if (LoadARTSLibrary() < 0) {
icculus@2049
   340
        return 0;
icculus@2049
   341
    } else {
icculus@2049
   342
        if (SDL_NAME(arts_init) () != 0) {
icculus@2049
   343
            UnloadARTSLibrary();
icculus@2049
   344
            SDL_SetError("ARTS: arts_init failed (no audio server?)");
icculus@2049
   345
            return 0;
icculus@2049
   346
        }
icculus@2049
   347
icculus@2049
   348
        /* Play a stream so aRts doesn't crash */
slouken@2141
   349
        if (SDL_NAME(arts_suspended) ()) {
icculus@2121
   350
            arts_stream_t stream;
icculus@2121
   351
            stream = SDL_NAME(arts_play_stream) (44100, 16, 2, "SDL");
icculus@2121
   352
            SDL_NAME(arts_write) (stream, "", 0);
icculus@2121
   353
            SDL_NAME(arts_close_stream) (stream);
icculus@2121
   354
        }
icculus@2121
   355
icculus@2049
   356
        SDL_NAME(arts_free) ();
icculus@2049
   357
    }
icculus@2049
   358
icculus@2049
   359
    /* Set the function pointers */
icculus@2049
   360
    impl->OpenDevice = ARTS_OpenDevice;
icculus@2049
   361
    impl->PlayDevice = ARTS_PlayDevice;
icculus@2049
   362
    impl->WaitDevice = ARTS_WaitDevice;
icculus@2049
   363
    impl->GetDeviceBuf = ARTS_GetDeviceBuf;
icculus@2049
   364
    impl->CloseDevice = ARTS_CloseDevice;
icculus@2049
   365
    impl->WaitDone = ARTS_WaitDone;
icculus@2049
   366
    impl->Deinitialize = ARTS_Deinitialize;
icculus@2049
   367
    impl->OnlyHasDefaultOutputDevice = 1;
icculus@2049
   368
icculus@3699
   369
    return 1;   /* this audio target is available. */
icculus@2049
   370
}
icculus@2049
   371
icculus@2049
   372
icculus@2049
   373
AudioBootStrap ARTS_bootstrap = {
icculus@5594
   374
    "arts", "Analog RealTime Synthesizer", ARTS_Init, 0
icculus@2049
   375
};
icculus@2049
   376
slouken@6044
   377
#endif /* SDL_AUDIO_DRIVER_ARTS */
slouken@6044
   378
slouken@1895
   379
/* vi: set ts=4 sw=4 expandtab: */