Mercurial > sdl-ios-xcode
annotate src/audio/arts/SDL_artsaudio.c @ 261:1c5f68da5fc2
*** empty log message ***
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Fri, 04 Jan 2002 20:48:51 +0000 |
parents | e8157fcb3114 |
children | d2d48e10f370 |
rev | line source |
---|---|
0 | 1 /* |
2 SDL - Simple DirectMedia Layer | |
3 Copyright (C) 1997, 1998, 1999, 2000, 2001 Sam Lantinga | |
4 | |
5 This library is free software; you can redistribute it and/or | |
6 modify it under the terms of the GNU Library General Public | |
7 License as published by the Free Software Foundation; either | |
8 version 2 of the License, or (at your option) any later version. | |
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 | |
13 Library General Public License for more details. | |
14 | |
15 You should have received a copy of the GNU Library General Public | |
16 License along with this library; if not, write to the Free | |
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
18 | |
19 Sam Lantinga | |
252
e8157fcb3114
Updated the source with the correct e-mail address
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
20 slouken@libsdl.org |
0 | 21 */ |
22 | |
23 #ifdef SAVE_RCSID | |
24 static char rcsid = | |
25 "@(#) $Id$"; | |
26 #endif | |
27 | |
28 /* Allow access to a raw mixing buffer */ | |
29 | |
30 #include <sys/types.h> | |
31 #include <stdlib.h> | |
32 #include <stdio.h> | |
33 #include <string.h> | |
34 #include <errno.h> | |
35 #include <signal.h> | |
36 #include <unistd.h> | |
37 | |
38 #include "SDL_audio.h" | |
39 #include "SDL_error.h" | |
40 #include "SDL_audiomem.h" | |
41 #include "SDL_audio_c.h" | |
42 #include "SDL_timer.h" | |
43 #include "SDL_audiodev_c.h" | |
44 #include "SDL_artsaudio.h" | |
45 | |
46 /* The tag name used by artsc audio */ | |
47 #define ARTSC_DRIVER_NAME "artsc" | |
48 | |
49 /* Audio driver functions */ | |
50 static int ARTSC_OpenAudio(_THIS, SDL_AudioSpec *spec); | |
51 static void ARTSC_WaitAudio(_THIS); | |
52 static void ARTSC_PlayAudio(_THIS); | |
53 static Uint8 *ARTSC_GetAudioBuf(_THIS); | |
54 static void ARTSC_CloseAudio(_THIS); | |
55 | |
56 /* Audio driver bootstrap functions */ | |
57 | |
58 static int Audio_Available(void) | |
59 { | |
60 if(arts_init()) | |
61 return 0; | |
62 else | |
63 return 1; | |
64 } | |
65 | |
66 static void Audio_DeleteDevice(SDL_AudioDevice *device) | |
67 { | |
68 free(device->hidden); | |
69 free(device); | |
70 } | |
71 | |
72 static SDL_AudioDevice *Audio_CreateDevice(int devindex) | |
73 { | |
74 SDL_AudioDevice *this; | |
75 | |
76 /* Initialize all variables that we clean on shutdown */ | |
77 this = (SDL_AudioDevice *)malloc(sizeof(SDL_AudioDevice)); | |
78 if ( this ) { | |
79 memset(this, 0, (sizeof *this)); | |
80 this->hidden = (struct SDL_PrivateAudioData *) | |
81 malloc((sizeof *this->hidden)); | |
82 } | |
83 if ( (this == NULL) || (this->hidden == NULL) ) { | |
84 SDL_OutOfMemory(); | |
85 if ( this ) { | |
86 free(this); | |
87 } | |
88 return(0); | |
89 } | |
90 memset(this->hidden, 0, (sizeof *this->hidden)); | |
91 stream = 0; | |
92 | |
93 /* Set the function pointers */ | |
94 this->OpenAudio = ARTSC_OpenAudio; | |
95 this->WaitAudio = ARTSC_WaitAudio; | |
96 this->PlayAudio = ARTSC_PlayAudio; | |
97 this->GetAudioBuf = ARTSC_GetAudioBuf; | |
98 this->CloseAudio = ARTSC_CloseAudio; | |
99 | |
100 this->free = Audio_DeleteDevice; | |
101 | |
102 return this; | |
103 } | |
104 | |
105 AudioBootStrap ARTSC_bootstrap = { | |
106 ARTSC_DRIVER_NAME, "Analog Realtime Synthesizer", | |
107 Audio_Available, Audio_CreateDevice | |
108 }; | |
109 | |
110 /* This function waits until it is possible to write a full sound buffer */ | |
111 static void ARTSC_WaitAudio(_THIS) | |
112 { | |
113 Sint32 ticks; | |
114 | |
115 /* Check to see if the thread-parent process is still alive */ | |
116 { static int cnt = 0; | |
117 /* Note that this only works with thread implementations | |
118 that use a different process id for each thread. | |
119 */ | |
120 if (parent && (((++cnt)%10) == 0)) { /* Check every 10 loops */ | |
121 if ( kill(parent, 0) < 0 ) { | |
122 this->enabled = 0; | |
123 } | |
124 } | |
125 } | |
126 | |
127 /* Use timer for general audio synchronization */ | |
128 ticks = ((Sint32)(next_frame - SDL_GetTicks()))-FUDGE_TICKS; | |
129 if ( ticks > 0 ) { | |
130 SDL_Delay(ticks); | |
131 } | |
132 } | |
133 | |
134 static void ARTSC_PlayAudio(_THIS) | |
135 { | |
136 int written; | |
137 | |
138 /* Write the audio data */ | |
139 written = arts_write(stream, mixbuf, mixlen); | |
140 | |
141 /* If timer synchronization is enabled, set the next write frame */ | |
142 if ( frame_ticks ) { | |
143 next_frame += frame_ticks; | |
144 } | |
145 | |
146 /* If we couldn't write, assume fatal error for now */ | |
147 if ( written < 0 ) { | |
148 this->enabled = 0; | |
149 } | |
150 #ifdef DEBUG_AUDIO | |
151 fprintf(stderr, "Wrote %d bytes of audio data\n", written); | |
152 #endif | |
153 } | |
154 | |
155 static Uint8 *ARTSC_GetAudioBuf(_THIS) | |
156 { | |
157 return(mixbuf); | |
158 } | |
159 | |
160 static void ARTSC_CloseAudio(_THIS) | |
161 { | |
162 if ( mixbuf != NULL ) { | |
163 SDL_FreeAudioMem(mixbuf); | |
164 mixbuf = NULL; | |
165 } | |
166 if ( stream ) { | |
167 arts_close_stream(stream); | |
168 stream = 0; | |
169 } | |
170 } | |
171 | |
172 static int ARTSC_OpenAudio(_THIS, SDL_AudioSpec *spec) | |
173 { | |
174 int bits, frag_spec; | |
175 Uint16 test_format, format; | |
176 | |
177 /* Reset the timer synchronization flag */ | |
178 frame_ticks = 0.0; | |
179 | |
180 mixbuf = NULL; | |
181 | |
182 /* Try for a closest match on audio format */ | |
183 format = 0; | |
184 bits = 0; | |
185 for ( test_format = SDL_FirstAudioFormat(spec->format); | |
186 ! format && test_format; ) { | |
187 #ifdef DEBUG_AUDIO | |
188 fprintf(stderr, "Trying format 0x%4.4x\n", test_format); | |
189 #endif | |
190 switch ( test_format ) { | |
191 case AUDIO_U8: | |
192 bits = 8; | |
193 format = 1; | |
194 break; | |
195 case AUDIO_S16LSB: | |
196 bits = 16; | |
197 format = 1; | |
198 break; | |
199 default: | |
200 format = 0; | |
201 break; | |
202 } | |
203 if ( ! format ) { | |
204 test_format = SDL_NextAudioFormat(); | |
205 } | |
206 } | |
207 if ( format == 0 ) { | |
208 SDL_SetError("Couldn't find any hardware audio formats"); | |
209 return(-1); | |
210 } | |
211 spec->format = test_format; | |
212 | |
213 stream = arts_play_stream(spec->freq, bits, spec->channels, "SDL"); | |
214 | |
215 /* Calculate the final parameters for this audio specification */ | |
216 SDL_CalculateAudioSpec(spec); | |
217 | |
218 /* Determine the power of two of the fragment size */ | |
219 for ( frag_spec = 0; (0x01<<frag_spec) < spec->size; ++frag_spec ); | |
220 if ( (0x01<<frag_spec) != spec->size ) { | |
221 SDL_SetError("Fragment size must be a power of two"); | |
222 return(-1); | |
223 } | |
224 frag_spec |= 0x00020000; /* two fragments, for low latency */ | |
225 | |
226 #ifdef ARTS_P_PACKET_SETTINGS | |
227 arts_stream_set(stream, ARTS_P_PACKET_SETTINGS, frag_spec); | |
228 #else | |
229 arts_stream_set(stream, ARTS_P_PACKET_SIZE, frag_spec&0xffff); | |
230 arts_stream_set(stream, ARTS_P_PACKET_COUNT, frag_spec>>16); | |
231 #endif | |
232 spec->size = arts_stream_get(stream, ARTS_P_PACKET_SIZE); | |
233 | |
234 /* Allocate mixing buffer */ | |
235 mixlen = spec->size; | |
236 mixbuf = (Uint8 *)SDL_AllocAudioMem(mixlen); | |
237 if ( mixbuf == NULL ) { | |
238 return(-1); | |
239 } | |
240 memset(mixbuf, spec->silence, spec->size); | |
241 | |
242 /* Get the parent process id (we're the parent of the audio thread) */ | |
243 parent = getpid(); | |
244 | |
245 /* We're ready to rock and roll. :-) */ | |
246 return(0); | |
247 } |