src/audio/dummy/SDL_dummyaudio.c
author Ryan C. Gordon
Tue, 03 Oct 2006 22:17:59 +0000
branchSDL-ryan-multiple-audio-device
changeset 3792 866c310e2cb5
parent 3784 37c9c4590689
child 3796 b19680c84cdf
permissions -rw-r--r--
Changed some 1.3 audio symbol names.
     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     This file written by Ryan C. Gordon (icculus@icculus.org)
    23 */
    24 #include "SDL_config.h"
    25 
    26 /* Output audio to nowhere... */
    27 
    28 #include "SDL_rwops.h"
    29 #include "SDL_timer.h"
    30 #include "SDL_audio.h"
    31 #include "../SDL_audiomem.h"
    32 #include "../SDL_audio_c.h"
    33 #include "../SDL_audiodev_c.h"
    34 #include "SDL_dummyaudio.h"
    35 
    36 /* The tag name used by DUMMY audio */
    37 #define DUMMYAUD_DRIVER_NAME         "dummy"
    38 
    39 /* Audio driver functions */
    40 static int DUMMYAUD_OpenDevice(_THIS, const char *devname, int iscapture);
    41 static void DUMMYAUD_WaitDevice(_THIS);
    42 static void DUMMYAUD_PlayDevice(_THIS);
    43 static Uint8 *DUMMYAUD_GetDeviceBuf(_THIS);
    44 static void DUMMYAUD_CloseDevice(_THIS);
    45 
    46 /* Audio driver bootstrap functions */
    47 static int
    48 DUMMYAUD_Available(void)
    49 {
    50     /* !!! FIXME: check this at a higher level... */
    51     /* only ever use this driver if explicitly requested. */
    52     const char *envr = SDL_getenv("SDL_AUDIODRIVER");
    53     if (envr && (SDL_strcmp(envr, DUMMYAUD_DRIVER_NAME) == 0)) {
    54         return (1);
    55     }
    56     return (0);
    57 }
    58 
    59 static int
    60 DUMMYAUD_Init(SDL_AudioDriverImpl *impl)
    61 {
    62     /* Set the function pointers */
    63     impl->OpenDevice = DUMMYAUD_OpenDevice;
    64     impl->WaitDevice = DUMMYAUD_WaitDevice;
    65     impl->PlayDevice = DUMMYAUD_PlayDevice;
    66     impl->GetDeviceBuf = DUMMYAUD_GetDeviceBuf;
    67     impl->CloseDevice = DUMMYAUD_CloseDevice;
    68 
    69     return 1;
    70 }
    71 
    72 AudioBootStrap DUMMYAUD_bootstrap = {
    73     DUMMYAUD_DRIVER_NAME, "SDL dummy audio driver",
    74     DUMMYAUD_Available, DUMMYAUD_Init
    75 };
    76 
    77 /* This function waits until it is possible to write a full sound buffer */
    78 static void
    79 DUMMYAUD_WaitDevice(_THIS)
    80 {
    81     /* Don't block on first calls to simulate initial fragment filling. */
    82     if (this->hidden->initial_calls)
    83         this->hidden->initial_calls--;
    84     else
    85         SDL_Delay(this->hidden->write_delay);
    86 }
    87 
    88 static void
    89 DUMMYAUD_PlayDevice(_THIS)
    90 {
    91     /* no-op...this is a null driver. */
    92 }
    93 
    94 static Uint8 *
    95 DUMMYAUD_GetDeviceBuf(_THIS)
    96 {
    97     return (this->hidden->mixbuf);
    98 }
    99 
   100 static void
   101 DUMMYAUD_CloseDevice(_THIS)
   102 {
   103     if (this->hidden->mixbuf != NULL) {
   104         SDL_FreeAudioMem(this->hidden->mixbuf);
   105         this->hidden->mixbuf = NULL;
   106     }
   107     SDL_free(this->hidden);
   108     this->hidden = NULL;
   109 }
   110 
   111 static int
   112 DUMMYAUD_OpenDevice(_THIS, const char *devname, int iscapture)
   113 {
   114     float bytes_per_sec = 0.0f;
   115 
   116     /* Initialize all variables that we clean on shutdown */
   117     this->hidden = (struct SDL_PrivateAudioData *)
   118                     SDL_malloc((sizeof *this->hidden));
   119     if (this->hidden == NULL) {
   120         SDL_OutOfMemory();
   121         return 0;
   122     }
   123     SDL_memset(this->hidden, 0, (sizeof *this->hidden));
   124 
   125     /* Allocate mixing buffer */
   126     this->hidden->mixlen = this->spec.size;
   127     this->hidden->mixbuf = (Uint8 *) SDL_AllocAudioMem(this->hidden->mixlen);
   128     if (this->hidden->mixbuf == NULL) {
   129         DUMMYAUD_CloseDevice(this);
   130         return 0;
   131     }
   132     SDL_memset(this->hidden->mixbuf, this->spec.silence, this->spec.size);
   133 
   134     bytes_per_sec = (float) (SDL_AUDIO_BITSIZE(this->spec.format) / 8) *
   135                              this->spec.channels * this->spec.freq;
   136 
   137     /*
   138      * We try to make this request more audio at the correct rate for
   139      *  a given audio spec, so timing stays fairly faithful.
   140      * Also, we have it not block at all for the first two calls, so
   141      *  it seems like we're filling two audio fragments right out of the
   142      *  gate, like other SDL drivers tend to do.
   143      */
   144     this->hidden->initial_calls = 2;
   145     this->hidden->write_delay =
   146         (Uint32) ((((float) this->spec.size) / bytes_per_sec) * 1000.0f);
   147 
   148     /* We're ready to rock and roll. :-) */
   149     return 1;
   150 }
   151 
   152 /* vi: set ts=4 sw=4 expandtab: */