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