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