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