SDL-1.2.14
[sdl_omap.git] / src / audio / dummy / SDL_dummyaudio.c
CommitLineData
e14743d1 1/*
2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997-2009 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 */
40static int DUMMYAUD_OpenAudio(_THIS, SDL_AudioSpec *spec);
41static void DUMMYAUD_WaitAudio(_THIS);
42static void DUMMYAUD_PlayAudio(_THIS);
43static Uint8 *DUMMYAUD_GetAudioBuf(_THIS);
44static void DUMMYAUD_CloseAudio(_THIS);
45
46/* Audio driver bootstrap functions */
47static int DUMMYAUD_Available(void)
48{
49 const char *envr = SDL_getenv("SDL_AUDIODRIVER");
50 if (envr && (SDL_strcmp(envr, DUMMYAUD_DRIVER_NAME) == 0)) {
51 return(1);
52 }
53 return(0);
54}
55
56static void DUMMYAUD_DeleteDevice(SDL_AudioDevice *device)
57{
58 SDL_free(device->hidden);
59 SDL_free(device);
60}
61
62static SDL_AudioDevice *DUMMYAUD_CreateDevice(int devindex)
63{
64 SDL_AudioDevice *this;
65
66 /* Initialize all variables that we clean on shutdown */
67 this = (SDL_AudioDevice *)SDL_malloc(sizeof(SDL_AudioDevice));
68 if ( this ) {
69 SDL_memset(this, 0, (sizeof *this));
70 this->hidden = (struct SDL_PrivateAudioData *)
71 SDL_malloc((sizeof *this->hidden));
72 }
73 if ( (this == NULL) || (this->hidden == NULL) ) {
74 SDL_OutOfMemory();
75 if ( this ) {
76 SDL_free(this);
77 }
78 return(0);
79 }
80 SDL_memset(this->hidden, 0, (sizeof *this->hidden));
81
82 /* Set the function pointers */
83 this->OpenAudio = DUMMYAUD_OpenAudio;
84 this->WaitAudio = DUMMYAUD_WaitAudio;
85 this->PlayAudio = DUMMYAUD_PlayAudio;
86 this->GetAudioBuf = DUMMYAUD_GetAudioBuf;
87 this->CloseAudio = DUMMYAUD_CloseAudio;
88
89 this->free = DUMMYAUD_DeleteDevice;
90
91 return this;
92}
93
94AudioBootStrap DUMMYAUD_bootstrap = {
95 DUMMYAUD_DRIVER_NAME, "SDL dummy audio driver",
96 DUMMYAUD_Available, DUMMYAUD_CreateDevice
97};
98
99/* This function waits until it is possible to write a full sound buffer */
100static void DUMMYAUD_WaitAudio(_THIS)
101{
102 /* Don't block on first calls to simulate initial fragment filling. */
103 if (this->hidden->initial_calls)
104 this->hidden->initial_calls--;
105 else
106 SDL_Delay(this->hidden->write_delay);
107}
108
109static void DUMMYAUD_PlayAudio(_THIS)
110{
111 /* no-op...this is a null driver. */
112}
113
114static Uint8 *DUMMYAUD_GetAudioBuf(_THIS)
115{
116 return(this->hidden->mixbuf);
117}
118
119static void DUMMYAUD_CloseAudio(_THIS)
120{
121 if ( this->hidden->mixbuf != NULL ) {
122 SDL_FreeAudioMem(this->hidden->mixbuf);
123 this->hidden->mixbuf = NULL;
124 }
125}
126
127static int DUMMYAUD_OpenAudio(_THIS, SDL_AudioSpec *spec)
128{
129 float bytes_per_sec = 0.0f;
130
131 /* Allocate mixing buffer */
132 this->hidden->mixlen = spec->size;
133 this->hidden->mixbuf = (Uint8 *) SDL_AllocAudioMem(this->hidden->mixlen);
134 if ( this->hidden->mixbuf == NULL ) {
135 return(-1);
136 }
137 SDL_memset(this->hidden->mixbuf, spec->silence, spec->size);
138
139 bytes_per_sec = (float) (((spec->format & 0xFF) / 8) *
140 spec->channels * spec->freq);
141
142 /*
143 * We try to make this request more audio at the correct rate for
144 * a given audio spec, so timing stays fairly faithful.
145 * Also, we have it not block at all for the first two calls, so
146 * it seems like we're filling two audio fragments right out of the
147 * gate, like other SDL drivers tend to do.
148 */
149 this->hidden->initial_calls = 2;
150 this->hidden->write_delay =
151 (Uint32) ((((float) spec->size) / bytes_per_sec) * 1000.0f);
152
153 /* We're ready to rock and roll. :-) */
154 return(0);
155}
156