comparison src/audio/dc/SDL_dcaudio.c @ 509:dad72daf44b3

Added initial support for Dreamcast (thanks HERO!)
author Sam Lantinga <slouken@libsdl.org>
date Sat, 05 Oct 2002 16:50:56 +0000
parents
children b8d311d90021
comparison
equal deleted inserted replaced
508:9ff7e90aaa94 509:dad72daf44b3
1 /*
2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002 Sam Lantinga
4
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.
9
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.
14
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
18
19 BERO <bero@geocities.co.jp>
20 based on SDL_diskaudio.c by Sam Lantinga <slouken@libsdl.org>
21
22 */
23
24 #ifdef SAVE_RCSID
25 static char rcsid =
26 "@(#) $Id$";
27 #endif
28
29 /* Output dreamcast aica */
30
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include <errno.h>
35 #include <unistd.h>
36 #include <sys/stat.h>
37 #include <sys/types.h>
38 #include <sys/stat.h>
39 #include <fcntl.h>
40
41
42 #include "SDL_audio.h"
43 #include "SDL_error.h"
44 #include "SDL_audiomem.h"
45 #include "SDL_audio_c.h"
46 #include "SDL_timer.h"
47 #include "SDL_audiodev_c.h"
48 #include "SDL_dcaudio.h"
49
50 #include "aica.h"
51 #include <dc/spu.h>
52
53 /* Audio driver functions */
54 static int DCAUD_OpenAudio(_THIS, SDL_AudioSpec *spec);
55 static void DCAUD_WaitAudio(_THIS);
56 static void DCAUD_PlayAudio(_THIS);
57 static Uint8 *DCAUD_GetAudioBuf(_THIS);
58 static void DCAUD_CloseAudio(_THIS);
59
60 /* Audio driver bootstrap functions */
61 static int DCAUD_Available(void)
62 {
63 return 1;
64 }
65
66 static void DCAUD_DeleteDevice(SDL_AudioDevice *device)
67 {
68 free(device->hidden);
69 free(device);
70 }
71
72 static SDL_AudioDevice *DCAUD_CreateDevice(int devindex)
73 {
74 SDL_AudioDevice *this;
75
76 /* Initialize all variables that we clean on shutdown */
77 this = (SDL_AudioDevice *)malloc(sizeof(SDL_AudioDevice));
78 if ( this ) {
79 memset(this, 0, (sizeof *this));
80 this->hidden = (struct SDL_PrivateAudioData *)
81 malloc((sizeof *this->hidden));
82 }
83 if ( (this == NULL) || (this->hidden == NULL) ) {
84 SDL_OutOfMemory();
85 if ( this ) {
86 free(this);
87 }
88 return(0);
89 }
90 memset(this->hidden, 0, (sizeof *this->hidden));
91
92 /* Set the function pointers */
93 this->OpenAudio = DCAUD_OpenAudio;
94 this->WaitAudio = DCAUD_WaitAudio;
95 this->PlayAudio = DCAUD_PlayAudio;
96 this->GetAudioBuf = DCAUD_GetAudioBuf;
97 this->CloseAudio = DCAUD_CloseAudio;
98
99 this->free = DCAUD_DeleteDevice;
100
101 spu_init();
102
103 return this;
104 }
105
106 AudioBootStrap DCAUD_bootstrap = {
107 "dcaudio", "Dreamcast AICA audio",
108 DCAUD_Available, DCAUD_CreateDevice
109 };
110
111 /* This function waits until it is possible to write a full sound buffer */
112 static void DCAUD_WaitAudio(_THIS)
113 {
114 if (this->hidden->playing) {
115 /* wait */
116 while(aica_get_pos(0)/this->spec.samples == this->hidden->nextbuf) {
117 thd_pass();
118 }
119 }
120 }
121
122 #define SPU_RAM_BASE 0xa0800000
123
124 static void spu_memload_stereo8(int leftpos,int rightpos,void *src0,size_t size)
125 {
126 uint8 *src = src0;
127 uint32 *left = (uint32*)(leftpos +SPU_RAM_BASE);
128 uint32 *right = (uint32*)(rightpos+SPU_RAM_BASE);
129 size = (size+7)/8;
130 while(size--) {
131 unsigned lval,rval;
132 lval = *src++;
133 rval = *src++;
134 lval|= (*src++)<<8;
135 rval|= (*src++)<<8;
136 lval|= (*src++)<<16;
137 rval|= (*src++)<<16;
138 lval|= (*src++)<<24;
139 rval|= (*src++)<<24;
140 g2_write_32(left++,lval);
141 g2_write_32(right++,rval);
142 g2_fifo_wait();
143 }
144 }
145
146 static void spu_memload_stereo16(int leftpos,int rightpos,void *src0,size_t size)
147 {
148 uint16 *src = src0;
149 uint32 *left = (uint32*)(leftpos +SPU_RAM_BASE);
150 uint32 *right = (uint32*)(rightpos+SPU_RAM_BASE);
151 size = (size+7)/8;
152 while(size--) {
153 unsigned lval,rval;
154 lval = *src++;
155 rval = *src++;
156 lval|= (*src++)<<16;
157 rval|= (*src++)<<16;
158 g2_write_32(left++,lval);
159 g2_write_32(right++,rval);
160 g2_fifo_wait();
161 }
162 }
163
164 static void DCAUD_PlayAudio(_THIS)
165 {
166 SDL_AudioSpec *spec = &this->spec;
167 unsigned int offset;
168
169 if (this->hidden->playing) {
170 /* wait */
171 while(aica_get_pos(0)/spec->samples == this->hidden->nextbuf) {
172 thd_pass();
173 }
174 }
175
176 offset = this->hidden->nextbuf*spec->size;
177 this->hidden->nextbuf^=1;
178 /* Write the audio data, checking for EAGAIN on broken audio drivers */
179 if (spec->channels==1) {
180 spu_memload(this->hidden->leftpos+offset,this->hidden->mixbuf,this->hidden->mixlen);
181 } else {
182 offset/=2;
183 if ((this->spec.format&255)==8) {
184 spu_memload_stereo8(this->hidden->leftpos+offset,this->hidden->rightpos+offset,this->hidden->mixbuf,this->hidden->mixlen);
185 } else {
186 spu_memload_stereo16(this->hidden->leftpos+offset,this->hidden->rightpos+offset,this->hidden->mixbuf,this->hidden->mixlen);
187 }
188 }
189
190 if (!this->hidden->playing) {
191 int mode;
192 this->hidden->playing = 1;
193 mode = (spec->format==AUDIO_S8)?SM_8BIT:SM_16BIT;
194 if (spec->channels==1) {
195 aica_play(0,mode,this->hidden->leftpos,0,spec->samples*2,spec->freq,255,128,1);
196 } else {
197 aica_play(0,mode,this->hidden->leftpos ,0,spec->samples*2,spec->freq,255,0,1);
198 aica_play(1,mode,this->hidden->rightpos,0,spec->samples*2,spec->freq,255,255,1);
199 }
200 }
201 }
202
203 static Uint8 *DCAUD_GetAudioBuf(_THIS)
204 {
205 return(this->hidden->mixbuf);
206 }
207
208 static void DCAUD_CloseAudio(_THIS)
209 {
210 aica_stop(0);
211 if (this->spec.channels==2) aica_stop(1);
212 if ( this->hidden->mixbuf != NULL ) {
213 SDL_FreeAudioMem(this->hidden->mixbuf);
214 this->hidden->mixbuf = NULL;
215 }
216 }
217
218 static int DCAUD_OpenAudio(_THIS, SDL_AudioSpec *spec)
219 {
220 switch(spec->format&0xff) {
221 case 8: spec->format = AUDIO_S8; break;
222 case 16: spec->format = AUDIO_S16LSB; break;
223 default:
224 SDL_SetError("Unsupported audio format");
225 return(-1);
226 }
227
228 /* Update the fragment size as size in bytes */
229 SDL_CalculateAudioSpec(spec);
230
231 /* Allocate mixing buffer */
232 this->hidden->mixlen = spec->size;
233 this->hidden->mixbuf = (Uint8 *) SDL_AllocAudioMem(this->hidden->mixlen);
234 if ( this->hidden->mixbuf == NULL ) {
235 return(-1);
236 }
237 memset(this->hidden->mixbuf, spec->silence, spec->size);
238 this->hidden->leftpos = 0x11000;
239 this->hidden->rightpos = 0x11000+spec->size;
240 this->hidden->playing = 0;
241 this->hidden->nextbuf = 0;
242
243 /* We're ready to rock and roll. :-) */
244 return(0);
245 }