comparison src/audio/mint/SDL_mintaudio_stfa.c @ 1895:c121d94672cb

SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
author Sam Lantinga <slouken@libsdl.org>
date Mon, 10 Jul 2006 21:04:37 +0000
parents a8181c4040b8
children 45af7d69f8eb
comparison
equal deleted inserted replaced
1894:c69cee13dd76 1895:c121d94672cb
60 /*--- Static variables ---*/ 60 /*--- Static variables ---*/
61 61
62 static unsigned long cookie_snd, cookie_mch; 62 static unsigned long cookie_snd, cookie_mch;
63 static cookie_stfa_t *cookie_stfa; 63 static cookie_stfa_t *cookie_stfa;
64 64
65 static const int freqs[16]={ 65 static const int freqs[16] = {
66 4995, 6269, 7493, 8192, 66 4995, 6269, 7493, 8192,
67 9830, 10971, 12538, 14985, 67 9830, 10971, 12538, 14985,
68 16384, 19819, 21943, 24576, 68 16384, 19819, 21943, 24576,
69 30720, 32336, 43885, 49152 69 30720, 32336, 43885, 49152
70 }; 70 };
71 71
72 /*--- Audio driver functions ---*/ 72 /*--- Audio driver functions ---*/
73 73
74 static void Mint_CloseAudio(_THIS); 74 static void Mint_CloseAudio(_THIS);
75 static int Mint_OpenAudio(_THIS, SDL_AudioSpec *spec); 75 static int Mint_OpenAudio(_THIS, SDL_AudioSpec * spec);
76 static void Mint_LockAudio(_THIS); 76 static void Mint_LockAudio(_THIS);
77 static void Mint_UnlockAudio(_THIS); 77 static void Mint_UnlockAudio(_THIS);
78 78
79 /* To check/init hardware audio */ 79 /* To check/init hardware audio */
80 static int Mint_CheckAudio(_THIS, SDL_AudioSpec *spec); 80 static int Mint_CheckAudio(_THIS, SDL_AudioSpec * spec);
81 static void Mint_InitAudio(_THIS, SDL_AudioSpec *spec); 81 static void Mint_InitAudio(_THIS, SDL_AudioSpec * spec);
82 82
83 /*--- Audio driver bootstrap functions ---*/ 83 /*--- Audio driver bootstrap functions ---*/
84 84
85 static int Audio_Available(void) 85 static int
86 { 86 Audio_Available(void)
87 const char *envr = SDL_getenv("SDL_AUDIODRIVER"); 87 {
88 88 const char *envr = SDL_getenv("SDL_AUDIODRIVER");
89 /* Check if user asked a different audio driver */ 89
90 if ((envr) && (SDL_strcmp(envr, MINT_AUDIO_DRIVER_NAME)!=0)) { 90 /* Check if user asked a different audio driver */
91 DEBUG_PRINT((DEBUG_NAME "user asked a different audio driver\n")); 91 if ((envr) && (SDL_strcmp(envr, MINT_AUDIO_DRIVER_NAME) != 0)) {
92 return(0); 92 DEBUG_PRINT((DEBUG_NAME "user asked a different audio driver\n"));
93 } 93 return (0);
94 94 }
95 /* Cookie _MCH present ? if not, assume ST machine */ 95
96 if (Getcookie(C__MCH, &cookie_mch) == C_NOTFOUND) { 96 /* Cookie _MCH present ? if not, assume ST machine */
97 cookie_mch = MCH_ST; 97 if (Getcookie(C__MCH, &cookie_mch) == C_NOTFOUND) {
98 } 98 cookie_mch = MCH_ST;
99 99 }
100 /* Cookie _SND present ? if not, assume ST machine */ 100
101 if (Getcookie(C__SND, &cookie_snd) == C_NOTFOUND) { 101 /* Cookie _SND present ? if not, assume ST machine */
102 cookie_snd = SND_PSG; 102 if (Getcookie(C__SND, &cookie_snd) == C_NOTFOUND) {
103 } 103 cookie_snd = SND_PSG;
104 104 }
105 /* Cookie STFA present ? */ 105
106 if (Getcookie(C_STFA, (long *) &cookie_stfa) != C_FOUND) { 106 /* Cookie STFA present ? */
107 DEBUG_PRINT((DEBUG_NAME "no STFA audio\n")); 107 if (Getcookie(C_STFA, (long *) &cookie_stfa) != C_FOUND) {
108 return(0); 108 DEBUG_PRINT((DEBUG_NAME "no STFA audio\n"));
109 } 109 return (0);
110 110 }
111 SDL_MintAudio_stfa = cookie_stfa; 111
112 112 SDL_MintAudio_stfa = cookie_stfa;
113 DEBUG_PRINT((DEBUG_NAME "STFA audio available!\n")); 113
114 return(1); 114 DEBUG_PRINT((DEBUG_NAME "STFA audio available!\n"));
115 } 115 return (1);
116 116 }
117 static void Audio_DeleteDevice(SDL_AudioDevice *device) 117
118 static void
119 Audio_DeleteDevice(SDL_AudioDevice * device)
118 { 120 {
119 SDL_free(device->hidden); 121 SDL_free(device->hidden);
120 SDL_free(device); 122 SDL_free(device);
121 } 123 }
122 124
123 static SDL_AudioDevice *Audio_CreateDevice(int devindex) 125 static SDL_AudioDevice *
124 { 126 Audio_CreateDevice(int devindex)
125 SDL_AudioDevice *this; 127 {
126 128 SDL_AudioDevice *this;
127 /* Initialize all variables that we clean on shutdown */ 129
128 this = (SDL_AudioDevice *)SDL_malloc(sizeof(SDL_AudioDevice)); 130 /* Initialize all variables that we clean on shutdown */
129 if ( this ) { 131 this = (SDL_AudioDevice *) SDL_malloc(sizeof(SDL_AudioDevice));
132 if (this) {
130 SDL_memset(this, 0, (sizeof *this)); 133 SDL_memset(this, 0, (sizeof *this));
131 this->hidden = (struct SDL_PrivateAudioData *) 134 this->hidden = (struct SDL_PrivateAudioData *)
132 SDL_malloc((sizeof *this->hidden)); 135 SDL_malloc((sizeof *this->hidden));
133 } 136 }
134 if ( (this == NULL) || (this->hidden == NULL) ) { 137 if ((this == NULL) || (this->hidden == NULL)) {
135 SDL_OutOfMemory(); 138 SDL_OutOfMemory();
136 if ( this ) { 139 if (this) {
137 SDL_free(this); 140 SDL_free(this);
138 } 141 }
139 return(0); 142 return (0);
140 } 143 }
141 SDL_memset(this->hidden, 0, (sizeof *this->hidden)); 144 SDL_memset(this->hidden, 0, (sizeof *this->hidden));
142 145
143 /* Set the function pointers */ 146 /* Set the function pointers */
144 this->OpenAudio = Mint_OpenAudio; 147 this->OpenAudio = Mint_OpenAudio;
145 this->CloseAudio = Mint_CloseAudio; 148 this->CloseAudio = Mint_CloseAudio;
146 this->LockAudio = Mint_LockAudio; 149 this->LockAudio = Mint_LockAudio;
147 this->UnlockAudio = Mint_UnlockAudio; 150 this->UnlockAudio = Mint_UnlockAudio;
148 this->free = Audio_DeleteDevice; 151 this->free = Audio_DeleteDevice;
149 152
150 return this; 153 return this;
151 } 154 }
152 155
153 AudioBootStrap MINTAUDIO_STFA_bootstrap = { 156 AudioBootStrap MINTAUDIO_STFA_bootstrap = {
154 MINT_AUDIO_DRIVER_NAME, "MiNT STFA audio driver", 157 MINT_AUDIO_DRIVER_NAME, "MiNT STFA audio driver",
155 Audio_Available, Audio_CreateDevice 158 Audio_Available, Audio_CreateDevice
156 }; 159 };
157 160
158 static void Mint_LockAudio(_THIS) 161 static void
159 { 162 Mint_LockAudio(_THIS)
160 void *oldpile; 163 {
161 164 void *oldpile;
162 /* Stop replay */ 165
163 oldpile=(void *)Super(0); 166 /* Stop replay */
164 cookie_stfa->sound_enable=STFA_PLAY_DISABLE; 167 oldpile = (void *) Super(0);
165 Super(oldpile); 168 cookie_stfa->sound_enable = STFA_PLAY_DISABLE;
166 } 169 Super(oldpile);
167 170 }
168 static void Mint_UnlockAudio(_THIS) 171
169 { 172 static void
170 void *oldpile; 173 Mint_UnlockAudio(_THIS)
171 174 {
172 /* Restart replay */ 175 void *oldpile;
173 oldpile=(void *)Super(0); 176
174 cookie_stfa->sound_enable=STFA_PLAY_ENABLE|STFA_PLAY_REPEAT; 177 /* Restart replay */
175 Super(oldpile); 178 oldpile = (void *) Super(0);
176 } 179 cookie_stfa->sound_enable = STFA_PLAY_ENABLE | STFA_PLAY_REPEAT;
177 180 Super(oldpile);
178 static void Mint_CloseAudio(_THIS) 181 }
179 { 182
180 void *oldpile; 183 static void
181 184 Mint_CloseAudio(_THIS)
182 /* Stop replay */ 185 {
183 oldpile=(void *)Super(0); 186 void *oldpile;
184 cookie_stfa->sound_enable=STFA_PLAY_DISABLE; 187
185 Super(oldpile); 188 /* Stop replay */
186 189 oldpile = (void *) Super(0);
187 /* Wait if currently playing sound */ 190 cookie_stfa->sound_enable = STFA_PLAY_DISABLE;
188 while (SDL_MintAudio_mutex != 0) { 191 Super(oldpile);
189 } 192
190 193 /* Wait if currently playing sound */
191 /* Clear buffers */ 194 while (SDL_MintAudio_mutex != 0) {
192 if (SDL_MintAudio_audiobuf[0]) { 195 }
193 Mfree(SDL_MintAudio_audiobuf[0]); 196
194 SDL_MintAudio_audiobuf[0] = SDL_MintAudio_audiobuf[1] = NULL; 197 /* Clear buffers */
195 } 198 if (SDL_MintAudio_audiobuf[0]) {
196 } 199 Mfree(SDL_MintAudio_audiobuf[0]);
197 200 SDL_MintAudio_audiobuf[0] = SDL_MintAudio_audiobuf[1] = NULL;
198 static int Mint_CheckAudio(_THIS, SDL_AudioSpec *spec) 201 }
199 { 202 }
200 int i; 203
201 204 static int
202 DEBUG_PRINT((DEBUG_NAME "asked: %d bits, ",spec->format & 0x00ff)); 205 Mint_CheckAudio(_THIS, SDL_AudioSpec * spec)
203 DEBUG_PRINT(("signed=%d, ", ((spec->format & 0x8000)!=0))); 206 {
204 DEBUG_PRINT(("big endian=%d, ", ((spec->format & 0x1000)!=0))); 207 int i;
205 DEBUG_PRINT(("channels=%d, ", spec->channels)); 208
206 DEBUG_PRINT(("freq=%d\n", spec->freq)); 209 DEBUG_PRINT((DEBUG_NAME "asked: %d bits, ", spec->format & 0x00ff));
207 210 DEBUG_PRINT(("signed=%d, ", ((spec->format & 0x8000) != 0)));
208 /* Check formats available */ 211 DEBUG_PRINT(("big endian=%d, ", ((spec->format & 0x1000) != 0)));
209 MINTAUDIO_freqcount=0; 212 DEBUG_PRINT(("channels=%d, ", spec->channels));
210 for (i=0;i<16;i++) { 213 DEBUG_PRINT(("freq=%d\n", spec->freq));
211 SDL_MintAudio_AddFrequency(this, freqs[i], 0, i, -1); 214
212 } 215 /* Check formats available */
216 MINTAUDIO_freqcount = 0;
217 for (i = 0; i < 16; i++) {
218 SDL_MintAudio_AddFrequency(this, freqs[i], 0, i, -1);
219 }
213 220
214 #if 1 221 #if 1
215 for (i=0; i<MINTAUDIO_freqcount; i++) { 222 for (i = 0; i < MINTAUDIO_freqcount; i++) {
216 DEBUG_PRINT((DEBUG_NAME "freq %d: %lu Hz, clock %lu, prediv %d\n", 223 DEBUG_PRINT((DEBUG_NAME "freq %d: %lu Hz, clock %lu, prediv %d\n",
217 i, MINTAUDIO_frequencies[i].frequency, MINTAUDIO_frequencies[i].masterclock, 224 i, MINTAUDIO_frequencies[i].frequency,
218 MINTAUDIO_frequencies[i].predivisor 225 MINTAUDIO_frequencies[i].masterclock,
219 )); 226 MINTAUDIO_frequencies[i].predivisor));
220 } 227 }
221 #endif 228 #endif
222 229
223 MINTAUDIO_numfreq=SDL_MintAudio_SearchFrequency(this, spec->freq); 230 MINTAUDIO_numfreq = SDL_MintAudio_SearchFrequency(this, spec->freq);
224 spec->freq=MINTAUDIO_frequencies[MINTAUDIO_numfreq].frequency; 231 spec->freq = MINTAUDIO_frequencies[MINTAUDIO_numfreq].frequency;
225 232
226 DEBUG_PRINT((DEBUG_NAME "obtained: %d bits, ",spec->format & 0x00ff)); 233 DEBUG_PRINT((DEBUG_NAME "obtained: %d bits, ", spec->format & 0x00ff));
227 DEBUG_PRINT(("signed=%d, ", ((spec->format & 0x8000)!=0))); 234 DEBUG_PRINT(("signed=%d, ", ((spec->format & 0x8000) != 0)));
228 DEBUG_PRINT(("big endian=%d, ", ((spec->format & 0x1000)!=0))); 235 DEBUG_PRINT(("big endian=%d, ", ((spec->format & 0x1000) != 0)));
229 DEBUG_PRINT(("channels=%d, ", spec->channels)); 236 DEBUG_PRINT(("channels=%d, ", spec->channels));
230 DEBUG_PRINT(("freq=%d\n", spec->freq)); 237 DEBUG_PRINT(("freq=%d\n", spec->freq));
231 238
232 return 0; 239 return 0;
233 } 240 }
234 241
235 static void Mint_InitAudio(_THIS, SDL_AudioSpec *spec) 242 static void
236 { 243 Mint_InitAudio(_THIS, SDL_AudioSpec * spec)
237 void *buffer; 244 {
238 void *oldpile; 245 void *buffer;
239 246 void *oldpile;
240 buffer = SDL_MintAudio_audiobuf[SDL_MintAudio_numbuf]; 247
241 248 buffer = SDL_MintAudio_audiobuf[SDL_MintAudio_numbuf];
242 oldpile=(void *)Super(0); 249
243 250 oldpile = (void *) Super(0);
244 /* Stop replay */ 251
245 cookie_stfa->sound_enable=STFA_PLAY_DISABLE; 252 /* Stop replay */
246 253 cookie_stfa->sound_enable = STFA_PLAY_DISABLE;
247 /* Select replay format */ 254
248 cookie_stfa->sound_control = MINTAUDIO_frequencies[MINTAUDIO_numfreq].predivisor; 255 /* Select replay format */
249 if ((spec->format & 0xff)==8) { 256 cookie_stfa->sound_control =
250 cookie_stfa->sound_control |= STFA_FORMAT_8BIT; 257 MINTAUDIO_frequencies[MINTAUDIO_numfreq].predivisor;
251 } else { 258 if ((spec->format & 0xff) == 8) {
252 cookie_stfa->sound_control |= STFA_FORMAT_16BIT; 259 cookie_stfa->sound_control |= STFA_FORMAT_8BIT;
253 } 260 } else {
254 if (spec->channels==2) { 261 cookie_stfa->sound_control |= STFA_FORMAT_16BIT;
255 cookie_stfa->sound_control |= STFA_FORMAT_STEREO; 262 }
256 } else { 263 if (spec->channels == 2) {
257 cookie_stfa->sound_control |= STFA_FORMAT_MONO; 264 cookie_stfa->sound_control |= STFA_FORMAT_STEREO;
258 } 265 } else {
259 if ((spec->format & 0x8000)!=0) { 266 cookie_stfa->sound_control |= STFA_FORMAT_MONO;
260 cookie_stfa->sound_control |= STFA_FORMAT_SIGNED; 267 }
261 } else { 268 if ((spec->format & 0x8000) != 0) {
262 cookie_stfa->sound_control |= STFA_FORMAT_UNSIGNED; 269 cookie_stfa->sound_control |= STFA_FORMAT_SIGNED;
263 } 270 } else {
264 if ((spec->format & 0x1000)!=0) { 271 cookie_stfa->sound_control |= STFA_FORMAT_UNSIGNED;
265 cookie_stfa->sound_control |= STFA_FORMAT_BIGENDIAN; 272 }
266 } else { 273 if ((spec->format & 0x1000) != 0) {
267 cookie_stfa->sound_control |= STFA_FORMAT_LITENDIAN; 274 cookie_stfa->sound_control |= STFA_FORMAT_BIGENDIAN;
268 } 275 } else {
269 276 cookie_stfa->sound_control |= STFA_FORMAT_LITENDIAN;
270 /* Set buffer */ 277 }
271 cookie_stfa->sound_start = (unsigned long) buffer; 278
272 cookie_stfa->sound_end = (unsigned long) (buffer + spec->size); 279 /* Set buffer */
273 280 cookie_stfa->sound_start = (unsigned long) buffer;
274 /* Set interrupt */ 281 cookie_stfa->sound_end = (unsigned long) (buffer + spec->size);
275 cookie_stfa->stfa_it = SDL_MintAudio_StfaInterrupt; 282
276 283 /* Set interrupt */
277 /* Restart replay */ 284 cookie_stfa->stfa_it = SDL_MintAudio_StfaInterrupt;
278 cookie_stfa->sound_enable=STFA_PLAY_ENABLE|STFA_PLAY_REPEAT; 285
279 286 /* Restart replay */
280 Super(oldpile); 287 cookie_stfa->sound_enable = STFA_PLAY_ENABLE | STFA_PLAY_REPEAT;
281 288
282 DEBUG_PRINT((DEBUG_NAME "hardware initialized\n")); 289 Super(oldpile);
283 } 290
284 291 DEBUG_PRINT((DEBUG_NAME "hardware initialized\n"));
285 static int Mint_OpenAudio(_THIS, SDL_AudioSpec *spec) 292 }
286 { 293
287 SDL_MintAudio_device = this; 294 static int
288 295 Mint_OpenAudio(_THIS, SDL_AudioSpec * spec)
289 /* Check audio capabilities */ 296 {
290 if (Mint_CheckAudio(this, spec)==-1) { 297 SDL_MintAudio_device = this;
291 return -1; 298
292 } 299 /* Check audio capabilities */
293 300 if (Mint_CheckAudio(this, spec) == -1) {
294 SDL_CalculateAudioSpec(spec); 301 return -1;
295 302 }
296 /* Allocate memory for audio buffers in DMA-able RAM */ 303
297 DEBUG_PRINT((DEBUG_NAME "buffer size=%d\n", spec->size)); 304 SDL_CalculateAudioSpec(spec);
298 305
299 SDL_MintAudio_audiobuf[0] = Atari_SysMalloc(spec->size *2, MX_STRAM); 306 /* Allocate memory for audio buffers in DMA-able RAM */
300 if (SDL_MintAudio_audiobuf[0]==NULL) { 307 DEBUG_PRINT((DEBUG_NAME "buffer size=%d\n", spec->size));
301 SDL_SetError("MINT_OpenAudio: Not enough memory for audio buffer"); 308
302 return (-1); 309 SDL_MintAudio_audiobuf[0] = Atari_SysMalloc(spec->size * 2, MX_STRAM);
303 } 310 if (SDL_MintAudio_audiobuf[0] == NULL) {
304 SDL_MintAudio_audiobuf[1] = SDL_MintAudio_audiobuf[0] + spec->size ; 311 SDL_SetError("MINT_OpenAudio: Not enough memory for audio buffer");
305 SDL_MintAudio_numbuf=0; 312 return (-1);
306 SDL_memset(SDL_MintAudio_audiobuf[0], spec->silence, spec->size *2); 313 }
307 SDL_MintAudio_audiosize = spec->size; 314 SDL_MintAudio_audiobuf[1] = SDL_MintAudio_audiobuf[0] + spec->size;
308 SDL_MintAudio_mutex = 0; 315 SDL_MintAudio_numbuf = 0;
309 316 SDL_memset(SDL_MintAudio_audiobuf[0], spec->silence, spec->size * 2);
310 DEBUG_PRINT((DEBUG_NAME "buffer 0 at 0x%08x\n", SDL_MintAudio_audiobuf[0])); 317 SDL_MintAudio_audiosize = spec->size;
311 DEBUG_PRINT((DEBUG_NAME "buffer 1 at 0x%08x\n", SDL_MintAudio_audiobuf[1])); 318 SDL_MintAudio_mutex = 0;
312 319
313 /* Setup audio hardware */ 320 DEBUG_PRINT((DEBUG_NAME "buffer 0 at 0x%08x\n",
314 Mint_InitAudio(this, spec); 321 SDL_MintAudio_audiobuf[0]));
315 322 DEBUG_PRINT((DEBUG_NAME "buffer 1 at 0x%08x\n",
316 return(1); /* We don't use threaded audio */ 323 SDL_MintAudio_audiobuf[1]));
317 } 324
325 /* Setup audio hardware */
326 Mint_InitAudio(this, spec);
327
328 return (1); /* We don't use threaded audio */
329 }
330
331 /* vi: set ts=4 sw=4 expandtab: */