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 Lesser General Public
79682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License as published by the Free Software Foundation; either
89682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    version 2.1 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    Lesser General Public License for more details.
149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    You should have received a copy of the GNU Lesser General Public
169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License along with this library; if not, write to the Free Software
179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Sam Lantinga
209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    slouken@libsdl.org
219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_config.h"
249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Output dreamcast aica */
269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_timer.h"
289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_audio.h"
299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../SDL_audiomem.h"
309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../SDL_audio_c.h"
319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../SDL_audiodev_c.h"
329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_dcaudio.h"
339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "aica.h"
359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <dc/spu.h>
369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Audio driver functions */
389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int DCAUD_OpenAudio(_THIS, SDL_AudioSpec *spec);
399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void DCAUD_WaitAudio(_THIS);
409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void DCAUD_PlayAudio(_THIS);
419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic Uint8 *DCAUD_GetAudioBuf(_THIS);
429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void DCAUD_CloseAudio(_THIS);
439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Audio driver bootstrap functions */
459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int DCAUD_Available(void)
469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return 1;
489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void DCAUD_DeleteDevice(SDL_AudioDevice *device)
519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_free(device->hidden);
539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_free(device);
549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic SDL_AudioDevice *DCAUD_CreateDevice(int devindex)
579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_AudioDevice *this;
599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Initialize all variables that we clean on shutdown */
619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this = (SDL_AudioDevice *)SDL_malloc(sizeof(SDL_AudioDevice));
629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( this ) {
639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_memset(this, 0, (sizeof *this));
649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		this->hidden = (struct SDL_PrivateAudioData *)
659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				SDL_malloc((sizeof *this->hidden));
669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( (this == NULL) || (this->hidden == NULL) ) {
689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_OutOfMemory();
699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( this ) {
709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_free(this);
719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(0);
739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_memset(this->hidden, 0, (sizeof *this->hidden));
759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Set the function pointers */
779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->OpenAudio = DCAUD_OpenAudio;
789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->WaitAudio = DCAUD_WaitAudio;
799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->PlayAudio = DCAUD_PlayAudio;
809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->GetAudioBuf = DCAUD_GetAudioBuf;
819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->CloseAudio = DCAUD_CloseAudio;
829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->free = DCAUD_DeleteDevice;
849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	spu_init();
869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return this;
889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallAudioBootStrap DCAUD_bootstrap = {
919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	"dcaudio", "Dreamcast AICA audio",
929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	DCAUD_Available, DCAUD_CreateDevice
939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall};
949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* This function waits until it is possible to write a full sound buffer */
969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void DCAUD_WaitAudio(_THIS)
979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (this->hidden->playing) {
999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* wait */
1009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		while(aica_get_pos(0)/this->spec.samples == this->hidden->nextbuf) {
1019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			thd_pass();
1029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
1039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define	SPU_RAM_BASE	0xa0800000
1079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void spu_memload_stereo8(int leftpos,int rightpos,void *src0,size_t size)
1099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	uint8 *src = src0;
1119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	uint32 *left  = (uint32*)(leftpos +SPU_RAM_BASE);
1129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	uint32 *right = (uint32*)(rightpos+SPU_RAM_BASE);
1139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	size = (size+7)/8;
1149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	while(size--) {
1159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		unsigned lval,rval;
1169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		lval = *src++;
1179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		rval = *src++;
1189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		lval|= (*src++)<<8;
1199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		rval|= (*src++)<<8;
1209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		lval|= (*src++)<<16;
1219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		rval|= (*src++)<<16;
1229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		lval|= (*src++)<<24;
1239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		rval|= (*src++)<<24;
1249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		g2_write_32(left++,lval);
1259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		g2_write_32(right++,rval);
1269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		g2_fifo_wait();
1279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void spu_memload_stereo16(int leftpos,int rightpos,void *src0,size_t size)
1319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	uint16 *src = src0;
1339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	uint32 *left  = (uint32*)(leftpos +SPU_RAM_BASE);
1349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	uint32 *right = (uint32*)(rightpos+SPU_RAM_BASE);
1359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	size = (size+7)/8;
1369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	while(size--) {
1379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		unsigned lval,rval;
1389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		lval = *src++;
1399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		rval = *src++;
1409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		lval|= (*src++)<<16;
1419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		rval|= (*src++)<<16;
1429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		g2_write_32(left++,lval);
1439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		g2_write_32(right++,rval);
1449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		g2_fifo_wait();
1459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void DCAUD_PlayAudio(_THIS)
1499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_AudioSpec *spec = &this->spec;
1519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	unsigned int offset;
1529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (this->hidden->playing) {
1549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* wait */
1559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		while(aica_get_pos(0)/spec->samples == this->hidden->nextbuf) {
1569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			thd_pass();
1579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
1589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	offset = this->hidden->nextbuf*spec->size;
1619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->hidden->nextbuf^=1;
1629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Write the audio data, checking for EAGAIN on broken audio drivers */
1639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (spec->channels==1) {
1649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		spu_memload(this->hidden->leftpos+offset,this->hidden->mixbuf,this->hidden->mixlen);
1659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else {
1669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		offset/=2;
1679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ((this->spec.format&255)==8) {
1689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			spu_memload_stereo8(this->hidden->leftpos+offset,this->hidden->rightpos+offset,this->hidden->mixbuf,this->hidden->mixlen);
1699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		} else {
1709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			spu_memload_stereo16(this->hidden->leftpos+offset,this->hidden->rightpos+offset,this->hidden->mixbuf,this->hidden->mixlen);
1719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
1729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (!this->hidden->playing) {
1759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		int mode;
1769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		this->hidden->playing = 1;
1779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		mode = (spec->format==AUDIO_S8)?SM_8BIT:SM_16BIT;
1789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if (spec->channels==1) {
1799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			aica_play(0,mode,this->hidden->leftpos,0,spec->samples*2,spec->freq,255,128,1);
1809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		} else {
1819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			aica_play(0,mode,this->hidden->leftpos ,0,spec->samples*2,spec->freq,255,0,1);
1829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			aica_play(1,mode,this->hidden->rightpos,0,spec->samples*2,spec->freq,255,255,1);
1839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
1849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic Uint8 *DCAUD_GetAudioBuf(_THIS)
1889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(this->hidden->mixbuf);
1909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void DCAUD_CloseAudio(_THIS)
1939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	aica_stop(0);
1959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (this->spec.channels==2) aica_stop(1);
1969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( this->hidden->mixbuf != NULL ) {
1979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_FreeAudioMem(this->hidden->mixbuf);
1989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		this->hidden->mixbuf = NULL;
1999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int DCAUD_OpenAudio(_THIS, SDL_AudioSpec *spec)
2039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Uint16 test_format = SDL_FirstAudioFormat(spec->format);
2059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int valid_datatype = 0;
2069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    while ((!valid_datatype) && (test_format)) {
2079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        spec->format = test_format;
2089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        switch (test_format) {
2099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            /* only formats Dreamcast accepts... */
2109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            case AUDIO_S8:
2119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            case AUDIO_S16LSB:
2129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                valid_datatype = 1;
2139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                break;
2149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            default:
2169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                test_format = SDL_NextAudioFormat();
2179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                break;
2189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
2199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
2209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (!valid_datatype) {  /* shouldn't happen, but just in case... */
2229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        SDL_SetError("Unsupported audio format");
2239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        return (-1);
2249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
2259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (spec->channels > 2)
2279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        spec->channels = 2;  /* no more than stereo on the Dreamcast. */
2289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Update the fragment size as size in bytes */
2309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_CalculateAudioSpec(spec);
2319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Allocate mixing buffer */
2339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->hidden->mixlen = spec->size;
2349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->hidden->mixbuf = (Uint8 *) SDL_AllocAudioMem(this->hidden->mixlen);
2359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( this->hidden->mixbuf == NULL ) {
2369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(-1);
2379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_memset(this->hidden->mixbuf, spec->silence, spec->size);
2399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->hidden->leftpos = 0x11000;
2409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->hidden->rightpos = 0x11000+spec->size;
2419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->hidden->playing = 0;
2429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->hidden->nextbuf = 0;
2439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* We're ready to rock and roll. :-) */
2459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(0);
2469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
247