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