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