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