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