comparison src/audio/arts/SDL_artsaudio.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
39 39
40 /* The tag name used by artsc audio */ 40 /* The tag name used by artsc audio */
41 #define ARTS_DRIVER_NAME "arts" 41 #define ARTS_DRIVER_NAME "arts"
42 42
43 /* Audio driver functions */ 43 /* Audio driver functions */
44 static int ARTS_OpenAudio (_THIS, SDL_AudioSpec * spec); 44 static int ARTS_OpenAudio(_THIS, SDL_AudioSpec * spec);
45 static void ARTS_WaitAudio (_THIS); 45 static void ARTS_WaitAudio(_THIS);
46 static void ARTS_PlayAudio (_THIS); 46 static void ARTS_PlayAudio(_THIS);
47 static Uint8 *ARTS_GetAudioBuf (_THIS); 47 static Uint8 *ARTS_GetAudioBuf(_THIS);
48 static void ARTS_CloseAudio (_THIS); 48 static void ARTS_CloseAudio(_THIS);
49 49
50 #ifdef SDL_AUDIO_DRIVER_ARTS_DYNAMIC 50 #ifdef SDL_AUDIO_DRIVER_ARTS_DYNAMIC
51 51
52 static const char *arts_library = SDL_AUDIO_DRIVER_ARTS_DYNAMIC; 52 static const char *arts_library = SDL_AUDIO_DRIVER_ARTS_DYNAMIC;
53 static void *arts_handle = NULL; 53 static void *arts_handle = NULL;
54 static int arts_loaded = 0; 54 static int arts_loaded = 0;
55 55
56 static int (*SDL_NAME (arts_init)) (void); 56 static int (*SDL_NAME(arts_init)) (void);
57 static void (*SDL_NAME (arts_free)) (void); 57 static void (*SDL_NAME(arts_free)) (void);
58 static arts_stream_t (*SDL_NAME (arts_play_stream)) (int rate, int bits, 58 static arts_stream_t(*SDL_NAME(arts_play_stream)) (int rate, int bits,
59 int channels, 59 int channels,
60 const char *name); 60 const char *name);
61 static int (*SDL_NAME (arts_stream_set)) (arts_stream_t s, 61 static int (*SDL_NAME(arts_stream_set)) (arts_stream_t s,
62 arts_parameter_t param, int value); 62 arts_parameter_t param, int value);
63 static int (*SDL_NAME (arts_stream_get)) (arts_stream_t s, 63 static int (*SDL_NAME(arts_stream_get)) (arts_stream_t s,
64 arts_parameter_t param); 64 arts_parameter_t param);
65 static int (*SDL_NAME (arts_write)) (arts_stream_t s, const void *buffer, 65 static int (*SDL_NAME(arts_write)) (arts_stream_t s, const void *buffer,
66 int count); 66 int count);
67 static void (*SDL_NAME (arts_close_stream)) (arts_stream_t s); 67 static void (*SDL_NAME(arts_close_stream)) (arts_stream_t s);
68 68
69 static struct 69 static struct
70 { 70 {
71 const char *name; 71 const char *name;
72 void **func; 72 void **func;
73 } arts_functions[] = { 73 } arts_functions[] = {
74 { 74 {
75 "arts_init", (void **) &SDL_NAME (arts_init)}, { 75 "arts_init", (void **) &SDL_NAME(arts_init)}, {
76 "arts_free", (void **) &SDL_NAME (arts_free)}, { 76 "arts_free", (void **) &SDL_NAME(arts_free)}, {
77 "arts_play_stream", (void **) &SDL_NAME (arts_play_stream)}, { 77 "arts_play_stream", (void **) &SDL_NAME(arts_play_stream)}, {
78 "arts_stream_set", (void **) &SDL_NAME (arts_stream_set)}, { 78 "arts_stream_set", (void **) &SDL_NAME(arts_stream_set)}, {
79 "arts_stream_get", (void **) &SDL_NAME (arts_stream_get)}, { 79 "arts_stream_get", (void **) &SDL_NAME(arts_stream_get)}, {
80 "arts_write", (void **) &SDL_NAME (arts_write)}, { 80 "arts_write", (void **) &SDL_NAME(arts_write)}, {
81 "arts_close_stream", (void **) &SDL_NAME (arts_close_stream)},}; 81 "arts_close_stream", (void **) &SDL_NAME(arts_close_stream)},};
82 82
83 static void 83 static void
84 UnloadARTSLibrary () 84 UnloadARTSLibrary()
85 { 85 {
86 if (arts_loaded) { 86 if (arts_loaded) {
87 SDL_UnloadObject (arts_handle); 87 SDL_UnloadObject(arts_handle);
88 arts_handle = NULL; 88 arts_handle = NULL;
89 arts_loaded = 0; 89 arts_loaded = 0;
90 } 90 }
91 } 91 }
92 92
93 static int 93 static int
94 LoadARTSLibrary (void) 94 LoadARTSLibrary(void)
95 { 95 {
96 int i, retval = -1; 96 int i, retval = -1;
97 97
98 arts_handle = SDL_LoadObject (arts_library); 98 arts_handle = SDL_LoadObject(arts_library);
99 if (arts_handle) { 99 if (arts_handle) {
100 arts_loaded = 1; 100 arts_loaded = 1;
101 retval = 0; 101 retval = 0;
102 for (i = 0; i < SDL_arraysize (arts_functions); ++i) { 102 for (i = 0; i < SDL_arraysize(arts_functions); ++i) {
103 *arts_functions[i].func = 103 *arts_functions[i].func =
104 SDL_LoadFunction (arts_handle, arts_functions[i].name); 104 SDL_LoadFunction(arts_handle, arts_functions[i].name);
105 if (!*arts_functions[i].func) { 105 if (!*arts_functions[i].func) {
106 retval = -1; 106 retval = -1;
107 UnloadARTSLibrary (); 107 UnloadARTSLibrary();
108 break; 108 break;
109 } 109 }
110 } 110 }
111 } 111 }
112 return retval; 112 return retval;
113 } 113 }
114 114
115 #else 115 #else
116 116
117 static void 117 static void
118 UnloadARTSLibrary () 118 UnloadARTSLibrary()
119 { 119 {
120 return; 120 return;
121 } 121 }
122 122
123 static int 123 static int
124 LoadARTSLibrary (void) 124 LoadARTSLibrary(void)
125 { 125 {
126 return 0; 126 return 0;
127 } 127 }
128 128
129 #endif /* SDL_AUDIO_DRIVER_ARTS_DYNAMIC */ 129 #endif /* SDL_AUDIO_DRIVER_ARTS_DYNAMIC */
130 130
131 /* Audio driver bootstrap functions */ 131 /* Audio driver bootstrap functions */
132 132
133 static int 133 static int
134 Audio_Available (void) 134 Audio_Available(void)
135 { 135 {
136 int available = 0; 136 int available = 0;
137 137
138 if (LoadARTSLibrary () < 0) { 138 if (LoadARTSLibrary() < 0) {
139 return available; 139 return available;
140 } 140 }
141 if (SDL_NAME (arts_init) () == 0) { 141 if (SDL_NAME(arts_init) () == 0) {
142 #define ARTS_CRASH_HACK /* Play a stream so aRts doesn't crash */ 142 #define ARTS_CRASH_HACK /* Play a stream so aRts doesn't crash */
143 #ifdef ARTS_CRASH_HACK 143 #ifdef ARTS_CRASH_HACK
144 arts_stream_t stream2; 144 arts_stream_t stream2;
145 stream2 = SDL_NAME (arts_play_stream) (44100, 16, 2, "SDL"); 145 stream2 = SDL_NAME(arts_play_stream) (44100, 16, 2, "SDL");
146 SDL_NAME (arts_write) (stream2, "", 0); 146 SDL_NAME(arts_write) (stream2, "", 0);
147 SDL_NAME (arts_close_stream) (stream2); 147 SDL_NAME(arts_close_stream) (stream2);
148 #endif 148 #endif
149 available = 1; 149 available = 1;
150 SDL_NAME (arts_free) (); 150 SDL_NAME(arts_free) ();
151 } 151 }
152 UnloadARTSLibrary (); 152 UnloadARTSLibrary();
153 153
154 return available; 154 return available;
155 } 155 }
156 156
157 static void 157 static void
158 Audio_DeleteDevice (SDL_AudioDevice * device) 158 Audio_DeleteDevice(SDL_AudioDevice * device)
159 { 159 {
160 SDL_free (device->hidden); 160 SDL_free(device->hidden);
161 SDL_free (device); 161 SDL_free(device);
162 UnloadARTSLibrary (); 162 UnloadARTSLibrary();
163 } 163 }
164 164
165 static SDL_AudioDevice * 165 static SDL_AudioDevice *
166 Audio_CreateDevice (int devindex) 166 Audio_CreateDevice(int devindex)
167 { 167 {
168 SDL_AudioDevice *this; 168 SDL_AudioDevice *this;
169 169
170 /* Initialize all variables that we clean on shutdown */ 170 /* Initialize all variables that we clean on shutdown */
171 LoadARTSLibrary (); 171 LoadARTSLibrary();
172 this = (SDL_AudioDevice *) SDL_malloc (sizeof (SDL_AudioDevice)); 172 this = (SDL_AudioDevice *) SDL_malloc(sizeof(SDL_AudioDevice));
173 if (this) { 173 if (this) {
174 SDL_memset (this, 0, (sizeof *this)); 174 SDL_memset(this, 0, (sizeof *this));
175 this->hidden = (struct SDL_PrivateAudioData *) 175 this->hidden = (struct SDL_PrivateAudioData *)
176 SDL_malloc ((sizeof *this->hidden)); 176 SDL_malloc((sizeof *this->hidden));
177 } 177 }
178 if ((this == NULL) || (this->hidden == NULL)) { 178 if ((this == NULL) || (this->hidden == NULL)) {
179 SDL_OutOfMemory (); 179 SDL_OutOfMemory();
180 if (this) { 180 if (this) {
181 SDL_free (this); 181 SDL_free(this);
182 } 182 }
183 return (0); 183 return (0);
184 } 184 }
185 SDL_memset (this->hidden, 0, (sizeof *this->hidden)); 185 SDL_memset(this->hidden, 0, (sizeof *this->hidden));
186 stream = 0; 186 stream = 0;
187 187
188 /* Set the function pointers */ 188 /* Set the function pointers */
189 this->OpenAudio = ARTS_OpenAudio; 189 this->OpenAudio = ARTS_OpenAudio;
190 this->WaitAudio = ARTS_WaitAudio; 190 this->WaitAudio = ARTS_WaitAudio;
202 Audio_Available, Audio_CreateDevice 202 Audio_Available, Audio_CreateDevice
203 }; 203 };
204 204
205 /* This function waits until it is possible to write a full sound buffer */ 205 /* This function waits until it is possible to write a full sound buffer */
206 static void 206 static void
207 ARTS_WaitAudio (_THIS) 207 ARTS_WaitAudio(_THIS)
208 { 208 {
209 Sint32 ticks; 209 Sint32 ticks;
210 210
211 /* Check to see if the thread-parent process is still alive */ 211 /* Check to see if the thread-parent process is still alive */
212 { 212 {
213 static int cnt = 0; 213 static int cnt = 0;
214 /* Note that this only works with thread implementations 214 /* Note that this only works with thread implementations
215 that use a different process id for each thread. 215 that use a different process id for each thread.
216 */ 216 */
217 if (parent && (((++cnt) % 10) == 0)) { /* Check every 10 loops */ 217 if (parent && (((++cnt) % 10) == 0)) { /* Check every 10 loops */
218 if (kill (parent, 0) < 0) { 218 if (kill(parent, 0) < 0) {
219 this->enabled = 0; 219 this->enabled = 0;
220 } 220 }
221 } 221 }
222 } 222 }
223 223
224 /* Use timer for general audio synchronization */ 224 /* Use timer for general audio synchronization */
225 ticks = ((Sint32) (next_frame - SDL_GetTicks ())) - FUDGE_TICKS; 225 ticks = ((Sint32) (next_frame - SDL_GetTicks())) - FUDGE_TICKS;
226 if (ticks > 0) { 226 if (ticks > 0) {
227 SDL_Delay (ticks); 227 SDL_Delay(ticks);
228 } 228 }
229 } 229 }
230 230
231 static void 231 static void
232 ARTS_PlayAudio (_THIS) 232 ARTS_PlayAudio(_THIS)
233 { 233 {
234 int written; 234 int written;
235 235
236 /* Write the audio data */ 236 /* Write the audio data */
237 written = SDL_NAME (arts_write) (stream, mixbuf, mixlen); 237 written = SDL_NAME(arts_write) (stream, mixbuf, mixlen);
238 238
239 /* If timer synchronization is enabled, set the next write frame */ 239 /* If timer synchronization is enabled, set the next write frame */
240 if (frame_ticks) { 240 if (frame_ticks) {
241 next_frame += frame_ticks; 241 next_frame += frame_ticks;
242 } 242 }
244 /* If we couldn't write, assume fatal error for now */ 244 /* If we couldn't write, assume fatal error for now */
245 if (written < 0) { 245 if (written < 0) {
246 this->enabled = 0; 246 this->enabled = 0;
247 } 247 }
248 #ifdef DEBUG_AUDIO 248 #ifdef DEBUG_AUDIO
249 fprintf (stderr, "Wrote %d bytes of audio data\n", written); 249 fprintf(stderr, "Wrote %d bytes of audio data\n", written);
250 #endif 250 #endif
251 } 251 }
252 252
253 static Uint8 * 253 static Uint8 *
254 ARTS_GetAudioBuf (_THIS) 254 ARTS_GetAudioBuf(_THIS)
255 { 255 {
256 return (mixbuf); 256 return (mixbuf);
257 } 257 }
258 258
259 static void 259 static void
260 ARTS_CloseAudio (_THIS) 260 ARTS_CloseAudio(_THIS)
261 { 261 {
262 if (mixbuf != NULL) { 262 if (mixbuf != NULL) {
263 SDL_FreeAudioMem (mixbuf); 263 SDL_FreeAudioMem(mixbuf);
264 mixbuf = NULL; 264 mixbuf = NULL;
265 } 265 }
266 if (stream) { 266 if (stream) {
267 SDL_NAME (arts_close_stream) (stream); 267 SDL_NAME(arts_close_stream) (stream);
268 stream = 0; 268 stream = 0;
269 } 269 }
270 SDL_NAME (arts_free) (); 270 SDL_NAME(arts_free) ();
271 } 271 }
272 272
273 static int 273 static int
274 ARTS_OpenAudio (_THIS, SDL_AudioSpec * spec) 274 ARTS_OpenAudio(_THIS, SDL_AudioSpec * spec)
275 { 275 {
276 int bits, frag_spec; 276 int bits, frag_spec;
277 Uint16 test_format, format; 277 Uint16 test_format, format;
278 278
279 /* Reset the timer synchronization flag */ 279 /* Reset the timer synchronization flag */
282 mixbuf = NULL; 282 mixbuf = NULL;
283 283
284 /* Try for a closest match on audio format */ 284 /* Try for a closest match on audio format */
285 format = 0; 285 format = 0;
286 bits = 0; 286 bits = 0;
287 for (test_format = SDL_FirstAudioFormat (spec->format); 287 for (test_format = SDL_FirstAudioFormat(spec->format);
288 !format && test_format;) { 288 !format && test_format;) {
289 #ifdef DEBUG_AUDIO 289 #ifdef DEBUG_AUDIO
290 fprintf (stderr, "Trying format 0x%4.4x\n", test_format); 290 fprintf(stderr, "Trying format 0x%4.4x\n", test_format);
291 #endif 291 #endif
292 switch (test_format) { 292 switch (test_format) {
293 case AUDIO_U8: 293 case AUDIO_U8:
294 bits = 8; 294 bits = 8;
295 format = 1; 295 format = 1;
301 default: 301 default:
302 format = 0; 302 format = 0;
303 break; 303 break;
304 } 304 }
305 if (!format) { 305 if (!format) {
306 test_format = SDL_NextAudioFormat (); 306 test_format = SDL_NextAudioFormat();
307 } 307 }
308 } 308 }
309 if (format == 0) { 309 if (format == 0) {
310 SDL_SetError ("Couldn't find any hardware audio formats"); 310 SDL_SetError("Couldn't find any hardware audio formats");
311 return (-1); 311 return (-1);
312 } 312 }
313 spec->format = test_format; 313 spec->format = test_format;
314 314
315 if (SDL_NAME (arts_init) () != 0) { 315 if (SDL_NAME(arts_init) () != 0) {
316 SDL_SetError ("Unable to initialize ARTS"); 316 SDL_SetError("Unable to initialize ARTS");
317 return (-1); 317 return (-1);
318 } 318 }
319 stream = 319 stream =
320 SDL_NAME (arts_play_stream) (spec->freq, bits, spec->channels, "SDL"); 320 SDL_NAME(arts_play_stream) (spec->freq, bits, spec->channels, "SDL");
321 321
322 /* Calculate the final parameters for this audio specification */ 322 /* Calculate the final parameters for this audio specification */
323 SDL_CalculateAudioSpec (spec); 323 SDL_CalculateAudioSpec(spec);
324 324
325 /* Determine the power of two of the fragment size */ 325 /* Determine the power of two of the fragment size */
326 for (frag_spec = 0; (0x01 << frag_spec) < spec->size; ++frag_spec); 326 for (frag_spec = 0; (0x01 << frag_spec) < spec->size; ++frag_spec);
327 if ((0x01 << frag_spec) != spec->size) { 327 if ((0x01 << frag_spec) != spec->size) {
328 SDL_SetError ("Fragment size must be a power of two"); 328 SDL_SetError("Fragment size must be a power of two");
329 return (-1); 329 return (-1);
330 } 330 }
331 frag_spec |= 0x00020000; /* two fragments, for low latency */ 331 frag_spec |= 0x00020000; /* two fragments, for low latency */
332 332
333 #ifdef ARTS_P_PACKET_SETTINGS 333 #ifdef ARTS_P_PACKET_SETTINGS
334 SDL_NAME (arts_stream_set) (stream, ARTS_P_PACKET_SETTINGS, frag_spec); 334 SDL_NAME(arts_stream_set) (stream, ARTS_P_PACKET_SETTINGS, frag_spec);
335 #else 335 #else
336 SDL_NAME (arts_stream_set) (stream, ARTS_P_PACKET_SIZE, 336 SDL_NAME(arts_stream_set) (stream, ARTS_P_PACKET_SIZE,
337 frag_spec & 0xffff); 337 frag_spec & 0xffff);
338 SDL_NAME (arts_stream_set) (stream, ARTS_P_PACKET_COUNT, frag_spec >> 16); 338 SDL_NAME(arts_stream_set) (stream, ARTS_P_PACKET_COUNT, frag_spec >> 16);
339 #endif 339 #endif
340 spec->size = SDL_NAME (arts_stream_get) (stream, ARTS_P_PACKET_SIZE); 340 spec->size = SDL_NAME(arts_stream_get) (stream, ARTS_P_PACKET_SIZE);
341 341
342 /* Allocate mixing buffer */ 342 /* Allocate mixing buffer */
343 mixlen = spec->size; 343 mixlen = spec->size;
344 mixbuf = (Uint8 *) SDL_AllocAudioMem (mixlen); 344 mixbuf = (Uint8 *) SDL_AllocAudioMem(mixlen);
345 if (mixbuf == NULL) { 345 if (mixbuf == NULL) {
346 return (-1); 346 return (-1);
347 } 347 }
348 SDL_memset (mixbuf, spec->silence, spec->size); 348 SDL_memset(mixbuf, spec->silence, spec->size);
349 349
350 /* Get the parent process id (we're the parent of the audio thread) */ 350 /* Get the parent process id (we're the parent of the audio thread) */
351 parent = getpid (); 351 parent = getpid();
352 352
353 /* We're ready to rock and roll. :-) */ 353 /* We're ready to rock and roll. :-) */
354 return (0); 354 return (0);
355 } 355 }
356 356