comparison src/audio/dmedia/SDL_irixaudio.c @ 3820:1f156fd874fa SDL-ryan-multiple-audio-device

Moved more audio drivers to 1.3 API (all 5 MiNT backends, BSD, IRIX...), and some other tweaks in already-converted drivers.
author Ryan C. Gordon <icculus@icculus.org>
date Sat, 07 Oct 2006 05:36:36 +0000
parents c8b3d3d13ed1
children 66fb40445587
comparison
equal deleted inserted replaced
3819:b225d9820ee3 3820:1f156fd874fa
43 #define alSetQueueSize(x,y) ALsetqueuesize(x,y) 43 #define alSetQueueSize(x,y) ALsetqueuesize(x,y)
44 #define alSetSampFmt(x,y) ALsetsampfmt(x,y) 44 #define alSetSampFmt(x,y) ALsetsampfmt(x,y)
45 #define alSetWidth(x,y) ALsetwidth(x,y) 45 #define alSetWidth(x,y) ALsetwidth(x,y)
46 #endif 46 #endif
47 47
48 /* Audio driver functions */
49 static int AL_OpenAudio(_THIS, SDL_AudioSpec * spec);
50 static void AL_WaitAudio(_THIS);
51 static void AL_PlayAudio(_THIS);
52 static Uint8 *AL_GetAudioBuf(_THIS);
53 static void AL_CloseAudio(_THIS);
54
55 /* Audio driver bootstrap functions */
56
57 static int 48 static int
58 Audio_Available(void) 49 IRIXAUDIO_Available(void)
59 { 50 {
60 return 1; 51 return 1;
61 } 52 }
62 53
63 static void
64 Audio_DeleteDevice(SDL_AudioDevice * device)
65 {
66 SDL_free(device->hidden);
67 SDL_free(device);
68 }
69
70 static SDL_AudioDevice *
71 Audio_CreateDevice(int devindex)
72 {
73 SDL_AudioDevice *this;
74
75 /* Initialize all variables that we clean on shutdown */
76 this = (SDL_AudioDevice *) SDL_malloc(sizeof(SDL_AudioDevice));
77 if (this) {
78 SDL_memset(this, 0, (sizeof *this));
79 this->hidden = (struct SDL_PrivateAudioData *)
80 SDL_malloc((sizeof *this->hidden));
81 }
82 if ((this == NULL) || (this->hidden == NULL)) {
83 SDL_OutOfMemory();
84 if (this) {
85 SDL_free(this);
86 }
87 return (0);
88 }
89 SDL_memset(this->hidden, 0, (sizeof *this->hidden));
90
91 /* Set the function pointers */
92 this->OpenAudio = AL_OpenAudio;
93 this->WaitAudio = AL_WaitAudio;
94 this->PlayAudio = AL_PlayAudio;
95 this->GetAudioBuf = AL_GetAudioBuf;
96 this->CloseAudio = AL_CloseAudio;
97
98 this->free = Audio_DeleteDevice;
99
100 return this;
101 }
102
103 AudioBootStrap DMEDIA_bootstrap = {
104 "AL", "IRIX DMedia audio",
105 Audio_Available, Audio_CreateDevice, 0
106 };
107
108
109 void static 54 void static
110 AL_WaitAudio(_THIS) 55 IRIXAUDIO_WaitDevice(_THIS)
111 { 56 {
112 Sint32 timeleft; 57 Sint32 timeleft;
113 58
114 timeleft = this->spec.samples - alGetFillable(audio_port); 59 timeleft = this->spec.samples - alGetFillable(this->hidden->audio_port);
115 if (timeleft > 0) { 60 if (timeleft > 0) {
116 timeleft /= (this->spec.freq / 1000); 61 timeleft /= (this->spec.freq / 1000);
117 SDL_Delay((Uint32) timeleft); 62 SDL_Delay((Uint32) timeleft);
118 } 63 }
119 } 64 }
120 65
121 static void 66 static void
122 AL_PlayAudio(_THIS) 67 IRIXAUDIO_PlayDevice(_THIS)
123 { 68 {
124 /* Write the audio data out */ 69 /* Write the audio data out */
125 if (alWriteFrames(audio_port, mixbuf, this->spec.samples) < 0) { 70 ALport port = this->hidden->audio_port;
71 Uint8 *mixbuf = this->hidden->mixbuf;
72 if (alWriteFrames(port, mixbuf, this->spec.samples) < 0) {
126 /* Assume fatal error, for now */ 73 /* Assume fatal error, for now */
127 this->enabled = 0; 74 this->enabled = 0;
128 } 75 }
129 } 76 }
130 77
131 static Uint8 * 78 static Uint8 *
132 AL_GetAudioBuf(_THIS) 79 IRIXAUDIO_GetDeviceBuf(_THIS)
133 { 80 {
134 return (mixbuf); 81 return (this->hidden->mixbuf);
135 } 82 }
136 83
137 static void 84 static void
138 AL_CloseAudio(_THIS) 85 IRIXAUDIO_CloseDevice(_THIS)
139 { 86 {
140 if (mixbuf != NULL) { 87 if (this->hidden != NULL) {
141 SDL_FreeAudioMem(mixbuf); 88 if (this->hidden->mixbuf != NULL) {
142 mixbuf = NULL; 89 SDL_FreeAudioMem(this->hidden->mixbuf);
143 } 90 this->hidden->mixbuf = NULL;
144 if (audio_port != NULL) { 91 }
145 alClosePort(audio_port); 92 if (this->hidden->audio_port != NULL) {
146 audio_port = NULL; 93 alClosePort(this->hidden->audio_port);
94 this->hidden->audio_port = NULL;
95 }
96 SDL_free(this->hidden);
97 this->hidden = NULL;
147 } 98 }
148 } 99 }
149 100
150 static int 101 static int
151 AL_OpenAudio(_THIS, SDL_AudioSpec * spec) 102 IRIXAUDIO_OpenDevice(_THIS, const char *devname, int iscapture)
152 { 103 {
153 SDL_AudioFormat test_format = SDL_FirstAudioFormat(spec->format); 104 SDL_AudioFormat test_format = SDL_FirstAudioFormat(this->spec.format);
154 long width = 0; 105 long width = 0;
155 long fmt = 0; 106 long fmt = 0;
156 int valid = 0; 107 int valid = 0;
108
109 /* !!! FIXME: Handle multiple devices and capture? */
110
111 /* Initialize all variables that we clean on shutdown */
112 this->hidden = (struct SDL_PrivateAudioData *)
113 SDL_malloc((sizeof *this->hidden));
114 if (this->hidden == NULL) {
115 SDL_OutOfMemory();
116 return 0;
117 }
118 SDL_memset(this->hidden, 0, (sizeof *this->hidden));
157 119
158 #ifdef OLD_IRIX_AUDIO 120 #ifdef OLD_IRIX_AUDIO
159 { 121 {
160 long audio_param[2]; 122 long audio_param[2];
161 audio_param[0] = AL_OUTPUT_RATE; 123 audio_param[0] = AL_OUTPUT_RATE;
162 audio_param[1] = spec->freq; 124 audio_param[1] = this->spec.freq;
163 valid = (ALsetparams(AL_DEFAULT_DEVICE, audio_param, 2) < 0); 125 valid = (ALsetparams(AL_DEFAULT_DEVICE, audio_param, 2) < 0);
164 } 126 }
165 #else 127 #else
166 { 128 {
167 ALpv audio_param; 129 ALpv audio_param;
168 audio_param.param = AL_RATE; 130 audio_param.param = AL_RATE;
169 audio_param.value.i = spec->freq; 131 audio_param.value.i = this->spec.freq;
170 valid = (alSetParams(AL_DEFAULT_OUTPUT, &audio_param, 1) < 0); 132 valid = (alSetParams(AL_DEFAULT_OUTPUT, &audio_param, 1) < 0);
171 } 133 }
172 #endif 134 #endif
173 135
174 while ((!valid) && (test_format)) { 136 while ((!valid) && (test_format)) {
175 valid = 1; 137 valid = 1;
176 spec->format = test_format; 138 this->spec.format = test_format;
177 139
178 switch (test_format) { 140 switch (test_format) {
179 case AUDIO_S8: 141 case AUDIO_S8:
180 width = AL_SAMPLE_8; 142 width = AL_SAMPLE_8;
181 fmt = AL_SAMPFMT_TWOSCOMP; 143 fmt = AL_SAMPFMT_TWOSCOMP;
201 163
202 if (valid) { 164 if (valid) {
203 ALconfig audio_config = alNewConfig(); 165 ALconfig audio_config = alNewConfig();
204 valid = 0; 166 valid = 0;
205 if (audio_config) { 167 if (audio_config) {
206 if (alSetChannels(audio_config, spec->channels) < 0) { 168 if (alSetChannels(audio_config, this->spec.channels) < 0) {
207 if (spec->channels > 2) { /* can't handle > stereo? */ 169 if (this->spec.channels > 2) { /* can't handle > stereo? */
208 spec->channels = 2; /* try again below. */ 170 this->spec.channels = 2; /* try again below. */
209 } 171 }
210 } 172 }
211 173
212 if ((alSetSampFmt(audio_config, fmt) >= 0) && 174 if ((alSetSampFmt(audio_config, fmt) >= 0) &&
213 ((!width) || (alSetWidth(audio_config, width) >= 0)) && 175 ((!width) || (alSetWidth(audio_config, width) >= 0)) &&
214 (alSetQueueSize(audio_config, spec->samples * 2) >= 0) && 176 (alSetQueueSize(audio_config,this->spec.samples*2) >= 0) &&
215 (alSetChannels(audio_config, spec->channels) >= 0)) { 177 (alSetChannels(audio_config, this->spec.channels) >= 0)) {
216 178
217 audio_port = alOpenPort("SDL audio", "w", audio_config); 179 this->hidden->audio_port = alOpenPort("SDL audio", "w",
218 if (audio_port == NULL) { 180 audio_config);
181 if (this->hidden->audio_port == NULL) {
219 /* docs say AL_BAD_CHANNELS happens here, too. */ 182 /* docs say AL_BAD_CHANNELS happens here, too. */
220 int err = oserror(); 183 int err = oserror();
221 if (err == AL_BAD_CHANNELS) { 184 if (err == AL_BAD_CHANNELS) {
222 spec->channels = 2; 185 this->spec.channels = 2;
223 alSetChannels(audio_config, spec->channels); 186 alSetChannels(audio_config, this->spec.channels);
224 audio_port = alOpenPort("SDL audio", "w", 187 this->hidden->audio_port = alOpenPort("SDL audio", "w",
225 audio_config); 188 audio_config);
226 } 189 }
227 } 190 }
228 191
229 if (audio_port != NULL) { 192 if (this->hidden->audio_port != NULL) {
230 valid = 1; 193 valid = 1;
231 } 194 }
232 } 195 }
233 196
234 alFreeConfig(audio_config); 197 alFreeConfig(audio_config);
235 } 198 }
236 } 199 }
237 } 200 }
238 201
239 if (!valid) { 202 if (!valid) {
203 IRIXAUDIO_CloseDevice(this);
240 SDL_SetError("Unsupported audio format"); 204 SDL_SetError("Unsupported audio format");
241 return (-1); 205 return 0;
242 } 206 }
243 207
244 /* Update the fragment size as size in bytes */ 208 /* Update the fragment size as size in bytes */
245 SDL_CalculateAudioSpec(spec); 209 SDL_CalculateAudioSpec(&this->spec);
246 210
247 /* Allocate mixing buffer */ 211 /* Allocate mixing buffer */
248 mixbuf = (Uint8 *) SDL_AllocAudioMem(spec->size); 212 this->hidden->mixbuf = (Uint8 *) SDL_AllocAudioMem(this->spec.size);
249 if (mixbuf == NULL) { 213 if (this->hidden->mixbuf == NULL) {
214 IRIXAUDIO_CloseDevice(this);
250 SDL_OutOfMemory(); 215 SDL_OutOfMemory();
251 return (-1); 216 return 0;
252 } 217 }
253 SDL_memset(mixbuf, spec->silence, spec->size); 218 SDL_memset(this->hidden->mixbuf, this->spec.silence, this->spec.size);
254 219
255 /* We're ready to rock and roll. :-) */ 220 /* We're ready to rock and roll. :-) */
256 return (0); 221 return 1;
257 } 222 }
223
224 static int
225 IRIXAUDIO_Init(SDL_AudioDriverImpl *impl)
226 {
227 /* Set the function pointers */
228 impl->OpenDevice = DSP_OpenDevice;
229 impl->PlayDevice = DSP_PlayDevice;
230 impl->WaitDevice = DSP_WaitDevice;
231 impl->GetDeviceBuf = DSP_GetDeviceBuf;
232 impl->CloseDevice = DSP_CloseDevice;
233 impl->OnlyHasDefaultOutputDevice = 1; /* !!! FIXME: not true, I think. */
234
235 return 1;
236 }
237
238 AudioBootStrap IRIXAUDIO_bootstrap = {
239 "AL", "IRIX DMedia audio",
240 IRIXAUDIO_Available, IRIXAUDIO_Init, 0
241 };
258 242
259 /* vi: set ts=4 sw=4 expandtab: */ 243 /* vi: set ts=4 sw=4 expandtab: */