Mercurial > sdl-ios-xcode
annotate src/audio/sun/SDL_sunaudio.c @ 1338:604d73db6802
Removed uses of stdlib.h and string.h
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Tue, 07 Feb 2006 09:29:18 +0000 |
parents | 3692456e7b0f |
children | c71e05b4dc2e |
rev | line source |
---|---|
0 | 1 /* |
2 SDL - Simple DirectMedia Layer | |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1184
diff
changeset
|
3 Copyright (C) 1997-2006 Sam Lantinga |
0 | 4 |
5 This library is free software; you can redistribute it and/or | |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1184
diff
changeset
|
6 modify it under the terms of the GNU Lesser General Public |
0 | 7 License as published by the Free Software Foundation; either |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1184
diff
changeset
|
8 version 2.1 of the License, or (at your option) any later version. |
0 | 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 | |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1184
diff
changeset
|
13 Lesser General Public License for more details. |
0 | 14 |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1184
diff
changeset
|
15 You should have received a copy of the GNU Lesser General Public |
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1184
diff
changeset
|
16 License along with this library; if not, write to the Free Software |
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1184
diff
changeset
|
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
0 | 18 |
19 Sam Lantinga | |
252
e8157fcb3114
Updated the source with the correct e-mail address
Sam Lantinga <slouken@libsdl.org>
parents:
148
diff
changeset
|
20 slouken@libsdl.org |
0 | 21 */ |
22 | |
23 /* Allow access to a raw mixing buffer */ | |
24 | |
25 #include <fcntl.h> | |
26 #include <errno.h> | |
27 #ifdef __NetBSD__ | |
28 #include <sys/ioctl.h> | |
29 #include <sys/audioio.h> | |
30 #endif | |
31 #ifdef __SVR4 | |
32 #include <sys/audioio.h> | |
33 #else | |
34 #include <sys/time.h> | |
35 #include <sys/types.h> | |
36 #endif | |
37 #include <unistd.h> | |
38 | |
1338
604d73db6802
Removed uses of stdlib.h and string.h
Sam Lantinga <slouken@libsdl.org>
parents:
1336
diff
changeset
|
39 #include "SDL_stdlib.h" |
604d73db6802
Removed uses of stdlib.h and string.h
Sam Lantinga <slouken@libsdl.org>
parents:
1336
diff
changeset
|
40 #include "SDL_string.h" |
0 | 41 #include "SDL_endian.h" |
42 #include "SDL_audio.h" | |
43 #include "SDL_audiomem.h" | |
44 #include "SDL_audiodev_c.h" | |
45 #include "SDL_sunaudio.h" | |
46 #include "SDL_audio_c.h" | |
47 #include "SDL_timer.h" | |
48 | |
49 /* Open the audio device for playback, and don't block if busy */ | |
50 #define OPEN_FLAGS (O_WRONLY|O_NONBLOCK) | |
51 | |
52 /* Audio driver functions */ | |
53 static int DSP_OpenAudio(_THIS, SDL_AudioSpec *spec); | |
54 static void DSP_WaitAudio(_THIS); | |
55 static void DSP_PlayAudio(_THIS); | |
56 static Uint8 *DSP_GetAudioBuf(_THIS); | |
57 static void DSP_CloseAudio(_THIS); | |
58 | |
59 /* Audio driver bootstrap functions */ | |
60 | |
61 static int Audio_Available(void) | |
62 { | |
63 int fd; | |
64 int available; | |
65 | |
66 available = 0; | |
67 fd = SDL_OpenAudioPath(NULL, 0, OPEN_FLAGS, 1); | |
68 if ( fd >= 0 ) { | |
69 available = 1; | |
70 close(fd); | |
71 } | |
72 return(available); | |
73 } | |
74 | |
75 static void Audio_DeleteDevice(SDL_AudioDevice *device) | |
76 { | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
77 SDL_free(device->hidden); |
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
78 SDL_free(device); |
0 | 79 } |
80 | |
81 static SDL_AudioDevice *Audio_CreateDevice(int devindex) | |
82 { | |
83 SDL_AudioDevice *this; | |
84 | |
85 /* Initialize all variables that we clean on shutdown */ | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
86 this = (SDL_AudioDevice *)SDL_malloc(sizeof(SDL_AudioDevice)); |
0 | 87 if ( this ) { |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
88 SDL_memset(this, 0, (sizeof *this)); |
0 | 89 this->hidden = (struct SDL_PrivateAudioData *) |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
90 SDL_malloc((sizeof *this->hidden)); |
0 | 91 } |
92 if ( (this == NULL) || (this->hidden == NULL) ) { | |
93 SDL_OutOfMemory(); | |
94 if ( this ) { | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
95 SDL_free(this); |
0 | 96 } |
97 return(0); | |
98 } | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
99 SDL_memset(this->hidden, 0, (sizeof *this->hidden)); |
0 | 100 audio_fd = -1; |
101 | |
102 /* Set the function pointers */ | |
103 this->OpenAudio = DSP_OpenAudio; | |
104 this->WaitAudio = DSP_WaitAudio; | |
105 this->PlayAudio = DSP_PlayAudio; | |
106 this->GetAudioBuf = DSP_GetAudioBuf; | |
107 this->CloseAudio = DSP_CloseAudio; | |
108 | |
109 this->free = Audio_DeleteDevice; | |
110 | |
111 return this; | |
112 } | |
113 | |
148
8758b8d42cd9
Audio subsystem no longer assumes sun audio API on UNIX systems
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
114 AudioBootStrap SUNAUDIO_bootstrap = { |
0 | 115 "audio", "UNIX /dev/audio interface", |
116 Audio_Available, Audio_CreateDevice | |
117 }; | |
118 | |
119 #ifdef DEBUG_AUDIO | |
120 void CheckUnderflow(_THIS) | |
121 { | |
122 #ifdef AUDIO_GETINFO | |
123 audio_info_t info; | |
124 int left; | |
125 | |
126 ioctl(audio_fd, AUDIO_GETINFO, &info); | |
127 left = (written - info.play.samples); | |
128 if ( written && (left == 0) ) { | |
129 fprintf(stderr, "audio underflow!\n"); | |
130 } | |
131 #endif | |
132 } | |
133 #endif | |
134 | |
135 void DSP_WaitAudio(_THIS) | |
136 { | |
137 #ifdef AUDIO_GETINFO | |
138 #define SLEEP_FUDGE 10 /* 10 ms scheduling fudge factor */ | |
139 audio_info_t info; | |
140 Sint32 left; | |
141 | |
142 ioctl(audio_fd, AUDIO_GETINFO, &info); | |
143 left = (written - info.play.samples); | |
144 if ( left > fragsize ) { | |
145 Sint32 sleepy; | |
146 | |
147 sleepy = ((left - fragsize)/frequency); | |
148 sleepy -= SLEEP_FUDGE; | |
149 if ( sleepy > 0 ) { | |
150 SDL_Delay(sleepy); | |
151 } | |
152 } | |
153 #else | |
154 fd_set fdset; | |
155 | |
156 FD_ZERO(&fdset); | |
157 FD_SET(audio_fd, &fdset); | |
158 select(audio_fd+1, NULL, &fdset, NULL, NULL); | |
159 #endif | |
160 } | |
161 | |
1184
a9542c38dcdb
Date: Tue, 22 Nov 2005 14:11:06 +0100 (MET)
Ryan C. Gordon <icculus@icculus.org>
parents:
769
diff
changeset
|
162 static Uint8 snd2au(int sample); |
0 | 163 void DSP_PlayAudio(_THIS) |
164 { | |
165 /* Write the audio data */ | |
166 if ( ulaw_only ) { | |
167 /* Assuming that this->spec.freq >= 8000 Hz */ | |
168 int accum, incr, pos; | |
169 Uint8 *aubuf; | |
170 | |
171 accum = 0; | |
172 incr = this->spec.freq/8; | |
173 aubuf = ulaw_buf; | |
174 switch (audio_fmt & 0xFF) { | |
175 case 8: { | |
176 Uint8 *sndbuf; | |
177 | |
178 sndbuf = mixbuf; | |
179 for ( pos=0; pos < fragsize; ++pos ) { | |
180 *aubuf = snd2au((0x80-*sndbuf)*64); | |
181 accum += incr; | |
182 while ( accum > 0 ) { | |
183 accum -= 1000; | |
184 sndbuf += 1; | |
185 } | |
186 aubuf += 1; | |
187 } | |
188 } | |
189 break; | |
190 case 16: { | |
191 Sint16 *sndbuf; | |
192 | |
193 sndbuf = (Sint16 *)mixbuf; | |
194 for ( pos=0; pos < fragsize; ++pos ) { | |
195 *aubuf = snd2au(*sndbuf/4); | |
196 accum += incr; | |
197 while ( accum > 0 ) { | |
198 accum -= 1000; | |
199 sndbuf += 1; | |
200 } | |
201 aubuf += 1; | |
202 } | |
203 } | |
204 break; | |
205 } | |
206 #ifdef DEBUG_AUDIO | |
207 CheckUnderflow(this); | |
208 #endif | |
209 if ( write(audio_fd, ulaw_buf, fragsize) < 0 ) { | |
210 /* Assume fatal error, for now */ | |
211 this->enabled = 0; | |
212 } | |
213 written += fragsize; | |
214 } else { | |
215 #ifdef DEBUG_AUDIO | |
216 CheckUnderflow(this); | |
217 #endif | |
218 if ( write(audio_fd, mixbuf, this->spec.size) < 0 ) { | |
219 /* Assume fatal error, for now */ | |
220 this->enabled = 0; | |
221 } | |
222 written += fragsize; | |
223 } | |
224 } | |
225 | |
226 Uint8 *DSP_GetAudioBuf(_THIS) | |
227 { | |
228 return(mixbuf); | |
229 } | |
230 | |
231 void DSP_CloseAudio(_THIS) | |
232 { | |
233 if ( mixbuf != NULL ) { | |
234 SDL_FreeAudioMem(mixbuf); | |
235 mixbuf = NULL; | |
236 } | |
237 if ( ulaw_buf != NULL ) { | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
238 SDL_free(ulaw_buf); |
0 | 239 ulaw_buf = NULL; |
240 } | |
241 close(audio_fd); | |
242 } | |
243 | |
244 int DSP_OpenAudio(_THIS, SDL_AudioSpec *spec) | |
245 { | |
246 char audiodev[1024]; | |
247 #ifdef AUDIO_SETINFO | |
248 int enc; | |
249 #endif | |
250 int desired_freq = spec->freq; | |
251 | |
252 /* Initialize our freeable variables, in case we fail*/ | |
253 audio_fd = -1; | |
254 mixbuf = NULL; | |
255 ulaw_buf = NULL; | |
256 | |
257 /* Determine the audio parameters from the AudioSpec */ | |
258 switch ( spec->format & 0xFF ) { | |
259 | |
260 case 8: { /* Unsigned 8 bit audio data */ | |
261 spec->format = AUDIO_U8; | |
262 #ifdef AUDIO_SETINFO | |
263 enc = AUDIO_ENCODING_LINEAR8; | |
264 #endif | |
265 } | |
266 break; | |
267 | |
268 case 16: { /* Signed 16 bit audio data */ | |
269 spec->format = AUDIO_S16SYS; | |
270 #ifdef AUDIO_SETINFO | |
271 enc = AUDIO_ENCODING_LINEAR; | |
272 #endif | |
273 } | |
274 break; | |
275 | |
276 default: { | |
277 SDL_SetError("Unsupported audio format"); | |
278 return(-1); | |
279 } | |
280 } | |
281 audio_fmt = spec->format; | |
282 | |
283 /* Open the audio device */ | |
284 audio_fd = SDL_OpenAudioPath(audiodev, sizeof(audiodev), OPEN_FLAGS, 1); | |
285 if ( audio_fd < 0 ) { | |
286 SDL_SetError("Couldn't open %s: %s", audiodev, | |
287 strerror(errno)); | |
288 return(-1); | |
289 } | |
290 | |
291 ulaw_only = 0; /* modern Suns do support linear audio */ | |
292 #ifdef AUDIO_SETINFO | |
293 for(;;) { | |
294 audio_info_t info; | |
295 AUDIO_INITINFO(&info); /* init all fields to "no change" */ | |
296 | |
297 /* Try to set the requested settings */ | |
298 info.play.sample_rate = spec->freq; | |
299 info.play.channels = spec->channels; | |
300 info.play.precision = (enc == AUDIO_ENCODING_ULAW) | |
301 ? 8 : spec->format & 0xff; | |
302 info.play.encoding = enc; | |
303 if( ioctl(audio_fd, AUDIO_SETINFO, &info) == 0 ) { | |
304 | |
305 /* Check to be sure we got what we wanted */ | |
306 if(ioctl(audio_fd, AUDIO_GETINFO, &info) < 0) { | |
307 SDL_SetError("Error getting audio parameters: %s", | |
308 strerror(errno)); | |
309 return -1; | |
310 } | |
311 if(info.play.encoding == enc | |
312 && info.play.precision == (spec->format & 0xff) | |
313 && info.play.channels == spec->channels) { | |
314 /* Yow! All seems to be well! */ | |
315 spec->freq = info.play.sample_rate; | |
316 break; | |
317 } | |
318 } | |
319 | |
320 switch(enc) { | |
321 case AUDIO_ENCODING_LINEAR8: | |
322 /* unsigned 8bit apparently not supported here */ | |
323 enc = AUDIO_ENCODING_LINEAR; | |
324 spec->format = AUDIO_S16SYS; | |
325 break; /* try again */ | |
326 | |
327 case AUDIO_ENCODING_LINEAR: | |
328 /* linear 16bit didn't work either, resort to µ-law */ | |
329 enc = AUDIO_ENCODING_ULAW; | |
330 spec->channels = 1; | |
331 spec->freq = 8000; | |
332 spec->format = AUDIO_U8; | |
333 ulaw_only = 1; | |
334 break; | |
335 | |
336 default: | |
337 /* oh well... */ | |
338 SDL_SetError("Error setting audio parameters: %s", | |
339 strerror(errno)); | |
340 return -1; | |
341 } | |
342 } | |
343 #endif /* AUDIO_SETINFO */ | |
344 written = 0; | |
345 | |
346 /* We can actually convert on-the-fly to U-Law */ | |
347 if ( ulaw_only ) { | |
348 spec->freq = desired_freq; | |
349 fragsize = (spec->samples*1000)/(spec->freq/8); | |
350 frequency = 8; | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
351 ulaw_buf = (Uint8 *)SDL_malloc(fragsize); |
0 | 352 if ( ulaw_buf == NULL ) { |
353 SDL_OutOfMemory(); | |
354 return(-1); | |
355 } | |
356 spec->channels = 1; | |
357 } else { | |
358 fragsize = spec->samples; | |
359 frequency = spec->freq/1000; | |
360 } | |
361 #ifdef DEBUG_AUDIO | |
362 fprintf(stderr, "Audio device %s U-Law only\n", | |
363 ulaw_only ? "is" : "is not"); | |
364 fprintf(stderr, "format=0x%x chan=%d freq=%d\n", | |
365 spec->format, spec->channels, spec->freq); | |
366 #endif | |
367 | |
368 /* Update the fragment size as size in bytes */ | |
369 SDL_CalculateAudioSpec(spec); | |
370 | |
371 /* Allocate mixing buffer */ | |
372 mixbuf = (Uint8 *)SDL_AllocAudioMem(spec->size); | |
373 if ( mixbuf == NULL ) { | |
374 SDL_OutOfMemory(); | |
375 return(-1); | |
376 } | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
377 SDL_memset(mixbuf, spec->silence, spec->size); |
0 | 378 |
379 /* We're ready to rock and roll. :-) */ | |
380 return(0); | |
381 } | |
382 | |
383 /************************************************************************/ | |
384 /* This function (snd2au()) copyrighted: */ | |
385 /************************************************************************/ | |
386 /* Copyright 1989 by Rich Gopstein and Harris Corporation */ | |
387 /* */ | |
388 /* Permission to use, copy, modify, and distribute this software */ | |
389 /* and its documentation for any purpose and without fee is */ | |
390 /* hereby granted, provided that the above copyright notice */ | |
391 /* appears in all copies and that both that copyright notice and */ | |
392 /* this permission notice appear in supporting documentation, and */ | |
393 /* that the name of Rich Gopstein and Harris Corporation not be */ | |
394 /* used in advertising or publicity pertaining to distribution */ | |
395 /* of the software without specific, written prior permission. */ | |
396 /* Rich Gopstein and Harris Corporation make no representations */ | |
397 /* about the suitability of this software for any purpose. It */ | |
398 /* provided "as is" without express or implied warranty. */ | |
399 /************************************************************************/ | |
400 | |
401 static Uint8 snd2au(int sample) | |
402 { | |
403 | |
404 int mask; | |
405 | |
406 if (sample < 0) { | |
407 sample = -sample; | |
408 mask = 0x7f; | |
409 } else { | |
410 mask = 0xff; | |
411 } | |
412 | |
413 if (sample < 32) { | |
414 sample = 0xF0 | (15 - sample / 2); | |
415 } else if (sample < 96) { | |
416 sample = 0xE0 | (15 - (sample - 32) / 4); | |
417 } else if (sample < 224) { | |
418 sample = 0xD0 | (15 - (sample - 96) / 8); | |
419 } else if (sample < 480) { | |
420 sample = 0xC0 | (15 - (sample - 224) / 16); | |
421 } else if (sample < 992) { | |
422 sample = 0xB0 | (15 - (sample - 480) / 32); | |
423 } else if (sample < 2016) { | |
424 sample = 0xA0 | (15 - (sample - 992) / 64); | |
425 } else if (sample < 4064) { | |
426 sample = 0x90 | (15 - (sample - 2016) / 128); | |
427 } else if (sample < 8160) { | |
428 sample = 0x80 | (15 - (sample - 4064) / 256); | |
429 } else { | |
430 sample = 0x80; | |
431 } | |
432 return (mask & sample); | |
433 } |