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