author | koda |
Sun, 12 Jul 2009 13:51:29 +0000 | |
changeset 2257 | 7eb31efcfb9b |
parent 2220 | 110266ba2ef7 |
child 2259 | ca42efdce3ce |
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 |
|
2257
7eb31efcfb9b
updates licence and fix a memory leak (which was consuming iphone memory)
koda
parents:
2220
diff
changeset
|
6 |
* it under the terms of the GNU Lesser General Public License as published by |
2191 | 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 |
|
2257
7eb31efcfb9b
updates licence and fix a memory leak (which was consuming iphone memory)
koda
parents:
2220
diff
changeset
|
12 |
* GNU Lesser General Public License for more details. |
2191 | 13 |
* |
2257
7eb31efcfb9b
updates licence and fix a memory leak (which was consuming iphone memory)
koda
parents:
2220
diff
changeset
|
14 |
* You should have received a copy of the GNU Lesser General Public License |
2191 | 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; |
2191 | 35 |
|
2216
82e7da49c26a
-Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents:
2215
diff
changeset
|
36 |
ALint openalReady = AL_FALSE; |
2191 | 37 |
|
38 |
ALint openal_close(void) { |
|
2213 | 39 |
/*Stop all sounds, deallocate all memory and close OpenAL */ |
2191 | 40 |
ALCcontext *context; |
41 |
ALCdevice *device; |
|
42 |
||
2216
82e7da49c26a
-Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents:
2215
diff
changeset
|
43 |
if(openalReady == AL_FALSE) |
82e7da49c26a
-Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents:
2215
diff
changeset
|
44 |
{ |
82e7da49c26a
-Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents:
2215
diff
changeset
|
45 |
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
|
46 |
return AL_FALSE; |
82e7da49c26a
-Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents:
2215
diff
changeset
|
47 |
} |
82e7da49c26a
-Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents:
2215
diff
changeset
|
48 |
|
2191 | 49 |
alSourceStopv (globalsize, Sources); |
50 |
alDeleteSources (globalsize, Sources); |
|
51 |
alDeleteBuffers (globalsize, Buffers); |
|
52 |
||
53 |
free(Sources); |
|
54 |
free(Buffers); |
|
55 |
||
56 |
context = alcGetCurrentContext(); |
|
57 |
device = alcGetContextsDevice(context); |
|
58 |
||
59 |
alcMakeContextCurrent(NULL); |
|
60 |
alcDestroyContext(context); |
|
61 |
alcCloseDevice(device); |
|
2216
82e7da49c26a
-Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents:
2215
diff
changeset
|
62 |
|
82e7da49c26a
-Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents:
2215
diff
changeset
|
63 |
openalReady = AL_FALSE; |
82e7da49c26a
-Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents:
2215
diff
changeset
|
64 |
|
2191 | 65 |
return AL_TRUE; |
66 |
} |
|
67 |
||
2216
82e7da49c26a
-Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents:
2215
diff
changeset
|
68 |
ALint openal_ready(void) { |
82e7da49c26a
-Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents:
2215
diff
changeset
|
69 |
return openalReady; |
82e7da49c26a
-Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents:
2215
diff
changeset
|
70 |
} |
2191 | 71 |
|
2215 | 72 |
ALint openal_init(uint32_t memorysize) { |
2213 | 73 |
/*Initialize an OpenAL contex and allocate memory space for data and buffers*/ |
2191 | 74 |
ALCcontext *context; |
75 |
ALCdevice *device; |
|
2211
288360b78f30
- fade in/out functions merged, but kept binary compatibility
koda
parents:
2210
diff
changeset
|
76 |
const ALCchar *default_device; |
2191 | 77 |
|
2212 | 78 |
/*Position of the listener*/ |
2191 | 79 |
ALfloat ListenerPos[] = { 0.0, 0.0, 0.0 }; |
2212 | 80 |
/*Velocity of the listener*/ |
2191 | 81 |
ALfloat ListenerVel[] = { 0.0, 0.0, 0.0 }; |
2212 | 82 |
/*Orientation of the listener. (first 3 elements are "at", second 3 are "up")*/ |
2191 | 83 |
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
|
84 |
|
82e7da49c26a
-Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents:
2215
diff
changeset
|
85 |
if(openalReady == AL_TRUE) |
82e7da49c26a
-Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents:
2215
diff
changeset
|
86 |
{ |
82e7da49c26a
-Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents:
2215
diff
changeset
|
87 |
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
|
88 |
return AL_FALSE; |
82e7da49c26a
-Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents:
2215
diff
changeset
|
89 |
} |
82e7da49c26a
-Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents:
2215
diff
changeset
|
90 |
|
2191 | 91 |
default_device = alcGetString(NULL, ALC_DEFAULT_DEVICE_SPECIFIER); |
92 |
fprintf(stderr, "Using default device: %s\n", default_device); |
|
93 |
||
94 |
if ((device = alcOpenDevice(default_device)) == NULL) { |
|
2200
8192be6e3aef
koda/Smaxx changes to openal for crossplatform building
nemo
parents:
2194
diff
changeset
|
95 |
fprintf(stderr, "ERROR: Failed to open sound device\n"); |
2191 | 96 |
return AL_FALSE; |
97 |
} |
|
2211
288360b78f30
- fade in/out functions merged, but kept binary compatibility
koda
parents:
2210
diff
changeset
|
98 |
|
2191 | 99 |
context = alcCreateContext(device, NULL); |
100 |
alcMakeContextCurrent(context); |
|
101 |
alcProcessContext(context); |
|
102 |
||
103 |
if (AlGetError("ERROR %d: Creating a new contex\n") != AL_TRUE) |
|
104 |
return AL_FALSE; |
|
105 |
||
2212 | 106 |
/*allocate memory space for buffers and sources*/ |
2191 | 107 |
globalsize = memorysize; |
2211
288360b78f30
- fade in/out functions merged, but kept binary compatibility
koda
parents:
2210
diff
changeset
|
108 |
increment = memorysize; |
288360b78f30
- fade in/out functions merged, but kept binary compatibility
koda
parents:
2210
diff
changeset
|
109 |
Buffers = (ALuint*) Malloc(sizeof(ALuint)*globalsize); |
288360b78f30
- fade in/out functions merged, but kept binary compatibility
koda
parents:
2210
diff
changeset
|
110 |
Sources = (ALuint*) Malloc(sizeof(ALuint)*globalsize); |
2191 | 111 |
|
2212 | 112 |
/*set the listener gain, position (on xyz axes), velocity (one value for each axe) and orientation*/ |
2191 | 113 |
alListenerf (AL_GAIN, 1.0f ); |
114 |
alListenerfv(AL_POSITION, ListenerPos); |
|
115 |
alListenerfv(AL_VELOCITY, ListenerVel); |
|
116 |
alListenerfv(AL_ORIENTATION, ListenerOri); |
|
117 |
||
118 |
if (AlGetError("ERROR %d: Setting Listener properties\n") != AL_TRUE) |
|
119 |
return AL_FALSE; |
|
120 |
||
2216
82e7da49c26a
-Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents:
2215
diff
changeset
|
121 |
openalReady = AL_TRUE; |
82e7da49c26a
-Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents:
2215
diff
changeset
|
122 |
|
2191 | 123 |
alGetError(); /* clear any AL errors beforehand */ |
124 |
return AL_TRUE; |
|
125 |
} |
|
126 |
||
2213 | 127 |
|
2215 | 128 |
uint8_t helper_realloc (void) { |
2213 | 129 |
/*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
|
130 |
globalsize += increment; |
288360b78f30
- fade in/out functions merged, but kept binary compatibility
koda
parents:
2210
diff
changeset
|
131 |
#ifdef DEBUG |
288360b78f30
- fade in/out functions merged, but kept binary compatibility
koda
parents:
2210
diff
changeset
|
132 |
fprintf(stderr, "OpenAL: Realloc in process %d\n", globalsize); |
288360b78f30
- fade in/out functions merged, but kept binary compatibility
koda
parents:
2210
diff
changeset
|
133 |
#endif |
2213 | 134 |
Buffers = (ALuint*) Realloc(Buffers, sizeof(ALuint)*globalsize); |
135 |
Sources = (ALuint*) Realloc(Sources, sizeof(ALuint)*globalsize); |
|
136 |
||
137 |
return 0; |
|
2211
288360b78f30
- fade in/out functions merged, but kept binary compatibility
koda
parents:
2210
diff
changeset
|
138 |
} |
288360b78f30
- fade in/out functions merged, but kept binary compatibility
koda
parents:
2210
diff
changeset
|
139 |
|
2191 | 140 |
|
2216
82e7da49c26a
-Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents:
2215
diff
changeset
|
141 |
int openal_loadfile (const char *filename){ |
2213 | 142 |
/*Open a file, load into memory and allocate the Source buffer for playing*/ |
2257
7eb31efcfb9b
updates licence and fix a memory leak (which was consuming iphone memory)
koda
parents:
2220
diff
changeset
|
143 |
ALfloat SourcePos[] = { 0.0, 0.0, 0.0 }; /*Position of the source sound*/ |
7eb31efcfb9b
updates licence and fix a memory leak (which was consuming iphone memory)
koda
parents:
2220
diff
changeset
|
144 |
ALfloat SourceVel[] = { 0.0, 0.0, 0.0 }; /*Velocity of the source sound*/ |
2191 | 145 |
ALenum format; |
146 |
ALsizei bitsize; |
|
147 |
ALsizei freq; |
|
2213 | 148 |
char *data; |
2191 | 149 |
uint32_t fileformat; |
2215 | 150 |
ALenum error; |
2191 | 151 |
FILE *fp; |
152 |
||
2216
82e7da49c26a
-Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents:
2215
diff
changeset
|
153 |
if(openalReady == AL_FALSE) |
82e7da49c26a
-Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents:
2215
diff
changeset
|
154 |
{ |
82e7da49c26a
-Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents:
2215
diff
changeset
|
155 |
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
|
156 |
return AL_FALSE; |
82e7da49c26a
-Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents:
2215
diff
changeset
|
157 |
} |
2191 | 158 |
|
2213 | 159 |
/*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
|
160 |
if (globalindex == globalsize) |
288360b78f30
- fade in/out functions merged, but kept binary compatibility
koda
parents:
2210
diff
changeset
|
161 |
helper_realloc(); |
288360b78f30
- fade in/out functions merged, but kept binary compatibility
koda
parents:
2210
diff
changeset
|
162 |
|
2191 | 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 |
||
2212 | 175 |
/*prepare the buffer to receive data*/ |
2191 | 176 |
alGenBuffers(1, &Buffers[globalindex]); |
177 |
||
178 |
if (AlGetError("ERROR %d: Allocating memory for buffers\n") != AL_TRUE) |
|
179 |
return -3; |
|
180 |
||
2212 | 181 |
/*prepare the source to emit sound*/ |
2191 | 182 |
alGenSources(1, &Sources[globalindex]); |
183 |
||
184 |
if (AlGetError("ERROR %d: Allocating memory for sources\n") != AL_TRUE) |
|
185 |
return -4; |
|
186 |
||
187 |
||
2212 | 188 |
if (fileformat == 0x5367674F) /*check if ogg*/ |
2257
7eb31efcfb9b
updates licence and fix a memory leak (which was consuming iphone memory)
koda
parents:
2220
diff
changeset
|
189 |
error = load_oggvorbis (filename, &format, &data, &bitsize, &freq); |
2191 | 190 |
else { |
2212 | 191 |
if (fileformat == 0x46464952) /*check if wav*/ |
2257
7eb31efcfb9b
updates licence and fix a memory leak (which was consuming iphone memory)
koda
parents:
2220
diff
changeset
|
192 |
error = load_wavpcm (filename, &format, &data, &bitsize, &freq); |
2191 | 193 |
else { |
194 |
fprintf(stderr, "ERROR: File format (%08X) not supported!\n", invert_endianness(fileformat)); |
|
195 |
return -5; |
|
196 |
} |
|
197 |
} |
|
198 |
||
2212 | 199 |
/*copy pcm data in one buffer*/ |
2191 | 200 |
alBufferData(Buffers[globalindex], format, data, bitsize, freq); |
2212 | 201 |
free(data); /*deallocate data to save memory*/ |
2191 | 202 |
|
203 |
if (AlGetError("ERROR %d: Writing data to buffer\n") != AL_TRUE) |
|
2210 | 204 |
return -6; |
2191 | 205 |
|
2212 | 206 |
/*set source properties that it will use when it's in playback*/ |
2191 | 207 |
alSourcei (Sources[globalindex], AL_BUFFER, Buffers[globalindex] ); |
208 |
alSourcef (Sources[globalindex], AL_PITCH, 1.0f ); |
|
209 |
alSourcef (Sources[globalindex], AL_GAIN, 1.0f ); |
|
2211
288360b78f30
- fade in/out functions merged, but kept binary compatibility
koda
parents:
2210
diff
changeset
|
210 |
alSourcefv(Sources[globalindex], AL_POSITION, SourcePos ); |
288360b78f30
- fade in/out functions merged, but kept binary compatibility
koda
parents:
2210
diff
changeset
|
211 |
alSourcefv(Sources[globalindex], AL_VELOCITY, SourceVel ); |
2191 | 212 |
alSourcei (Sources[globalindex], AL_LOOPING, 0 ); |
213 |
||
214 |
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
|
215 |
return -7; |
2191 | 216 |
|
217 |
alGetError(); /* clear any AL errors beforehand */ |
|
218 |
||
2212 | 219 |
/*returns the index of the source you just loaded, increments it and exits*/ |
2191 | 220 |
return globalindex++; |
221 |
} |
|
222 |
||
223 |
||
2215 | 224 |
ALint openal_toggleloop (uint32_t index){ |
2191 | 225 |
/*Set or unset looping mode*/ |
226 |
ALint loop; |
|
227 |
||
2216
82e7da49c26a
-Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents:
2215
diff
changeset
|
228 |
if(openalReady == AL_FALSE) |
82e7da49c26a
-Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents:
2215
diff
changeset
|
229 |
{ |
82e7da49c26a
-Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents:
2215
diff
changeset
|
230 |
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
|
231 |
return AL_FALSE; |
82e7da49c26a
-Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents:
2215
diff
changeset
|
232 |
} |
82e7da49c26a
-Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents:
2215
diff
changeset
|
233 |
|
2191 | 234 |
if (index >= globalsize) { |
2211
288360b78f30
- fade in/out functions merged, but kept binary compatibility
koda
parents:
2210
diff
changeset
|
235 |
fprintf(stderr, "ERROR: index out of bounds (got %d, max %d)\n", index, globalindex); |
2191 | 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 |
||
2215 | 250 |
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
|
251 |
if(openalReady == AL_FALSE) |
82e7da49c26a
-Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents:
2215
diff
changeset
|
252 |
{ |
82e7da49c26a
-Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents:
2215
diff
changeset
|
253 |
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
|
254 |
return AL_FALSE; |
82e7da49c26a
-Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents:
2215
diff
changeset
|
255 |
} |
82e7da49c26a
-Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents:
2215
diff
changeset
|
256 |
|
2191 | 257 |
/*Set volume for sound number index*/ |
258 |
if (index >= globalindex) { |
|
2211
288360b78f30
- fade in/out functions merged, but kept binary compatibility
koda
parents:
2210
diff
changeset
|
259 |
fprintf(stderr, "ERROR: index out of bounds (got %d, max %d)\n", index, globalindex); |
2191 | 260 |
return AL_FALSE; |
261 |
} |
|
262 |
||
263 |
if (percentage > 100) |
|
264 |
percentage = 100; |
|
2213 | 265 |
alSourcef (Sources[index], AL_GAIN, (float) percentage/100.0f); |
2191 | 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 |
||
2215 | 275 |
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
|
276 |
if(openalReady == AL_FALSE) |
82e7da49c26a
-Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents:
2215
diff
changeset
|
277 |
{ |
82e7da49c26a
-Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents:
2215
diff
changeset
|
278 |
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
|
279 |
return AL_FALSE; |
82e7da49c26a
-Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents:
2215
diff
changeset
|
280 |
} |
82e7da49c26a
-Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents:
2215
diff
changeset
|
281 |
|
2191 | 282 |
/*Set volume for all sounds*/ |
283 |
if (percentage > 100) |
|
284 |
percentage = 100; |
|
2213 | 285 |
alListenerf (AL_GAIN, (float) percentage/100.0f); |
2191 | 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 |
||
2216
82e7da49c26a
-Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents:
2215
diff
changeset
|
299 |
if(openalReady == AL_FALSE) |
82e7da49c26a
-Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents:
2215
diff
changeset
|
300 |
{ |
82e7da49c26a
-Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents:
2215
diff
changeset
|
301 |
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
|
302 |
return AL_FALSE; |
82e7da49c26a
-Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents:
2215
diff
changeset
|
303 |
} |
82e7da49c26a
-Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents:
2215
diff
changeset
|
304 |
|
2191 | 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 |
||
2211
288360b78f30
- fade in/out functions merged, but kept binary compatibility
koda
parents:
2210
diff
changeset
|
319 |
|
2215 | 320 |
ALint openal_fade(uint32_t index, uint16_t quantity, uint8_t direction) { |
2213 | 321 |
/*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
|
322 |
#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
|
323 |
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
|
324 |
#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
|
325 |
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
|
326 |
DWORD threadID; |
2209 | 327 |
#endif |
328 |
fade_t *fade; |
|
329 |
||
2216
82e7da49c26a
-Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents:
2215
diff
changeset
|
330 |
if(openalReady == AL_FALSE) |
82e7da49c26a
-Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents:
2215
diff
changeset
|
331 |
{ |
82e7da49c26a
-Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents:
2215
diff
changeset
|
332 |
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
|
333 |
return AL_FALSE; |
82e7da49c26a
-Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents:
2215
diff
changeset
|
334 |
} |
82e7da49c26a
-Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents:
2215
diff
changeset
|
335 |
|
2209 | 336 |
fade = (fade_t*) Malloc(sizeof(fade_t)); |
2200
8192be6e3aef
koda/Smaxx changes to openal for crossplatform building
nemo
parents:
2194
diff
changeset
|
337 |
fade->index = index; |
8192be6e3aef
koda/Smaxx changes to openal for crossplatform building
nemo
parents:
2194
diff
changeset
|
338 |
fade->quantity = quantity; |
2191 | 339 |
|
340 |
if (index >= globalindex) { |
|
2211
288360b78f30
- fade in/out functions merged, but kept binary compatibility
koda
parents:
2210
diff
changeset
|
341 |
fprintf(stderr, "ERROR: index out of bounds (got %d, max %d)\n", index, globalindex); |
2191 | 342 |
return AL_FALSE; |
343 |
} |
|
2211
288360b78f30
- fade in/out functions merged, but kept binary compatibility
koda
parents:
2210
diff
changeset
|
344 |
|
2213 | 345 |
if (direction == FADE_IN) |
2211
288360b78f30
- fade in/out functions merged, but kept binary compatibility
koda
parents:
2210
diff
changeset
|
346 |
#ifndef _WIN32 |
288360b78f30
- fade in/out functions merged, but kept binary compatibility
koda
parents:
2210
diff
changeset
|
347 |
pthread_create(&thread, NULL, helper_fadein, (void*) fade); |
288360b78f30
- fade in/out functions merged, but kept binary compatibility
koda
parents:
2210
diff
changeset
|
348 |
#else |
288360b78f30
- fade in/out functions merged, but kept binary compatibility
koda
parents:
2210
diff
changeset
|
349 |
Thread = _beginthread(&helper_fadein, 0, (void*) fade); |
288360b78f30
- fade in/out functions merged, but kept binary compatibility
koda
parents:
2210
diff
changeset
|
350 |
#endif |
288360b78f30
- fade in/out functions merged, but kept binary compatibility
koda
parents:
2210
diff
changeset
|
351 |
else { |
2213 | 352 |
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
|
353 |
#ifndef _WIN32 |
2211
288360b78f30
- fade in/out functions merged, but kept binary compatibility
koda
parents:
2210
diff
changeset
|
354 |
pthread_create(&thread, NULL, helper_fadeout, (void*) fade); |
288360b78f30
- fade in/out functions merged, but kept binary compatibility
koda
parents:
2210
diff
changeset
|
355 |
#else |
288360b78f30
- fade in/out functions merged, but kept binary compatibility
koda
parents:
2210
diff
changeset
|
356 |
Thread = _beginthread(&helper_fadeout, 0, (void*) fade); |
288360b78f30
- fade in/out functions merged, but kept binary compatibility
koda
parents:
2210
diff
changeset
|
357 |
#endif |
288360b78f30
- fade in/out functions merged, but kept binary compatibility
koda
parents:
2210
diff
changeset
|
358 |
else { |
2213 | 359 |
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
|
360 |
free(fade); |
288360b78f30
- fade in/out functions merged, but kept binary compatibility
koda
parents:
2210
diff
changeset
|
361 |
return AL_FALSE; |
288360b78f30
- fade in/out functions merged, but kept binary compatibility
koda
parents:
2210
diff
changeset
|
362 |
} |
288360b78f30
- fade in/out functions merged, but kept binary compatibility
koda
parents:
2210
diff
changeset
|
363 |
} |
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 |
#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
|
366 |
pthread_detach(thread); |
2191 | 367 |
#endif |
368 |
||
369 |
alGetError(); /* clear any AL errors beforehand */ |
|
2211
288360b78f30
- fade in/out functions merged, but kept binary compatibility
koda
parents:
2210
diff
changeset
|
370 |
|
2191 | 371 |
return AL_TRUE; |
372 |
} |
|
2211
288360b78f30
- fade in/out functions merged, but kept binary compatibility
koda
parents:
2210
diff
changeset
|
373 |
|
2191 | 374 |
|
2215 | 375 |
ALint openal_fadeout(uint32_t index, uint16_t quantity) { |
2213 | 376 |
/*wrapper for fadeout*/ |
2211
288360b78f30
- fade in/out functions merged, but kept binary compatibility
koda
parents:
2210
diff
changeset
|
377 |
return openal_fade(index, quantity, FADE_OUT); |
288360b78f30
- fade in/out functions merged, but kept binary compatibility
koda
parents:
2210
diff
changeset
|
378 |
} |
288360b78f30
- fade in/out functions merged, but kept binary compatibility
koda
parents:
2210
diff
changeset
|
379 |
|
288360b78f30
- fade in/out functions merged, but kept binary compatibility
koda
parents:
2210
diff
changeset
|
380 |
|
2215 | 381 |
ALint openal_fadein(uint32_t index, uint16_t quantity) { |
2213 | 382 |
/*wrapper for fadein*/ |
2211
288360b78f30
- fade in/out functions merged, but kept binary compatibility
koda
parents:
2210
diff
changeset
|
383 |
return openal_fade(index, quantity, FADE_IN); |
288360b78f30
- fade in/out functions merged, but kept binary compatibility
koda
parents:
2210
diff
changeset
|
384 |
} |
288360b78f30
- fade in/out functions merged, but kept binary compatibility
koda
parents:
2210
diff
changeset
|
385 |
|
2191 | 386 |
|
2220 | 387 |
ALint openal_setposition(uint32_t index, float x, float y, float z) { |
388 |
if(openalReady == AL_FALSE) { |
|
389 |
fprintf(stderr, "ERROR: OpenAL not initialized\n"); |
|
390 |
return AL_FALSE; |
|
391 |
} |
|
392 |
if (index >= globalindex) { |
|
393 |
fprintf(stderr, "ERROR: index out of bounds (got %d, max %d)\n", index, globalindex); |
|
394 |
return AL_FALSE; |
|
395 |
} |
|
396 |
||
397 |
alSource3f(Sources[index], AL_POSITION, x, y, z); |
|
398 |
if (AlGetError("ERROR %d: setting position for last sound\n") != AL_TRUE) |
|
399 |
return AL_FALSE; |
|
400 |
||
401 |
return AL_TRUE; |
|
402 |
} |
|
2215 | 403 |
ALint openal_playsound(uint32_t index){ |
2220 | 404 |
if(openalReady == AL_FALSE) { |
2216
82e7da49c26a
-Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents:
2215
diff
changeset
|
405 |
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
|
406 |
return AL_FALSE; |
82e7da49c26a
-Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents:
2215
diff
changeset
|
407 |
} |
82e7da49c26a
-Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents:
2215
diff
changeset
|
408 |
|
2191 | 409 |
/*Play sound number index*/ |
410 |
if (index >= globalindex) { |
|
2211
288360b78f30
- fade in/out functions merged, but kept binary compatibility
koda
parents:
2210
diff
changeset
|
411 |
fprintf(stderr, "ERROR: index out of bounds (got %d, max %d)\n", index, globalindex); |
2191 | 412 |
return AL_FALSE; |
413 |
} |
|
414 |
alSourcePlay(Sources[index]); |
|
415 |
if (AlGetError("ERROR %d: Playing last sound\n") != AL_TRUE) |
|
416 |
return AL_FALSE; |
|
417 |
||
418 |
alGetError(); /* clear any AL errors beforehand */ |
|
419 |
||
420 |
return AL_TRUE; |
|
421 |
} |
|
422 |
||
423 |
||
2215 | 424 |
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
|
425 |
if(openalReady == AL_FALSE) |
82e7da49c26a
-Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents:
2215
diff
changeset
|
426 |
{ |
82e7da49c26a
-Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents:
2215
diff
changeset
|
427 |
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
|
428 |
return AL_FALSE; |
82e7da49c26a
-Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents:
2215
diff
changeset
|
429 |
} |
82e7da49c26a
-Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents:
2215
diff
changeset
|
430 |
|
2191 | 431 |
/*Pause sound number index*/ |
432 |
if (index >= globalindex) { |
|
2211
288360b78f30
- fade in/out functions merged, but kept binary compatibility
koda
parents:
2210
diff
changeset
|
433 |
fprintf(stderr, "ERROR: index out of bounds (got %d, max %d)\n", index, globalindex); |
2191 | 434 |
return AL_FALSE; |
435 |
} |
|
436 |
alSourcePause(Sources[index]); |
|
437 |
if (AlGetError("ERROR %d: Pausing last sound\n") != AL_TRUE) |
|
438 |
return AL_FALSE; |
|
439 |
||
440 |
return AL_TRUE; |
|
441 |
} |
|
442 |
||
443 |
||
2215 | 444 |
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
|
445 |
if(openalReady == AL_FALSE) |
82e7da49c26a
-Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents:
2215
diff
changeset
|
446 |
{ |
82e7da49c26a
-Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents:
2215
diff
changeset
|
447 |
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
|
448 |
return AL_FALSE; |
82e7da49c26a
-Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents:
2215
diff
changeset
|
449 |
} |
82e7da49c26a
-Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents:
2215
diff
changeset
|
450 |
|
2191 | 451 |
/*Stop sound number index*/ |
452 |
if (index >= globalindex) { |
|
2211
288360b78f30
- fade in/out functions merged, but kept binary compatibility
koda
parents:
2210
diff
changeset
|
453 |
fprintf(stderr, "ERROR: index out of bounds (got %d, max %d)\n", index, globalindex); |
2191 | 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 |
|
466 |
} |
|
467 |
#endif |