comparison src/audio/esd/SDL_esdaudio.c @ 1668:4da1ee79c9af SDL-1.3

more tweaking indent options
author Sam Lantinga <slouken@libsdl.org>
date Mon, 29 May 2006 04:04:35 +0000
parents 782fd950bd46
children
comparison
equal deleted inserted replaced
1667:1fddae038bc8 1668:4da1ee79c9af
45 45
46 /* The tag name used by ESD audio */ 46 /* The tag name used by ESD audio */
47 #define ESD_DRIVER_NAME "esd" 47 #define ESD_DRIVER_NAME "esd"
48 48
49 /* Audio driver functions */ 49 /* Audio driver functions */
50 static int ESD_OpenAudio (_THIS, SDL_AudioSpec * spec); 50 static int ESD_OpenAudio(_THIS, SDL_AudioSpec * spec);
51 static void ESD_WaitAudio (_THIS); 51 static void ESD_WaitAudio(_THIS);
52 static void ESD_PlayAudio (_THIS); 52 static void ESD_PlayAudio(_THIS);
53 static Uint8 *ESD_GetAudioBuf (_THIS); 53 static Uint8 *ESD_GetAudioBuf(_THIS);
54 static void ESD_CloseAudio (_THIS); 54 static void ESD_CloseAudio(_THIS);
55 55
56 #ifdef SDL_AUDIO_DRIVER_ESD_DYNAMIC 56 #ifdef SDL_AUDIO_DRIVER_ESD_DYNAMIC
57 57
58 static const char *esd_library = SDL_AUDIO_DRIVER_ESD_DYNAMIC; 58 static const char *esd_library = SDL_AUDIO_DRIVER_ESD_DYNAMIC;
59 static void *esd_handle = NULL; 59 static void *esd_handle = NULL;
60 static int esd_loaded = 0; 60 static int esd_loaded = 0;
61 61
62 static int (*SDL_NAME (esd_open_sound)) (const char *host); 62 static int (*SDL_NAME(esd_open_sound)) (const char *host);
63 static int (*SDL_NAME (esd_close)) (int esd); 63 static int (*SDL_NAME(esd_close)) (int esd);
64 static int (*SDL_NAME (esd_play_stream)) (esd_format_t format, int rate, 64 static int (*SDL_NAME(esd_play_stream)) (esd_format_t format, int rate,
65 const char *host, const char *name); 65 const char *host, const char *name);
66 static struct 66 static struct
67 { 67 {
68 const char *name; 68 const char *name;
69 void **func; 69 void **func;
70 } esd_functions[] = { 70 } esd_functions[] = {
71 { 71 {
72 "esd_open_sound", (void **) &SDL_NAME (esd_open_sound)}, { 72 "esd_open_sound", (void **) &SDL_NAME(esd_open_sound)}, {
73 "esd_close", (void **) &SDL_NAME (esd_close)}, { 73 "esd_close", (void **) &SDL_NAME(esd_close)}, {
74 "esd_play_stream", (void **) &SDL_NAME (esd_play_stream)},}; 74 "esd_play_stream", (void **) &SDL_NAME(esd_play_stream)},};
75 75
76 static void 76 static void
77 UnloadESDLibrary () 77 UnloadESDLibrary()
78 { 78 {
79 if (esd_loaded) { 79 if (esd_loaded) {
80 SDL_UnloadObject (esd_handle); 80 SDL_UnloadObject(esd_handle);
81 esd_handle = NULL; 81 esd_handle = NULL;
82 esd_loaded = 0; 82 esd_loaded = 0;
83 } 83 }
84 } 84 }
85 85
86 static int 86 static int
87 LoadESDLibrary (void) 87 LoadESDLibrary(void)
88 { 88 {
89 int i, retval = -1; 89 int i, retval = -1;
90 90
91 esd_handle = SDL_LoadObject (esd_library); 91 esd_handle = SDL_LoadObject(esd_library);
92 if (esd_handle) { 92 if (esd_handle) {
93 esd_loaded = 1; 93 esd_loaded = 1;
94 retval = 0; 94 retval = 0;
95 for (i = 0; i < SDL_arraysize (esd_functions); ++i) { 95 for (i = 0; i < SDL_arraysize(esd_functions); ++i) {
96 *esd_functions[i].func = 96 *esd_functions[i].func =
97 SDL_LoadFunction (esd_handle, esd_functions[i].name); 97 SDL_LoadFunction(esd_handle, esd_functions[i].name);
98 if (!*esd_functions[i].func) { 98 if (!*esd_functions[i].func) {
99 retval = -1; 99 retval = -1;
100 UnloadESDLibrary (); 100 UnloadESDLibrary();
101 break; 101 break;
102 } 102 }
103 } 103 }
104 } 104 }
105 return retval; 105 return retval;
106 } 106 }
107 107
108 #else 108 #else
109 109
110 static void 110 static void
111 UnloadESDLibrary () 111 UnloadESDLibrary()
112 { 112 {
113 return; 113 return;
114 } 114 }
115 115
116 static int 116 static int
117 LoadESDLibrary (void) 117 LoadESDLibrary(void)
118 { 118 {
119 return 0; 119 return 0;
120 } 120 }
121 121
122 #endif /* SDL_AUDIO_DRIVER_ESD_DYNAMIC */ 122 #endif /* SDL_AUDIO_DRIVER_ESD_DYNAMIC */
123 123
124 /* Audio driver bootstrap functions */ 124 /* Audio driver bootstrap functions */
125 125
126 static int 126 static int
127 Audio_Available (void) 127 Audio_Available(void)
128 { 128 {
129 int connection; 129 int connection;
130 int available; 130 int available;
131 131
132 available = 0; 132 available = 0;
133 if (LoadESDLibrary () < 0) { 133 if (LoadESDLibrary() < 0) {
134 return available; 134 return available;
135 } 135 }
136 connection = SDL_NAME (esd_open_sound) (NULL); 136 connection = SDL_NAME(esd_open_sound) (NULL);
137 if (connection >= 0) { 137 if (connection >= 0) {
138 available = 1; 138 available = 1;
139 SDL_NAME (esd_close) (connection); 139 SDL_NAME(esd_close) (connection);
140 } 140 }
141 UnloadESDLibrary (); 141 UnloadESDLibrary();
142 return (available); 142 return (available);
143 } 143 }
144 144
145 static void 145 static void
146 Audio_DeleteDevice (SDL_AudioDevice * device) 146 Audio_DeleteDevice(SDL_AudioDevice * device)
147 { 147 {
148 SDL_free (device->hidden); 148 SDL_free(device->hidden);
149 SDL_free (device); 149 SDL_free(device);
150 UnloadESDLibrary (); 150 UnloadESDLibrary();
151 } 151 }
152 152
153 static SDL_AudioDevice * 153 static SDL_AudioDevice *
154 Audio_CreateDevice (int devindex) 154 Audio_CreateDevice(int devindex)
155 { 155 {
156 SDL_AudioDevice *this; 156 SDL_AudioDevice *this;
157 157
158 /* Initialize all variables that we clean on shutdown */ 158 /* Initialize all variables that we clean on shutdown */
159 LoadESDLibrary (); 159 LoadESDLibrary();
160 this = (SDL_AudioDevice *) SDL_malloc (sizeof (SDL_AudioDevice)); 160 this = (SDL_AudioDevice *) SDL_malloc(sizeof(SDL_AudioDevice));
161 if (this) { 161 if (this) {
162 SDL_memset (this, 0, (sizeof *this)); 162 SDL_memset(this, 0, (sizeof *this));
163 this->hidden = (struct SDL_PrivateAudioData *) 163 this->hidden = (struct SDL_PrivateAudioData *)
164 SDL_malloc ((sizeof *this->hidden)); 164 SDL_malloc((sizeof *this->hidden));
165 } 165 }
166 if ((this == NULL) || (this->hidden == NULL)) { 166 if ((this == NULL) || (this->hidden == NULL)) {
167 SDL_OutOfMemory (); 167 SDL_OutOfMemory();
168 if (this) { 168 if (this) {
169 SDL_free (this); 169 SDL_free(this);
170 } 170 }
171 return (0); 171 return (0);
172 } 172 }
173 SDL_memset (this->hidden, 0, (sizeof *this->hidden)); 173 SDL_memset(this->hidden, 0, (sizeof *this->hidden));
174 audio_fd = -1; 174 audio_fd = -1;
175 175
176 /* Set the function pointers */ 176 /* Set the function pointers */
177 this->OpenAudio = ESD_OpenAudio; 177 this->OpenAudio = ESD_OpenAudio;
178 this->WaitAudio = ESD_WaitAudio; 178 this->WaitAudio = ESD_WaitAudio;
190 Audio_Available, Audio_CreateDevice 190 Audio_Available, Audio_CreateDevice
191 }; 191 };
192 192
193 /* This function waits until it is possible to write a full sound buffer */ 193 /* This function waits until it is possible to write a full sound buffer */
194 static void 194 static void
195 ESD_WaitAudio (_THIS) 195 ESD_WaitAudio(_THIS)
196 { 196 {
197 Sint32 ticks; 197 Sint32 ticks;
198 198
199 /* Check to see if the thread-parent process is still alive */ 199 /* Check to see if the thread-parent process is still alive */
200 { 200 {
201 static int cnt = 0; 201 static int cnt = 0;
202 /* Note that this only works with thread implementations 202 /* Note that this only works with thread implementations
203 that use a different process id for each thread. 203 that use a different process id for each thread.
204 */ 204 */
205 if (parent && (((++cnt) % 10) == 0)) { /* Check every 10 loops */ 205 if (parent && (((++cnt) % 10) == 0)) { /* Check every 10 loops */
206 if (kill (parent, 0) < 0) { 206 if (kill(parent, 0) < 0) {
207 this->enabled = 0; 207 this->enabled = 0;
208 } 208 }
209 } 209 }
210 } 210 }
211 211
212 /* Use timer for general audio synchronization */ 212 /* Use timer for general audio synchronization */
213 ticks = ((Sint32) (next_frame - SDL_GetTicks ())) - FUDGE_TICKS; 213 ticks = ((Sint32) (next_frame - SDL_GetTicks())) - FUDGE_TICKS;
214 if (ticks > 0) { 214 if (ticks > 0) {
215 SDL_Delay (ticks); 215 SDL_Delay(ticks);
216 } 216 }
217 } 217 }
218 218
219 static void 219 static void
220 ESD_PlayAudio (_THIS) 220 ESD_PlayAudio(_THIS)
221 { 221 {
222 int written; 222 int written;
223 223
224 /* Write the audio data, checking for EAGAIN on broken audio drivers */ 224 /* Write the audio data, checking for EAGAIN on broken audio drivers */
225 do { 225 do {
226 written = write (audio_fd, mixbuf, mixlen); 226 written = write(audio_fd, mixbuf, mixlen);
227 if ((written < 0) && ((errno == 0) || (errno == EAGAIN))) { 227 if ((written < 0) && ((errno == 0) || (errno == EAGAIN))) {
228 SDL_Delay (1); /* Let a little CPU time go by */ 228 SDL_Delay(1); /* Let a little CPU time go by */
229 } 229 }
230 } 230 }
231 while ((written < 0) && 231 while ((written < 0) &&
232 ((errno == 0) || (errno == EAGAIN) || (errno == EINTR))); 232 ((errno == 0) || (errno == EAGAIN) || (errno == EINTR)));
233 233
239 this->enabled = 0; 239 this->enabled = 0;
240 } 240 }
241 } 241 }
242 242
243 static Uint8 * 243 static Uint8 *
244 ESD_GetAudioBuf (_THIS) 244 ESD_GetAudioBuf(_THIS)
245 { 245 {
246 return (mixbuf); 246 return (mixbuf);
247 } 247 }
248 248
249 static void 249 static void
250 ESD_CloseAudio (_THIS) 250 ESD_CloseAudio(_THIS)
251 { 251 {
252 if (mixbuf != NULL) { 252 if (mixbuf != NULL) {
253 SDL_FreeAudioMem (mixbuf); 253 SDL_FreeAudioMem(mixbuf);
254 mixbuf = NULL; 254 mixbuf = NULL;
255 } 255 }
256 if (audio_fd >= 0) { 256 if (audio_fd >= 0) {
257 SDL_NAME (esd_close) (audio_fd); 257 SDL_NAME(esd_close) (audio_fd);
258 audio_fd = -1; 258 audio_fd = -1;
259 } 259 }
260 } 260 }
261 261
262 /* Try to get the name of the program */ 262 /* Try to get the name of the program */
263 static char * 263 static char *
264 get_progname (void) 264 get_progname(void)
265 { 265 {
266 char *progname = NULL; 266 char *progname = NULL;
267 #ifdef __LINUX__ 267 #ifdef __LINUX__
268 FILE *fp; 268 FILE *fp;
269 static char temp[BUFSIZ]; 269 static char temp[BUFSIZ];
270 270
271 SDL_snprintf (temp, SDL_arraysize (temp), "/proc/%d/cmdline", getpid ()); 271 SDL_snprintf(temp, SDL_arraysize(temp), "/proc/%d/cmdline", getpid());
272 fp = fopen (temp, "r"); 272 fp = fopen(temp, "r");
273 if (fp != NULL) { 273 if (fp != NULL) {
274 if (fgets (temp, sizeof (temp) - 1, fp)) { 274 if (fgets(temp, sizeof(temp) - 1, fp)) {
275 progname = SDL_strrchr (temp, '/'); 275 progname = SDL_strrchr(temp, '/');
276 if (progname == NULL) { 276 if (progname == NULL) {
277 progname = temp; 277 progname = temp;
278 } else { 278 } else {
279 progname = progname + 1; 279 progname = progname + 1;
280 } 280 }
281 } 281 }
282 fclose (fp); 282 fclose(fp);
283 } 283 }
284 #endif 284 #endif
285 return (progname); 285 return (progname);
286 } 286 }
287 287
288 static int 288 static int
289 ESD_OpenAudio (_THIS, SDL_AudioSpec * spec) 289 ESD_OpenAudio(_THIS, SDL_AudioSpec * spec)
290 { 290 {
291 esd_format_t format; 291 esd_format_t format;
292 292
293 /* Convert audio spec to the ESD audio format */ 293 /* Convert audio spec to the ESD audio format */
294 format = (ESD_STREAM | ESD_PLAY); 294 format = (ESD_STREAM | ESD_PLAY);
298 break; 298 break;
299 case 16: 299 case 16:
300 format |= ESD_BITS16; 300 format |= ESD_BITS16;
301 break; 301 break;
302 default: 302 default:
303 SDL_SetError ("Unsupported ESD audio format"); 303 SDL_SetError("Unsupported ESD audio format");
304 return (-1); 304 return (-1);
305 } 305 }
306 if (spec->channels == 1) { 306 if (spec->channels == 1) {
307 format |= ESD_MONO; 307 format |= ESD_MONO;
308 } else { 308 } else {
312 spec->samples = ESD_BUF_SIZE; /* Darn, no way to change this yet */ 312 spec->samples = ESD_BUF_SIZE; /* Darn, no way to change this yet */
313 #endif 313 #endif
314 314
315 /* Open a connection to the ESD audio server */ 315 /* Open a connection to the ESD audio server */
316 audio_fd = 316 audio_fd =
317 SDL_NAME (esd_play_stream) (format, spec->freq, NULL, 317 SDL_NAME(esd_play_stream) (format, spec->freq, NULL, get_progname());
318 get_progname ());
319 if (audio_fd < 0) { 318 if (audio_fd < 0) {
320 SDL_SetError ("Couldn't open ESD connection"); 319 SDL_SetError("Couldn't open ESD connection");
321 return (-1); 320 return (-1);
322 } 321 }
323 322
324 /* Calculate the final parameters for this audio specification */ 323 /* Calculate the final parameters for this audio specification */
325 SDL_CalculateAudioSpec (spec); 324 SDL_CalculateAudioSpec(spec);
326 frame_ticks = (float) (spec->samples * 1000) / spec->freq; 325 frame_ticks = (float) (spec->samples * 1000) / spec->freq;
327 next_frame = SDL_GetTicks () + frame_ticks; 326 next_frame = SDL_GetTicks() + frame_ticks;
328 327
329 /* Allocate mixing buffer */ 328 /* Allocate mixing buffer */
330 mixlen = spec->size; 329 mixlen = spec->size;
331 mixbuf = (Uint8 *) SDL_AllocAudioMem (mixlen); 330 mixbuf = (Uint8 *) SDL_AllocAudioMem(mixlen);
332 if (mixbuf == NULL) { 331 if (mixbuf == NULL) {
333 return (-1); 332 return (-1);
334 } 333 }
335 SDL_memset (mixbuf, spec->silence, spec->size); 334 SDL_memset(mixbuf, spec->silence, spec->size);
336 335
337 /* Get the parent process id (we're the parent of the audio thread) */ 336 /* Get the parent process id (we're the parent of the audio thread) */
338 parent = getpid (); 337 parent = getpid();
339 338
340 /* We're ready to rock and roll. :-) */ 339 /* We're ready to rock and roll. :-) */
341 return (0); 340 return (0);
342 } 341 }
343 342