src/audio/dma/SDL_dmaaudio.c

/* [<][>]
[^][v][top][bottom][index][help] */

FUNCTIONS

This source file includes following functions.
  1. Audio_Available
  2. Audio_DeleteDevice
  3. Audio_CreateDevice
  4. DMA_WaitAudio
  5. DMA_PlayAudio
  6. DMA_GetAudioBuf
  7. DMA_CloseAudio
  8. DMA_ReopenAudio
  9. DMA_OpenAudio

   1 /*
   2     SDL - Simple DirectMedia Layer
   3     Copyright (C) 1997, 1998, 1999, 2000, 2001  Sam Lantinga
   4 
   5     This library is free software; you can redistribute it and/or
   6     modify it under the terms of the GNU Library General Public
   7     License as published by the Free Software Foundation; either
   8     version 2 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     Library General Public License for more details.
  14 
  15     You should have received a copy of the GNU Library General Public
  16     License along with this library; if not, write to the Free
  17     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  18 
  19     Sam Lantinga
  20     slouken@devolution.com
  21 */
  22 
  23 #ifdef SAVE_RCSID
  24 static char rcsid =
  25  "@(#) $Id: SDL_dmaaudio.c,v 1.1.2.3 2001/03/21 17:19:56 hercules Exp $";
  26 #endif
  27 
  28 /* Allow access to a raw mixing buffer */
  29 
  30 #include <stdlib.h>
  31 #include <stdio.h>
  32 #include <string.h>
  33 #include <errno.h>
  34 #include <unistd.h>
  35 #include <fcntl.h>
  36 #include <signal.h>
  37 #include <sys/types.h>
  38 #include <sys/time.h>
  39 #include <sys/ioctl.h>
  40 #include <sys/stat.h>
  41 #include <sys/mman.h>
  42 #ifdef linux
  43 #include <linux/soundcard.h>
  44 #endif
  45 #ifdef __bsdi__
  46 #include <sys/soundcard.h>
  47 #endif
  48 #ifdef __FreeBSD__
  49 #include <machine/soundcard.h>
  50 #endif
  51 #ifdef __USLC__
  52 #include <sys/soundcard.h>
  53 #endif
  54 
  55 #ifndef MAP_FAILED
  56 #define MAP_FAILED      ((Uint8 *)-1)
  57 #endif
  58 
  59 #include "SDL_audio.h"
  60 #include "SDL_error.h"
  61 #include "SDL_audiomem.h"
  62 #include "SDL_audio_c.h"
  63 #include "SDL_timer.h"
  64 #include "SDL_audiodev_c.h"
  65 #include "SDL_dmaaudio.h"
  66 
  67 /* The tag name used by DMA audio */
  68 #define DMA_DRIVER_NAME         "dma"
  69 
  70 /* Open the audio device for playback, and don't block if busy */
  71 #define OPEN_FLAGS      (O_RDWR|O_NONBLOCK)
  72 
  73 /* Audio driver functions */
  74 static int DMA_OpenAudio(_THIS, SDL_AudioSpec *spec);
  75 static void DMA_WaitAudio(_THIS);
  76 static void DMA_PlayAudio(_THIS);
  77 static Uint8 *DMA_GetAudioBuf(_THIS);
  78 static void DMA_CloseAudio(_THIS);
  79 
  80 /* Audio driver bootstrap functions */
  81 
  82 static int Audio_Available(void)
     /* [<][>][^][v][top][bottom][index][help] */
  83 {
  84         int available;
  85         int fd;
  86 
  87         available = 0;
  88 
  89         fd = SDL_OpenAudioPath(NULL, 0, OPEN_FLAGS, 0);
  90         if ( fd >= 0 ) {
  91                 int caps;
  92                 struct audio_buf_info info;
  93 
  94                 if ( (ioctl(fd, SNDCTL_DSP_GETCAPS, &caps) == 0) &&
  95                      (caps & DSP_CAP_TRIGGER) && (caps & DSP_CAP_MMAP) &&
  96                      (ioctl(fd, SNDCTL_DSP_GETOSPACE, &info) == 0) ) {
  97                         available = 1;
  98                 }
  99                 close(fd);
 100         }
 101         return(available);
 102 }
 103 
 104 static void Audio_DeleteDevice(SDL_AudioDevice *device)
     /* [<][>][^][v][top][bottom][index][help] */
 105 {
 106         free(device->hidden);
 107         free(device);
 108 }
 109 
 110 static SDL_AudioDevice *Audio_CreateDevice(int devindex)
     /* [<][>][^][v][top][bottom][index][help] */
 111 {
 112         SDL_AudioDevice *this;
 113 
 114         /* Initialize all variables that we clean on shutdown */
 115         this = (SDL_AudioDevice *)malloc(sizeof(SDL_AudioDevice));
 116         if ( this ) {
 117                 memset(this, 0, (sizeof *this));
 118                 this->hidden = (struct SDL_PrivateAudioData *)
 119                                 malloc((sizeof *this->hidden));
 120         }
 121         if ( (this == NULL) || (this->hidden == NULL) ) {
 122                 SDL_OutOfMemory();
 123                 if ( this ) {
 124                         free(this);
 125                 }
 126                 return(0);
 127         }
 128         memset(this->hidden, 0, (sizeof *this->hidden));
 129         audio_fd = -1;
 130 
 131         /* Set the function pointers */
 132         this->OpenAudio = DMA_OpenAudio;
 133         this->WaitAudio = DMA_WaitAudio;
 134         this->PlayAudio = DMA_PlayAudio;
 135         this->GetAudioBuf = DMA_GetAudioBuf;
 136         this->CloseAudio = DMA_CloseAudio;
 137 
 138         this->free = Audio_DeleteDevice;
 139 
 140         return this;
 141 }
 142 
 143 AudioBootStrap DMA_bootstrap = {
 144         DMA_DRIVER_NAME, "OSS /dev/dsp DMA audio",
 145         Audio_Available, Audio_CreateDevice
 146 };
 147 
 148 /* This function waits until it is possible to write a full sound buffer */
 149 static void DMA_WaitAudio(_THIS)
     /* [<][>][^][v][top][bottom][index][help] */
 150 {
 151         fd_set fdset;
 152 
 153         /* Check to see if the thread-parent process is still alive */
 154         { static int cnt = 0;
 155                 /* Note that this only works with thread implementations 
 156                    that use a different process id for each thread.
 157                 */
 158                 if (parent && (((++cnt)%10) == 0)) { /* Check every 10 loops */
 159                         if ( kill(parent, 0) < 0 ) {
 160                                 this->enabled = 0;
 161                         }
 162                 }
 163         }
 164 
 165         /* See if we need to use timed audio synchronization */
 166         if ( frame_ticks ) {
 167                 /* Use timer for general audio synchronization */
 168                 Sint32 ticks;
 169 
 170                 ticks = ((Sint32)(next_frame - SDL_GetTicks()))-FUDGE_TICKS;
 171                 if ( ticks > 0 ) {
 172                         SDL_Delay(ticks);
 173                 }
 174         } else {
 175                 /* Use select() for audio synchronization */
 176                 struct timeval timeout;
 177                 FD_ZERO(&fdset);
 178                 FD_SET(audio_fd, &fdset);
 179                 timeout.tv_sec = 10;
 180                 timeout.tv_usec = 0;
 181 #ifdef DEBUG_AUDIO
 182                 fprintf(stderr, "Waiting for audio to get ready\n");
 183 #endif
 184                 if ( select(audio_fd+1, NULL, &fdset, NULL, &timeout) <= 0 ) {
 185                         const char *message =
 186 #ifdef AUDIO_OSPACE_HACK
 187                         "Audio timeout - buggy audio driver? (trying ospace)";
 188 #else
 189                         "Audio timeout - buggy audio driver? (disabled)";
 190 #endif
 191                         /* In general we should never print to the screen,
 192                            but in this case we have no other way of letting
 193                            the user know what happened.
 194                         */
 195                         fprintf(stderr, "SDL: %s\n", message);
 196 #ifdef AUDIO_OSPACE_HACK
 197                         /* We may be able to use GET_OSPACE trick */
 198                         frame_ticks = (float)(this->spec->samples*1000) /
 199                                               this->spec->freq;
 200                         next_frame = SDL_GetTicks()+frame_ticks;
 201 #else
 202                         this->enabled = 0;
 203                         /* Don't try to close - may hang */
 204                         audio_fd = -1;
 205 #ifdef DEBUG_AUDIO
 206                         fprintf(stderr, "Done disabling audio\n");
 207 #endif
 208 #endif /* AUDIO_OSPACE_HACK */
 209                 }
 210 #ifdef DEBUG_AUDIO
 211                 fprintf(stderr, "Ready!\n");
 212 #endif
 213         }
 214 }
 215 
 216 static void DMA_PlayAudio(_THIS)
     /* [<][>][^][v][top][bottom][index][help] */
 217 {
 218         /* If timer synchronization is enabled, set the next write frame */
 219         if ( frame_ticks ) {
 220                 next_frame += frame_ticks;
 221         }
 222         return;
 223 }
 224 
 225 static Uint8 *DMA_GetAudioBuf(_THIS)
     /* [<][>][^][v][top][bottom][index][help] */
 226 {
 227         count_info info;
 228         int playing;
 229         int filling;
 230 
 231         /* Get number of blocks, looping if we're not using select() */
 232         do {
 233                 if ( ioctl(audio_fd, SNDCTL_DSP_GETOPTR, &info) < 0 ) {
 234                         /* Uh oh... */
 235                         this->enabled = 0;
 236                         return(NULL);
 237                 }
 238         } while ( frame_ticks && (info.blocks < 1) );
 239 #ifdef DEBUG_AUDIO
 240         if ( info.blocks > 1 ) {
 241                 printf("Warning: audio underflow (%d frags)\n", info.blocks-1);
 242         }
 243 #endif
 244         playing = info.ptr / this->spec.size;
 245         filling = (playing + 1)%num_buffers;
 246         return (dma_buf + (filling * this->spec.size));
 247 }
 248 
 249 static void DMA_CloseAudio(_THIS)
     /* [<][>][^][v][top][bottom][index][help] */
 250 {
 251         if ( dma_buf != NULL ) {
 252                 munmap(dma_buf, dma_len);
 253                 dma_buf = NULL;
 254         }
 255         if ( audio_fd >= 0 ) {
 256                 close(audio_fd);
 257                 audio_fd = -1;
 258         }
 259 }
 260 
 261 static int DMA_ReopenAudio(_THIS, const char *audiodev, int format, int stereo,
     /* [<][>][^][v][top][bottom][index][help] */
 262                                                         SDL_AudioSpec *spec)
 263 {
 264         int frag_spec;
 265         int value;
 266 
 267         /* Close and then reopen the audio device */
 268         close(audio_fd);
 269         audio_fd = open(audiodev, O_RDWR, 0);
 270         if ( audio_fd < 0 ) {
 271                 SDL_SetError("Couldn't open %s: %s", audiodev, strerror(errno));
 272                 return(-1);
 273         }
 274         ioctl(audio_fd, SNDCTL_DSP_RESET, 0);
 275 
 276         /* Calculate the final parameters for this audio specification */
 277         SDL_CalculateAudioSpec(spec);
 278 
 279         /* Determine the power of two of the fragment size */
 280         for ( frag_spec = 0; (0x01<<frag_spec) < spec->size; ++frag_spec );
 281         if ( (0x01<<frag_spec) != spec->size ) {
 282                 SDL_SetError("Fragment size must be a power of two");
 283                 return(-1);
 284         }
 285 
 286         /* Set the audio buffering parameters */
 287         if ( ioctl(audio_fd, SNDCTL_DSP_SETFRAGMENT, &frag_spec) < 0 ) {
 288                 SDL_SetError("Couldn't set audio fragment spec");
 289                 return(-1);
 290         }
 291 
 292         /* Set the audio format */
 293         value = format;
 294         if ( (ioctl(audio_fd, SNDCTL_DSP_SETFMT, &value) < 0) ||
 295                                                 (value != format) ) {
 296                 SDL_SetError("Couldn't set audio format");
 297                 return(-1);
 298         }
 299 
 300         /* Set mono or stereo audio */
 301         value = (spec->channels > 1);
 302         if ( (ioctl(audio_fd, SNDCTL_DSP_STEREO, &stereo) < 0) ||
 303                                                 (value != stereo) ) {
 304                 SDL_SetError("Couldn't set audio channels");
 305                 return(-1);
 306         }
 307 
 308         /* Set the DSP frequency */
 309         value = spec->freq;
 310         if ( ioctl(audio_fd, SOUND_PCM_WRITE_RATE, &value) < 0 ) {
 311                 SDL_SetError("Couldn't set audio frequency");
 312                 return(-1);
 313         }
 314         spec->freq = value;
 315 
 316         /* We successfully re-opened the audio */
 317         return(0);
 318 }
 319 
 320 static int DMA_OpenAudio(_THIS, SDL_AudioSpec *spec)
     /* [<][>][^][v][top][bottom][index][help] */
 321 {
 322         char audiodev[1024];
 323         int format;
 324         int stereo;
 325         int value;
 326         Uint16 test_format;
 327         struct audio_buf_info info;
 328 
 329         /* Reset the timer synchronization flag */
 330         frame_ticks = 0.0;
 331 
 332         /* Open the audio device */
 333         audio_fd = SDL_OpenAudioPath(audiodev, sizeof(audiodev), OPEN_FLAGS, 0);
 334         if ( audio_fd < 0 ) {
 335                 SDL_SetError("Couldn't open %s: %s", audiodev, strerror(errno));
 336                 return(-1);
 337         }
 338         dma_buf = NULL;
 339         ioctl(audio_fd, SNDCTL_DSP_RESET, 0);
 340 
 341         /* Get a list of supported hardware formats */
 342         if ( ioctl(audio_fd, SNDCTL_DSP_GETFMTS, &value) < 0 ) {
 343                 SDL_SetError("Couldn't get audio format list");
 344                 return(-1);
 345         }
 346 
 347         /* Try for a closest match on audio format */
 348         format = 0;
 349         for ( test_format = SDL_FirstAudioFormat(spec->format);
 350                                                 ! format && test_format; ) {
 351 #ifdef DEBUG_AUDIO
 352                 fprintf(stderr, "Trying format 0x%4.4x\n", test_format);
 353 #endif
 354                 switch ( test_format ) {
 355                         case AUDIO_U8:
 356                                 if ( value & AFMT_U8 ) {
 357                                         format = AFMT_U8;
 358                                 }
 359                                 break;
 360                         case AUDIO_S8:
 361                                 if ( value & AFMT_S8 ) {
 362                                         format = AFMT_S8;
 363                                 }
 364                                 break;
 365                         case AUDIO_S16LSB:
 366                                 if ( value & AFMT_S16_LE ) {
 367                                         format = AFMT_S16_LE;
 368                                 }
 369                                 break;
 370                         case AUDIO_S16MSB:
 371                                 if ( value & AFMT_S16_BE ) {
 372                                         format = AFMT_S16_BE;
 373                                 }
 374                                 break;
 375                         case AUDIO_U16LSB:
 376                                 if ( value & AFMT_U16_LE ) {
 377                                         format = AFMT_U16_LE;
 378                                 }
 379                                 break;
 380                         case AUDIO_U16MSB:
 381                                 if ( value & AFMT_U16_BE ) {
 382                                         format = AFMT_U16_BE;
 383                                 }
 384                                 break;
 385                         default:
 386                                 break;
 387                 }
 388                 if ( ! format ) {
 389                         test_format = SDL_NextAudioFormat();
 390                 }
 391         }
 392         if ( format == 0 ) {
 393                 SDL_SetError("Couldn't find any hardware audio formats");
 394                 return(-1);
 395         }
 396         spec->format = test_format;
 397 
 398         /* Set the audio format */
 399         value = format;
 400         if ( (ioctl(audio_fd, SNDCTL_DSP_SETFMT, &value) < 0) ||
 401                                                 (value != format) ) {
 402                 SDL_SetError("Couldn't set audio format");
 403                 return(-1);
 404         }
 405 
 406         /* Set mono or stereo audio (currently only two channels supported) */
 407         stereo = (spec->channels > 1);
 408         ioctl(audio_fd, SNDCTL_DSP_STEREO, &stereo);
 409         if ( stereo ) {
 410                 spec->channels = 2;
 411         } else {
 412                 spec->channels = 1;
 413         }
 414 
 415         /* Because some drivers don't allow setting the buffer size
 416            after setting the format, we must re-open the audio device
 417            once we know what format and channels are supported
 418          */
 419         if ( DMA_ReopenAudio(this, audiodev, format, stereo, spec) < 0 ) {
 420                 /* Error is set by DMA_ReopenAudio() */
 421                 return(-1);
 422         }
 423 
 424         /* Memory map the audio buffer */
 425         if ( ioctl(audio_fd, SNDCTL_DSP_GETOSPACE, &info) < 0 ) {
 426                 SDL_SetError("Couldn't get OSPACE parameters");
 427                 return(-1);
 428         }
 429         spec->size = info.fragsize;
 430         spec->samples = spec->size / ((spec->format & 0xFF) / 8);
 431         spec->samples /= spec->channels;
 432         num_buffers = info.fragstotal;
 433         dma_len = num_buffers*spec->size;
 434         dma_buf = (Uint8 *)mmap(NULL, dma_len, PROT_WRITE, MAP_SHARED,
 435                                                         audio_fd, 0);
 436         if ( dma_buf == MAP_FAILED ) {
 437                 SDL_SetError("DMA memory map failed");
 438                 dma_buf = NULL;
 439                 return(-1);
 440         }
 441         memset(dma_buf, spec->silence, dma_len);
 442 
 443         /* Check to see if we need to use select() workaround */
 444         { char *workaround;
 445                 workaround = getenv("SDL_DSP_NOSELECT");
 446                 if ( workaround ) {
 447                         frame_ticks = (float)(spec->samples*1000)/spec->freq;
 448                         next_frame = SDL_GetTicks()+frame_ticks;
 449                 }
 450         }
 451 
 452         /* Trigger audio playback */
 453         value = 0;
 454         ioctl(audio_fd, SNDCTL_DSP_SETTRIGGER, &value);
 455         value = PCM_ENABLE_OUTPUT;
 456         if ( ioctl(audio_fd, SNDCTL_DSP_SETTRIGGER, &value) < 0 ) {
 457                 SDL_SetError("Couldn't trigger audio output");
 458                 return(-1);
 459         }
 460 
 461         /* Get the parent process id (we're the parent of the audio thread) */
 462         parent = getpid();
 463 
 464         /* We're ready to rock and roll. :-) */
 465         return(0);
 466 }

/* [<][>][^][v][top][bottom][index][help] */