2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997-2009 Sam Lantinga
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.
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.
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
22 #include "SDL_config.h"
26 using XBIOS functions (STFA driver)
32 #include <mint/osbind.h>
33 #include <mint/falcon.h>
34 #include <mint/cookie.h>
36 #include "SDL_audio.h"
37 #include "../SDL_audio_c.h"
38 #include "../SDL_sysaudio.h"
40 #include "../../video/ataricommon/SDL_atarimxalloc_c.h"
42 #include "SDL_mintaudio.h"
43 #include "SDL_mintaudio_stfa.h"
47 #define MINT_AUDIO_DRIVER_NAME "mint_stfa"
49 /* Debug print info */
50 #define DEBUG_NAME "audio:stfa: "
52 #define DEBUG_PRINT(what) \
57 #define DEBUG_PRINT(what)
60 /*--- Static variables ---*/
62 static unsigned long cookie_snd, cookie_mch;
63 static cookie_stfa_t *cookie_stfa;
65 static const int freqs[16]={
66 4995, 6269, 7493, 8192,
67 9830, 10971, 12538, 14985,
68 16384, 19819, 21943, 24576,
69 30720, 32336, 43885, 49152
72 /*--- Audio driver functions ---*/
74 static void Mint_CloseAudio(_THIS);
75 static int Mint_OpenAudio(_THIS, SDL_AudioSpec *spec);
76 static void Mint_LockAudio(_THIS);
77 static void Mint_UnlockAudio(_THIS);
79 /* To check/init hardware audio */
80 static int Mint_CheckAudio(_THIS, SDL_AudioSpec *spec);
81 static void Mint_InitAudio(_THIS, SDL_AudioSpec *spec);
83 /*--- Audio driver bootstrap functions ---*/
85 static int Audio_Available(void)
87 const char *envr = SDL_getenv("SDL_AUDIODRIVER");
89 /* Check if user asked a different audio driver */
90 if ((envr) && (SDL_strcmp(envr, MINT_AUDIO_DRIVER_NAME)!=0)) {
91 DEBUG_PRINT((DEBUG_NAME "user asked a different audio driver\n"));
95 /* Cookie _MCH present ? if not, assume ST machine */
96 if (Getcookie(C__MCH, &cookie_mch) == C_NOTFOUND) {
100 /* Cookie _SND present ? if not, assume ST machine */
101 if (Getcookie(C__SND, &cookie_snd) == C_NOTFOUND) {
102 cookie_snd = SND_PSG;
105 /* Cookie STFA present ? */
106 if (Getcookie(C_STFA, (long *) &cookie_stfa) != C_FOUND) {
107 DEBUG_PRINT((DEBUG_NAME "no STFA audio\n"));
111 SDL_MintAudio_stfa = cookie_stfa;
113 DEBUG_PRINT((DEBUG_NAME "STFA audio available!\n"));
117 static void Audio_DeleteDevice(SDL_AudioDevice *device)
119 SDL_free(device->hidden);
123 static SDL_AudioDevice *Audio_CreateDevice(int devindex)
125 SDL_AudioDevice *this;
127 /* Initialize all variables that we clean on shutdown */
128 this = (SDL_AudioDevice *)SDL_malloc(sizeof(SDL_AudioDevice));
130 SDL_memset(this, 0, (sizeof *this));
131 this->hidden = (struct SDL_PrivateAudioData *)
132 SDL_malloc((sizeof *this->hidden));
134 if ( (this == NULL) || (this->hidden == NULL) ) {
141 SDL_memset(this->hidden, 0, (sizeof *this->hidden));
143 /* Set the function pointers */
144 this->OpenAudio = Mint_OpenAudio;
145 this->CloseAudio = Mint_CloseAudio;
146 this->LockAudio = Mint_LockAudio;
147 this->UnlockAudio = Mint_UnlockAudio;
148 this->free = Audio_DeleteDevice;
153 AudioBootStrap MINTAUDIO_STFA_bootstrap = {
154 MINT_AUDIO_DRIVER_NAME, "MiNT STFA audio driver",
155 Audio_Available, Audio_CreateDevice
158 static void Mint_LockAudio(_THIS)
163 oldpile=(void *)Super(0);
164 cookie_stfa->sound_enable=STFA_PLAY_DISABLE;
168 static void Mint_UnlockAudio(_THIS)
173 oldpile=(void *)Super(0);
174 cookie_stfa->sound_enable=STFA_PLAY_ENABLE|STFA_PLAY_REPEAT;
178 static void Mint_CloseAudio(_THIS)
183 oldpile=(void *)Super(0);
184 cookie_stfa->sound_enable=STFA_PLAY_DISABLE;
187 /* Wait if currently playing sound */
188 while (SDL_MintAudio_mutex != 0) {
192 if (SDL_MintAudio_audiobuf[0]) {
193 Mfree(SDL_MintAudio_audiobuf[0]);
194 SDL_MintAudio_audiobuf[0] = SDL_MintAudio_audiobuf[1] = NULL;
198 static int Mint_CheckAudio(_THIS, SDL_AudioSpec *spec)
202 DEBUG_PRINT((DEBUG_NAME "asked: %d bits, ",spec->format & 0x00ff));
203 DEBUG_PRINT(("signed=%d, ", ((spec->format & 0x8000)!=0)));
204 DEBUG_PRINT(("big endian=%d, ", ((spec->format & 0x1000)!=0)));
205 DEBUG_PRINT(("channels=%d, ", spec->channels));
206 DEBUG_PRINT(("freq=%d\n", spec->freq));
208 if (spec->channels > 2) {
209 spec->channels = 2; /* no more than stereo! */
212 /* Check formats available */
213 MINTAUDIO_freqcount=0;
215 SDL_MintAudio_AddFrequency(this, freqs[i], 0, i, -1);
219 for (i=0; i<MINTAUDIO_freqcount; i++) {
220 DEBUG_PRINT((DEBUG_NAME "freq %d: %lu Hz, clock %lu, prediv %d\n",
221 i, MINTAUDIO_frequencies[i].frequency, MINTAUDIO_frequencies[i].masterclock,
222 MINTAUDIO_frequencies[i].predivisor
227 MINTAUDIO_numfreq=SDL_MintAudio_SearchFrequency(this, spec->freq);
228 spec->freq=MINTAUDIO_frequencies[MINTAUDIO_numfreq].frequency;
230 DEBUG_PRINT((DEBUG_NAME "obtained: %d bits, ",spec->format & 0x00ff));
231 DEBUG_PRINT(("signed=%d, ", ((spec->format & 0x8000)!=0)));
232 DEBUG_PRINT(("big endian=%d, ", ((spec->format & 0x1000)!=0)));
233 DEBUG_PRINT(("channels=%d, ", spec->channels));
234 DEBUG_PRINT(("freq=%d\n", spec->freq));
239 static void Mint_InitAudio(_THIS, SDL_AudioSpec *spec)
244 buffer = SDL_MintAudio_audiobuf[SDL_MintAudio_numbuf];
246 oldpile=(void *)Super(0);
249 cookie_stfa->sound_enable=STFA_PLAY_DISABLE;
251 /* Select replay format */
252 cookie_stfa->sound_control = MINTAUDIO_frequencies[MINTAUDIO_numfreq].predivisor;
253 if ((spec->format & 0xff)==8) {
254 cookie_stfa->sound_control |= STFA_FORMAT_8BIT;
256 cookie_stfa->sound_control |= STFA_FORMAT_16BIT;
258 if (spec->channels==2) {
259 cookie_stfa->sound_control |= STFA_FORMAT_STEREO;
261 cookie_stfa->sound_control |= STFA_FORMAT_MONO;
263 if ((spec->format & 0x8000)!=0) {
264 cookie_stfa->sound_control |= STFA_FORMAT_SIGNED;
266 cookie_stfa->sound_control |= STFA_FORMAT_UNSIGNED;
268 if ((spec->format & 0x1000)!=0) {
269 cookie_stfa->sound_control |= STFA_FORMAT_BIGENDIAN;
271 cookie_stfa->sound_control |= STFA_FORMAT_LITENDIAN;
275 cookie_stfa->sound_start = (unsigned long) buffer;
276 cookie_stfa->sound_end = (unsigned long) (buffer + spec->size);
279 cookie_stfa->stfa_it = SDL_MintAudio_StfaInterrupt;
282 cookie_stfa->sound_enable=STFA_PLAY_ENABLE|STFA_PLAY_REPEAT;
286 DEBUG_PRINT((DEBUG_NAME "hardware initialized\n"));
289 static int Mint_OpenAudio(_THIS, SDL_AudioSpec *spec)
291 SDL_MintAudio_device = this;
293 /* Check audio capabilities */
294 if (Mint_CheckAudio(this, spec)==-1) {
298 SDL_CalculateAudioSpec(spec);
300 /* Allocate memory for audio buffers in DMA-able RAM */
301 DEBUG_PRINT((DEBUG_NAME "buffer size=%d\n", spec->size));
303 SDL_MintAudio_audiobuf[0] = Atari_SysMalloc(spec->size *2, MX_STRAM);
304 if (SDL_MintAudio_audiobuf[0]==NULL) {
305 SDL_SetError("MINT_OpenAudio: Not enough memory for audio buffer");
308 SDL_MintAudio_audiobuf[1] = SDL_MintAudio_audiobuf[0] + spec->size ;
309 SDL_MintAudio_numbuf=0;
310 SDL_memset(SDL_MintAudio_audiobuf[0], spec->silence, spec->size *2);
311 SDL_MintAudio_audiosize = spec->size;
312 SDL_MintAudio_mutex = 0;
314 DEBUG_PRINT((DEBUG_NAME "buffer 0 at 0x%08x\n", SDL_MintAudio_audiobuf[0]));
315 DEBUG_PRINT((DEBUG_NAME "buffer 1 at 0x%08x\n", SDL_MintAudio_audiobuf[1]));
317 SDL_MintAudio_CheckFpu();
319 /* Setup audio hardware */
320 Mint_InitAudio(this, spec);
322 return(1); /* We don't use threaded audio */