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