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