Mercurial > sdl-ios-xcode
comparison src/audio/dummy/SDL_dummyaudio.c @ 2049:5f6550e5184f
Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
author | Ryan C. Gordon <icculus@icculus.org> |
---|---|
date | Tue, 17 Oct 2006 09:15:21 +0000 |
parents | c121d94672cb |
children | 866052b01ee5 |
comparison
equal
deleted
inserted
replaced
2048:6067c7f9a672 | 2049:5f6550e5184f |
---|---|
23 */ | 23 */ |
24 #include "SDL_config.h" | 24 #include "SDL_config.h" |
25 | 25 |
26 /* Output audio to nowhere... */ | 26 /* Output audio to nowhere... */ |
27 | 27 |
28 #include "SDL_rwops.h" | |
29 #include "SDL_timer.h" | |
30 #include "SDL_audio.h" | 28 #include "SDL_audio.h" |
31 #include "../SDL_audiomem.h" | |
32 #include "../SDL_audio_c.h" | 29 #include "../SDL_audio_c.h" |
33 #include "../SDL_audiodev_c.h" | |
34 #include "SDL_dummyaudio.h" | 30 #include "SDL_dummyaudio.h" |
35 | 31 |
36 /* The tag name used by DUMMY audio */ | |
37 #define DUMMYAUD_DRIVER_NAME "dummy" | |
38 | |
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); | |
45 | |
46 /* Audio driver bootstrap functions */ | |
47 static int | 32 static int |
48 DUMMYAUD_Available(void) | 33 DUMMYAUD_OpenDevice(_THIS, const char *devname, int iscapture) |
49 { | 34 { |
50 const char *envr = SDL_getenv("SDL_AUDIODRIVER"); | 35 return 1; /* always succeeds. */ |
51 if (envr && (SDL_strcmp(envr, DUMMYAUD_DRIVER_NAME) == 0)) { | |
52 return (1); | |
53 } | |
54 return (0); | |
55 } | 36 } |
56 | 37 |
57 static void | 38 static int |
58 DUMMYAUD_DeleteDevice(SDL_AudioDevice * device) | 39 DUMMYAUD_Init(SDL_AudioDriverImpl *impl) |
59 { | 40 { |
60 SDL_free(device->hidden); | |
61 SDL_free(device); | |
62 } | |
63 | |
64 static SDL_AudioDevice * | |
65 DUMMYAUD_CreateDevice(int devindex) | |
66 { | |
67 SDL_AudioDevice *this; | |
68 | |
69 /* Initialize all variables that we clean on shutdown */ | |
70 this = (SDL_AudioDevice *) SDL_malloc(sizeof(SDL_AudioDevice)); | |
71 if (this) { | |
72 SDL_memset(this, 0, (sizeof *this)); | |
73 this->hidden = (struct SDL_PrivateAudioData *) | |
74 SDL_malloc((sizeof *this->hidden)); | |
75 } | |
76 if ((this == NULL) || (this->hidden == NULL)) { | |
77 SDL_OutOfMemory(); | |
78 if (this) { | |
79 SDL_free(this); | |
80 } | |
81 return (0); | |
82 } | |
83 SDL_memset(this->hidden, 0, (sizeof *this->hidden)); | |
84 | |
85 /* Set the function pointers */ | 41 /* Set the function pointers */ |
86 this->OpenAudio = DUMMYAUD_OpenAudio; | 42 impl->OpenDevice = DUMMYAUD_OpenDevice; |
87 this->WaitAudio = DUMMYAUD_WaitAudio; | 43 impl->OnlyHasDefaultOutputDevice = 1; |
88 this->PlayAudio = DUMMYAUD_PlayAudio; | 44 return 1; |
89 this->GetAudioBuf = DUMMYAUD_GetAudioBuf; | |
90 this->CloseAudio = DUMMYAUD_CloseAudio; | |
91 | |
92 this->free = DUMMYAUD_DeleteDevice; | |
93 | |
94 return this; | |
95 } | 45 } |
96 | 46 |
97 AudioBootStrap DUMMYAUD_bootstrap = { | 47 AudioBootStrap DUMMYAUD_bootstrap = { |
98 DUMMYAUD_DRIVER_NAME, "SDL dummy audio driver", | 48 "dummy", "SDL dummy audio driver", DUMMYAUD_Init, 1 |
99 DUMMYAUD_Available, DUMMYAUD_CreateDevice | |
100 }; | 49 }; |
101 | 50 |
102 /* This function waits until it is possible to write a full sound buffer */ | |
103 static void | |
104 DUMMYAUD_WaitAudio(_THIS) | |
105 { | |
106 /* Don't block on first calls to simulate initial fragment filling. */ | |
107 if (this->hidden->initial_calls) | |
108 this->hidden->initial_calls--; | |
109 else | |
110 SDL_Delay(this->hidden->write_delay); | |
111 } | |
112 | |
113 static void | |
114 DUMMYAUD_PlayAudio(_THIS) | |
115 { | |
116 /* no-op...this is a null driver. */ | |
117 } | |
118 | |
119 static Uint8 * | |
120 DUMMYAUD_GetAudioBuf(_THIS) | |
121 { | |
122 return (this->hidden->mixbuf); | |
123 } | |
124 | |
125 static void | |
126 DUMMYAUD_CloseAudio(_THIS) | |
127 { | |
128 if (this->hidden->mixbuf != NULL) { | |
129 SDL_FreeAudioMem(this->hidden->mixbuf); | |
130 this->hidden->mixbuf = NULL; | |
131 } | |
132 } | |
133 | |
134 static int | |
135 DUMMYAUD_OpenAudio(_THIS, SDL_AudioSpec * spec) | |
136 { | |
137 float bytes_per_sec = 0.0f; | |
138 | |
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) { | |
143 return (-1); | |
144 } | |
145 SDL_memset(this->hidden->mixbuf, spec->silence, spec->size); | |
146 | |
147 bytes_per_sec = (float) (((spec->format & 0xFF) / 8) * | |
148 spec->channels * spec->freq); | |
149 | |
150 /* | |
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. | |
156 */ | |
157 this->hidden->initial_calls = 2; | |
158 this->hidden->write_delay = | |
159 (Uint32) ((((float) spec->size) / bytes_per_sec) * 1000.0f); | |
160 | |
161 /* We're ready to rock and roll. :-) */ | |
162 return (0); | |
163 } | |
164 | |
165 /* vi: set ts=4 sw=4 expandtab: */ | 51 /* vi: set ts=4 sw=4 expandtab: */ |