src/audio/arts/SDL_artsaudio.c
author David Ludwig
Thu, 31 Dec 2015 01:54:11 -0500
changeset 9993 e05d46c27ce3
parent 9619 b94b6d0bff0f
child 9998 f67cf37e9cd4
permissions -rw-r--r--
WinRT: minor code-comment cleanups
slouken@0
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@9619
     3
  Copyright (C) 1997-2015 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
*/
icculus@8093
    21
#include "../../SDL_internal.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) {
icculus@9394
   154
                SDL_OpenedAudioDeviceDisconnected(this);
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) {
icculus@9394
   182
        SDL_OpenedAudioDeviceDisconnected(this);
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) {
slouken@7719
   207
        SDL_FreeAudioMem(this->hidden->mixbuf);
slouken@7719
   208
        this->hidden->mixbuf = NULL;
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@6152
   219
static int
icculus@6152
   220
ARTS_Suspend(void)
icculus@6152
   221
{
icculus@6152
   222
    const Uint32 abortms = SDL_GetTicks() + 3000; /* give up after 3 secs */
slouken@7857
   223
    while ( (!SDL_NAME(arts_suspended)()) && !SDL_TICKS_PASSED(SDL_GetTicks(), abortms) ) {
icculus@6152
   224
        if ( SDL_NAME(arts_suspend)() ) {
icculus@6152
   225
            break;
icculus@6152
   226
        }
icculus@6152
   227
    }
icculus@6152
   228
    return SDL_NAME(arts_suspended)();
icculus@6152
   229
}
icculus@2049
   230
slouken@1895
   231
static int
icculus@9394
   232
ARTS_OpenDevice(_THIS, void *handle, const char *devname, int iscapture)
slouken@0
   233
{
icculus@2049
   234
    int rc = 0;
icculus@2049
   235
    int bits = 0, frag_spec = 0;
icculus@2049
   236
    SDL_AudioFormat test_format = 0, format = 0;
slouken@0
   237
icculus@2049
   238
    /* Initialize all variables that we clean on shutdown */
icculus@2049
   239
    this->hidden = (struct SDL_PrivateAudioData *)
slouken@2060
   240
        SDL_malloc((sizeof *this->hidden));
icculus@2049
   241
    if (this->hidden == NULL) {
icculus@7038
   242
        return SDL_OutOfMemory();
icculus@2049
   243
    }
icculus@2049
   244
    SDL_memset(this->hidden, 0, (sizeof *this->hidden));
slouken@0
   245
slouken@1895
   246
    /* Try for a closest match on audio format */
icculus@2049
   247
    for (test_format = SDL_FirstAudioFormat(this->spec.format);
slouken@1895
   248
         !format && test_format;) {
slouken@0
   249
#ifdef DEBUG_AUDIO
slouken@1895
   250
        fprintf(stderr, "Trying format 0x%4.4x\n", test_format);
slouken@0
   251
#endif
slouken@1895
   252
        switch (test_format) {
slouken@1895
   253
        case AUDIO_U8:
slouken@1895
   254
            bits = 8;
slouken@1895
   255
            format = 1;
slouken@1895
   256
            break;
slouken@1895
   257
        case AUDIO_S16LSB:
slouken@1895
   258
            bits = 16;
slouken@1895
   259
            format = 1;
slouken@1895
   260
            break;
slouken@1895
   261
        default:
slouken@1895
   262
            format = 0;
slouken@1895
   263
            break;
slouken@1895
   264
        }
slouken@1895
   265
        if (!format) {
slouken@1895
   266
            test_format = SDL_NextAudioFormat();
slouken@1895
   267
        }
slouken@1895
   268
    }
slouken@1895
   269
    if (format == 0) {
icculus@2049
   270
        ARTS_CloseDevice(this);
icculus@7038
   271
        return SDL_SetError("Couldn't find any hardware audio formats");
slouken@1895
   272
    }
icculus@2049
   273
    this->spec.format = test_format;
slouken@0
   274
icculus@2049
   275
    if ((rc = SDL_NAME(arts_init) ()) != 0) {
icculus@2049
   276
        ARTS_CloseDevice(this);
icculus@7038
   277
        return SDL_SetError("Unable to initialize ARTS: %s",
icculus@7038
   278
                            SDL_NAME(arts_error_text) (rc));
slouken@1895
   279
    }
icculus@2121
   280
icculus@6152
   281
    if (!ARTS_Suspend()) {
icculus@2121
   282
        ARTS_CloseDevice(this);
icculus@7038
   283
        return SDL_SetError("ARTS can not open audio device");
icculus@2121
   284
    }
icculus@2121
   285
slouken@2060
   286
    this->hidden->stream = SDL_NAME(arts_play_stream) (this->spec.freq,
slouken@2060
   287
                                                       bits,
slouken@2060
   288
                                                       this->spec.channels,
slouken@2060
   289
                                                       "SDL");
slouken@0
   290
icculus@2121
   291
    /* Play nothing so we have at least one write (server bug workaround). */
icculus@2121
   292
    SDL_NAME(arts_write) (this->hidden->stream, "", 0);
icculus@2121
   293
slouken@1895
   294
    /* Calculate the final parameters for this audio specification */
icculus@2049
   295
    SDL_CalculateAudioSpec(&this->spec);
slouken@0
   296
slouken@1895
   297
    /* Determine the power of two of the fragment size */
icculus@2049
   298
    for (frag_spec = 0; (0x01 << frag_spec) < this->spec.size; ++frag_spec);
icculus@2049
   299
    if ((0x01 << frag_spec) != this->spec.size) {
icculus@2049
   300
        ARTS_CloseDevice(this);
icculus@7038
   301
        return SDL_SetError("Fragment size must be a power of two");
slouken@1895
   302
    }
slouken@1895
   303
    frag_spec |= 0x00020000;    /* two fragments, for low latency */
slouken@0
   304
slouken@0
   305
#ifdef ARTS_P_PACKET_SETTINGS
icculus@2049
   306
    SDL_NAME(arts_stream_set) (this->hidden->stream,
slouken@2060
   307
                               ARTS_P_PACKET_SETTINGS, frag_spec);
slouken@0
   308
#else
icculus@2049
   309
    SDL_NAME(arts_stream_set) (this->hidden->stream, ARTS_P_PACKET_SIZE,
slouken@1895
   310
                               frag_spec & 0xffff);
icculus@2049
   311
    SDL_NAME(arts_stream_set) (this->hidden->stream, ARTS_P_PACKET_COUNT,
icculus@2049
   312
                               frag_spec >> 16);
slouken@0
   313
#endif
icculus@2049
   314
    this->spec.size = SDL_NAME(arts_stream_get) (this->hidden->stream,
icculus@2049
   315
                                                 ARTS_P_PACKET_SIZE);
slouken@0
   316
slouken@1895
   317
    /* Allocate mixing buffer */
icculus@2049
   318
    this->hidden->mixlen = this->spec.size;
icculus@2049
   319
    this->hidden->mixbuf = (Uint8 *) SDL_AllocAudioMem(this->hidden->mixlen);
icculus@2049
   320
    if (this->hidden->mixbuf == NULL) {
icculus@2049
   321
        ARTS_CloseDevice(this);
icculus@7038
   322
        return SDL_OutOfMemory();
slouken@1895
   323
    }
icculus@2049
   324
    SDL_memset(this->hidden->mixbuf, this->spec.silence, this->spec.size);
slouken@0
   325
slouken@1895
   326
    /* Get the parent process id (we're the parent of the audio thread) */
icculus@2049
   327
    this->hidden->parent = getpid();
slouken@0
   328
slouken@1895
   329
    /* We're ready to rock and roll. :-) */
icculus@7038
   330
    return 0;
icculus@2049
   331
}
icculus@2049
   332
icculus@2049
   333
icculus@2049
   334
static void
icculus@2049
   335
ARTS_Deinitialize(void)
icculus@2049
   336
{
icculus@2049
   337
    UnloadARTSLibrary();
slouken@0
   338
}
slouken@1895
   339
icculus@2049
   340
icculus@2049
   341
static int
slouken@2060
   342
ARTS_Init(SDL_AudioDriverImpl * impl)
icculus@2049
   343
{
icculus@2049
   344
    if (LoadARTSLibrary() < 0) {
icculus@2049
   345
        return 0;
icculus@2049
   346
    } else {
icculus@2049
   347
        if (SDL_NAME(arts_init) () != 0) {
icculus@2049
   348
            UnloadARTSLibrary();
icculus@2049
   349
            SDL_SetError("ARTS: arts_init failed (no audio server?)");
icculus@2049
   350
            return 0;
icculus@2049
   351
        }
icculus@2049
   352
icculus@2049
   353
        /* Play a stream so aRts doesn't crash */
icculus@6152
   354
        if (ARTS_Suspend()) {
icculus@2121
   355
            arts_stream_t stream;
icculus@2121
   356
            stream = SDL_NAME(arts_play_stream) (44100, 16, 2, "SDL");
icculus@2121
   357
            SDL_NAME(arts_write) (stream, "", 0);
icculus@2121
   358
            SDL_NAME(arts_close_stream) (stream);
icculus@2121
   359
        }
icculus@2121
   360
icculus@2049
   361
        SDL_NAME(arts_free) ();
icculus@2049
   362
    }
icculus@2049
   363
icculus@2049
   364
    /* Set the function pointers */
icculus@2049
   365
    impl->OpenDevice = ARTS_OpenDevice;
icculus@2049
   366
    impl->PlayDevice = ARTS_PlayDevice;
icculus@2049
   367
    impl->WaitDevice = ARTS_WaitDevice;
icculus@2049
   368
    impl->GetDeviceBuf = ARTS_GetDeviceBuf;
icculus@2049
   369
    impl->CloseDevice = ARTS_CloseDevice;
icculus@2049
   370
    impl->WaitDone = ARTS_WaitDone;
icculus@2049
   371
    impl->Deinitialize = ARTS_Deinitialize;
icculus@2049
   372
    impl->OnlyHasDefaultOutputDevice = 1;
icculus@2049
   373
icculus@3699
   374
    return 1;   /* this audio target is available. */
icculus@2049
   375
}
icculus@2049
   376
icculus@2049
   377
icculus@2049
   378
AudioBootStrap ARTS_bootstrap = {
icculus@5594
   379
    "arts", "Analog RealTime Synthesizer", ARTS_Init, 0
icculus@2049
   380
};
icculus@2049
   381
slouken@6044
   382
#endif /* SDL_AUDIO_DRIVER_ARTS */
slouken@6044
   383
slouken@1895
   384
/* vi: set ts=4 sw=4 expandtab: */