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