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 Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 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 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 #include "SDL_config.h"
25 /* Output dreamcast aica */
27 #include "SDL_timer.h"
28 #include "SDL_audio.h"
29 #include "../SDL_audiomem.h"
30 #include "../SDL_audio_c.h"
31 #include "../SDL_audiodev_c.h"
32 #include "SDL_dcaudio.h"
37 /* Audio driver functions */
38 static int DCAUD_OpenAudio(_THIS, SDL_AudioSpec *spec);
39 static void DCAUD_WaitAudio(_THIS);
40 static void DCAUD_PlayAudio(_THIS);
41 static Uint8 *DCAUD_GetAudioBuf(_THIS);
42 static void DCAUD_CloseAudio(_THIS);
44 /* Audio driver bootstrap functions */
45 static int DCAUD_Available(void)
50 static void DCAUD_DeleteDevice(SDL_AudioDevice *device)
52 SDL_free(device->hidden);
56 static SDL_AudioDevice *DCAUD_CreateDevice(int devindex)
58 SDL_AudioDevice *this;
60 /* Initialize all variables that we clean on shutdown */
61 this = (SDL_AudioDevice *)SDL_malloc(sizeof(SDL_AudioDevice));
63 SDL_memset(this, 0, (sizeof *this));
64 this->hidden = (struct SDL_PrivateAudioData *)
65 SDL_malloc((sizeof *this->hidden));
67 if ( (this == NULL) || (this->hidden == NULL) ) {
74 SDL_memset(this->hidden, 0, (sizeof *this->hidden));
76 /* Set the function pointers */
77 this->OpenAudio = DCAUD_OpenAudio;
78 this->WaitAudio = DCAUD_WaitAudio;
79 this->PlayAudio = DCAUD_PlayAudio;
80 this->GetAudioBuf = DCAUD_GetAudioBuf;
81 this->CloseAudio = DCAUD_CloseAudio;
83 this->free = DCAUD_DeleteDevice;
90 AudioBootStrap DCAUD_bootstrap = {
91 "dcaudio", "Dreamcast AICA audio",
92 DCAUD_Available, DCAUD_CreateDevice
95 /* This function waits until it is possible to write a full sound buffer */
96 static void DCAUD_WaitAudio(_THIS)
98 if (this->hidden->playing) {
100 while(aica_get_pos(0)/this->spec.samples == this->hidden->nextbuf) {
106 #define SPU_RAM_BASE 0xa0800000
108 static void spu_memload_stereo8(int leftpos,int rightpos,void *src0,size_t size)
111 uint32 *left = (uint32*)(leftpos +SPU_RAM_BASE);
112 uint32 *right = (uint32*)(rightpos+SPU_RAM_BASE);
124 g2_write_32(left++,lval);
125 g2_write_32(right++,rval);
130 static void spu_memload_stereo16(int leftpos,int rightpos,void *src0,size_t size)
133 uint32 *left = (uint32*)(leftpos +SPU_RAM_BASE);
134 uint32 *right = (uint32*)(rightpos+SPU_RAM_BASE);
142 g2_write_32(left++,lval);
143 g2_write_32(right++,rval);
148 static void DCAUD_PlayAudio(_THIS)
150 SDL_AudioSpec *spec = &this->spec;
153 if (this->hidden->playing) {
155 while(aica_get_pos(0)/spec->samples == this->hidden->nextbuf) {
160 offset = this->hidden->nextbuf*spec->size;
161 this->hidden->nextbuf^=1;
162 /* Write the audio data, checking for EAGAIN on broken audio drivers */
163 if (spec->channels==1) {
164 spu_memload(this->hidden->leftpos+offset,this->hidden->mixbuf,this->hidden->mixlen);
167 if ((this->spec.format&255)==8) {
168 spu_memload_stereo8(this->hidden->leftpos+offset,this->hidden->rightpos+offset,this->hidden->mixbuf,this->hidden->mixlen);
170 spu_memload_stereo16(this->hidden->leftpos+offset,this->hidden->rightpos+offset,this->hidden->mixbuf,this->hidden->mixlen);
174 if (!this->hidden->playing) {
176 this->hidden->playing = 1;
177 mode = (spec->format==AUDIO_S8)?SM_8BIT:SM_16BIT;
178 if (spec->channels==1) {
179 aica_play(0,mode,this->hidden->leftpos,0,spec->samples*2,spec->freq,255,128,1);
181 aica_play(0,mode,this->hidden->leftpos ,0,spec->samples*2,spec->freq,255,0,1);
182 aica_play(1,mode,this->hidden->rightpos,0,spec->samples*2,spec->freq,255,255,1);
187 static Uint8 *DCAUD_GetAudioBuf(_THIS)
189 return(this->hidden->mixbuf);
192 static void DCAUD_CloseAudio(_THIS)
195 if (this->spec.channels==2) aica_stop(1);
196 if ( this->hidden->mixbuf != NULL ) {
197 SDL_FreeAudioMem(this->hidden->mixbuf);
198 this->hidden->mixbuf = NULL;
202 static int DCAUD_OpenAudio(_THIS, SDL_AudioSpec *spec)
204 Uint16 test_format = SDL_FirstAudioFormat(spec->format);
205 int valid_datatype = 0;
206 while ((!valid_datatype) && (test_format)) {
207 spec->format = test_format;
208 switch (test_format) {
209 /* only formats Dreamcast accepts... */
216 test_format = SDL_NextAudioFormat();
221 if (!valid_datatype) { /* shouldn't happen, but just in case... */
222 SDL_SetError("Unsupported audio format");
226 if (spec->channels > 2)
227 spec->channels = 2; /* no more than stereo on the Dreamcast. */
229 /* Update the fragment size as size in bytes */
230 SDL_CalculateAudioSpec(spec);
232 /* Allocate mixing buffer */
233 this->hidden->mixlen = spec->size;
234 this->hidden->mixbuf = (Uint8 *) SDL_AllocAudioMem(this->hidden->mixlen);
235 if ( this->hidden->mixbuf == NULL ) {
238 SDL_memset(this->hidden->mixbuf, spec->silence, spec->size);
239 this->hidden->leftpos = 0x11000;
240 this->hidden->rightpos = 0x11000+spec->size;
241 this->hidden->playing = 0;
242 this->hidden->nextbuf = 0;
244 /* We're ready to rock and roll. :-) */