comparison src/audio/amigaos/SDL_ahiaudio.c @ 1662:782fd950bd46 SDL-1.3

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