23 #include "endianness.h" |
23 #include "endianness.h" |
24 |
24 |
25 #ifdef __CPLUSPLUS |
25 #ifdef __CPLUSPLUS |
26 extern "C" { |
26 extern "C" { |
27 #endif |
27 #endif |
28 |
28 |
29 /*Sources are points emitting sound*/ |
29 /*Sources are points emitting sound*/ |
30 ALuint *Sources; |
30 ALuint *Sources; |
31 /*Buffers hold sound data*/ |
31 /*Buffers hold sound data*/ |
32 ALuint *Buffers; |
32 ALuint *Buffers; |
33 /*index for Sources and Buffers*/ |
33 /*index for Sources and Buffers*/ |
34 ALuint globalindex, globalsize, increment; |
34 ALuint globalindex, globalsize, increment; |
35 |
35 |
36 ALint openalReady = AL_FALSE; |
36 ALboolean openalReady = AL_FALSE; |
37 |
37 |
38 ALint openal_close(void) { |
38 ALboolean openal_close (void) { |
39 /*Stop all sounds, deallocate all memory and close OpenAL */ |
39 /*Stop all sounds, deallocate all memory and close OpenAL */ |
40 ALCcontext *context; |
40 ALCcontext *context; |
41 ALCdevice *device; |
41 ALCdevice *device; |
42 |
42 |
43 if(openalReady == AL_FALSE) |
43 if(openalReady == AL_FALSE) { |
44 { |
44 fprintf(stderr, "ERROR: OpenAL not initialized\n"); |
45 fprintf(stderr, "ERROR: OpenAL not initialized\n"); |
45 return AL_FALSE; |
46 return AL_FALSE; |
46 } |
47 } |
47 |
|
48 alSourceStopv (globalsize, Sources); |
|
49 alDeleteSources (globalsize, Sources); |
|
50 alDeleteBuffers (globalsize, Buffers); |
|
51 |
|
52 free(Sources); |
|
53 free(Buffers); |
|
54 |
|
55 context = alcGetCurrentContext(); |
|
56 device = alcGetContextsDevice(context); |
|
57 |
|
58 alcMakeContextCurrent(NULL); |
|
59 alcDestroyContext(context); |
|
60 alcCloseDevice(device); |
|
61 |
|
62 openalReady = AL_FALSE; |
|
63 |
|
64 return AL_TRUE; |
|
65 } |
|
66 |
|
67 ALboolean openal_ready(void) { |
|
68 return openalReady; |
|
69 } |
|
70 |
|
71 ALboolean openal_init(uint32_t memorysize) { |
|
72 /*Initialize an OpenAL contex and allocate memory space for data and buffers*/ |
|
73 ALCcontext *context; |
|
74 ALCdevice *device; |
|
75 const ALCchar *default_device; |
|
76 |
|
77 /*Position of the listener*/ |
|
78 ALfloat ListenerPos[] = { 0.0, 0.0, 0.0 }; |
|
79 /*Velocity of the listener*/ |
|
80 ALfloat ListenerVel[] = { 0.0, 0.0, 0.0 }; |
|
81 /*Orientation of the listener. (first 3 elements are "at", second 3 are "up")*/ |
|
82 ALfloat ListenerOri[] = { 0.0, 0.0, -1.0, 0.0, 1.0, 0.0 }; |
|
83 |
|
84 if(openalReady == AL_TRUE) { |
|
85 fprintf(stderr, "ERROR: OpenAL already initialized\n"); |
|
86 return AL_FALSE; |
|
87 } |
|
88 |
|
89 default_device = alcGetString(NULL, ALC_DEFAULT_DEVICE_SPECIFIER); |
|
90 fprintf(stderr, "Using default device: %s\n", default_device); |
|
91 |
|
92 if ((device = alcOpenDevice(default_device)) == NULL) { |
|
93 fprintf(stderr, "ERROR: Failed to open sound device\n"); |
|
94 return AL_FALSE; |
|
95 } |
|
96 |
|
97 context = alcCreateContext(device, NULL); |
|
98 alcMakeContextCurrent(context); |
|
99 alcProcessContext(context); |
|
100 |
|
101 if (AlGetError("ERROR %d: Creating a new contex\n") != AL_TRUE) |
|
102 return AL_FALSE; |
|
103 |
|
104 /*allocate memory space for buffers and sources*/ |
|
105 globalsize = memorysize; |
|
106 increment = memorysize; |
|
107 Buffers = (ALuint*) Malloc(sizeof(ALuint)*globalsize); |
|
108 Sources = (ALuint*) Malloc(sizeof(ALuint)*globalsize); |
|
109 |
|
110 /*set the listener gain, position (on xyz axes), velocity (one value for each axe) and orientation*/ |
|
111 alListenerf (AL_GAIN, 1.0f ); |
|
112 alListenerfv(AL_POSITION, ListenerPos); |
|
113 alListenerfv(AL_VELOCITY, ListenerVel); |
|
114 alListenerfv(AL_ORIENTATION, ListenerOri); |
|
115 |
|
116 if (AlGetError("ERROR %d: Setting Listener properties\n") != AL_TRUE) |
|
117 return AL_FALSE; |
|
118 |
|
119 openalReady = AL_TRUE; |
|
120 |
|
121 alGetError(); /* clear any AL errors beforehand */ |
|
122 return AL_TRUE; |
|
123 } |
|
124 |
|
125 |
|
126 ALboolean helper_realloc (void) { |
|
127 /*expands allocated memory when loading more sound files than expected*/ |
|
128 #ifdef DEBUG |
|
129 fprintf(stderr, "OpenAL: Realloc in process %d\n", globalsize); |
|
130 #endif |
|
131 globalsize += increment; |
48 |
132 |
49 alSourceStopv (globalsize, Sources); |
133 Buffers = (ALuint*) Realloc(Buffers, sizeof(ALuint)*globalsize); |
50 alDeleteSources (globalsize, Sources); |
134 Sources = (ALuint*) Realloc(Sources, sizeof(ALuint)*globalsize); |
51 alDeleteBuffers (globalsize, Buffers); |
135 |
52 |
136 return AL_TRUE; |
53 free(Sources); |
137 } |
54 free(Buffers); |
138 |
55 |
139 |
56 context = alcGetCurrentContext(); |
140 ALint openal_loadfile (const char *filename){ |
57 device = alcGetContextsDevice(context); |
141 /*Open a file, load into memory and allocate the Source buffer for playing*/ |
58 |
142 ALfloat SourcePos[] = { 0.0, 0.0, 0.0 }; /*Position of the source sound*/ |
59 alcMakeContextCurrent(NULL); |
143 ALfloat SourceVel[] = { 0.0, 0.0, 0.0 }; /*Velocity of the source sound*/ |
60 alcDestroyContext(context); |
144 ALenum format; |
61 alcCloseDevice(device); |
145 ALsizei bitsize, freq; |
62 |
146 char *data; |
63 openalReady = AL_FALSE; |
147 uint32_t fileformat; |
64 |
148 ALenum error; |
65 return AL_TRUE; |
149 FILE *fp; |
66 } |
150 |
67 |
151 if(openalReady == AL_FALSE) { |
68 ALint openal_ready(void) { |
152 fprintf(stderr, "ERROR: OpenAL not initialized\n"); |
69 return openalReady; |
153 return AL_FALSE; |
70 } |
154 } |
71 |
155 |
72 ALint openal_init(uint32_t memorysize) { |
156 /*when the buffers are all used, we can expand memory to accept new files*/ |
73 /*Initialize an OpenAL contex and allocate memory space for data and buffers*/ |
157 if (globalindex == globalsize) |
74 ALCcontext *context; |
158 helper_realloc(); |
75 ALCdevice *device; |
159 |
76 const ALCchar *default_device; |
160 /*detect the file format, as written in the first 4 bytes of the header*/ |
77 |
161 fp = Fopen (filename, "rb"); |
78 /*Position of the listener*/ |
162 if (fp == NULL) |
79 ALfloat ListenerPos[] = { 0.0, 0.0, 0.0 }; |
163 return -1; |
80 /*Velocity of the listener*/ |
164 error = fread (&fileformat, sizeof(uint32_t), 1, fp); |
81 ALfloat ListenerVel[] = { 0.0, 0.0, 0.0 }; |
165 fclose (fp); |
82 /*Orientation of the listener. (first 3 elements are "at", second 3 are "up")*/ |
166 |
83 ALfloat ListenerOri[] = { 0.0, 0.0, -1.0, 0.0, 1.0, 0.0 }; |
167 if (error < 0) { |
84 |
168 fprintf(stderr, "ERROR: file %s is too short \n", filename); |
85 if(openalReady == AL_TRUE) |
169 return -2; |
86 { |
170 } |
87 fprintf(stderr, "ERROR: OpenAL already initialized\n"); |
171 |
88 return AL_FALSE; |
172 /*prepare the buffer to receive data*/ |
89 } |
173 alGenBuffers(1, &Buffers[globalindex]); |
90 |
174 |
91 default_device = alcGetString(NULL, ALC_DEFAULT_DEVICE_SPECIFIER); |
175 if (AlGetError("ERROR %d: Allocating memory for buffers\n") != AL_TRUE) |
92 fprintf(stderr, "Using default device: %s\n", default_device); |
176 return -3; |
93 |
177 |
94 if ((device = alcOpenDevice(default_device)) == NULL) { |
178 /*prepare the source to emit sound*/ |
95 fprintf(stderr, "ERROR: Failed to open sound device\n"); |
179 alGenSources(1, &Sources[globalindex]); |
96 return AL_FALSE; |
180 |
97 } |
181 if (AlGetError("ERROR %d: Allocating memory for sources\n") != AL_TRUE) |
98 |
182 return -4; |
99 context = alcCreateContext(device, NULL); |
183 |
100 alcMakeContextCurrent(context); |
184 |
101 alcProcessContext(context); |
185 if (fileformat == 0x5367674F) /*check if ogg*/ |
102 |
186 error = load_oggvorbis (filename, &format, &data, &bitsize, &freq); |
103 if (AlGetError("ERROR %d: Creating a new contex\n") != AL_TRUE) |
187 else { |
104 return AL_FALSE; |
188 if (fileformat == 0x46464952) /*check if wav*/ |
105 |
189 error = load_wavpcm (filename, &format, &data, &bitsize, &freq); |
106 /*allocate memory space for buffers and sources*/ |
190 else { |
107 globalsize = memorysize; |
191 fprintf(stderr, "ERROR: File format (%08X) not supported!\n", invert_endianness(fileformat)); |
108 increment = memorysize; |
192 return -5; |
109 Buffers = (ALuint*) Malloc(sizeof(ALuint)*globalsize); |
193 } |
110 Sources = (ALuint*) Malloc(sizeof(ALuint)*globalsize); |
194 } |
111 |
195 |
112 /*set the listener gain, position (on xyz axes), velocity (one value for each axe) and orientation*/ |
196 /*copy pcm data in one buffer*/ |
113 alListenerf (AL_GAIN, 1.0f ); |
197 alBufferData(Buffers[globalindex], format, data, bitsize, freq); |
114 alListenerfv(AL_POSITION, ListenerPos); |
198 free(data); /*deallocate data to save memory*/ |
115 alListenerfv(AL_VELOCITY, ListenerVel); |
199 |
116 alListenerfv(AL_ORIENTATION, ListenerOri); |
200 if (AlGetError("ERROR %d: Writing data to buffer\n") != AL_TRUE) |
117 |
201 return -6; |
118 if (AlGetError("ERROR %d: Setting Listener properties\n") != AL_TRUE) |
202 |
119 return AL_FALSE; |
203 /*set source properties that it will use when it's in playback*/ |
120 |
204 alSourcei (Sources[globalindex], AL_BUFFER, Buffers[globalindex] ); |
121 openalReady = AL_TRUE; |
205 alSourcef (Sources[globalindex], AL_PITCH, 1.0f ); |
122 |
206 alSourcef (Sources[globalindex], AL_GAIN, 1.0f ); |
123 alGetError(); /* clear any AL errors beforehand */ |
207 alSourcefv(Sources[globalindex], AL_POSITION, SourcePos ); |
124 return AL_TRUE; |
208 alSourcefv(Sources[globalindex], AL_VELOCITY, SourceVel ); |
125 } |
209 alSourcei (Sources[globalindex], AL_LOOPING, 0 ); |
126 |
210 |
127 |
211 if (AlGetError("ERROR %d: Setting source properties\n") != AL_TRUE) |
128 uint8_t helper_realloc (void) { |
212 return -7; |
129 /*expands allocated memory when loading more sound files than expected*/ |
213 |
130 globalsize += increment; |
214 alGetError(); /* clear any AL errors beforehand */ |
131 #ifdef DEBUG |
215 |
132 fprintf(stderr, "OpenAL: Realloc in process %d\n", globalsize); |
216 /*returns the index of the source you just loaded, increments it and exits*/ |
|
217 return globalindex++; |
|
218 } |
|
219 |
|
220 |
|
221 ALboolean openal_toggleloop (uint32_t index){ |
|
222 /*Set or unset looping mode*/ |
|
223 ALint loop; |
|
224 |
|
225 if(openalReady == AL_FALSE) { |
|
226 fprintf(stderr, "ERROR: OpenAL not initialized\n"); |
|
227 return AL_FALSE; |
|
228 } |
|
229 |
|
230 if (index >= globalsize) { |
|
231 fprintf(stderr, "ERROR: index out of bounds (got %d, max %d)\n", index, globalindex); |
|
232 return AL_FALSE; |
|
233 } |
|
234 |
|
235 alGetSourcei (Sources[index], AL_LOOPING, &loop); |
|
236 alSourcei (Sources[index], AL_LOOPING, !((uint8_t) loop) & 0x00000001); |
|
237 if (AlGetError("ERROR %d: Getting or setting loop property\n") != AL_TRUE) |
|
238 return AL_FALSE; |
|
239 |
|
240 alGetError(); /* clear any AL errors beforehand */ |
|
241 |
|
242 return AL_TRUE; |
|
243 } |
|
244 |
|
245 |
|
246 ALboolean openal_setvolume (uint32_t index, uint8_t percentage) { |
|
247 if(openalReady == AL_FALSE) { |
|
248 fprintf(stderr, "ERROR: OpenAL not initialized\n"); |
|
249 return AL_FALSE; |
|
250 } |
|
251 |
|
252 /*Set volume for sound number index*/ |
|
253 if (index >= globalindex) { |
|
254 fprintf(stderr, "ERROR: index out of bounds (got %d, max %d)\n", index, globalindex); |
|
255 return AL_FALSE; |
|
256 } |
|
257 |
|
258 if (percentage > 100) |
|
259 percentage = 100; |
|
260 alSourcef (Sources[index], AL_GAIN, (float) percentage/100.0f); |
|
261 if (AlGetError2("ERROR %d: setting volume for sound %d\n", index) != AL_TRUE) |
|
262 return AL_FALSE; |
|
263 |
|
264 alGetError(); /* clear any AL errors beforehand */ |
|
265 |
|
266 return AL_TRUE; |
|
267 } |
|
268 |
|
269 |
|
270 ALboolean openal_setglobalvolume (uint8_t percentage) { |
|
271 if(openalReady == AL_FALSE) { |
|
272 fprintf(stderr, "ERROR: OpenAL not initialized\n"); |
|
273 return AL_FALSE; |
|
274 } |
|
275 |
|
276 /*Set volume for all sounds*/ |
|
277 if (percentage > 100) |
|
278 percentage = 100; |
|
279 alListenerf (AL_GAIN, (float) percentage/100.0f); |
|
280 if (AlGetError("ERROR %d: Setting global volume\n") != AL_TRUE) |
|
281 return AL_FALSE; |
|
282 |
|
283 alGetError(); /* clear any AL errors beforehand */ |
|
284 |
|
285 return AL_TRUE; |
|
286 } |
|
287 |
|
288 |
|
289 ALboolean openal_togglemute () { |
|
290 /*Mute or unmute sound*/ |
|
291 ALfloat mute; |
|
292 |
|
293 if(openalReady == AL_FALSE) { |
|
294 fprintf(stderr, "ERROR: OpenAL not initialized\n"); |
|
295 return AL_FALSE; |
|
296 } |
|
297 |
|
298 alGetListenerf (AL_GAIN, &mute); |
|
299 if (mute > 0) |
|
300 mute = 0; |
|
301 else |
|
302 mute = 1.0; |
|
303 alListenerf (AL_GAIN, mute); |
|
304 if (AlGetError("ERROR %d: Setting mute property\n") != AL_TRUE) |
|
305 return AL_FALSE; |
|
306 |
|
307 alGetError(); /* clear any AL errors beforehand */ |
|
308 |
|
309 return AL_TRUE; |
|
310 } |
|
311 |
|
312 |
|
313 ALboolean openal_fade (uint32_t index, uint16_t quantity, bool direction) { |
|
314 /*Fade in or out by calling a helper thread*/ |
|
315 #ifndef _WIN32 |
|
316 pthread_t thread; |
|
317 #else |
|
318 HANDLE Thread; |
|
319 DWORD threadID; |
133 #endif |
320 #endif |
134 Buffers = (ALuint*) Realloc(Buffers, sizeof(ALuint)*globalsize); |
321 fade_t *fade; |
135 Sources = (ALuint*) Realloc(Sources, sizeof(ALuint)*globalsize); |
322 |
136 |
323 if(openalReady == AL_FALSE) { |
137 return 0; |
324 fprintf(stderr, "ERROR: OpenAL not initialized\n"); |
138 } |
325 return AL_FALSE; |
139 |
326 } |
140 |
327 |
141 int openal_loadfile (const char *filename){ |
328 fade = (fade_t*) Malloc(sizeof(fade_t)); |
142 /*Open a file, load into memory and allocate the Source buffer for playing*/ |
329 fade->index = index; |
143 ALfloat SourcePos[] = { 0.0, 0.0, 0.0 }; /*Position of the source sound*/ |
330 fade->quantity = quantity; |
144 ALfloat SourceVel[] = { 0.0, 0.0, 0.0 }; /*Velocity of the source sound*/ |
331 |
145 ALenum format; |
332 if (index >= globalindex) { |
146 ALsizei bitsize; |
333 fprintf(stderr, "ERROR: index out of bounds (got %d, max %d)\n", index, globalindex); |
147 ALsizei freq; |
334 return AL_FALSE; |
148 char *data; |
335 } |
149 uint32_t fileformat; |
336 |
150 ALenum error; |
337 if (direction == FADE_IN) |
151 FILE *fp; |
|
152 |
|
153 if(openalReady == AL_FALSE) |
|
154 { |
|
155 fprintf(stderr, "ERROR: OpenAL not initialized\n"); |
|
156 return AL_FALSE; |
|
157 } |
|
158 |
|
159 /*when the buffers are all used, we can expand memory to accept new files*/ |
|
160 if (globalindex == globalsize) |
|
161 helper_realloc(); |
|
162 |
|
163 /*detect the file format, as written in the first 4 bytes of the header*/ |
|
164 fp = Fopen (filename, "rb"); |
|
165 if (fp == NULL) |
|
166 return -1; |
|
167 error = fread (&fileformat, sizeof(uint32_t), 1, fp); |
|
168 fclose (fp); |
|
169 |
|
170 if (error < 0) { |
|
171 fprintf(stderr, "ERROR: file %s is too short \n", filename); |
|
172 return -2; |
|
173 } |
|
174 |
|
175 /*prepare the buffer to receive data*/ |
|
176 alGenBuffers(1, &Buffers[globalindex]); |
|
177 |
|
178 if (AlGetError("ERROR %d: Allocating memory for buffers\n") != AL_TRUE) |
|
179 return -3; |
|
180 |
|
181 /*prepare the source to emit sound*/ |
|
182 alGenSources(1, &Sources[globalindex]); |
|
183 |
|
184 if (AlGetError("ERROR %d: Allocating memory for sources\n") != AL_TRUE) |
|
185 return -4; |
|
186 |
|
187 |
|
188 if (fileformat == 0x5367674F) /*check if ogg*/ |
|
189 error = load_oggvorbis (filename, &format, &data, &bitsize, &freq); |
|
190 else { |
|
191 if (fileformat == 0x46464952) /*check if wav*/ |
|
192 error = load_wavpcm (filename, &format, &data, &bitsize, &freq); |
|
193 else { |
|
194 fprintf(stderr, "ERROR: File format (%08X) not supported!\n", invert_endianness(fileformat)); |
|
195 return -5; |
|
196 } |
|
197 } |
|
198 |
|
199 /*copy pcm data in one buffer*/ |
|
200 alBufferData(Buffers[globalindex], format, data, bitsize, freq); |
|
201 free(data); /*deallocate data to save memory*/ |
|
202 |
|
203 if (AlGetError("ERROR %d: Writing data to buffer\n") != AL_TRUE) |
|
204 return -6; |
|
205 |
|
206 /*set source properties that it will use when it's in playback*/ |
|
207 alSourcei (Sources[globalindex], AL_BUFFER, Buffers[globalindex] ); |
|
208 alSourcef (Sources[globalindex], AL_PITCH, 1.0f ); |
|
209 alSourcef (Sources[globalindex], AL_GAIN, 1.0f ); |
|
210 alSourcefv(Sources[globalindex], AL_POSITION, SourcePos ); |
|
211 alSourcefv(Sources[globalindex], AL_VELOCITY, SourceVel ); |
|
212 alSourcei (Sources[globalindex], AL_LOOPING, 0 ); |
|
213 |
|
214 if (AlGetError("ERROR %d: Setting source properties\n") != AL_TRUE) |
|
215 return -7; |
|
216 |
|
217 alGetError(); /* clear any AL errors beforehand */ |
|
218 |
|
219 /*returns the index of the source you just loaded, increments it and exits*/ |
|
220 return globalindex++; |
|
221 } |
|
222 |
|
223 |
|
224 ALint openal_toggleloop (uint32_t index){ |
|
225 /*Set or unset looping mode*/ |
|
226 ALint loop; |
|
227 |
|
228 if(openalReady == AL_FALSE) |
|
229 { |
|
230 fprintf(stderr, "ERROR: OpenAL not initialized\n"); |
|
231 return AL_FALSE; |
|
232 } |
|
233 |
|
234 if (index >= globalsize) { |
|
235 fprintf(stderr, "ERROR: index out of bounds (got %d, max %d)\n", index, globalindex); |
|
236 return AL_FALSE; |
|
237 } |
|
238 |
|
239 alGetSourcei (Sources[index], AL_LOOPING, &loop); |
|
240 alSourcei (Sources[index], AL_LOOPING, !((uint8_t) loop) & 0x00000001); |
|
241 if (AlGetError("ERROR %d: Getting or setting loop property\n") != AL_TRUE) |
|
242 return AL_FALSE; |
|
243 |
|
244 alGetError(); /* clear any AL errors beforehand */ |
|
245 |
|
246 return AL_TRUE; |
|
247 } |
|
248 |
|
249 |
|
250 ALint openal_setvolume (uint32_t index, uint8_t percentage) { |
|
251 if(openalReady == AL_FALSE) |
|
252 { |
|
253 fprintf(stderr, "ERROR: OpenAL not initialized\n"); |
|
254 return AL_FALSE; |
|
255 } |
|
256 |
|
257 /*Set volume for sound number index*/ |
|
258 if (index >= globalindex) { |
|
259 fprintf(stderr, "ERROR: index out of bounds (got %d, max %d)\n", index, globalindex); |
|
260 return AL_FALSE; |
|
261 } |
|
262 |
|
263 if (percentage > 100) |
|
264 percentage = 100; |
|
265 alSourcef (Sources[index], AL_GAIN, (float) percentage/100.0f); |
|
266 if (AlGetError("ERROR %d: Setting volume for last sound\n") != AL_TRUE) |
|
267 return AL_FALSE; |
|
268 |
|
269 alGetError(); /* clear any AL errors beforehand */ |
|
270 |
|
271 return AL_TRUE; |
|
272 } |
|
273 |
|
274 |
|
275 ALint openal_setglobalvolume (uint8_t percentage) { |
|
276 if(openalReady == AL_FALSE) |
|
277 { |
|
278 fprintf(stderr, "ERROR: OpenAL not initialized\n"); |
|
279 return AL_FALSE; |
|
280 } |
|
281 |
|
282 /*Set volume for all sounds*/ |
|
283 if (percentage > 100) |
|
284 percentage = 100; |
|
285 alListenerf (AL_GAIN, (float) percentage/100.0f); |
|
286 if (AlGetError("ERROR %d: Setting global volume\n") != AL_TRUE) |
|
287 return AL_FALSE; |
|
288 |
|
289 alGetError(); /* clear any AL errors beforehand */ |
|
290 |
|
291 return AL_TRUE; |
|
292 } |
|
293 |
|
294 |
|
295 ALint openal_togglemute () { |
|
296 /*Mute or unmute sound*/ |
|
297 ALfloat mute; |
|
298 |
|
299 if(openalReady == AL_FALSE) |
|
300 { |
|
301 fprintf(stderr, "ERROR: OpenAL not initialized\n"); |
|
302 return AL_FALSE; |
|
303 } |
|
304 |
|
305 alGetListenerf (AL_GAIN, &mute); |
|
306 if (mute > 0) |
|
307 mute = 0; |
|
308 else |
|
309 mute = 1.0; |
|
310 alListenerf (AL_GAIN, mute); |
|
311 if (AlGetError("ERROR %d: Setting mute property\n") != AL_TRUE) |
|
312 return AL_FALSE; |
|
313 |
|
314 alGetError(); /* clear any AL errors beforehand */ |
|
315 |
|
316 return AL_TRUE; |
|
317 } |
|
318 |
|
319 |
|
320 ALint openal_fade(uint32_t index, uint16_t quantity, uint8_t direction) { |
|
321 /*Fade in or out by calling a helper thread*/ |
|
322 #ifndef _WIN32 |
338 #ifndef _WIN32 |
323 pthread_t thread; |
339 pthread_create(&thread, NULL, helper_fadein, (void*) fade); |
324 #else |
340 #else |
325 HANDLE Thread; |
341 Thread = _beginthread(&helper_fadein, 0, (void*) fade); |
326 DWORD threadID; |
|
327 #endif |
342 #endif |
328 fade_t *fade; |
343 else { |
329 |
344 if (direction == FADE_OUT) |
330 if(openalReady == AL_FALSE) |
|
331 { |
|
332 fprintf(stderr, "ERROR: OpenAL not initialized\n"); |
|
333 return AL_FALSE; |
|
334 } |
|
335 |
|
336 fade = (fade_t*) Malloc(sizeof(fade_t)); |
|
337 fade->index = index; |
|
338 fade->quantity = quantity; |
|
339 |
|
340 if (index >= globalindex) { |
|
341 fprintf(stderr, "ERROR: index out of bounds (got %d, max %d)\n", index, globalindex); |
|
342 return AL_FALSE; |
|
343 } |
|
344 |
|
345 if (direction == FADE_IN) |
|
346 #ifndef _WIN32 |
345 #ifndef _WIN32 |
347 pthread_create(&thread, NULL, helper_fadein, (void*) fade); |
346 pthread_create(&thread, NULL, helper_fadeout, (void*) fade); |
348 #else |
347 #else |
349 Thread = _beginthread(&helper_fadein, 0, (void*) fade); |
348 Thread = _beginthread(&helper_fadeout, 0, (void*) fade); |
|
349 #endif |
|
350 else { |
|
351 fprintf(stderr, "ERROR: unknown direction for fade (%d)\n", direction); |
|
352 free(fade); |
|
353 return AL_FALSE; |
|
354 } |
|
355 } |
|
356 |
|
357 #ifndef _WIN32 |
|
358 pthread_detach(thread); |
350 #endif |
359 #endif |
351 else { |
360 |
352 if (direction == FADE_OUT) |
361 alGetError(); /* clear any AL errors beforehand */ |
353 #ifndef _WIN32 |
362 |
354 pthread_create(&thread, NULL, helper_fadeout, (void*) fade); |
363 return AL_TRUE; |
355 #else |
364 } |
356 Thread = _beginthread(&helper_fadeout, 0, (void*) fade); |
365 |
357 #endif |
366 |
358 else { |
367 ALboolean openal_fadeout (uint32_t index, uint16_t quantity) { |
359 fprintf(stderr, "ERROR: unknown direction for fade (%d)\n", direction); |
368 /*wrapper for fadeout*/ |
360 free(fade); |
369 return openal_fade(index, quantity, FADE_OUT); |
361 return AL_FALSE; |
370 } |
362 } |
371 |
363 } |
372 |
364 |
373 ALboolean openal_fadein (uint32_t index, uint16_t quantity) { |
365 #ifndef _WIN32 |
374 /*wrapper for fadein*/ |
366 pthread_detach(thread); |
375 return openal_fade(index, quantity, FADE_IN); |
367 #endif |
376 } |
368 |
377 |
369 alGetError(); /* clear any AL errors beforehand */ |
378 |
370 |
379 ALboolean openal_setposition (uint32_t index, float x, float y, float z) { |
371 return AL_TRUE; |
380 if(openalReady == AL_FALSE) { |
372 } |
381 fprintf(stderr, "ERROR: OpenAL not initialized\n"); |
373 |
382 return AL_FALSE; |
374 |
383 } |
375 ALint openal_fadeout(uint32_t index, uint16_t quantity) { |
384 if (index >= globalindex) { |
376 /*wrapper for fadeout*/ |
385 fprintf(stderr, "ERROR: index out of bounds (got %d, max %d)\n", index, globalindex); |
377 return openal_fade(index, quantity, FADE_OUT); |
386 return AL_FALSE; |
378 } |
387 } |
379 |
388 |
380 |
389 alSource3f(Sources[index], AL_POSITION, x, y, z); |
381 ALint openal_fadein(uint32_t index, uint16_t quantity) { |
390 if (AlGetError2("ERROR %d: setting position for sound %d\n", index) != AL_TRUE) |
382 /*wrapper for fadein*/ |
391 return AL_FALSE; |
383 return openal_fade(index, quantity, FADE_IN); |
392 |
384 } |
393 return AL_TRUE; |
385 |
394 } |
386 |
395 |
387 ALint openal_setposition(uint32_t index, float x, float y, float z) { |
396 |
388 if(openalReady == AL_FALSE) { |
397 ALboolean openal_playsound (uint32_t index){ |
389 fprintf(stderr, "ERROR: OpenAL not initialized\n"); |
398 if(openalReady == AL_FALSE) { |
390 return AL_FALSE; |
399 fprintf(stderr, "ERROR: OpenAL not initialized\n"); |
391 } |
400 return AL_FALSE; |
392 if (index >= globalindex) { |
401 } |
393 fprintf(stderr, "ERROR: index out of bounds (got %d, max %d)\n", index, globalindex); |
402 |
394 return AL_FALSE; |
403 /*Play sound number index*/ |
395 } |
404 if (index >= globalindex) { |
396 |
405 fprintf(stderr, "ERROR: index out of bounds (got %d, max %d)\n", index, globalindex); |
397 alSource3f(Sources[index], AL_POSITION, x, y, z); |
406 return AL_FALSE; |
398 if (AlGetError("ERROR %d: setting position for last sound\n") != AL_TRUE) |
407 } |
399 return AL_FALSE; |
408 alSourcePlay(Sources[index]); |
400 |
409 if (AlGetError2("ERROR %d: Playing sound %d\n", index) != AL_TRUE) |
401 return AL_TRUE; |
410 return AL_FALSE; |
402 } |
411 |
403 ALint openal_playsound(uint32_t index){ |
412 alGetError(); /* clear any AL errors beforehand */ |
404 if(openalReady == AL_FALSE) { |
413 |
405 fprintf(stderr, "ERROR: OpenAL not initialized\n"); |
414 return AL_TRUE; |
406 return AL_FALSE; |
415 } |
407 } |
416 |
408 |
417 |
409 /*Play sound number index*/ |
418 ALboolean openal_pausesound(uint32_t index){ |
410 if (index >= globalindex) { |
419 if(openalReady == AL_FALSE) { |
411 fprintf(stderr, "ERROR: index out of bounds (got %d, max %d)\n", index, globalindex); |
420 fprintf(stderr, "ERROR: OpenAL not initialized\n"); |
412 return AL_FALSE; |
421 return AL_FALSE; |
413 } |
422 } |
414 alSourcePlay(Sources[index]); |
423 |
415 if (AlGetError("ERROR %d: Playing last sound\n") != AL_TRUE) |
424 /*Pause sound number index*/ |
416 return AL_FALSE; |
425 if (index >= globalindex) { |
417 |
426 fprintf(stderr, "ERROR: index out of bounds (got %d, max %d)\n", index, globalindex); |
418 alGetError(); /* clear any AL errors beforehand */ |
427 return AL_FALSE; |
419 |
428 } |
420 return AL_TRUE; |
429 alSourcePause(Sources[index]); |
421 } |
430 if (AlGetError2("ERROR %d: Pausing sound %d\n", index) != AL_TRUE) |
422 |
431 return AL_FALSE; |
423 |
432 |
424 ALint openal_pausesound(uint32_t index){ |
433 return AL_TRUE; |
425 if(openalReady == AL_FALSE) |
434 } |
426 { |
435 |
427 fprintf(stderr, "ERROR: OpenAL not initialized\n"); |
436 |
428 return AL_FALSE; |
437 ALboolean openal_stopsound(uint32_t index){ |
429 } |
438 if(openalReady == AL_FALSE) { |
430 |
439 fprintf(stderr, "ERROR: OpenAL not initialized\n"); |
431 /*Pause sound number index*/ |
440 return AL_FALSE; |
432 if (index >= globalindex) { |
441 } |
433 fprintf(stderr, "ERROR: index out of bounds (got %d, max %d)\n", index, globalindex); |
442 |
434 return AL_FALSE; |
443 /*Stop sound number index*/ |
435 } |
444 if (index >= globalindex) { |
436 alSourcePause(Sources[index]); |
445 fprintf(stderr, "ERROR: index out of bounds (got %d, max %d)\n", index, globalindex); |
437 if (AlGetError("ERROR %d: Pausing last sound\n") != AL_TRUE) |
446 return AL_FALSE; |
438 return AL_FALSE; |
447 } |
439 |
448 alSourceStop(Sources[index]); |
440 return AL_TRUE; |
449 if (AlGetError2("ERROR %d: Stopping sound %d\n", index) != AL_TRUE) |
441 } |
450 return AL_FALSE; |
442 |
451 |
443 |
452 alGetError(); /* clear any AL errors beforehand */ |
444 ALint openal_stopsound(uint32_t index){ |
453 |
445 if(openalReady == AL_FALSE) |
454 return AL_TRUE; |
446 { |
455 } |
447 fprintf(stderr, "ERROR: OpenAL not initialized\n"); |
456 |
448 return AL_FALSE; |
|
449 } |
|
450 |
|
451 /*Stop sound number index*/ |
|
452 if (index >= globalindex) { |
|
453 fprintf(stderr, "ERROR: index out of bounds (got %d, max %d)\n", index, globalindex); |
|
454 return AL_FALSE; |
|
455 } |
|
456 alSourceStop(Sources[index]); |
|
457 if (AlGetError("ERROR %d: Stopping last sound\n") != AL_TRUE) |
|
458 return AL_FALSE; |
|
459 |
|
460 alGetError(); /* clear any AL errors beforehand */ |
|
461 |
|
462 return AL_TRUE; |
|
463 } |
|
464 |
|
465 #ifdef __CPLUSPLUS |
457 #ifdef __CPLUSPLUS |
466 } |
458 } |
467 #endif |
459 #endif |