19682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*
29682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL - Simple DirectMedia Layer
39682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Copyright (C) 1997-2012 Sam Lantinga
49682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
59682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    This library is free software; you can redistribute it and/or
69682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    modify it under the terms of the GNU Library General Public
79682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License as published by the Free Software Foundation; either
89682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    version 2 of the License, or (at your option) any later version.
99682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    This library is distributed in the hope that it will be useful,
119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    but WITHOUT ANY WARRANTY; without even the implied warranty of
129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Library General Public License for more details.
149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    You should have received a copy of the GNU Library General Public
169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License along with this library; if not, write to the Free
179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Sam Lantinga
209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    slouken@libsdl.org
219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_config.h"
239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*
259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MiNT audio driver
269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	using XBIOS functions (STFA driver)
279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Patrice Mandin
299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Mint includes */
329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <mint/osbind.h>
339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <mint/falcon.h>
349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <mint/cookie.h>
359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_audio.h"
379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../SDL_audio_c.h"
389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../SDL_sysaudio.h"
399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../../video/ataricommon/SDL_atarimxalloc_c.h"
419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../../video/ataricommon/SDL_atarisuper.h"
429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_mintaudio.h"
449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_mintaudio_stfa.h"
459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*--- Defines ---*/
479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define MINT_AUDIO_DRIVER_NAME "mint_stfa"
499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Debug print info */
519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define DEBUG_NAME "audio:stfa: "
529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if 0
539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define DEBUG_PRINT(what) \
549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{ \
559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		printf what; \
569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else
589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define DEBUG_PRINT(what)
599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*--- Static variables ---*/
629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic long cookie_snd, cookie_mch;
649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic cookie_stfa_t *cookie_stfa;
659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic const int freqs[16]={
679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	4995,	6269,	7493,	8192,
689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	9830,	10971,	12538,	14985,
699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	16384,	19819,	21943,	24576,
709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	30720,	32336,	43885,	49152
719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall};
729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*--- Audio driver functions ---*/
749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void Mint_CloseAudio(_THIS);
769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int Mint_OpenAudio(_THIS, SDL_AudioSpec *spec);
779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void Mint_LockAudio(_THIS);
789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void Mint_UnlockAudio(_THIS);
799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* To check/init hardware audio */
819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int Mint_CheckAudio(_THIS, SDL_AudioSpec *spec);
829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void Mint_InitAudio(_THIS, SDL_AudioSpec *spec);
839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*--- Audio driver bootstrap functions ---*/
859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int Audio_Available(void)
879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	long dummy;
899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	const char *envr = SDL_getenv("SDL_AUDIODRIVER");
909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Check if user asked a different audio driver */
929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ((envr) && (SDL_strcmp(envr, MINT_AUDIO_DRIVER_NAME)!=0)) {
939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		DEBUG_PRINT((DEBUG_NAME "user asked a different audio driver\n"));
949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(0);
959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Cookie _MCH present ? if not, assume ST machine */
989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (Getcookie(C__MCH, &cookie_mch) == C_NOTFOUND) {
999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		cookie_mch = MCH_ST;
1009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Cookie _SND present ? if not, assume ST machine */
1039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (Getcookie(C__SND, &cookie_snd) == C_NOTFOUND) {
1049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		cookie_snd = SND_PSG;
1059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Cookie STFA present ? */
1089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (Getcookie(C_STFA, &dummy) != C_FOUND) {
1099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		DEBUG_PRINT((DEBUG_NAME "no STFA audio\n"));
1109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(0);
1119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	cookie_stfa = (cookie_stfa_t *) dummy;
1139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_MintAudio_stfa = cookie_stfa;
1159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	DEBUG_PRINT((DEBUG_NAME "STFA audio available!\n"));
1179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(1);
1189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void Audio_DeleteDevice(SDL_AudioDevice *device)
1219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_free(device->hidden);
1239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_free(device);
1249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic SDL_AudioDevice *Audio_CreateDevice(int devindex)
1279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_AudioDevice *this;
1299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Initialize all variables that we clean on shutdown */
1319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this = (SDL_AudioDevice *)SDL_malloc(sizeof(SDL_AudioDevice));
1329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( this ) {
1339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        SDL_memset(this, 0, (sizeof *this));
1349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        this->hidden = (struct SDL_PrivateAudioData *)
1359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                SDL_malloc((sizeof *this->hidden));
1369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
1379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( (this == NULL) || (this->hidden == NULL) ) {
1389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        SDL_OutOfMemory();
1399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if ( this ) {
1409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            SDL_free(this);
1419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
1429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        return(0);
1439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
1449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_memset(this->hidden, 0, (sizeof *this->hidden));
1459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* Set the function pointers */
1479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    this->OpenAudio   = Mint_OpenAudio;
1489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    this->CloseAudio  = Mint_CloseAudio;
1499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    this->LockAudio   = Mint_LockAudio;
1509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    this->UnlockAudio = Mint_UnlockAudio;
1519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    this->free        = Audio_DeleteDevice;
1529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return this;
1549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallAudioBootStrap MINTAUDIO_STFA_bootstrap = {
1579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MINT_AUDIO_DRIVER_NAME, "MiNT STFA audio driver",
1589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Audio_Available, Audio_CreateDevice
1599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall};
1609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void Mint_LockAudio(_THIS)
1629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	void *oldpile;
1649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Stop replay */
1669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	oldpile=(void *)Super(0);
1679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	cookie_stfa->sound_enable=STFA_PLAY_DISABLE;
1689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SuperToUser(oldpile);
1699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void Mint_UnlockAudio(_THIS)
1729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	void *oldpile;
1749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Restart replay */
1769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	oldpile=(void *)Super(0);
1779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	cookie_stfa->sound_enable=STFA_PLAY_ENABLE|STFA_PLAY_REPEAT;
1789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SuperToUser(oldpile);
1799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void Mint_CloseAudio(_THIS)
1829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	void *oldpile;
1849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Stop replay */
1869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	oldpile=(void *)Super(0);
1879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	cookie_stfa->sound_enable=STFA_PLAY_DISABLE;
1889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SuperToUser(oldpile);
1899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Wait if currently playing sound */
1919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	while (SDL_MintAudio_mutex != 0) {
1929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Clear buffers */
1959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (SDL_MintAudio_audiobuf[0]) {
1969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		Mfree(SDL_MintAudio_audiobuf[0]);
1979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_MintAudio_audiobuf[0] = SDL_MintAudio_audiobuf[1] = NULL;
1989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int Mint_CheckAudio(_THIS, SDL_AudioSpec *spec)
2029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int i;
2049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	DEBUG_PRINT((DEBUG_NAME "asked: %d bits, ",spec->format & 0x00ff));
2069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	DEBUG_PRINT(("signed=%d, ", ((spec->format & 0x8000)!=0)));
2079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	DEBUG_PRINT(("big endian=%d, ", ((spec->format & 0x1000)!=0)));
2089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	DEBUG_PRINT(("channels=%d, ", spec->channels));
2099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	DEBUG_PRINT(("freq=%d\n", spec->freq));
2109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (spec->channels > 2) {
2129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        spec->channels = 2;  /* no more than stereo! */
2139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
2149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Check formats available */
2169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MINTAUDIO_freqcount=0;
2179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	for (i=0;i<16;i++) {
2189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_MintAudio_AddFrequency(this, freqs[i], 0, i, -1);
2199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if 1
2229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	for (i=0; i<MINTAUDIO_freqcount; i++) {
2239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		DEBUG_PRINT((DEBUG_NAME "freq %d: %lu Hz, clock %lu, prediv %d\n",
2249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			i, MINTAUDIO_frequencies[i].frequency, MINTAUDIO_frequencies[i].masterclock,
2259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			MINTAUDIO_frequencies[i].predivisor
2269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		));
2279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
2299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MINTAUDIO_numfreq=SDL_MintAudio_SearchFrequency(this, spec->freq);
2319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	spec->freq=MINTAUDIO_frequencies[MINTAUDIO_numfreq].frequency;
2329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	DEBUG_PRINT((DEBUG_NAME "obtained: %d bits, ",spec->format & 0x00ff));
2349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	DEBUG_PRINT(("signed=%d, ", ((spec->format & 0x8000)!=0)));
2359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	DEBUG_PRINT(("big endian=%d, ", ((spec->format & 0x1000)!=0)));
2369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	DEBUG_PRINT(("channels=%d, ", spec->channels));
2379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	DEBUG_PRINT(("freq=%d\n", spec->freq));
2389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return 0;
2409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void Mint_InitAudio(_THIS, SDL_AudioSpec *spec)
2439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	void *buffer;
2459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	void *oldpile;
2469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	buffer = SDL_MintAudio_audiobuf[SDL_MintAudio_numbuf];
2489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	oldpile=(void *)Super(0);
2509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Stop replay */
2529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	cookie_stfa->sound_enable=STFA_PLAY_DISABLE;
2539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Select replay format */
2559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	cookie_stfa->sound_control = MINTAUDIO_frequencies[MINTAUDIO_numfreq].predivisor;
2569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ((spec->format & 0xff)==8) {
2579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		cookie_stfa->sound_control |= STFA_FORMAT_8BIT;
2589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else {
2599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		cookie_stfa->sound_control |= STFA_FORMAT_16BIT;
2609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (spec->channels==2) {
2629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		cookie_stfa->sound_control |= STFA_FORMAT_STEREO;
2639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else {
2649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		cookie_stfa->sound_control |= STFA_FORMAT_MONO;
2659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ((spec->format & 0x8000)!=0) {
2679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		cookie_stfa->sound_control |= STFA_FORMAT_SIGNED;
2689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else {
2699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		cookie_stfa->sound_control |= STFA_FORMAT_UNSIGNED;
2709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ((spec->format & 0x1000)!=0) {
2729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		cookie_stfa->sound_control |= STFA_FORMAT_BIGENDIAN;
2739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else {
2749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		cookie_stfa->sound_control |= STFA_FORMAT_LITENDIAN;
2759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Set buffer */
2789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	cookie_stfa->sound_start = (unsigned long) buffer;
2799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	cookie_stfa->sound_end = (unsigned long) (buffer + spec->size);
2809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Set interrupt */
2829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	cookie_stfa->stfa_it = SDL_MintAudio_StfaInterrupt;
2839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Restart replay */
2859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	cookie_stfa->sound_enable=STFA_PLAY_ENABLE|STFA_PLAY_REPEAT;
2869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SuperToUser(oldpile);
2889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	DEBUG_PRINT((DEBUG_NAME "hardware initialized\n"));
2909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int Mint_OpenAudio(_THIS, SDL_AudioSpec *spec)
2939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_MintAudio_device = this;
2959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Check audio capabilities */
2979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (Mint_CheckAudio(this, spec)==-1) {
2989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return -1;
2999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_CalculateAudioSpec(spec);
3029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Allocate memory for audio buffers in DMA-able RAM */
3049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	DEBUG_PRINT((DEBUG_NAME "buffer size=%d\n", spec->size));
3059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_MintAudio_audiobuf[0] = Atari_SysMalloc(spec->size *2, MX_STRAM);
3079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (SDL_MintAudio_audiobuf[0]==NULL) {
3089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("MINT_OpenAudio: Not enough memory for audio buffer");
3099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return (-1);
3109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_MintAudio_audiobuf[1] = SDL_MintAudio_audiobuf[0] + spec->size ;
3129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_MintAudio_numbuf=0;
3139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_memset(SDL_MintAudio_audiobuf[0], spec->silence, spec->size *2);
3149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_MintAudio_audiosize = spec->size;
3159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_MintAudio_mutex = 0;
3169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	DEBUG_PRINT((DEBUG_NAME "buffer 0 at 0x%08x\n", SDL_MintAudio_audiobuf[0]));
3189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	DEBUG_PRINT((DEBUG_NAME "buffer 1 at 0x%08x\n", SDL_MintAudio_audiobuf[1]));
3199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_MintAudio_CheckFpu();
3219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Setup audio hardware */
3239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Mint_InitAudio(this, spec);
3249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return(1);	/* We don't use threaded audio */
3269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
327