2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997-2006 Sam Lantinga
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.
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.
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
22 This file written by Ryan C. Gordon (icculus@icculus.org)
24 #include "SDL_config.h"
26 /* Output audio to nowhere... */
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"
36 /* The tag name used by DUMMY audio */
37 #define DUMMYAUD_DRIVER_NAME "dummy"
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);
46 /* Audio driver bootstrap functions */
48 DUMMYAUD_Available(void)
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)) {
60 DUMMYAUD_Init(SDL_AudioDriverImpl *impl)
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 impl->OnlyHasDefaultOutputDevice = 1;
73 AudioBootStrap DUMMYAUD_bootstrap = {
74 DUMMYAUD_DRIVER_NAME, "SDL dummy audio driver",
75 DUMMYAUD_Available, DUMMYAUD_Init
78 /* This function waits until it is possible to write a full sound buffer */
80 DUMMYAUD_WaitDevice(_THIS)
82 /* Don't block on first calls to simulate initial fragment filling. */
83 if (this->hidden->initial_calls)
84 this->hidden->initial_calls--;
86 SDL_Delay(this->hidden->write_delay);
90 DUMMYAUD_PlayDevice(_THIS)
92 /* no-op...this is a null driver. */
96 DUMMYAUD_GetDeviceBuf(_THIS)
98 return (this->hidden->mixbuf);
102 DUMMYAUD_CloseDevice(_THIS)
104 if (this->hidden->mixbuf != NULL) {
105 SDL_FreeAudioMem(this->hidden->mixbuf);
106 this->hidden->mixbuf = NULL;
108 SDL_free(this->hidden);
113 DUMMYAUD_OpenDevice(_THIS, const char *devname, int iscapture)
115 float bytes_per_sec = 0.0f;
117 /* Initialize all variables that we clean on shutdown */
118 this->hidden = (struct SDL_PrivateAudioData *)
119 SDL_malloc((sizeof *this->hidden));
120 if (this->hidden == NULL) {
124 SDL_memset(this->hidden, 0, (sizeof *this->hidden));
126 /* Allocate mixing buffer */
127 this->hidden->mixlen = this->spec.size;
128 this->hidden->mixbuf = (Uint8 *) SDL_AllocAudioMem(this->hidden->mixlen);
129 if (this->hidden->mixbuf == NULL) {
130 DUMMYAUD_CloseDevice(this);
133 SDL_memset(this->hidden->mixbuf, this->spec.silence, this->spec.size);
135 bytes_per_sec = (float) (SDL_AUDIO_BITSIZE(this->spec.format) / 8) *
136 this->spec.channels * this->spec.freq;
139 * We try to make this request more audio at the correct rate for
140 * a given audio spec, so timing stays fairly faithful.
141 * Also, we have it not block at all for the first two calls, so
142 * it seems like we're filling two audio fragments right out of the
143 * gate, like other SDL drivers tend to do.
145 this->hidden->initial_calls = 2;
146 this->hidden->write_delay =
147 (Uint32) ((((float) this->spec.size) / bytes_per_sec) * 1000.0f);
149 /* We're ready to rock and roll. :-) */
153 /* vi: set ts=4 sw=4 expandtab: */