author | koda |
Wed, 21 Apr 2010 01:57:23 +0000 | |
changeset 3361 | cfc6cd502f85 |
parent 3360 | 717b4e46e855 |
child 3362 | 8d3b4d19ce27 |
permissions | -rw-r--r-- |
3353 | 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 Lesser 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 Lesser General Public License for more details. |
|
13 |
* |
|
14 |
* You should have received a copy of the GNU Lesser 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 "openalbridge.h" |
|
20 |
#include "globals.h" |
|
21 |
#include "wrappers.h" |
|
22 |
#include "alc.h" |
|
23 |
#include "loaders.h" |
|
24 |
||
25 |
#ifdef __CPLUSPLUS |
|
26 |
extern "C" { |
|
27 |
#endif |
|
28 |
||
29 |
/*Sources are points emitting sound*/ |
|
30 |
ALuint *Sources; |
|
31 |
/*Buffers hold sound data*/ |
|
32 |
ALuint *Buffers; |
|
33 |
/*index for Sources and Buffers*/ |
|
34 |
ALuint globalindex, globalsize, increment; |
|
35 |
||
36 |
ALboolean openalReady = AL_FALSE; |
|
3360
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
37 |
ALfloat old_gain; |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
38 |
|
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
39 |
ALboolean openal_init(ALboolean usehardware, int memorysize) { |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
40 |
/*Initialize an OpenAL contex and allocate memory space for data and buffers*/ |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
41 |
ALCcontext *context; |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
42 |
ALCdevice *device; |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
43 |
|
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
44 |
prog = "OpenAL subsystem"; |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
45 |
|
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
46 |
// set the memory dimentsion and the increment width when reallocating |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
47 |
if (memorysize <= 0) |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
48 |
globalsize = 50; |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
49 |
else |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
50 |
globalsize = memorysize; |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
51 |
increment = globalsize; |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
52 |
|
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
53 |
// reuse old context but keep the new value for increment |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
54 |
if (openalReady == AL_TRUE) { |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
55 |
err_msg("(%s) WARN - already initialized", prog); |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
56 |
return AL_FALSE; |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
57 |
} |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
58 |
|
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
59 |
if (usehardware == AL_TRUE) |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
60 |
device = alcOpenDevice(NULL); |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
61 |
else |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
62 |
device = alcOpenDevice("Generic Software"); |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
63 |
|
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
64 |
err_msg("(%s) INFO - Output device: %s", prog, alcGetString(device, ALC_DEVICE_SPECIFIER)); |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
65 |
|
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
66 |
if (device == NULL) { |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
67 |
errno = ENODEV; |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
68 |
err_ret("(%s) WARN - failed to open sound device", prog); |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
69 |
return AL_FALSE; |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
70 |
} |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
71 |
|
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
72 |
context = alcCreateContext(device, NULL); |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
73 |
alcMakeContextCurrent(context); |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
74 |
alcProcessContext(context); |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
75 |
|
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
76 |
if (AlGetError("(%s) WARN - Failed to create a new contex") != AL_TRUE) |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
77 |
return AL_FALSE; |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
78 |
|
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
79 |
// allocate memory space for buffers and sources |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
80 |
Buffers = (ALuint*) Malloc(sizeof(ALuint)*globalsize); |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
81 |
Sources = (ALuint*) Malloc(sizeof(ALuint)*globalsize); |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
82 |
|
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
83 |
// set the listener gain, position (on xyz axes), velocity (one value for each axe) and orientation |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
84 |
// Position, Velocity and Orientation of the listener |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
85 |
ALfloat ListenerPos[] = {0.0, 0.0, 0.0}; |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
86 |
ALfloat ListenerVel[] = {0.0, 0.0, 0.0}; |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
87 |
ALfloat ListenerOri[] = {0.0, 0.0, -1.0, 0.0, 1.0, 0.0}; |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
88 |
|
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
89 |
alListenerf (AL_GAIN, 1.0f ); |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
90 |
alListenerfv(AL_POSITION, ListenerPos); |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
91 |
alListenerfv(AL_VELOCITY, ListenerVel); |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
92 |
alListenerfv(AL_ORIENTATION, ListenerOri); |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
93 |
|
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
94 |
if (AlGetError("(%s) WARN - Failed to set Listener properties") != AL_TRUE) |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
95 |
return AL_FALSE; |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
96 |
|
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
97 |
openalReady = AL_TRUE; |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
98 |
|
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
99 |
alGetError(); // clear any AL errors beforehand |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
100 |
return AL_TRUE; |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
101 |
} |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
102 |
|
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
103 |
void openal_close (void) { |
3353 | 104 |
/*Stop all sounds, deallocate all memory and close OpenAL */ |
105 |
ALCcontext *context; |
|
106 |
ALCdevice *device; |
|
107 |
||
108 |
if (openalReady == AL_FALSE) { |
|
109 |
errno = EPERM; |
|
110 |
err_ret("(%s) WARN - OpenAL not initialized", prog); |
|
3360
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
111 |
return; |
3353 | 112 |
} |
113 |
||
114 |
alSourceStopv (globalsize, Sources); |
|
115 |
alDeleteSources (globalsize, Sources); |
|
116 |
alDeleteBuffers (globalsize, Buffers); |
|
117 |
||
118 |
free(Sources); |
|
119 |
free(Buffers); |
|
120 |
||
121 |
context = alcGetCurrentContext(); |
|
122 |
device = alcGetContextsDevice(context); |
|
123 |
||
124 |
alcMakeContextCurrent(NULL); |
|
125 |
alcDestroyContext(context); |
|
126 |
alcCloseDevice(device); |
|
127 |
||
128 |
openalReady = AL_FALSE; |
|
129 |
||
3360
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
130 |
err_msg("(%s) INFO - closed", prog); |
3353 | 131 |
|
3361 | 132 |
return; |
3353 | 133 |
} |
134 |
||
135 |
ALboolean openal_ready(void) { |
|
136 |
return openalReady; |
|
137 |
} |
|
138 |
||
139 |
||
3360
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
140 |
void helper_realloc (void) { |
3353 | 141 |
/*expands allocated memory when loading more sound files than expected*/ |
142 |
int oldsize = globalsize; |
|
143 |
globalsize += increment; |
|
144 |
||
145 |
err_msg("(%s) INFO - Realloc in process from %d to %d\n", prog, oldsize, globalsize); |
|
146 |
||
147 |
Buffers = (ALuint*) Realloc(Buffers, sizeof(ALuint)*globalsize); |
|
148 |
Sources = (ALuint*) Realloc(Sources, sizeof(ALuint)*globalsize); |
|
149 |
||
3360
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
150 |
return; |
3353 | 151 |
} |
152 |
||
153 |
||
3360
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
154 |
int openal_loadfile (const char *filename){ |
3353 | 155 |
/*Open a file, load into memory and allocate the Source buffer for playing*/ |
156 |
ALfloat SourcePos[] = { 0.0, 0.0, 0.0 }; /*Position of the source sound*/ |
|
157 |
ALfloat SourceVel[] = { 0.0, 0.0, 0.0 }; /*Velocity of the source sound*/ |
|
158 |
ALenum format; |
|
159 |
ALsizei bitsize, freq; |
|
160 |
char *data; |
|
161 |
uint32_t fileformat; |
|
162 |
ALenum error; |
|
163 |
FILE *fp; |
|
164 |
||
165 |
if (openalReady == AL_FALSE) { |
|
3360
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
166 |
err_msg("(%s) WARN - not initialized", prog); |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
167 |
return -1; |
3353 | 168 |
} |
169 |
||
170 |
/*when the buffers are all used, we can expand memory to accept new files*/ |
|
171 |
if (globalindex == globalsize) |
|
172 |
helper_realloc(); |
|
173 |
||
174 |
/*detect the file format, as written in the first 4 bytes of the header*/ |
|
175 |
fp = Fopen (filename, "rb"); |
|
176 |
||
177 |
if (fp == NULL) |
|
3360
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
178 |
return -2; |
3353 | 179 |
|
180 |
error = fread (&fileformat, sizeof(uint32_t), 1, fp); |
|
181 |
fclose (fp); |
|
182 |
||
183 |
if (error < 0) { |
|
3360
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
184 |
err_msg("(%s) ERROR - File %s is too short", prog, filename); |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
185 |
return -3; |
3353 | 186 |
} |
187 |
||
188 |
/*prepare the buffer to receive data*/ |
|
189 |
alGenBuffers(1, &Buffers[globalindex]); |
|
190 |
||
191 |
if (AlGetError("(%s) ERROR - Failed to allocate memory for buffers") != AL_TRUE) |
|
3360
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
192 |
return -4; |
3353 | 193 |
|
194 |
/*prepare the source to emit sound*/ |
|
195 |
alGenSources(1, &Sources[globalindex]); |
|
196 |
||
197 |
if (AlGetError("(%s) ERROR - Failed to allocate memory for sources") != AL_TRUE) |
|
3360
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
198 |
return -5; |
3353 | 199 |
|
200 |
||
201 |
switch (ENDIAN_BIG_32(fileformat)) { |
|
202 |
case OGG_FILE_FORMAT: |
|
203 |
error = load_oggvorbis (filename, &format, &data, &bitsize, &freq); |
|
204 |
break; |
|
205 |
case WAV_FILE_FORMAT: |
|
206 |
error = load_wavpcm (filename, &format, &data, &bitsize, &freq); |
|
207 |
break; |
|
208 |
default: |
|
3360
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
209 |
err_msg ("(%s) ERROR - File format (%08X) not supported", prog, ENDIAN_BIG_32(fileformat)); |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
210 |
return -6; |
3353 | 211 |
break; |
212 |
} |
|
213 |
||
214 |
||
215 |
/*copy pcm data in one buffer*/ |
|
216 |
alBufferData(Buffers[globalindex], format, data, bitsize, freq); |
|
217 |
free(data); /*deallocate data to save memory*/ |
|
218 |
||
219 |
if (AlGetError("(%s) ERROR - Failed to write data to buffers") != AL_TRUE) |
|
220 |
return -6; |
|
221 |
||
222 |
/*set source properties that it will use when it's in playback*/ |
|
223 |
alSourcei (Sources[globalindex], AL_BUFFER, Buffers[globalindex] ); |
|
224 |
alSourcef (Sources[globalindex], AL_PITCH, 1.0f ); |
|
225 |
alSourcef (Sources[globalindex], AL_GAIN, 1.0f ); |
|
226 |
alSourcefv(Sources[globalindex], AL_POSITION, SourcePos ); |
|
227 |
alSourcefv(Sources[globalindex], AL_VELOCITY, SourceVel ); |
|
228 |
alSourcei (Sources[globalindex], AL_LOOPING, 0 ); |
|
229 |
||
230 |
if (AlGetError("(%s) ERROR - Failed to set Source properties") != AL_TRUE) |
|
231 |
return -7; |
|
232 |
||
233 |
alGetError(); /* clear any AL errors beforehand */ |
|
234 |
||
235 |
/*returns the index of the source you just loaded, increments it and exits*/ |
|
236 |
return globalindex++; |
|
237 |
} |
|
238 |
||
239 |
||
3360
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
240 |
void openal_playsound (uint32_t index) { |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
241 |
openal_playsound_loop (index, 0); |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
242 |
} |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
243 |
|
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
244 |
|
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
245 |
void openal_pausesound (uint32_t index) { |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
246 |
if (openalReady == AL_TRUE && index < globalsize) |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
247 |
alSourcePause(Sources[index]); |
3353 | 248 |
} |
249 |
||
250 |
||
3360
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
251 |
void openal_stopsound (uint32_t index) { |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
252 |
openal_stopsound_free(index, 0); |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
253 |
} |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
254 |
|
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
255 |
|
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
256 |
void openal_freesound (uint32_t index){ |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
257 |
if (openalReady == AL_TRUE && index < globalsize) |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
258 |
alSourceStop(Sources[index]); |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
259 |
// STUB |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
260 |
} |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
261 |
|
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
262 |
|
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
263 |
void openal_playsound_loop (unsigned int index, char loops) { |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
264 |
if (openalReady == AL_TRUE && index < globalsize) { |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
265 |
alSourcePlay(Sources[index]); |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
266 |
if (loops != 0) |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
267 |
openal_toggleloop(index); |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
268 |
} |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
269 |
} |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
270 |
|
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
271 |
void openal_stopsound_free (unsigned int index, char freesource) { |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
272 |
if (openalReady == AL_TRUE && index < globalsize) { |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
273 |
alSourceStop(Sources[index]); |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
274 |
if (freesource != 0) |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
275 |
openal_freesound(index); |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
276 |
} |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
277 |
} |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
278 |
|
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
279 |
void openal_toggleloop (uint32_t index) { |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
280 |
ALint loop; |
3353 | 281 |
|
3360
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
282 |
if (openalReady == AL_TRUE && index < globalsize) { |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
283 |
alGetSourcei (Sources[index], AL_LOOPING, &loop); |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
284 |
alSourcei (Sources[index], AL_LOOPING, !((uint8_t) loop) & 0x00000001); |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
285 |
} |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
286 |
|
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
287 |
} |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
288 |
|
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
289 |
|
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
290 |
void openal_setvolume (uint32_t index, float gain) { |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
291 |
if (openalReady == AL_TRUE && index < globalsize) |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
292 |
alSourcef (Sources[index], AL_GAIN, gain); |
3353 | 293 |
} |
294 |
||
295 |
||
3360
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
296 |
void openal_setglobalvolume (float gain) { |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
297 |
if (openalReady == AL_TRUE) |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
298 |
alListenerf (AL_GAIN, gain); |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
299 |
} |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
300 |
|
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
301 |
void openal_togglemute () { |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
302 |
ALfloat gain; |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
303 |
|
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
304 |
if (openalReady == AL_TRUE) { |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
305 |
alGetListenerf (AL_GAIN, &gain); |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
306 |
if (gain > 0) { |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
307 |
old_gain = gain; |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
308 |
gain = 0; |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
309 |
} else |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
310 |
gain = old_gain; |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
311 |
|
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
312 |
alListenerf (AL_GAIN, gain); |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
313 |
} |
3353 | 314 |
} |
315 |
||
316 |
||
3360
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
317 |
void openal_fade (uint32_t index, uint16_t quantity, char direction) { |
3353 | 318 |
/*Fade in or out by calling a helper thread*/ |
319 |
#ifndef _WIN32 |
|
320 |
pthread_t thread; |
|
321 |
#else |
|
322 |
HANDLE Thread; |
|
323 |
DWORD threadID; |
|
324 |
#endif |
|
325 |
fade_t *fade; |
|
326 |
||
3360
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
327 |
if (openalReady == AL_TRUE && index < globalsize) { |
3353 | 328 |
|
3360
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
329 |
fade = (fade_t*) Malloc(sizeof(fade_t)); |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
330 |
fade->index = index; |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
331 |
fade->quantity = quantity; |
3353 | 332 |
|
3360
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
333 |
if (direction > 0) { |
3353 | 334 |
#ifndef _WIN32 |
335 |
pthread_create(&thread, NULL, helper_fadein, (void*) fade); |
|
336 |
#else |
|
337 |
Thread = _beginthread(&helper_fadein, 0, (void*) fade); |
|
338 |
#endif |
|
3360
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
339 |
} else { |
3353 | 340 |
#ifndef _WIN32 |
341 |
pthread_create(&thread, NULL, helper_fadeout, (void*) fade); |
|
342 |
#else |
|
343 |
Thread = _beginthread(&helper_fadeout, 0, (void*) fade); |
|
344 |
#endif |
|
3360
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
345 |
} |
3353 | 346 |
|
347 |
#ifndef _WIN32 |
|
3360
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
348 |
pthread_detach(thread); |
3353 | 349 |
#endif |
3360
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
350 |
} |
3353 | 351 |
} |
352 |
||
353 |
||
3360
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
354 |
void openal_fadeout (uint32_t index, uint16_t quantity) { |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
355 |
openal_fade(index, quantity, AL_FADE_OUT); |
3353 | 356 |
} |
357 |
||
358 |
||
3360
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
359 |
void openal_fadein (uint32_t index, uint16_t quantity) { |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
360 |
openal_fade(index, quantity, AL_FADE_IN); |
3353 | 361 |
} |
362 |
||
363 |
||
3360
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
364 |
void openal_setposition (uint32_t index, float x, float y, float z) { |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
365 |
if (openalReady == AL_TRUE && index < globalsize) |
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
366 |
alSource3f(Sources[index], AL_POSITION, x, y, z);; |
3353 | 367 |
} |
368 |
||
3360
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
369 |
|
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
370 |
|
717b4e46e855
updates to openalbridge (modified api and simplified some sections)
koda
parents:
3356
diff
changeset
|
371 |
|
3353 | 372 |
#ifdef __CPLUSPLUS |
373 |
} |
|
374 |
#endif |