comparison src/audio/dmedia/SDL_irixaudio.c @ 1895:c121d94672cb

SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
author Sam Lantinga <slouken@libsdl.org>
date Mon, 10 Jul 2006 21:04:37 +0000
parents d910939febfa
children 7590aaf89a60
comparison
equal deleted inserted replaced
1894:c69cee13dd76 1895:c121d94672cb
29 #include "../SDL_audiomem.h" 29 #include "../SDL_audiomem.h"
30 #include "../SDL_audio_c.h" 30 #include "../SDL_audio_c.h"
31 #include "SDL_irixaudio.h" 31 #include "SDL_irixaudio.h"
32 32
33 33
34 #ifndef AL_RESOURCE /* as a test whether we use the old IRIX audio libraries */ 34 #ifndef AL_RESOURCE /* as a test whether we use the old IRIX audio libraries */
35 #define OLD_IRIX_AUDIO 35 #define OLD_IRIX_AUDIO
36 #define alClosePort(x) ALcloseport(x) 36 #define alClosePort(x) ALcloseport(x)
37 #define alFreeConfig(x) ALfreeconfig(x) 37 #define alFreeConfig(x) ALfreeconfig(x)
38 #define alGetFillable(x) ALgetfillable(x) 38 #define alGetFillable(x) ALgetfillable(x)
39 #define alNewConfig() ALnewconfig() 39 #define alNewConfig() ALnewconfig()
43 #define alSetSampFmt(x,y) ALsetsampfmt(x,y) 43 #define alSetSampFmt(x,y) ALsetsampfmt(x,y)
44 #define alSetWidth(x,y) ALsetwidth(x,y) 44 #define alSetWidth(x,y) ALsetwidth(x,y)
45 #endif 45 #endif
46 46
47 /* Audio driver functions */ 47 /* Audio driver functions */
48 static int AL_OpenAudio(_THIS, SDL_AudioSpec *spec); 48 static int AL_OpenAudio(_THIS, SDL_AudioSpec * spec);
49 static void AL_WaitAudio(_THIS); 49 static void AL_WaitAudio(_THIS);
50 static void AL_PlayAudio(_THIS); 50 static void AL_PlayAudio(_THIS);
51 static Uint8 *AL_GetAudioBuf(_THIS); 51 static Uint8 *AL_GetAudioBuf(_THIS);
52 static void AL_CloseAudio(_THIS); 52 static void AL_CloseAudio(_THIS);
53 53
54 /* Audio driver bootstrap functions */ 54 /* Audio driver bootstrap functions */
55 55
56 static int Audio_Available(void) 56 static int
57 { 57 Audio_Available(void)
58 return 1; 58 {
59 } 59 return 1;
60 60 }
61 static void Audio_DeleteDevice(SDL_AudioDevice *device) 61
62 { 62 static void
63 SDL_free(device->hidden); 63 Audio_DeleteDevice(SDL_AudioDevice * device)
64 SDL_free(device); 64 {
65 } 65 SDL_free(device->hidden);
66 66 SDL_free(device);
67 static SDL_AudioDevice *Audio_CreateDevice(int devindex) 67 }
68 { 68
69 SDL_AudioDevice *this; 69 static SDL_AudioDevice *
70 70 Audio_CreateDevice(int devindex)
71 /* Initialize all variables that we clean on shutdown */ 71 {
72 this = (SDL_AudioDevice *)SDL_malloc(sizeof(SDL_AudioDevice)); 72 SDL_AudioDevice *this;
73 if ( this ) { 73
74 SDL_memset(this, 0, (sizeof *this)); 74 /* Initialize all variables that we clean on shutdown */
75 this->hidden = (struct SDL_PrivateAudioData *) 75 this = (SDL_AudioDevice *) SDL_malloc(sizeof(SDL_AudioDevice));
76 SDL_malloc((sizeof *this->hidden)); 76 if (this) {
77 } 77 SDL_memset(this, 0, (sizeof *this));
78 if ( (this == NULL) || (this->hidden == NULL) ) { 78 this->hidden = (struct SDL_PrivateAudioData *)
79 SDL_OutOfMemory(); 79 SDL_malloc((sizeof *this->hidden));
80 if ( this ) { 80 }
81 SDL_free(this); 81 if ((this == NULL) || (this->hidden == NULL)) {
82 } 82 SDL_OutOfMemory();
83 return(0); 83 if (this) {
84 } 84 SDL_free(this);
85 SDL_memset(this->hidden, 0, (sizeof *this->hidden)); 85 }
86 86 return (0);
87 /* Set the function pointers */ 87 }
88 this->OpenAudio = AL_OpenAudio; 88 SDL_memset(this->hidden, 0, (sizeof *this->hidden));
89 this->WaitAudio = AL_WaitAudio; 89
90 this->PlayAudio = AL_PlayAudio; 90 /* Set the function pointers */
91 this->GetAudioBuf = AL_GetAudioBuf; 91 this->OpenAudio = AL_OpenAudio;
92 this->CloseAudio = AL_CloseAudio; 92 this->WaitAudio = AL_WaitAudio;
93 93 this->PlayAudio = AL_PlayAudio;
94 this->free = Audio_DeleteDevice; 94 this->GetAudioBuf = AL_GetAudioBuf;
95 95 this->CloseAudio = AL_CloseAudio;
96 return this; 96
97 this->free = Audio_DeleteDevice;
98
99 return this;
97 } 100 }
98 101
99 AudioBootStrap DMEDIA_bootstrap = { 102 AudioBootStrap DMEDIA_bootstrap = {
100 "AL", "IRIX DMedia audio", 103 "AL", "IRIX DMedia audio",
101 Audio_Available, Audio_CreateDevice 104 Audio_Available, Audio_CreateDevice
102 }; 105 };
103 106
104 107
105 void static AL_WaitAudio(_THIS) 108 void static
106 { 109 AL_WaitAudio(_THIS)
107 Sint32 timeleft; 110 {
108 111 Sint32 timeleft;
109 timeleft = this->spec.samples - alGetFillable(audio_port); 112
110 if ( timeleft > 0 ) { 113 timeleft = this->spec.samples - alGetFillable(audio_port);
111 timeleft /= (this->spec.freq/1000); 114 if (timeleft > 0) {
112 SDL_Delay((Uint32)timeleft); 115 timeleft /= (this->spec.freq / 1000);
113 } 116 SDL_Delay((Uint32) timeleft);
114 } 117 }
115 118 }
116 static void AL_PlayAudio(_THIS) 119
117 { 120 static void
118 /* Write the audio data out */ 121 AL_PlayAudio(_THIS)
119 if ( alWriteFrames(audio_port, mixbuf, this->spec.samples) < 0 ) { 122 {
120 /* Assume fatal error, for now */ 123 /* Write the audio data out */
121 this->enabled = 0; 124 if (alWriteFrames(audio_port, mixbuf, this->spec.samples) < 0) {
122 } 125 /* Assume fatal error, for now */
123 } 126 this->enabled = 0;
124 127 }
125 static Uint8 *AL_GetAudioBuf(_THIS) 128 }
126 { 129
127 return(mixbuf); 130 static Uint8 *
128 } 131 AL_GetAudioBuf(_THIS)
129 132 {
130 static void AL_CloseAudio(_THIS) 133 return (mixbuf);
131 { 134 }
132 if ( mixbuf != NULL ) { 135
133 SDL_FreeAudioMem(mixbuf); 136 static void
134 mixbuf = NULL; 137 AL_CloseAudio(_THIS)
135 } 138 {
136 if ( audio_port != NULL ) { 139 if (mixbuf != NULL) {
137 alClosePort(audio_port); 140 SDL_FreeAudioMem(mixbuf);
138 audio_port = NULL; 141 mixbuf = NULL;
139 } 142 }
140 } 143 if (audio_port != NULL) {
141 144 alClosePort(audio_port);
142 static int AL_OpenAudio(_THIS, SDL_AudioSpec *spec) 145 audio_port = NULL;
143 { 146 }
144 ALconfig audio_config; 147 }
148
149 static int
150 AL_OpenAudio(_THIS, SDL_AudioSpec * spec)
151 {
152 ALconfig audio_config;
145 #ifdef OLD_IRIX_AUDIO 153 #ifdef OLD_IRIX_AUDIO
146 long audio_param[2]; 154 long audio_param[2];
147 #else 155 #else
148 ALpv audio_param; 156 ALpv audio_param;
149 #endif 157 #endif
150 int width; 158 int width;
151 159
152 /* Determine the audio parameters from the AudioSpec */ 160 /* Determine the audio parameters from the AudioSpec */
153 switch ( spec->format & 0xFF ) { 161 switch (spec->format & 0xFF) {
154 162
155 case 8: { /* Signed 8 bit audio data */ 163 case 8:
156 spec->format = AUDIO_S8; 164 { /* Signed 8 bit audio data */
157 width = AL_SAMPLE_8; 165 spec->format = AUDIO_S8;
158 } 166 width = AL_SAMPLE_8;
159 break; 167 }
160 168 break;
161 case 16: { /* Signed 16 bit audio data */ 169
162 spec->format = AUDIO_S16MSB; 170 case 16:
163 width = AL_SAMPLE_16; 171 { /* Signed 16 bit audio data */
164 } 172 spec->format = AUDIO_S16MSB;
165 break; 173 width = AL_SAMPLE_16;
166 174 }
167 default: { 175 break;
168 SDL_SetError("Unsupported audio format"); 176
169 return(-1); 177 default:
170 } 178 {
171 } 179 SDL_SetError("Unsupported audio format");
172 180 return (-1);
173 /* Update the fragment size as size in bytes */ 181 }
174 SDL_CalculateAudioSpec(spec); 182 }
175 183
176 /* Set output frequency */ 184 /* Update the fragment size as size in bytes */
185 SDL_CalculateAudioSpec(spec);
186
187 /* Set output frequency */
177 #ifdef OLD_IRIX_AUDIO 188 #ifdef OLD_IRIX_AUDIO
178 audio_param[0] = AL_OUTPUT_RATE; 189 audio_param[0] = AL_OUTPUT_RATE;
179 audio_param[1] = spec->freq; 190 audio_param[1] = spec->freq;
180 if( ALsetparams(AL_DEFAULT_DEVICE, audio_param, 2) < 0 ) { 191 if (ALsetparams(AL_DEFAULT_DEVICE, audio_param, 2) < 0) {
181 #else 192 #else
182 audio_param.param = AL_RATE; 193 audio_param.param = AL_RATE;
183 audio_param.value.i = spec->freq; 194 audio_param.value.i = spec->freq;
184 if( alSetParams(AL_DEFAULT_OUTPUT, &audio_param, 1) < 0 ) { 195 if (alSetParams(AL_DEFAULT_OUTPUT, &audio_param, 1) < 0) {
185 #endif 196 #endif
186 SDL_SetError("alSetParams failed"); 197 SDL_SetError("alSetParams failed");
187 return(-1); 198 return (-1);
188 } 199 }
189 200
190 /* Open the audio port with the requested frequency */ 201 /* Open the audio port with the requested frequency */
191 audio_port = NULL; 202 audio_port = NULL;
192 audio_config = alNewConfig(); 203 audio_config = alNewConfig();
193 if ( audio_config && 204 if (audio_config &&
194 (alSetSampFmt(audio_config, AL_SAMPFMT_TWOSCOMP) >= 0) && 205 (alSetSampFmt(audio_config, AL_SAMPFMT_TWOSCOMP) >= 0) &&
195 (alSetWidth(audio_config, width) >= 0) && 206 (alSetWidth(audio_config, width) >= 0) &&
196 (alSetQueueSize(audio_config, spec->samples*2) >= 0) && 207 (alSetQueueSize(audio_config, spec->samples * 2) >= 0) &&
197 (alSetChannels(audio_config, spec->channels) >= 0) ) { 208 (alSetChannels(audio_config, spec->channels) >= 0)) {
198 audio_port = alOpenPort("SDL audio", "w", audio_config); 209 audio_port = alOpenPort("SDL audio", "w", audio_config);
199 } 210 }
200 alFreeConfig(audio_config); 211 alFreeConfig(audio_config);
201 if( audio_port == NULL ) { 212 if (audio_port == NULL) {
202 SDL_SetError("Unable to open audio port"); 213 SDL_SetError("Unable to open audio port");
203 return(-1); 214 return (-1);
204 } 215 }
205 216
206 /* Allocate mixing buffer */ 217 /* Allocate mixing buffer */
207 mixbuf = (Uint8 *)SDL_AllocAudioMem(spec->size); 218 mixbuf = (Uint8 *) SDL_AllocAudioMem(spec->size);
208 if ( mixbuf == NULL ) { 219 if (mixbuf == NULL) {
209 SDL_OutOfMemory(); 220 SDL_OutOfMemory();
210 return(-1); 221 return (-1);
211 } 222 }
212 SDL_memset(mixbuf, spec->silence, spec->size); 223 SDL_memset(mixbuf, spec->silence, spec->size);
213 224
214 /* We're ready to rock and roll. :-) */ 225 /* We're ready to rock and roll. :-) */
215 return(0); 226 return (0);
216 } 227 }
228
229 /* vi: set ts=4 sw=4 expandtab: */