Mercurial > sdl-ios-xcode
comparison src/audio/sun/SDL_sunaudio.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 |
---|---|
46 | 46 |
47 /* Open the audio device for playback, and don't block if busy */ | 47 /* Open the audio device for playback, and don't block if busy */ |
48 #define OPEN_FLAGS (O_WRONLY|O_NONBLOCK) | 48 #define OPEN_FLAGS (O_WRONLY|O_NONBLOCK) |
49 | 49 |
50 /* Audio driver functions */ | 50 /* Audio driver functions */ |
51 static int DSP_OpenAudio (_THIS, SDL_AudioSpec * spec); | 51 static int DSP_OpenAudio(_THIS, SDL_AudioSpec * spec); |
52 static void DSP_WaitAudio (_THIS); | 52 static void DSP_WaitAudio(_THIS); |
53 static void DSP_PlayAudio (_THIS); | 53 static void DSP_PlayAudio(_THIS); |
54 static Uint8 *DSP_GetAudioBuf (_THIS); | 54 static Uint8 *DSP_GetAudioBuf(_THIS); |
55 static void DSP_CloseAudio (_THIS); | 55 static void DSP_CloseAudio(_THIS); |
56 | 56 |
57 static Uint8 snd2au (int sample); | 57 static Uint8 snd2au(int sample); |
58 | 58 |
59 /* Audio driver bootstrap functions */ | 59 /* Audio driver bootstrap functions */ |
60 | 60 |
61 static int | 61 static int |
62 Audio_Available (void) | 62 Audio_Available(void) |
63 { | 63 { |
64 int fd; | 64 int fd; |
65 int available; | 65 int available; |
66 | 66 |
67 available = 0; | 67 available = 0; |
68 fd = SDL_OpenAudioPath (NULL, 0, OPEN_FLAGS, 1); | 68 fd = SDL_OpenAudioPath(NULL, 0, OPEN_FLAGS, 1); |
69 if (fd >= 0) { | 69 if (fd >= 0) { |
70 available = 1; | 70 available = 1; |
71 close (fd); | 71 close(fd); |
72 } | 72 } |
73 return (available); | 73 return (available); |
74 } | 74 } |
75 | 75 |
76 static void | 76 static void |
77 Audio_DeleteDevice (SDL_AudioDevice * device) | 77 Audio_DeleteDevice(SDL_AudioDevice * device) |
78 { | 78 { |
79 SDL_free (device->hidden); | 79 SDL_free(device->hidden); |
80 SDL_free (device); | 80 SDL_free(device); |
81 } | 81 } |
82 | 82 |
83 static SDL_AudioDevice * | 83 static SDL_AudioDevice * |
84 Audio_CreateDevice (int devindex) | 84 Audio_CreateDevice(int devindex) |
85 { | 85 { |
86 SDL_AudioDevice *this; | 86 SDL_AudioDevice *this; |
87 | 87 |
88 /* Initialize all variables that we clean on shutdown */ | 88 /* Initialize all variables that we clean on shutdown */ |
89 this = (SDL_AudioDevice *) SDL_malloc (sizeof (SDL_AudioDevice)); | 89 this = (SDL_AudioDevice *) SDL_malloc(sizeof(SDL_AudioDevice)); |
90 if (this) { | 90 if (this) { |
91 SDL_memset (this, 0, (sizeof *this)); | 91 SDL_memset(this, 0, (sizeof *this)); |
92 this->hidden = (struct SDL_PrivateAudioData *) | 92 this->hidden = (struct SDL_PrivateAudioData *) |
93 SDL_malloc ((sizeof *this->hidden)); | 93 SDL_malloc((sizeof *this->hidden)); |
94 } | 94 } |
95 if ((this == NULL) || (this->hidden == NULL)) { | 95 if ((this == NULL) || (this->hidden == NULL)) { |
96 SDL_OutOfMemory (); | 96 SDL_OutOfMemory(); |
97 if (this) { | 97 if (this) { |
98 SDL_free (this); | 98 SDL_free(this); |
99 } | 99 } |
100 return (0); | 100 return (0); |
101 } | 101 } |
102 SDL_memset (this->hidden, 0, (sizeof *this->hidden)); | 102 SDL_memset(this->hidden, 0, (sizeof *this->hidden)); |
103 audio_fd = -1; | 103 audio_fd = -1; |
104 | 104 |
105 /* Set the function pointers */ | 105 /* Set the function pointers */ |
106 this->OpenAudio = DSP_OpenAudio; | 106 this->OpenAudio = DSP_OpenAudio; |
107 this->WaitAudio = DSP_WaitAudio; | 107 this->WaitAudio = DSP_WaitAudio; |
119 Audio_Available, Audio_CreateDevice | 119 Audio_Available, Audio_CreateDevice |
120 }; | 120 }; |
121 | 121 |
122 #ifdef DEBUG_AUDIO | 122 #ifdef DEBUG_AUDIO |
123 void | 123 void |
124 CheckUnderflow (_THIS) | 124 CheckUnderflow(_THIS) |
125 { | 125 { |
126 #ifdef AUDIO_GETINFO | 126 #ifdef AUDIO_GETINFO |
127 audio_info_t info; | 127 audio_info_t info; |
128 int left; | 128 int left; |
129 | 129 |
130 ioctl (audio_fd, AUDIO_GETINFO, &info); | 130 ioctl(audio_fd, AUDIO_GETINFO, &info); |
131 left = (written - info.play.samples); | 131 left = (written - info.play.samples); |
132 if (written && (left == 0)) { | 132 if (written && (left == 0)) { |
133 fprintf (stderr, "audio underflow!\n"); | 133 fprintf(stderr, "audio underflow!\n"); |
134 } | 134 } |
135 #endif | 135 #endif |
136 } | 136 } |
137 #endif | 137 #endif |
138 | 138 |
139 void | 139 void |
140 DSP_WaitAudio (_THIS) | 140 DSP_WaitAudio(_THIS) |
141 { | 141 { |
142 #ifdef AUDIO_GETINFO | 142 #ifdef AUDIO_GETINFO |
143 #define SLEEP_FUDGE 10 /* 10 ms scheduling fudge factor */ | 143 #define SLEEP_FUDGE 10 /* 10 ms scheduling fudge factor */ |
144 audio_info_t info; | 144 audio_info_t info; |
145 Sint32 left; | 145 Sint32 left; |
146 | 146 |
147 ioctl (audio_fd, AUDIO_GETINFO, &info); | 147 ioctl(audio_fd, AUDIO_GETINFO, &info); |
148 left = (written - info.play.samples); | 148 left = (written - info.play.samples); |
149 if (left > fragsize) { | 149 if (left > fragsize) { |
150 Sint32 sleepy; | 150 Sint32 sleepy; |
151 | 151 |
152 sleepy = ((left - fragsize) / frequency); | 152 sleepy = ((left - fragsize) / frequency); |
153 sleepy -= SLEEP_FUDGE; | 153 sleepy -= SLEEP_FUDGE; |
154 if (sleepy > 0) { | 154 if (sleepy > 0) { |
155 SDL_Delay (sleepy); | 155 SDL_Delay(sleepy); |
156 } | 156 } |
157 } | 157 } |
158 #else | 158 #else |
159 fd_set fdset; | 159 fd_set fdset; |
160 | 160 |
161 FD_ZERO (&fdset); | 161 FD_ZERO(&fdset); |
162 FD_SET (audio_fd, &fdset); | 162 FD_SET(audio_fd, &fdset); |
163 select (audio_fd + 1, NULL, &fdset, NULL, NULL); | 163 select(audio_fd + 1, NULL, &fdset, NULL, NULL); |
164 #endif | 164 #endif |
165 } | 165 } |
166 | 166 |
167 void | 167 void |
168 DSP_PlayAudio (_THIS) | 168 DSP_PlayAudio(_THIS) |
169 { | 169 { |
170 /* Write the audio data */ | 170 /* Write the audio data */ |
171 if (ulaw_only) { | 171 if (ulaw_only) { |
172 /* Assuming that this->spec.freq >= 8000 Hz */ | 172 /* Assuming that this->spec.freq >= 8000 Hz */ |
173 int accum, incr, pos; | 173 int accum, incr, pos; |
181 { | 181 { |
182 Uint8 *sndbuf; | 182 Uint8 *sndbuf; |
183 | 183 |
184 sndbuf = mixbuf; | 184 sndbuf = mixbuf; |
185 for (pos = 0; pos < fragsize; ++pos) { | 185 for (pos = 0; pos < fragsize; ++pos) { |
186 *aubuf = snd2au ((0x80 - *sndbuf) * 64); | 186 *aubuf = snd2au((0x80 - *sndbuf) * 64); |
187 accum += incr; | 187 accum += incr; |
188 while (accum > 0) { | 188 while (accum > 0) { |
189 accum -= 1000; | 189 accum -= 1000; |
190 sndbuf += 1; | 190 sndbuf += 1; |
191 } | 191 } |
197 { | 197 { |
198 Sint16 *sndbuf; | 198 Sint16 *sndbuf; |
199 | 199 |
200 sndbuf = (Sint16 *) mixbuf; | 200 sndbuf = (Sint16 *) mixbuf; |
201 for (pos = 0; pos < fragsize; ++pos) { | 201 for (pos = 0; pos < fragsize; ++pos) { |
202 *aubuf = snd2au (*sndbuf / 4); | 202 *aubuf = snd2au(*sndbuf / 4); |
203 accum += incr; | 203 accum += incr; |
204 while (accum > 0) { | 204 while (accum > 0) { |
205 accum -= 1000; | 205 accum -= 1000; |
206 sndbuf += 1; | 206 sndbuf += 1; |
207 } | 207 } |
209 } | 209 } |
210 } | 210 } |
211 break; | 211 break; |
212 } | 212 } |
213 #ifdef DEBUG_AUDIO | 213 #ifdef DEBUG_AUDIO |
214 CheckUnderflow (this); | 214 CheckUnderflow(this); |
215 #endif | 215 #endif |
216 if (write (audio_fd, ulaw_buf, fragsize) < 0) { | 216 if (write(audio_fd, ulaw_buf, fragsize) < 0) { |
217 /* Assume fatal error, for now */ | 217 /* Assume fatal error, for now */ |
218 this->enabled = 0; | 218 this->enabled = 0; |
219 } | 219 } |
220 written += fragsize; | 220 written += fragsize; |
221 } else { | 221 } else { |
222 #ifdef DEBUG_AUDIO | 222 #ifdef DEBUG_AUDIO |
223 CheckUnderflow (this); | 223 CheckUnderflow(this); |
224 #endif | 224 #endif |
225 if (write (audio_fd, mixbuf, this->spec.size) < 0) { | 225 if (write(audio_fd, mixbuf, this->spec.size) < 0) { |
226 /* Assume fatal error, for now */ | 226 /* Assume fatal error, for now */ |
227 this->enabled = 0; | 227 this->enabled = 0; |
228 } | 228 } |
229 written += fragsize; | 229 written += fragsize; |
230 } | 230 } |
231 } | 231 } |
232 | 232 |
233 Uint8 * | 233 Uint8 * |
234 DSP_GetAudioBuf (_THIS) | 234 DSP_GetAudioBuf(_THIS) |
235 { | 235 { |
236 return (mixbuf); | 236 return (mixbuf); |
237 } | 237 } |
238 | 238 |
239 void | 239 void |
240 DSP_CloseAudio (_THIS) | 240 DSP_CloseAudio(_THIS) |
241 { | 241 { |
242 if (mixbuf != NULL) { | 242 if (mixbuf != NULL) { |
243 SDL_FreeAudioMem (mixbuf); | 243 SDL_FreeAudioMem(mixbuf); |
244 mixbuf = NULL; | 244 mixbuf = NULL; |
245 } | 245 } |
246 if (ulaw_buf != NULL) { | 246 if (ulaw_buf != NULL) { |
247 SDL_free (ulaw_buf); | 247 SDL_free(ulaw_buf); |
248 ulaw_buf = NULL; | 248 ulaw_buf = NULL; |
249 } | 249 } |
250 close (audio_fd); | 250 close(audio_fd); |
251 } | 251 } |
252 | 252 |
253 int | 253 int |
254 DSP_OpenAudio (_THIS, SDL_AudioSpec * spec) | 254 DSP_OpenAudio(_THIS, SDL_AudioSpec * spec) |
255 { | 255 { |
256 char audiodev[1024]; | 256 char audiodev[1024]; |
257 #ifdef AUDIO_SETINFO | 257 #ifdef AUDIO_SETINFO |
258 int enc; | 258 int enc; |
259 #endif | 259 #endif |
285 } | 285 } |
286 break; | 286 break; |
287 | 287 |
288 default: | 288 default: |
289 { | 289 { |
290 SDL_SetError ("Unsupported audio format"); | 290 SDL_SetError("Unsupported audio format"); |
291 return (-1); | 291 return (-1); |
292 } | 292 } |
293 } | 293 } |
294 audio_fmt = spec->format; | 294 audio_fmt = spec->format; |
295 | 295 |
296 /* Open the audio device */ | 296 /* Open the audio device */ |
297 audio_fd = SDL_OpenAudioPath (audiodev, sizeof (audiodev), OPEN_FLAGS, 1); | 297 audio_fd = SDL_OpenAudioPath(audiodev, sizeof(audiodev), OPEN_FLAGS, 1); |
298 if (audio_fd < 0) { | 298 if (audio_fd < 0) { |
299 SDL_SetError ("Couldn't open %s: %s", audiodev, strerror (errno)); | 299 SDL_SetError("Couldn't open %s: %s", audiodev, strerror(errno)); |
300 return (-1); | 300 return (-1); |
301 } | 301 } |
302 | 302 |
303 ulaw_only = 0; /* modern Suns do support linear audio */ | 303 ulaw_only = 0; /* modern Suns do support linear audio */ |
304 #ifdef AUDIO_SETINFO | 304 #ifdef AUDIO_SETINFO |
305 for (;;) { | 305 for (;;) { |
306 audio_info_t info; | 306 audio_info_t info; |
307 AUDIO_INITINFO (&info); /* init all fields to "no change" */ | 307 AUDIO_INITINFO(&info); /* init all fields to "no change" */ |
308 | 308 |
309 /* Try to set the requested settings */ | 309 /* Try to set the requested settings */ |
310 info.play.sample_rate = spec->freq; | 310 info.play.sample_rate = spec->freq; |
311 info.play.channels = spec->channels; | 311 info.play.channels = spec->channels; |
312 info.play.precision = (enc == AUDIO_ENCODING_ULAW) | 312 info.play.precision = (enc == AUDIO_ENCODING_ULAW) |
313 ? 8 : spec->format & 0xff; | 313 ? 8 : spec->format & 0xff; |
314 info.play.encoding = enc; | 314 info.play.encoding = enc; |
315 if (ioctl (audio_fd, AUDIO_SETINFO, &info) == 0) { | 315 if (ioctl(audio_fd, AUDIO_SETINFO, &info) == 0) { |
316 | 316 |
317 /* Check to be sure we got what we wanted */ | 317 /* Check to be sure we got what we wanted */ |
318 if (ioctl (audio_fd, AUDIO_GETINFO, &info) < 0) { | 318 if (ioctl(audio_fd, AUDIO_GETINFO, &info) < 0) { |
319 SDL_SetError ("Error getting audio parameters: %s", | 319 SDL_SetError("Error getting audio parameters: %s", |
320 strerror (errno)); | 320 strerror(errno)); |
321 return -1; | 321 return -1; |
322 } | 322 } |
323 if (info.play.encoding == enc | 323 if (info.play.encoding == enc |
324 && info.play.precision == (spec->format & 0xff) | 324 && info.play.precision == (spec->format & 0xff) |
325 && info.play.channels == spec->channels) { | 325 && info.play.channels == spec->channels) { |
345 ulaw_only = 1; | 345 ulaw_only = 1; |
346 break; | 346 break; |
347 | 347 |
348 default: | 348 default: |
349 /* oh well... */ | 349 /* oh well... */ |
350 SDL_SetError ("Error setting audio parameters: %s", | 350 SDL_SetError("Error setting audio parameters: %s", |
351 strerror (errno)); | 351 strerror(errno)); |
352 return -1; | 352 return -1; |
353 } | 353 } |
354 } | 354 } |
355 #endif /* AUDIO_SETINFO */ | 355 #endif /* AUDIO_SETINFO */ |
356 written = 0; | 356 written = 0; |
358 /* We can actually convert on-the-fly to U-Law */ | 358 /* We can actually convert on-the-fly to U-Law */ |
359 if (ulaw_only) { | 359 if (ulaw_only) { |
360 spec->freq = desired_freq; | 360 spec->freq = desired_freq; |
361 fragsize = (spec->samples * 1000) / (spec->freq / 8); | 361 fragsize = (spec->samples * 1000) / (spec->freq / 8); |
362 frequency = 8; | 362 frequency = 8; |
363 ulaw_buf = (Uint8 *) SDL_malloc (fragsize); | 363 ulaw_buf = (Uint8 *) SDL_malloc(fragsize); |
364 if (ulaw_buf == NULL) { | 364 if (ulaw_buf == NULL) { |
365 SDL_OutOfMemory (); | 365 SDL_OutOfMemory(); |
366 return (-1); | 366 return (-1); |
367 } | 367 } |
368 spec->channels = 1; | 368 spec->channels = 1; |
369 } else { | 369 } else { |
370 fragsize = spec->samples; | 370 fragsize = spec->samples; |
371 frequency = spec->freq / 1000; | 371 frequency = spec->freq / 1000; |
372 } | 372 } |
373 #ifdef DEBUG_AUDIO | 373 #ifdef DEBUG_AUDIO |
374 fprintf (stderr, "Audio device %s U-Law only\n", | 374 fprintf(stderr, "Audio device %s U-Law only\n", |
375 ulaw_only ? "is" : "is not"); | 375 ulaw_only ? "is" : "is not"); |
376 fprintf (stderr, "format=0x%x chan=%d freq=%d\n", | 376 fprintf(stderr, "format=0x%x chan=%d freq=%d\n", |
377 spec->format, spec->channels, spec->freq); | 377 spec->format, spec->channels, spec->freq); |
378 #endif | 378 #endif |
379 | 379 |
380 /* Update the fragment size as size in bytes */ | 380 /* Update the fragment size as size in bytes */ |
381 SDL_CalculateAudioSpec (spec); | 381 SDL_CalculateAudioSpec(spec); |
382 | 382 |
383 /* Allocate mixing buffer */ | 383 /* Allocate mixing buffer */ |
384 mixbuf = (Uint8 *) SDL_AllocAudioMem (spec->size); | 384 mixbuf = (Uint8 *) SDL_AllocAudioMem(spec->size); |
385 if (mixbuf == NULL) { | 385 if (mixbuf == NULL) { |
386 SDL_OutOfMemory (); | 386 SDL_OutOfMemory(); |
387 return (-1); | 387 return (-1); |
388 } | 388 } |
389 SDL_memset (mixbuf, spec->silence, spec->size); | 389 SDL_memset(mixbuf, spec->silence, spec->size); |
390 | 390 |
391 /* We're ready to rock and roll. :-) */ | 391 /* We're ready to rock and roll. :-) */ |
392 return (0); | 392 return (0); |
393 } | 393 } |
394 | 394 |
409 /* about the suitability of this software for any purpose. It */ | 409 /* about the suitability of this software for any purpose. It */ |
410 /* provided "as is" without express or implied warranty. */ | 410 /* provided "as is" without express or implied warranty. */ |
411 /************************************************************************/ | 411 /************************************************************************/ |
412 | 412 |
413 static Uint8 | 413 static Uint8 |
414 snd2au (int sample) | 414 snd2au(int sample) |
415 { | 415 { |
416 | 416 |
417 int mask; | 417 int mask; |
418 | 418 |
419 if (sample < 0) { | 419 if (sample < 0) { |