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_OpenAudio(_THIS, SDL_AudioSpec * spec);
41 static void DUMMYAUD_WaitAudio(_THIS);
42 static void DUMMYAUD_PlayAudio(_THIS);
43 static Uint8 *DUMMYAUD_GetAudioBuf(_THIS);
44 static void DUMMYAUD_CloseAudio(_THIS);
46 /* Audio driver bootstrap functions */
48 DUMMYAUD_Available(void)
50 const char *envr = SDL_getenv("SDL_AUDIODRIVER");
51 if (envr && (SDL_strcmp(envr, DUMMYAUD_DRIVER_NAME) == 0)) {
58 DUMMYAUD_DeleteDevice(SDL_AudioDevice * device)
60 SDL_free(device->hidden);
64 static SDL_AudioDevice *
65 DUMMYAUD_CreateDevice(int devindex)
67 SDL_AudioDevice *this;
69 /* Initialize all variables that we clean on shutdown */
70 this = (SDL_AudioDevice *) SDL_malloc(sizeof(SDL_AudioDevice));
72 SDL_memset(this, 0, (sizeof *this));
73 this->hidden = (struct SDL_PrivateAudioData *)
74 SDL_malloc((sizeof *this->hidden));
76 if ((this == NULL) || (this->hidden == NULL)) {
83 SDL_memset(this->hidden, 0, (sizeof *this->hidden));
85 /* Set the function pointers */
86 this->OpenAudio = DUMMYAUD_OpenAudio;
87 this->WaitAudio = DUMMYAUD_WaitAudio;
88 this->PlayAudio = DUMMYAUD_PlayAudio;
89 this->GetAudioBuf = DUMMYAUD_GetAudioBuf;
90 this->CloseAudio = DUMMYAUD_CloseAudio;
92 this->free = DUMMYAUD_DeleteDevice;
97 AudioBootStrap DUMMYAUD_bootstrap = {
98 DUMMYAUD_DRIVER_NAME, "SDL dummy audio driver",
99 DUMMYAUD_Available, DUMMYAUD_CreateDevice
102 /* This function waits until it is possible to write a full sound buffer */
104 DUMMYAUD_WaitAudio(_THIS)
106 /* Don't block on first calls to simulate initial fragment filling. */
107 if (this->hidden->initial_calls)
108 this->hidden->initial_calls--;
110 SDL_Delay(this->hidden->write_delay);
114 DUMMYAUD_PlayAudio(_THIS)
116 /* no-op...this is a null driver. */
120 DUMMYAUD_GetAudioBuf(_THIS)
122 return (this->hidden->mixbuf);
126 DUMMYAUD_CloseAudio(_THIS)
128 if (this->hidden->mixbuf != NULL) {
129 SDL_FreeAudioMem(this->hidden->mixbuf);
130 this->hidden->mixbuf = NULL;
135 DUMMYAUD_OpenAudio(_THIS, SDL_AudioSpec * spec)
137 float bytes_per_sec = 0.0f;
139 /* Allocate mixing buffer */
140 this->hidden->mixlen = spec->size;
141 this->hidden->mixbuf = (Uint8 *) SDL_AllocAudioMem(this->hidden->mixlen);
142 if (this->hidden->mixbuf == NULL) {
145 SDL_memset(this->hidden->mixbuf, spec->silence, spec->size);
147 bytes_per_sec = (float) (((spec->format & 0xFF) / 8) *
148 spec->channels * spec->freq);
151 * We try to make this request more audio at the correct rate for
152 * a given audio spec, so timing stays fairly faithful.
153 * Also, we have it not block at all for the first two calls, so
154 * it seems like we're filling two audio fragments right out of the
155 * gate, like other SDL drivers tend to do.
157 this->hidden->initial_calls = 2;
158 this->hidden->write_delay =
159 (Uint32) ((((float) spec->size) / bytes_per_sec) * 1000.0f);
161 /* We're ready to rock and roll. :-) */
165 /* vi: set ts=4 sw=4 expandtab: */