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