src/audio/mint/SDL_mintaudio.c
changeset 644 594422ab8f9f
parent 557 0ce5a68278fd
child 704 c4803992e09c
     1.1 --- a/src/audio/mint/SDL_mintaudio.c	Tue Jul 01 16:35:54 2003 +0000
     1.2 +++ b/src/audio/mint/SDL_mintaudio.c	Mon Jul 07 19:16:03 2003 +0000
     1.3 @@ -1,628 +1,93 @@
     1.4  /*
     1.5 - * MiNT audio driver
     1.6 - * 
     1.7 - * Patrice Mandin
     1.8 - */
     1.9 +    SDL - Simple DirectMedia Layer
    1.10 +    Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002  Sam Lantinga
    1.11  
    1.12 -#include <stdlib.h>
    1.13 -#include <stdio.h>
    1.14 +    This library is free software; you can redistribute it and/or
    1.15 +    modify it under the terms of the GNU Library General Public
    1.16 +    License as published by the Free Software Foundation; either
    1.17 +    version 2 of the License, or (at your option) any later version.
    1.18 +
    1.19 +    This library is distributed in the hope that it will be useful,
    1.20 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.21 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    1.22 +    Library General Public License for more details.
    1.23 +
    1.24 +    You should have received a copy of the GNU Library General Public
    1.25 +    License along with this library; if not, write to the Free
    1.26 +    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    1.27 +
    1.28 +    Sam Lantinga
    1.29 +    slouken@libsdl.org
    1.30 +*/
    1.31 +
    1.32 +/*
    1.33 +	Audio interrupt variables and callback function
    1.34 +
    1.35 +	Patrice Mandin
    1.36 +*/
    1.37 +
    1.38  #include <string.h>
    1.39  
    1.40 -/* Mint includes */
    1.41 -#include <mint/osbind.h>
    1.42 -#include <mint/falcon.h>
    1.43 -#include <mint/cookie.h>
    1.44 -
    1.45 -#include "SDL_endian.h"
    1.46 +#include "SDL_types.h"
    1.47  #include "SDL_audio.h"
    1.48 -#include "SDL_audio_c.h"
    1.49 -#include "SDL_audiomem.h"
    1.50 -#include "SDL_sysaudio.h"
    1.51  
    1.52  #include "SDL_mintaudio.h"
    1.53 -#include "SDL_mintaudiodma.h"
    1.54 -#include "SDL_mintaudiogsxb.h"
    1.55 -#include "SDL_mintaudiointerrupt_s.h"
    1.56 +#include "SDL_mintaudio_stfa.h"
    1.57  
    1.58 -#include "SDL_atarimxalloc_c.h"
    1.59 +/* The audio device */
    1.60  
    1.61 -/*--- Defines ---*/
    1.62 +SDL_AudioDevice *SDL_MintAudio_device;
    1.63 +Uint8 *SDL_MintAudio_audiobuf[2];	/* Pointers to buffers */
    1.64 +long SDL_MintAudio_audiosize;		/* Length of audio buffer=spec->size */
    1.65 +long SDL_MintAudio_numbuf;		/* Buffer to play */
    1.66 +long SDL_MintAudio_mutex;
    1.67 +cookie_stfa_t	*SDL_MintAudio_stfa;
    1.68  
    1.69 -#define MINT_AUDIO_DRIVER_NAME "mint"
    1.70 +/* The callback function, called by each driver whenever needed */
    1.71  
    1.72 -/* Master clocks for replay frequencies */
    1.73 -#define MASTERCLOCK_STE		8010666		/* Not sure of this one */
    1.74 -#define MASTERCLOCK_TT		16107953	/* Not sure of this one */
    1.75 -#define MASTERCLOCK_FALCON1	25175000
    1.76 -#define MASTERCLOCK_FALCON2	32000000	/* Only usable for DSP56K */
    1.77 -#define MASTERCLOCK_FALCONEXT	-1		/* Clock on DSP56K port, unknown */
    1.78 -#define MASTERCLOCK_MILAN1	22579200	/* Standard clock for 44.1 Khz */
    1.79 -#define MASTERCLOCK_MILAN2	24576000	/* Standard clock for 48 Khz */
    1.80 -
    1.81 -/* Master clock predivisors */
    1.82 -#define MASTERPREDIV_STE	160
    1.83 -#define MASTERPREDIV_TT		320
    1.84 -#define MASTERPREDIV_FALCON	256
    1.85 -#define MASTERPREDIV_MILAN	256
    1.86 -
    1.87 -/* Values>>16 in _MCH cookie */
    1.88 -enum {
    1.89 -	MCH_ST=0,
    1.90 -	MCH_STE,
    1.91 -	MCH_TT,
    1.92 -	MCH_F30
    1.93 -};
    1.94 -
    1.95 -/* MFP 68901 interrupt sources */
    1.96 -enum {
    1.97 -	MFP_PARALLEL=0,
    1.98 -	MFP_DCD,
    1.99 -	MFP_CTS,
   1.100 -	MFP_BITBLT,
   1.101 -	MFP_TIMERD,
   1.102 -	MFP_BAUDRATE=MFP_TIMERD,
   1.103 -	MFP_TIMERC,
   1.104 -	MFP_200HZ=MFP_TIMERC,
   1.105 -	MFP_ACIA,
   1.106 -	MFP_DISK,
   1.107 -	MFP_TIMERB,
   1.108 -	MFP_HBLANK=MFP_TIMERB,
   1.109 -	MFP_TERR,
   1.110 -	MFP_TBE,
   1.111 -	MFP_RERR,
   1.112 -	MFP_RBF,
   1.113 -	MFP_TIMERA,
   1.114 -	MFP_DMASOUND=MFP_TIMERA,
   1.115 -	MFP_RING,
   1.116 -	MFP_MONODETECT
   1.117 -};
   1.118 -
   1.119 -/* Xbtimer() timers */
   1.120 -enum {
   1.121 -	XB_TIMERA=0,
   1.122 -	XB_TIMERB,
   1.123 -	XB_TIMERC,
   1.124 -	XB_TIMERD
   1.125 -};
   1.126 -
   1.127 -/*--- Static variables ---*/
   1.128 -
   1.129 -static unsigned long cookie_snd, cookie_mch, cookie_gsxb;
   1.130 -static Uint16 hardfreq[16];
   1.131 -static Uint16 numfreq;
   1.132 -static SDL_AudioDevice *SDL_MintAudio_device;
   1.133 -
   1.134 -/*--- Audio driver functions ---*/
   1.135 -
   1.136 -static void Mint_CloseAudio(_THIS);
   1.137 -static int Mint_OpenAudio(_THIS, SDL_AudioSpec *spec);
   1.138 -static void Mint_LockAudio(_THIS);
   1.139 -static void Mint_UnlockAudio(_THIS);
   1.140 -
   1.141 -/*--- Audio driver bootstrap functions ---*/
   1.142 -
   1.143 -static int Audio_Available(void)
   1.144 -{
   1.145 -	const char *envr = getenv("SDL_AUDIODRIVER");
   1.146 -
   1.147 -	/* Check if user asked a different audio driver */
   1.148 -	if ((envr) && (strcmp(envr, MINT_AUDIO_DRIVER_NAME)!=0)) {
   1.149 -		return 0;
   1.150 -	}
   1.151 -
   1.152 -	/* Cookie _SND present ? if not, assume ST machine */
   1.153 -	if (Getcookie(C__SND, &cookie_snd) == C_NOTFOUND) {
   1.154 -		cookie_snd = SND_PSG;
   1.155 -	}
   1.156 -
   1.157 -	/* Cookie _MCH present ? if not, assume ST machine */
   1.158 -	if (Getcookie(C__MCH, &cookie_mch) == C_NOTFOUND) {
   1.159 -		cookie_mch = MCH_ST << 16;
   1.160 -	}
   1.161 -
   1.162 -	/* Cookie GSXB present ? */
   1.163 -	cookie_gsxb = (Getcookie(C_GSXB, &cookie_gsxb) == C_FOUND);
   1.164 -
   1.165 -	/* Check if we have xbios functions (Falcon, clones) */
   1.166 -	if ((cookie_snd & SND_16BIT)!=0) {
   1.167 -		/* Check if audio is lockable */
   1.168 -		if (Locksnd()==1) {
   1.169 -			Unlocksnd();
   1.170 -		} else {
   1.171 -			/* Already in use */
   1.172 -			return(0);
   1.173 -		}
   1.174 -
   1.175 -		return(1);
   1.176 -	}
   1.177 -
   1.178 -	/* Check if we have 8 bits DMA audio (STE, TT) */
   1.179 -	if ((cookie_snd & SND_8BIT)!=0) {
   1.180 -		return(1);
   1.181 -	}
   1.182 -
   1.183 -    return(0);
   1.184 -}
   1.185 -
   1.186 -static void Audio_DeleteDevice(SDL_AudioDevice *device)
   1.187 -{
   1.188 -    free(device->hidden);
   1.189 -    free(device);
   1.190 -}
   1.191 -
   1.192 -static SDL_AudioDevice *Audio_CreateDevice(int devindex)
   1.193 -{
   1.194 -    SDL_AudioDevice *this;
   1.195 -
   1.196 -    /* Initialize all variables that we clean on shutdown */
   1.197 -    this = (SDL_AudioDevice *)malloc(sizeof(SDL_AudioDevice));
   1.198 -    if ( this ) {
   1.199 -        memset(this, 0, (sizeof *this));
   1.200 -    }
   1.201 -    if ( this == NULL ) {
   1.202 -        SDL_OutOfMemory();
   1.203 -        if ( this ) {
   1.204 -            free(this);
   1.205 -        }
   1.206 -        return(0);
   1.207 -    }
   1.208 -
   1.209 -    /* Set the function pointers */
   1.210 -    this->OpenAudio   = Mint_OpenAudio;
   1.211 -    this->CloseAudio  = Mint_CloseAudio;
   1.212 -    this->LockAudio   = Mint_LockAudio;
   1.213 -    this->UnlockAudio = Mint_UnlockAudio;
   1.214 -    this->free        = Audio_DeleteDevice;
   1.215 -
   1.216 -    return this;
   1.217 -}
   1.218 -
   1.219 -AudioBootStrap MINTAUDIO_bootstrap = {
   1.220 -	MINT_AUDIO_DRIVER_NAME, "MiNT audio driver",
   1.221 -	Audio_Available, Audio_CreateDevice
   1.222 -};
   1.223 -
   1.224 -static void Mint_LockAudio(_THIS)
   1.225 -{
   1.226 -	void *oldpile;
   1.227 -
   1.228 -	/* Stop replay */
   1.229 -	if ((cookie_snd & SND_16BIT)!=0) {
   1.230 -		Buffoper(0);
   1.231 -	} else if ((cookie_snd & SND_8BIT)!=0) {
   1.232 -		oldpile=(void *)Super(0);
   1.233 -		DMAAUDIO_IO.control=0;
   1.234 -		Super(oldpile);
   1.235 -	}
   1.236 -}
   1.237 -
   1.238 -static void Mint_UnlockAudio(_THIS)
   1.239 -{
   1.240 -	void *oldpile;
   1.241 -
   1.242 -	/* Restart replay */
   1.243 -	if ((cookie_snd & SND_16BIT)!=0) {
   1.244 -		Buffoper(SB_PLA_ENA|SB_PLA_RPT);
   1.245 -	} else if ((cookie_snd & SND_8BIT)!=0) {
   1.246 -		oldpile=(void *)Super(0);
   1.247 -		DMAAUDIO_IO.control=3;
   1.248 -		Super(oldpile);
   1.249 -	}
   1.250 -}
   1.251 -
   1.252 -/* This is called from the interrupt routine */
   1.253  void SDL_MintAudio_Callback(void)
   1.254  {
   1.255 -	SDL_AudioDevice *audio;
   1.256  	Uint8 *buffer;
   1.257  
   1.258 -	audio = SDL_MintAudio_device;
   1.259   	buffer = SDL_MintAudio_audiobuf[SDL_MintAudio_numbuf];
   1.260 +	memset(buffer, SDL_MintAudio_device->spec.silence, SDL_MintAudio_device->spec.size);
   1.261  
   1.262 -	if ( ! audio->paused ) {
   1.263 -		if ( audio->convert.needed ) {
   1.264 -			audio->spec.callback(audio->spec.userdata,
   1.265 -				(Uint8 *)audio->convert.buf,audio->convert.len);
   1.266 -			SDL_ConvertAudio(&audio->convert);
   1.267 -			memcpy(buffer, audio->convert.buf, audio->convert.len_cvt);
   1.268 +	if ( ! SDL_MintAudio_device->paused ) {
   1.269 +		if ( SDL_MintAudio_device->convert.needed ) {
   1.270 +			SDL_MintAudio_device->spec.callback(SDL_MintAudio_device->spec.userdata,
   1.271 +				(Uint8 *)SDL_MintAudio_device->convert.buf,SDL_MintAudio_device->convert.len);
   1.272 +			SDL_ConvertAudio(&SDL_MintAudio_device->convert);
   1.273 +			memcpy(buffer, SDL_MintAudio_device->convert.buf, SDL_MintAudio_device->convert.len_cvt);
   1.274  		} else {
   1.275 -			audio->spec.callback(audio->spec.userdata, buffer, audio->spec.size);
   1.276 +			SDL_MintAudio_device->spec.callback(SDL_MintAudio_device->spec.userdata, buffer, SDL_MintAudio_device->spec.size);
   1.277  		}
   1.278  	}
   1.279  }
   1.280  
   1.281 -static void Mint_StopAudio_Dma8(void)
   1.282 -{
   1.283 -	void *oldpile;
   1.284 -	
   1.285 -	oldpile=(void *)Super(0);
   1.286 -	DMAAUDIO_IO.control=0;
   1.287 -	Super(oldpile);
   1.288 -
   1.289 -	Jdisint(MFP_DMASOUND);
   1.290 -}
   1.291 -
   1.292 -static void Mint_StopAudio_Xbios(void)
   1.293 -{
   1.294 -	Buffoper(0);
   1.295 -	Jdisint(MFP_DMASOUND);
   1.296 -}
   1.297 -
   1.298 -static void Mint_StopAudio_Gsxb(void)
   1.299 -{
   1.300 -	Buffoper(0);
   1.301 -}
   1.302 -
   1.303 -static void Mint_CloseAudio(_THIS)
   1.304 -{
   1.305 -	if (cookie_gsxb && ((cookie_snd & (SND_GSXB|SND_16BIT))==(SND_GSXB|SND_16BIT)) ) {
   1.306 -		Mint_StopAudio_Gsxb();
   1.307 -	} else if ((cookie_snd & SND_16BIT)!=0) {
   1.308 -		Mint_StopAudio_Xbios();
   1.309 -	} else if ((cookie_snd & SND_8BIT)!=0) {
   1.310 -		Mint_StopAudio_Dma8();
   1.311 -	}
   1.312 -
   1.313 -	/* Clear buffers */
   1.314 -	if (SDL_MintAudio_audiobuf[0]) {
   1.315 -		Mfree(SDL_MintAudio_audiobuf[0]);
   1.316 -		SDL_MintAudio_audiobuf[0] = SDL_MintAudio_audiobuf[1] = NULL;
   1.317 -	}
   1.318 -
   1.319 -	/* Unlock sound system */
   1.320 -	if ((cookie_snd & SND_16BIT)!=0) {
   1.321 -		Unlocksnd();
   1.322 -	}
   1.323 -}
   1.324 -
   1.325 -static void Mint_CheckAudio_Dma8(SDL_AudioSpec *spec)
   1.326 +/* Simple function to search for the nearest frequency */
   1.327 +int SDL_MintAudio_SearchFrequency(_THIS, int falcon_codec, int desired_freq)
   1.328  {
   1.329  	int i;
   1.330  
   1.331 -	spec->format = AUDIO_S8;
   1.332 -
   1.333 -	switch(cookie_mch>>16) {
   1.334 -		case MCH_STE:
   1.335 -			/* STE replay frequencies */
   1.336 -			for (i=0;i<4;i++) {
   1.337 -				hardfreq[i]=MASTERCLOCK_STE/(MASTERPREDIV_STE*(i+1));
   1.338 -			}
   1.339 -
   1.340 -			if (spec->freq>=(hardfreq[0]+hardfreq[1])>>1) {
   1.341 -				numfreq=3;		/* 50066 */
   1.342 -			} else if (spec->freq>=(hardfreq[1]+hardfreq[2])>>1) {
   1.343 -				numfreq=2;		/* 25033 */
   1.344 -			} else if (spec->freq>=(hardfreq[2]+hardfreq[3])>>1) {
   1.345 -				numfreq=1;		/* 12517 */
   1.346 -			} else {
   1.347 -				numfreq=0;		/* 6258 */
   1.348 -			}
   1.349 -
   1.350 -			spec->freq=hardfreq[numfreq];
   1.351 -			break;
   1.352 -		case MCH_TT:
   1.353 -			/* TT replay frequencies */
   1.354 -			for (i=0;i<4;i++) {
   1.355 -				hardfreq[i]=MASTERCLOCK_TT/(MASTERPREDIV_TT*(i+1));
   1.356 -			}
   1.357 -
   1.358 -			if (spec->freq>=(hardfreq[0]+hardfreq[1])>>1) {
   1.359 -				numfreq=3;		/* 50337 */
   1.360 -			} else if (spec->freq>=(hardfreq[1]+hardfreq[2])>>1) {
   1.361 -				numfreq=2;		/* 25169 */
   1.362 -			} else if (spec->freq>=(hardfreq[2]+hardfreq[3])>>1) {
   1.363 -				numfreq=1;		/* 12584 */
   1.364 -			} else {
   1.365 -				numfreq=0;		/* 6292 */
   1.366 -			}
   1.367 -			spec->freq=hardfreq[numfreq];
   1.368 -			break;
   1.369 -	}
   1.370 -}
   1.371 -
   1.372 -static void Mint_CheckAudio_Xbios(SDL_AudioSpec *spec)
   1.373 -{
   1.374 -	int i;
   1.375 -
   1.376 -	/* Check conversions needed */
   1.377 -	switch (spec->format & 0xff) {
   1.378 -		case 8:
   1.379 -			spec->format = AUDIO_S8;
   1.380 -			break;
   1.381 -		case 16:
   1.382 -			spec->format = AUDIO_S16MSB;
   1.383 -			break;
   1.384 -	}
   1.385 -	
   1.386 -	/* Check hardware channels */
   1.387 -	if ((spec->channels==1) && ((spec->format & 0xff)==16)) {
   1.388 -		spec->channels=2;
   1.389 +	/* Only 1 freq ? */
   1.390 +	if (MINTAUDIO_nfreq==1) {
   1.391 +		return(MINTAUDIO_sfreq);
   1.392  	}
   1.393  
   1.394 -	/* Falcon replay frequencies */
   1.395 -	for (i=0;i<16;i++) {
   1.396 -		hardfreq[i]=MASTERCLOCK_FALCON1/(MASTERPREDIV_FALCON*(i+1));
   1.397 -	}
   1.398 +	/* Check the array */
   1.399 +	for (i=MINTAUDIO_sfreq; i<MINTAUDIO_nfreq-1; i++) {
   1.400 +		/* Remove unusable falcon codec frequencies */
   1.401 +		if (falcon_codec) {
   1.402 +			if ((i==6) || (i==8) || (i==10)) {
   1.403 +				continue;
   1.404 +			}
   1.405 +		}
   1.406  
   1.407 -	/* The Falcon CODEC only support some frequencies */
   1.408 -	if (spec->freq>=(hardfreq[CLK50K]+hardfreq[CLK33K])>>1) {
   1.409 -		numfreq=CLK50K;		/* 49170 */
   1.410 -	} else if (spec->freq>=(hardfreq[CLK33K]+hardfreq[CLK25K])>>1) {
   1.411 -		numfreq=CLK33K;		/* 32780 */
   1.412 -	} else if (spec->freq>=(hardfreq[CLK25K]+hardfreq[CLK20K])>>1) {
   1.413 -		numfreq=CLK25K;		/* 24585 */
   1.414 -	} else if (spec->freq>=(hardfreq[CLK20K]+hardfreq[CLK16K])>>1) {
   1.415 -		numfreq=CLK20K;		/* 19668 */
   1.416 -	} else if (spec->freq>=(hardfreq[CLK16K]+hardfreq[CLK12K])>>1) {
   1.417 -		numfreq=CLK16K;		/* 16390 */
   1.418 -	} else if (spec->freq>=(hardfreq[CLK12K]+hardfreq[CLK10K])>>1) {
   1.419 -		numfreq=CLK12K;		/* 12292 */
   1.420 -	} else if (spec->freq>=(hardfreq[CLK10K]+hardfreq[CLK8K])>>1) {
   1.421 -		numfreq=CLK10K;		/* 9834 */
   1.422 -	} else {
   1.423 -		numfreq=CLK8K;		/* 8195 */
   1.424 -	}				
   1.425 -
   1.426 -	spec->freq=hardfreq[numfreq];
   1.427 -}
   1.428 -
   1.429 -static int Mint_CheckAudio_Gsxb(SDL_AudioSpec *spec)
   1.430 -{
   1.431 -	long snd_format;
   1.432 -	int i, resolution, format_signed, format_bigendian;
   1.433 -
   1.434 -	resolution = spec->format & 0x00ff;
   1.435 -	format_signed = ((spec->format & 0x8000)!=0);
   1.436 -	format_bigendian = ((spec->format & 0x1000)!=0);
   1.437 -
   1.438 -	/* Check formats available */
   1.439 -	snd_format = Sndstatus(SND_QUERYFORMATS);
   1.440 -	switch (resolution) {
   1.441 -		case 8:
   1.442 -			if ((snd_format & SND_FORMAT8)==0) {
   1.443 -				SDL_SetError("Mint_CheckAudio: 8 bits samples not supported");
   1.444 -				return -1;
   1.445 -			}
   1.446 -			snd_format = Sndstatus(SND_QUERY8BIT);
   1.447 -			break;
   1.448 -		case 16:
   1.449 -			if ((snd_format & SND_FORMAT16)==0) {
   1.450 -				SDL_SetError("Mint_CheckAudio: 16 bits samples not supported");
   1.451 -				return -1;
   1.452 -			}
   1.453 -			snd_format = Sndstatus(SND_QUERY16BIT);
   1.454 -			break;
   1.455 -		default:
   1.456 -			SDL_SetError("Mint_CheckAudio: Unsupported sample resolution");
   1.457 -			return -1;
   1.458 -			break;
   1.459 -	}
   1.460 -
   1.461 -	/* Check signed/unsigned format */
   1.462 -	if (format_signed) {
   1.463 -		if (snd_format & SND_FORMATSIGNED) {
   1.464 -			/* Ok */
   1.465 -		} else if (snd_format & SND_FORMATUNSIGNED) {
   1.466 -			/* Give unsigned format */
   1.467 -			spec->format = spec->format & (~0x8000);
   1.468 -		}
   1.469 -	} else {
   1.470 -		if (snd_format & SND_FORMATUNSIGNED) {
   1.471 -			/* Ok */
   1.472 -		} else if (snd_format & SND_FORMATSIGNED) {
   1.473 -			/* Give signed format */
   1.474 -			spec->format |= 0x8000;
   1.475 +		if (desired_freq >= ((MINTAUDIO_hardfreq[i]+MINTAUDIO_hardfreq[i+1])>>1)) {
   1.476 +			return i;
   1.477  		}
   1.478  	}
   1.479  
   1.480 -	if (format_bigendian) {
   1.481 -		if (snd_format & SND_FORMATBIGENDIAN) {
   1.482 -			/* Ok */
   1.483 -		} else if (snd_format & SND_FORMATLITTLEENDIAN) {
   1.484 -			/* Give little endian format */
   1.485 -			spec->format = spec->format & (~0x1000);
   1.486 -		}
   1.487 -	} else {
   1.488 -		if (snd_format & SND_FORMATBIGENDIAN) {
   1.489 -			/* Ok */
   1.490 -		} else if (snd_format & SND_FORMATLITTLEENDIAN) {
   1.491 -			/* Give big endian format */
   1.492 -			spec->format |= 0x1000;
   1.493 -		}
   1.494 -	}
   1.495 -	
   1.496 -	/* Only xbios functions available = clone with PC board */
   1.497 -	for (i=0;i<8;i++) {
   1.498 -		hardfreq[i]=MASTERCLOCK_MILAN1/(MASTERPREDIV_MILAN*(i+1));
   1.499 -	}
   1.500 -
   1.501 -	if (spec->freq>=(hardfreq[CLK_44K]+hardfreq[CLK_22K])>>1) {
   1.502 -		numfreq = CLK_44K;	/* 44100 */
   1.503 -	} else if (spec->freq>=(hardfreq[CLK_22K]+hardfreq[CLK_11K])>>1) {
   1.504 -		numfreq = CLK_22K;	/* 22050 */
   1.505 -	} else {
   1.506 -		numfreq = CLK_11K;	/* 11025 */
   1.507 -	}				
   1.508 -
   1.509 -	spec->freq=hardfreq[numfreq];
   1.510 -
   1.511 -	return 0;
   1.512 +	/* Not in the array, give the latest */
   1.513 +	return MINTAUDIO_nfreq-1;
   1.514  }
   1.515 -
   1.516 -static void Mint_InitAudio_Dma8(SDL_AudioSpec *spec)
   1.517 -{
   1.518 -	void *oldpile;
   1.519 -	unsigned long buffer;
   1.520 -	unsigned char mode;
   1.521 -	
   1.522 -	oldpile=(void *)Super(0);
   1.523 -
   1.524 -	/* Stop currently playing sound */
   1.525 -	DMAAUDIO_IO.control=0;
   1.526 -
   1.527 -	/* Set buffer */
   1.528 -	buffer = (unsigned long) SDL_MintAudio_audiobuf[SDL_MintAudio_numbuf];
   1.529 -	DMAAUDIO_IO.start_high = (buffer>>16) & 255;
   1.530 -	DMAAUDIO_IO.start_mid = (buffer>>8) & 255;
   1.531 -	DMAAUDIO_IO.start_low = buffer & 255;
   1.532 -
   1.533 -	buffer += SDL_MintAudio_audiosize;
   1.534 -	DMAAUDIO_IO.end_high = (buffer>>16) & 255;
   1.535 -	DMAAUDIO_IO.end_mid = (buffer>>8) & 255;
   1.536 -	DMAAUDIO_IO.end_low = buffer & 255;
   1.537 -
   1.538 -	mode = numfreq;
   1.539 -	if (spec->channels==1) {
   1.540 -		mode |= 1<<7;
   1.541 -	}
   1.542 -	DMAAUDIO_IO.mode = mode;	
   1.543 -
   1.544 -	/* Set interrupt */
   1.545 -	Jdisint(MFP_DMASOUND);
   1.546 -	Xbtimer(XB_TIMERA, 8, 1, SDL_MintAudio_IntDma);
   1.547 -	Jenabint(MFP_DMASOUND);
   1.548 -
   1.549 -	/* Go */
   1.550 -	DMAAUDIO_IO.control = 3;	/* playback + repeat */
   1.551 -
   1.552 -	Super(oldpile);
   1.553 -}
   1.554 -
   1.555 -static void Mint_InitAudio_Xbios(SDL_AudioSpec *spec)
   1.556 -{
   1.557 -	int channels_mode;
   1.558 -	void *buffer;
   1.559 -
   1.560 -	/* Stop currently playing sound */
   1.561 -	Buffoper(0);
   1.562 -
   1.563 -	Settracks(0,0);
   1.564 -	Setmontracks(0);
   1.565 -
   1.566 -	switch (spec->format & 0xff) {
   1.567 -		case 8:
   1.568 -			if (spec->channels==2) {
   1.569 -				channels_mode=STEREO8;
   1.570 -			} else {
   1.571 -				channels_mode=MONO8;
   1.572 -			}
   1.573 -			break;
   1.574 -		case 16:
   1.575 -		default:
   1.576 -			channels_mode=STEREO16;
   1.577 -			break;
   1.578 -	}
   1.579 -	Setmode(channels_mode);
   1.580 -
   1.581 -	Devconnect(DMAPLAY, DAC, CLK25M, numfreq, 1);
   1.582 -
   1.583 -	/* Set buffer */
   1.584 -	buffer = SDL_MintAudio_audiobuf[SDL_MintAudio_numbuf];
   1.585 -	Setbuffer(0, buffer, buffer+SDL_MintAudio_audiosize);
   1.586 -	
   1.587 -	/* Install interrupt */
   1.588 -	Setinterrupt(SI_TIMERA, SI_PLAY);
   1.589 -
   1.590 -	Jdisint(MFP_DMASOUND);
   1.591 -	Xbtimer(XB_TIMERA, 8, 1, SDL_MintAudio_IntXbios);
   1.592 -	Jenabint(MFP_DMASOUND);
   1.593 -
   1.594 -	/* Go */
   1.595 -	Buffoper(SB_PLA_ENA|SB_PLA_RPT);
   1.596 -}
   1.597 -
   1.598 -static void Mint_InitAudio_Gsxb(SDL_AudioSpec *spec)
   1.599 -{
   1.600 -	int channels_mode;
   1.601 -	void *buffer;
   1.602 -
   1.603 -	/* Stop currently playing sound */
   1.604 -	Buffoper(0);
   1.605 -
   1.606 -	switch (spec->format & 0xff) {
   1.607 -		case 8:
   1.608 -			if (spec->channels==2) {
   1.609 -				channels_mode=STEREO8;
   1.610 -			} else {
   1.611 -				channels_mode=MONO8;
   1.612 -			}
   1.613 -			break;
   1.614 -		case 16:
   1.615 -			if (spec->channels==2) {
   1.616 -				channels_mode=STEREO16;
   1.617 -			} else {
   1.618 -				channels_mode=MONO16;
   1.619 -			}
   1.620 -			break;
   1.621 -		default:
   1.622 -			channels_mode=STEREO16;
   1.623 -			break;
   1.624 -	}
   1.625 -	Setmode(channels_mode);
   1.626 -
   1.627 -	Devconnect(0, 0, CLKEXT, numfreq, 1);
   1.628 -
   1.629 -	/* Set buffer */
   1.630 -	buffer = SDL_MintAudio_audiobuf[SDL_MintAudio_numbuf];
   1.631 -	Setbuffer(0, buffer, buffer+SDL_MintAudio_audiosize);
   1.632 -	
   1.633 -	/* Install interrupt */
   1.634 -	NSetinterrupt(2, SI_PLAY, SDL_MintAudio_IntGsxb);
   1.635 -		
   1.636 -	/* Go */
   1.637 -	Buffoper(SB_PLA_ENA|SB_PLA_RPT);
   1.638 -}
   1.639 -
   1.640 -static int Mint_OpenAudio(_THIS, SDL_AudioSpec *spec)
   1.641 -{
   1.642 -	/* Lock sound system */
   1.643 -	if ((cookie_snd & SND_16BIT)!=0) {
   1.644 -		if (Locksnd()!=1) {
   1.645 -    	    SDL_SetError("Mint_OpenAudio: Audio system already in use");
   1.646 -	        return(-1);
   1.647 -		}
   1.648 -	}
   1.649 -
   1.650 -	/* Check audio capabilities */
   1.651 -	if (cookie_gsxb && ((cookie_snd & (SND_GSXB|SND_16BIT))==(SND_GSXB|SND_16BIT)) ) {
   1.652 -		if (Mint_CheckAudio_Gsxb(spec)==-1) {
   1.653 -			return -1;
   1.654 -		}
   1.655 -	} else if ((cookie_snd & SND_16BIT)!=0) {
   1.656 -		Mint_CheckAudio_Xbios(spec);
   1.657 -	} else if ((cookie_snd & SND_8BIT)!=0) {
   1.658 -		Mint_CheckAudio_Dma8(spec);
   1.659 -	}
   1.660 -
   1.661 -	SDL_CalculateAudioSpec(spec);
   1.662 -
   1.663 -	/* Allocate memory for audio buffers in DMA-able RAM */
   1.664 -	spec->size = spec->samples;
   1.665 -	spec->size *= spec->channels;
   1.666 -	spec->size *= (spec->format & 0xFF)/8;
   1.667 -
   1.668 -	SDL_MintAudio_audiosize = spec->size;
   1.669 -
   1.670 -	SDL_MintAudio_audiobuf[0] = Atari_SysMalloc(SDL_MintAudio_audiosize *2, MX_STRAM);
   1.671 -	if (SDL_MintAudio_audiobuf[0]==NULL) {
   1.672 -		SDL_SetError("MINT_OpenAudio: Not enough memory for audio buffer");
   1.673 -		return (-1);
   1.674 -	}
   1.675 -	SDL_MintAudio_audiobuf[1] = SDL_MintAudio_audiobuf[0] + SDL_MintAudio_audiosize;
   1.676 -	SDL_MintAudio_numbuf=0;
   1.677 -	memset(SDL_MintAudio_audiobuf[0], 0, SDL_MintAudio_audiosize * 2);
   1.678 -	SDL_MintAudio_mutex = 0;
   1.679 -
   1.680 -	SDL_MintAudio_device = this;
   1.681 -
   1.682 -	/* Setup audio hardware */
   1.683 -	if (cookie_gsxb && ((cookie_snd & (SND_GSXB|SND_16BIT))==(SND_GSXB|SND_16BIT)) ) {
   1.684 -		Mint_InitAudio_Gsxb(spec);
   1.685 -	} else if ((cookie_snd & SND_16BIT)!=0) {
   1.686 -		Mint_InitAudio_Xbios(spec);
   1.687 -	} else if ((cookie_snd & SND_8BIT)!=0) {
   1.688 -		Mint_InitAudio_Dma8(spec);
   1.689 -	}
   1.690 -
   1.691 -    return 1;
   1.692 -}