0
|
1 /*
|
|
2 SDL - Simple DirectMedia Layer
|
|
3 Copyright (C) 1997, 1998 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 Sam Lantinga
|
|
20 5635-34 Springhouse Dr.
|
|
21 Pleasanton, CA 94588 (USA)
|
|
22 slouken@devolution.com
|
|
23 */
|
|
24
|
|
25 #ifdef SAVE_RCSID
|
|
26 static char rcsid =
|
|
27 "@(#) $Id$";
|
|
28 #endif
|
|
29
|
|
30 /* Allow access to a raw mixing buffer (For IRIX 6.5 and higher) */
|
|
31
|
|
32 #include "SDL_endian.h"
|
|
33 #include "SDL_audio.h"
|
|
34 #include "SDL_audiomem.h"
|
|
35 #include "SDL_audio_c.h"
|
|
36 #include "SDL_lowaudio.h"
|
|
37
|
|
38 /* Audio driver functions */
|
|
39 static int AHI_OpenAudio(_THIS, SDL_AudioSpec *spec);
|
|
40 static void AHI_WaitAudio(_THIS);
|
|
41 static void AHI_PlayAudio(_THIS);
|
|
42 static Uint8 *AHI_GetAudioBuf(_THIS);
|
|
43 static void AHI_CloseAudio(_THIS);
|
|
44
|
|
45 #ifndef __SASC
|
|
46 #define mymalloc(x) AllocVec(x,MEMF_PUBLIC)
|
|
47 #define myfree FreeVec
|
|
48 #else
|
|
49 #define mymalloc malloc
|
|
50 #define myfree free
|
|
51 #endif
|
|
52
|
|
53 /* Audio driver bootstrap functions */
|
|
54
|
|
55 static int Audio_Available(void)
|
|
56 {
|
|
57 #ifndef NO_AMIGADEBUG
|
|
58 D(bug("AHI available.\n"));
|
|
59 #endif
|
|
60
|
|
61 return 1;
|
|
62 }
|
|
63
|
|
64 static void Audio_DeleteDevice(SDL_AudioDevice *device)
|
|
65 {
|
|
66 free(device->hidden);
|
|
67 free(device);
|
|
68 }
|
|
69
|
|
70 static SDL_AudioDevice *Audio_CreateDevice(int devindex)
|
|
71 {
|
|
72 SDL_AudioDevice *this;
|
|
73
|
|
74 #ifndef NO_AMIGADEBUG
|
|
75 D(bug("AHI created...\n"));
|
|
76 #endif
|
|
77
|
|
78 /* Initialize all variables that we clean on shutdown */
|
|
79 this = (SDL_AudioDevice *)malloc(sizeof(SDL_AudioDevice));
|
|
80 if ( this ) {
|
|
81 memset(this, 0, (sizeof *this));
|
|
82 this->hidden = (struct SDL_PrivateAudioData *)
|
|
83 malloc((sizeof *this->hidden));
|
|
84 }
|
|
85 if ( (this == NULL) || (this->hidden == NULL) ) {
|
|
86 SDL_OutOfMemory();
|
|
87 if ( this ) {
|
|
88 free(this);
|
|
89 }
|
|
90 return(0);
|
|
91 }
|
|
92 memset(this->hidden, 0, (sizeof *this->hidden));
|
|
93
|
|
94 /* Set the function pointers */
|
|
95 this->OpenAudio = AHI_OpenAudio;
|
|
96 this->WaitAudio = AHI_WaitAudio;
|
|
97 this->PlayAudio = AHI_PlayAudio;
|
|
98 this->GetAudioBuf = AHI_GetAudioBuf;
|
|
99 this->CloseAudio = AHI_CloseAudio;
|
|
100
|
|
101 this->free = Audio_DeleteDevice;
|
|
102
|
|
103 return this;
|
|
104 }
|
|
105
|
|
106 AudioBootStrap AHI_bootstrap = {
|
|
107 "AHI", Audio_Available, Audio_CreateDevice
|
|
108 };
|
|
109
|
|
110
|
|
111 void static AHI_WaitAudio(_THIS)
|
|
112 {
|
|
113 if(!CheckIO((struct IORequest *)audio_req[current_buffer]))
|
|
114 {
|
|
115 WaitIO((struct IORequest *)audio_req[current_buffer]);
|
|
116 // AbortIO((struct IORequest *)audio_req[current_buffer]);
|
|
117 }
|
|
118 }
|
|
119
|
|
120 static void AHI_PlayAudio(_THIS)
|
|
121 {
|
|
122 if(playing>1)
|
|
123 WaitIO((struct IORequest *)audio_req[current_buffer]);
|
|
124
|
|
125 /* Write the audio data out */
|
|
126 audio_req[current_buffer] -> ahir_Std. io_Message.mn_Node.ln_Pri = 60;
|
|
127 audio_req[current_buffer] -> ahir_Std. io_Data = mixbuf[current_buffer];
|
|
128 audio_req[current_buffer] -> ahir_Std. io_Length = this->spec.samples*this->hidden->bytespersample;
|
|
129 audio_req[current_buffer] -> ahir_Std. io_Offset = 0;
|
|
130 audio_req[current_buffer] -> ahir_Std . io_Command = CMD_WRITE;
|
|
131 audio_req[current_buffer] -> ahir_Frequency = this->hidden->freq;
|
|
132 audio_req[current_buffer] -> ahir_Volume = 0x10000;
|
|
133 audio_req[current_buffer] -> ahir_Type = this->hidden->type;
|
|
134 audio_req[current_buffer] -> ahir_Position = 0x8000;
|
|
135 audio_req[current_buffer] -> ahir_Link = (playing>0 ? audio_req[current_buffer^1] : NULL);
|
|
136
|
|
137 SendIO((struct IORequest *)audio_req[current_buffer]);
|
|
138 current_buffer^=1;
|
|
139
|
|
140 playing++;
|
|
141 }
|
|
142
|
|
143 static Uint8 *AHI_GetAudioBuf(_THIS)
|
|
144 {
|
|
145 return(mixbuf[current_buffer]);
|
|
146 }
|
|
147
|
|
148 static void AHI_CloseAudio(_THIS)
|
|
149 {
|
|
150 D(bug("Closing audio...\n"));
|
|
151
|
|
152 if ( mixbuf[0] != NULL ) {
|
|
153 myfree(mixbuf[0]);
|
|
154 // SDL_FreeAudioMem(mixbuf[0]);
|
|
155 mixbuf[0] = NULL;
|
|
156 }
|
|
157
|
|
158 if ( mixbuf[1] != NULL ) {
|
|
159 myfree(mixbuf[1]);
|
|
160 // SDL_FreeAudioMem(mixbuf[1]);
|
|
161 mixbuf[1] = NULL;
|
|
162 }
|
|
163
|
|
164 playing=0;
|
|
165
|
|
166 if(audio_req[0])
|
|
167 {
|
|
168 if(audio_req[1])
|
|
169 {
|
|
170 if(!CheckIO((struct IORequest *)audio_req[1]))
|
|
171 {
|
|
172 AbortIO((struct IORequest *)audio_req[1]);
|
|
173 WaitIO((struct IORequest *)audio_req[1]);
|
|
174 }
|
|
175 myfree(audio_req[1]);
|
|
176 }
|
|
177
|
|
178 if(!CheckIO((struct IORequest *)audio_req[0]))
|
|
179 {
|
|
180 AbortIO((struct IORequest *)audio_req[0]);
|
|
181 WaitIO((struct IORequest *)audio_req[0]);
|
|
182 }
|
|
183
|
|
184 CloseDevice((struct IORequest *)audio_req[0]);
|
|
185 DeleteIORequest((struct IORequest *)audio_req[0]);
|
|
186 audio_req[0]=audio_req[1]=NULL;
|
|
187 }
|
|
188
|
|
189 if ( audio_port != NULL ) {
|
|
190 DeleteMsgPort(audio_port);
|
|
191 audio_port = NULL;
|
|
192 }
|
|
193 D(bug("...done!\n"));
|
|
194 }
|
|
195
|
|
196 static int AHI_OpenAudio(_THIS, SDL_AudioSpec *spec)
|
|
197 {
|
|
198 // int width;
|
|
199
|
|
200 D(bug("AHI opening...\n"));
|
|
201
|
|
202 /* Determine the audio parameters from the AudioSpec */
|
|
203 switch ( spec->format & 0xFF ) {
|
|
204
|
|
205 case 8: { /* Signed 8 bit audio data */
|
|
206 D(bug("Samples a 8 bit...\n"));
|
|
207 spec->format = AUDIO_S8;
|
|
208 this->hidden->bytespersample=1;
|
|
209 this->hidden->type = AHIST_M8S;
|
|
210
|
|
211 }
|
|
212 break;
|
|
213
|
|
214 case 16: { /* Signed 16 bit audio data */
|
|
215 D(bug("Samples a 16 bit...\n"));
|
|
216 spec->format = AUDIO_S16MSB;
|
|
217 this->hidden->bytespersample=2;
|
|
218 this->hidden->type = AHIST_M16S;
|
|
219 }
|
|
220 break;
|
|
221
|
|
222 default: {
|
|
223 SDL_SetError("Unsupported audio format");
|
|
224 return(-1);
|
|
225 }
|
|
226 }
|
|
227
|
|
228 D(bug("Before CalculateAudioSpec\n"));
|
|
229 /* Update the fragment size as size in bytes */
|
|
230 SDL_CalculateAudioSpec(spec);
|
|
231
|
|
232 D(bug("Before CreateMsgPort\n"));
|
|
233
|
|
234 if(!(audio_port=CreateMsgPort()))
|
|
235 {
|
|
236 SDL_SetError("Unable to create a MsgPort");
|
|
237 return -1;
|
|
238 }
|
|
239
|
|
240 D(bug("Before CreateIORequest\n"));
|
|
241
|
|
242 if(!(audio_req[0]=(struct AHIRequest *)CreateIORequest(audio_port,sizeof(struct AHIRequest))))
|
|
243 {
|
|
244 SDL_SetError("Unable to create an AHIRequest");
|
|
245 DeleteMsgPort(audio_port);
|
|
246 return -1;
|
|
247 }
|
|
248
|
|
249 audio_req[0]->ahir_Version = 4;
|
|
250
|
|
251 if(OpenDevice(AHINAME,0,(struct IORequest *)audio_req[0],NULL))
|
|
252 {
|
|
253 SDL_SetError("Unable to open AHI device!\n");
|
|
254 DeleteIORequest((struct IORequest *)audio_req[0]);
|
|
255 DeleteMsgPort(audio_port);
|
|
256 return -1;
|
|
257 }
|
|
258
|
|
259 D(bug("AFTER opendevice\n"));
|
|
260
|
|
261 /* Set output frequency */
|
|
262 this->hidden->freq = spec->freq;
|
|
263
|
|
264 D(bug("Before buffer allocation\n"));
|
|
265
|
|
266 /* Allocate mixing buffer */
|
|
267 mixbuf[0] = (Uint8 *)mymalloc(spec->size);
|
|
268 mixbuf[1] = (Uint8 *)mymalloc(spec->size);
|
|
269
|
|
270 D(bug("Before audio_req allocation\n"));
|
|
271
|
|
272 if(!(audio_req[1]=mymalloc(sizeof(struct AHIRequest))))
|
|
273 {
|
|
274 SDL_OutOfMemory();
|
|
275 return(-1);
|
|
276 }
|
|
277
|
|
278 D(bug("Before audio_req memcpy\n"));
|
|
279
|
|
280 memcpy(audio_req[1],audio_req[0],sizeof(struct AHIRequest));
|
|
281
|
|
282 if ( mixbuf[0] == NULL || mixbuf[1] == NULL ) {
|
|
283 SDL_OutOfMemory();
|
|
284 return(-1);
|
|
285 }
|
|
286
|
|
287 D(bug("Before mixbuf memset\n"));
|
|
288
|
|
289 memset(mixbuf[0], spec->silence, spec->size);
|
|
290 memset(mixbuf[1], spec->silence, spec->size);
|
|
291
|
|
292 current_buffer=0;
|
|
293 playing=0;
|
|
294
|
|
295 D(bug("AHI opened: freq:%ld mixbuf:%lx/%lx buflen:%ld bits:%ld\n",spec->freq,mixbuf[0],mixbuf[1],spec->size,this->hidden->bytespersample*8));
|
|
296
|
|
297 /* We're ready to rock and roll. :-) */
|
|
298 return(0);
|
|
299 }
|