fix missing dlfcn.h include
[rofl0r-df-libgraphics.git] / g_src / music_and_sound_openal.cpp
blob71955763a9d015be698c749b0c8e9212e6639311
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <assert.h>
4 #include <dlfcn.h>
6 #include "music_and_sound_openal.h"
7 #include "music_and_sound_v.h"
9 #define ABORT(str) do { printf("%s: line %d: %s\n", __FILE__, __LINE__, str); abort(); } while(0);
10 static bool init_openal();
11 static bool init_sndfile();
13 using namespace std;
15 #define alPrintErrors() do { alPrintErrors_(__FILE__,__LINE__); } while(0);
17 static void alPrintErrors_(const char* file, int line) {
18 ALenum err;
19 while ((err = alGetError()) != AL_NO_ERROR) {
20 printf("At %s: %d: ", file, line);
21 switch (err) {
22 case AL_INVALID_NAME: puts("AL_INVALID_NAME detected"); break;
23 case AL_INVALID_ENUM: puts("AL_INVALID_ENUM detected"); break;
24 case AL_INVALID_VALUE: puts("AL_INVALID_VALUE detected"); break;
25 case AL_INVALID_OPERATION: puts("AL_INVALID_OPERATION detected"); break;
26 case AL_OUT_OF_MEMORY: puts("AL_OUT_OF_MEMORY detected"); break;
31 bool musicsoundst::initsound() {
32 if (functional) return true;
34 // Load the libraries
35 if (!init_openal()) {
36 puts("Dynamically loading the OpenAL library failed, disabling sound");
37 MessageBox(NULL, "Dynamically loading the OpenAL library failed, disabling sound", 0, 0);
38 return false;
40 if (!init_sndfile()) {
41 puts("Dynamically loading the sndfile library failed, disabling sound");
42 MessageBox(NULL, "Dynamically loading the sndfile library failed, disabling sound", 0, 0);
43 return false;
46 // Find out what devices we have available
47 const char *devices = alcGetString(NULL, ALC_DEVICE_SPECIFIER);
48 if (!devices) {
49 puts("No sound devices available. Sound disabled. OpenAL broken?");
50 return false;
53 const char *firstdevice = devices;
54 puts("Sound devices available:");
55 while (*devices) {
56 puts(devices);
57 devices += strlen(devices) + 1;
59 printf("Picking %s. If your desired device was missing, make sure you have the appropriate 32-bit libraries installed. If you wanted a different device, configure ~/.openalrc appropriately.\n",
60 firstdevice);
62 // Create the context
63 device = alcOpenDevice(firstdevice);
64 if (!device)
65 return false;
67 const ALCint attrlist[] = { ALC_FREQUENCY, SOUND_FREQUENCY,
68 ALC_MONO_SOURCES, 0,
69 ALC_STEREO_SOURCES, SOUND_CHANNELNUM };
70 context = alcCreateContext(device, attrlist);
71 if (context) {
72 puts("Perfect OpenAL context attributes GET");
73 goto done;
75 context = alcCreateContext(device, NULL);
76 if (context) {
77 puts("Using OpenAL in compatibility mode");
78 goto done;
80 alcCloseDevice(device);
81 return false;
83 done:
84 if (ALC_FALSE == alcMakeContextCurrent(context)) {
85 puts("alcMakeContextCurrent failed");
86 return false;
88 functional = true;
89 return true;
92 // int main() {
93 // musicsound.initsound();
94 // string str = "data/sound/song_title.ogg";
95 // musicsound.set_song(str, 14);
96 // musicsound.startbackgroundmusic(14);
97 // sleep(9999);
98 // exit(1);
99 // }
101 void musicsoundst::set_song(string &filename, slot slot) {
102 if (!functional) return;
104 // printf("%s requested in %d-%d\n", filename.c_str(), (int)slot.first, slot.second);
105 if (!buffers.count(filename)) {
106 // Song not already loaded. Load it.
107 SF_INFO sfinfo;
108 sfinfo.format = 0;
109 SNDFILE *sf = sf_open(filename.c_str(), SFM_READ, &sfinfo);
110 if (!sf) {
111 printf("%s not found, sound not loaded\n", filename.c_str());
112 goto end;
114 short *buffer = new short[sfinfo.channels * sfinfo.frames];
115 sf_count_t frames_read = sf_readf_short(sf, buffer, sfinfo.frames);
116 if (frames_read != sfinfo.frames)
117 printf("%s: %d frames requested, %d frames read. Corrupted file?\n",
118 filename.c_str(), (int)sfinfo.frames, (int)frames_read);
119 sf_close(sf);
120 // Construct openal buffer and load this
121 ALuint albuf;
122 alGenBuffers(1, &albuf);
123 if (!alIsBuffer(albuf)) {
124 puts("Constructing OpenAL buffer mysteriously failed!");
125 goto end;
127 ALenum format;
128 switch (sfinfo.channels) {
129 case 1: format = AL_FORMAT_MONO16;
130 break;
131 case 2: format = AL_FORMAT_STEREO16;
132 break;
133 default:
134 printf("%s: Unexpected number of channels: %d\n",
135 filename.c_str(), (int)sfinfo.channels);
136 goto end;
138 alBufferData(albuf, format, (ALvoid*)buffer,
139 sfinfo.channels * sfinfo.frames * 2, sfinfo.samplerate);
140 alPrintErrors();
141 delete[] buffer;
143 // Create a source for this song
144 ALuint source;
145 alGenSources(1, &source);
146 if (!alIsSource(source)) {
147 puts("Constructing OpenAL source mysteriously failed!");
148 goto end;
150 alSourceQueueBuffers(source, 1, &albuf);
152 buffers[filename] = albuf;
153 sources[filename] = source;
156 // Store the requested song in the requested slot.
157 // Say, should this alter the playing song if that slot is already playing?
158 slot_buffer[slot] = buffers[filename];
159 slot_source[slot] = sources[filename];
161 end:
162 alPrintErrors();
165 void musicsoundst::set_song(string &filename, int slot) {
166 set_song(filename, make_pair(true, slot));
169 void musicsoundst::set_master_volume(long newvol) {
170 if (!functional) return;
171 alListenerf(AL_GAIN, newvol / 255.0f);
174 void musicsoundst::playsound(slot slot) {
175 if (!functional) return;
176 // printf("%d requested\n", slot);
177 if (!slot_source.count(slot)) {
178 // printf("Slot %d-%d requested, but no song loaded\n", (int)slot.first, slot.second);
179 return;
181 if (background_slot == slot) {
182 puts("playsound called on background song, background song cancelled!?");
183 background_slot = make_pair(false,-1);
185 alSourcei(slot_source[slot], AL_LOOPING, AL_FALSE);
186 alSourcePlay(slot_source[slot]);
187 alPrintErrors();
190 void musicsoundst::playsound(int slot) {
191 playsound(make_pair(false,slot));
194 void musicsoundst::startbackgroundmusic(slot slot) {
195 if (!functional) return;
197 if (!slot_source.count(slot)) {
198 // printf("Slot %d-%d requested, but no song loaded\n", (int)slot.first, slot.second);
199 return;
202 if (background_slot == slot)
203 return; // Verily, it is already playing
204 stop_sound(background_slot);
205 background_slot = slot;
206 // printf("%d backgrounded\n", slot);
208 alSourcei(slot_source[slot], AL_LOOPING, AL_TRUE);
209 alSourcePlay(slot_source[slot]);
210 alPrintErrors();
213 void musicsoundst::startbackgroundmusic(int slot) {
214 startbackgroundmusic(make_pair(true,slot));
217 void musicsoundst::stopbackgroundmusic() {
218 if (!functional) return;
219 if (background_slot == make_pair(false,-1)) return;
221 alSourceStop(slot_source[background_slot]);
224 void musicsoundst::stop_sound() {
225 if (!functional) return;
226 // Stop all playing sounds. Does this include background music?
227 std::map<std::string,ALuint>::iterator it;
228 for (it = sources.begin(); it != sources.end(); ++it)
229 alSourceStop(it->second);
232 void musicsoundst::stop_sound(slot slot) {
233 if (!functional) return;
234 if (slot_source.count(slot) == 0) return;
235 ALuint source = slot_source[slot];
236 alSourceStop(source);
239 void musicsoundst::deinitsound() {
240 if (!functional) return;
242 std::map<std::string,ALuint>::iterator it;
243 // Free all sources
244 for (it = sources.begin(); it != sources.end(); ++it) {
245 ALuint source = it->second;
246 alDeleteSources(1, &source);
248 // Free all sample memory
249 for (it = buffers.begin(); it != buffers.end(); ++it) {
250 ALuint buffer = it->second;
251 alDeleteBuffers(1, &buffer);
253 // Deinit OpenAL
254 alcMakeContextCurrent(NULL);
255 alcDestroyContext(context);
256 alcCloseDevice(device);
258 functional=false;
261 void musicsoundst::set_sound(string &filename, int slot, int pan, int priority) {
262 if (!functional) return;
263 set_song(filename, make_pair(false,slot));
266 // Deprecated stuff below
268 void musicsoundst::playsound(int s, int channel) {
269 if (!functional) return;
270 playsound(s);
274 //// OpenAL, ALC and sndfile stub ////
276 static void (*_alEnable)( ALenum capability );
277 static void (*_alDisable)( ALenum capability );
278 static ALboolean (*_alIsEnabled)( ALenum capability );
279 static const ALchar* (*_alGetString)( ALenum param );
280 static void (*_alGetBooleanv)( ALenum param, ALboolean* data );
281 static void (*_alGetIntegerv)( ALenum param, ALint* data );
282 static void (*_alGetFloatv)( ALenum param, ALfloat* data );
283 static void (*_alGetDoublev)( ALenum param, ALdouble* data );
284 static ALboolean (*_alGetBoolean)( ALenum param );
285 static ALint (*_alGetInteger)( ALenum param );
286 static ALfloat (*_alGetFloat)( ALenum param );
287 static ALdouble (*_alGetDouble)( ALenum param );
288 static ALenum (*_alGetError)( void );
289 static ALboolean (*_alIsExtensionPresent)( const ALchar* extname );
290 static void* (*_alGetProcAddress)( const ALchar* fname );
291 static ALenum (*_alGetEnumValue)( const ALchar* ename );
292 static void (*_alListenerf)( ALenum param, ALfloat value );
293 static void (*_alListener3f)( ALenum param, ALfloat value1, ALfloat value2, ALfloat value3 );
294 static void (*_alListenerfv)( ALenum param, const ALfloat* values );
295 static void (*_alListeneri)( ALenum param, ALint value );
296 static void (*_alListener3i)( ALenum param, ALint value1, ALint value2, ALint value3 );
297 static void (*_alListeneriv)( ALenum param, const ALint* values );
298 static void (*_alGetListenerf)( ALenum param, ALfloat* value );
299 static void (*_alGetListener3f)( ALenum param, ALfloat *value1, ALfloat *value2, ALfloat *value3 );
300 static void (*_alGetListenerfv)( ALenum param, ALfloat* values );
301 static void (*_alGetListeneri)( ALenum param, ALint* value );
302 static void (*_alGetListener3i)( ALenum param, ALint *value1, ALint *value2, ALint *value3 );
303 static void (*_alGetListeneriv)( ALenum param, ALint* values );
304 static void (*_alGenSources)( ALsizei n, ALuint* sources );
305 static void (*_alDeleteSources)( ALsizei n, const ALuint* sources );
306 static ALboolean (*_alIsSource)( ALuint sid );
307 static void (*_alSourcef)( ALuint sid, ALenum param, ALfloat value );
308 static void (*_alSource3f)( ALuint sid, ALenum param, ALfloat value1, ALfloat value2, ALfloat value3 );
309 static void (*_alSourcefv)( ALuint sid, ALenum param, const ALfloat* values );
310 static void (*_alSourcei)( ALuint sid, ALenum param, ALint value );
311 static void (*_alSource3i)( ALuint sid, ALenum param, ALint value1, ALint value2, ALint value3 );
312 static void (*_alSourceiv)( ALuint sid, ALenum param, const ALint* values );
313 static void (*_alGetSourcef)( ALuint sid, ALenum param, ALfloat* value );
314 static void (*_alGetSource3f)( ALuint sid, ALenum param, ALfloat* value1, ALfloat* value2, ALfloat* value3);
315 static void (*_alGetSourcefv)( ALuint sid, ALenum param, ALfloat* values );
316 static void (*_alGetSourcei)( ALuint sid, ALenum param, ALint* value );
317 static void (*_alGetSource3i)( ALuint sid, ALenum param, ALint* value1, ALint* value2, ALint* value3);
318 static void (*_alGetSourceiv)( ALuint sid, ALenum param, ALint* values );
319 static void (*_alSourcePlayv)( ALsizei ns, const ALuint *sids );
320 static void (*_alSourceStopv)( ALsizei ns, const ALuint *sids );
321 static void (*_alSourceRewindv)( ALsizei ns, const ALuint *sids );
322 static void (*_alSourcePausev)( ALsizei ns, const ALuint *sids );
323 static void (*_alSourcePlay)( ALuint sid );
324 static void (*_alSourceStop)( ALuint sid );
325 static void (*_alSourceRewind)( ALuint sid );
326 static void (*_alSourcePause)( ALuint sid );
327 static void (*_alSourceQueueBuffers)( ALuint sid, ALsizei numEntries, const ALuint *bids );
328 static void (*_alSourceUnqueueBuffers)( ALuint sid, ALsizei numEntries, ALuint *bids );
329 static void (*_alGenBuffers)( ALsizei n, ALuint* buffers );
330 static void (*_alDeleteBuffers)( ALsizei n, const ALuint* buffers );
331 static ALboolean (*_alIsBuffer)( ALuint bid );
332 static void (*_alBufferData)( ALuint bid, ALenum format, const ALvoid* data, ALsizei size, ALsizei freq );
333 static void (*_alBufferf)( ALuint bid, ALenum param, ALfloat value );
334 static void (*_alBuffer3f)( ALuint bid, ALenum param, ALfloat value1, ALfloat value2, ALfloat value3 );
335 static void (*_alBufferfv)( ALuint bid, ALenum param, const ALfloat* values );
336 static void (*_alBufferi)( ALuint bid, ALenum param, ALint value );
337 static void (*_alBuffer3i)( ALuint bid, ALenum param, ALint value1, ALint value2, ALint value3 );
338 static void (*_alBufferiv)( ALuint bid, ALenum param, const ALint* values );
339 static void (*_alGetBufferf)( ALuint bid, ALenum param, ALfloat* value );
340 static void (*_alGetBuffer3f)( ALuint bid, ALenum param, ALfloat* value1, ALfloat* value2, ALfloat* value3);
341 static void (*_alGetBufferfv)( ALuint bid, ALenum param, ALfloat* values );
342 static void (*_alGetBufferi)( ALuint bid, ALenum param, ALint* value );
343 static void (*_alGetBuffer3i)( ALuint bid, ALenum param, ALint* value1, ALint* value2, ALint* value3);
344 static void (*_alGetBufferiv)( ALuint bid, ALenum param, ALint* values );
345 static void (*_alDopplerFactor)( ALfloat value );
346 static void (*_alDopplerVelocity)( ALfloat value );
347 static void (*_alSpeedOfSound)( ALfloat value );
348 static void (*_alDistanceModel)( ALenum distanceModel );
349 static ALCcontext * (*_alcCreateContext)( ALCdevice *device, const ALCint* attrlist );
350 static ALCboolean (*_alcMakeContextCurrent)( ALCcontext *context );
351 static void (*_alcProcessContext)( ALCcontext *context );
352 static void (*_alcSuspendContext)( ALCcontext *context );
353 static void (*_alcDestroyContext)( ALCcontext *context );
354 static ALCcontext * (*_alcGetCurrentContext)( void );
355 static ALCdevice* (*_alcGetContextsDevice)( ALCcontext *context );
356 static ALCdevice * (*_alcOpenDevice)( const ALCchar *devicename );
357 static ALCboolean (*_alcCloseDevice)( ALCdevice *device );
358 static ALCenum (*_alcGetError)( ALCdevice *device );
359 static ALCboolean (*_alcIsExtensionPresent)( ALCdevice *device, const ALCchar *extname );
360 static void * (*_alcGetProcAddress)( ALCdevice *device, const ALCchar *funcname );
361 static ALCenum (*_alcGetEnumValue)( ALCdevice *device, const ALCchar *enumname );
362 static const ALCchar * (*_alcGetString)( ALCdevice *device, ALCenum param );
363 static void (*_alcGetIntegerv)( ALCdevice *device, ALCenum param, ALCsizei size, ALCint *data );
364 static ALCdevice* (*_alcCaptureOpenDevice)( const ALCchar *devicename, ALCuint frequency, ALCenum format, ALCsizei buffersize );
365 static ALCboolean (*_alcCaptureCloseDevice)( ALCdevice *device );
366 static void (*_alcCaptureStart)( ALCdevice *device );
367 static void (*_alcCaptureStop)( ALCdevice *device );
368 static void (*_alcCaptureSamples)( ALCdevice *device, ALCvoid *buffer, ALCsizei samples );
370 static bool linkit(void **target, const char *symbol, void *handle) {
371 *target = dlsym(handle, symbol);
372 if (!*target) {
373 printf("Failed to link %s\n", symbol);
374 return false;
376 return true;
379 static bool init_openal() {
380 void *handle = dlopen("libopenal.so", RTLD_LAZY);
381 if (!handle) handle = dlopen("libopenal.so.1", RTLD_LAZY);
382 if (!handle) return false;
384 if (!linkit((void**)&_alEnable, "alEnable", handle)) return false;
385 if (!linkit((void**)&_alDisable, "alDisable", handle)) return false;
386 if (!linkit((void**)&_alIsEnabled, "alIsEnabled", handle)) return false;
387 if (!linkit((void**)&_alGetString, "alGetString", handle)) return false;
388 if (!linkit((void**)&_alGetBooleanv, "alGetBooleanv", handle)) return false;
389 if (!linkit((void**)&_alGetIntegerv, "alGetIntegerv", handle)) return false;
390 if (!linkit((void**)&_alGetFloatv, "alGetFloatv", handle)) return false;
391 if (!linkit((void**)&_alGetDoublev, "alGetDoublev", handle)) return false;
392 if (!linkit((void**)&_alGetBoolean, "alGetBoolean", handle)) return false;
393 if (!linkit((void**)&_alGetInteger, "alGetInteger", handle)) return false;
394 if (!linkit((void**)&_alGetFloat, "alGetFloat", handle)) return false;
395 if (!linkit((void**)&_alGetDouble, "alGetDouble", handle)) return false;
396 if (!linkit((void**)&_alGetError, "alGetError", handle)) return false;
397 if (!linkit((void**)&_alIsExtensionPresent, "alIsExtensionPresent", handle)) return false;
398 if (!linkit((void**)&_alGetProcAddress, "alGetProcAddress", handle)) return false;
399 if (!linkit((void**)&_alGetEnumValue, "alGetEnumValue", handle)) return false;
400 if (!linkit((void**)&_alListenerf, "alListenerf", handle)) return false;
401 if (!linkit((void**)&_alListener3f, "alListener3f", handle)) return false;
402 if (!linkit((void**)&_alListenerfv, "alListenerfv", handle)) return false;
403 if (!linkit((void**)&_alListeneri, "alListeneri", handle)) return false;
404 if (!linkit((void**)&_alListener3i, "alListener3i", handle)) return false;
405 if (!linkit((void**)&_alListeneriv, "alListeneriv", handle)) return false;
406 if (!linkit((void**)&_alGetListenerf, "alGetListenerf", handle)) return false;
407 if (!linkit((void**)&_alGetListener3f, "alGetListener3f", handle)) return false;
408 if (!linkit((void**)&_alGetListenerfv, "alGetListenerfv", handle)) return false;
409 if (!linkit((void**)&_alGetListeneri, "alGetListeneri", handle)) return false;
410 if (!linkit((void**)&_alGetListener3i, "alGetListener3i", handle)) return false;
411 if (!linkit((void**)&_alGetListeneriv, "alGetListeneriv", handle)) return false;
412 if (!linkit((void**)&_alGenSources, "alGenSources", handle)) return false;
413 if (!linkit((void**)&_alDeleteSources, "alDeleteSources", handle)) return false;
414 if (!linkit((void**)&_alIsSource, "alIsSource", handle)) return false;
415 if (!linkit((void**)&_alSourcef, "alSourcef", handle)) return false;
416 if (!linkit((void**)&_alSource3f, "alSource3f", handle)) return false;
417 if (!linkit((void**)&_alSourcefv, "alSourcefv", handle)) return false;
418 if (!linkit((void**)&_alSourcei, "alSourcei", handle)) return false;
419 if (!linkit((void**)&_alSource3i, "alSource3i", handle)) return false;
420 if (!linkit((void**)&_alSourceiv, "alSourceiv", handle)) return false;
421 if (!linkit((void**)&_alGetSourcef, "alGetSourcef", handle)) return false;
422 if (!linkit((void**)&_alGetSource3f, "alGetSource3f", handle)) return false;
423 if (!linkit((void**)&_alGetSourcefv, "alGetSourcefv", handle)) return false;
424 if (!linkit((void**)&_alGetSourcei, "alGetSourcei", handle)) return false;
425 if (!linkit((void**)&_alGetSource3i, "alGetSource3i", handle)) return false;
426 if (!linkit((void**)&_alGetSourceiv, "alGetSourceiv", handle)) return false;
427 if (!linkit((void**)&_alSourcePlayv, "alSourcePlayv", handle)) return false;
428 if (!linkit((void**)&_alSourceStopv, "alSourceStopv", handle)) return false;
429 if (!linkit((void**)&_alSourceRewindv, "alSourceRewindv", handle)) return false;
430 if (!linkit((void**)&_alSourcePausev, "alSourcePausev", handle)) return false;
431 if (!linkit((void**)&_alSourcePlay, "alSourcePlay", handle)) return false;
432 if (!linkit((void**)&_alSourceStop, "alSourceStop", handle)) return false;
433 if (!linkit((void**)&_alSourceRewind, "alSourceRewind", handle)) return false;
434 if (!linkit((void**)&_alSourcePause, "alSourcePause", handle)) return false;
435 if (!linkit((void**)&_alSourceQueueBuffers, "alSourceQueueBuffers", handle)) return false;
436 if (!linkit((void**)&_alSourceUnqueueBuffers, "alSourceUnqueueBuffers", handle)) return false;
437 if (!linkit((void**)&_alGenBuffers, "alGenBuffers", handle)) return false;
438 if (!linkit((void**)&_alDeleteBuffers, "alDeleteBuffers", handle)) return false;
439 if (!linkit((void**)&_alIsBuffer, "alIsBuffer", handle)) return false;
440 if (!linkit((void**)&_alBufferData, "alBufferData", handle)) return false;
441 if (!linkit((void**)&_alBufferf, "alBufferf", handle)) return false;
442 if (!linkit((void**)&_alBuffer3f, "alBuffer3f", handle)) return false;
443 if (!linkit((void**)&_alBufferfv, "alBufferfv", handle)) return false;
444 if (!linkit((void**)&_alBufferi, "alBufferi", handle)) return false;
445 if (!linkit((void**)&_alBuffer3i, "alBuffer3i", handle)) return false;
446 if (!linkit((void**)&_alBufferiv, "alBufferiv", handle)) return false;
447 if (!linkit((void**)&_alGetBufferf, "alGetBufferf", handle)) return false;
448 if (!linkit((void**)&_alGetBuffer3f, "alGetBuffer3f", handle)) return false;
449 if (!linkit((void**)&_alGetBufferfv, "alGetBufferfv", handle)) return false;
450 if (!linkit((void**)&_alGetBufferi, "alGetBufferi", handle)) return false;
451 if (!linkit((void**)&_alGetBuffer3i, "alGetBuffer3i", handle)) return false;
452 if (!linkit((void**)&_alGetBufferiv, "alGetBufferiv", handle)) return false;
453 if (!linkit((void**)&_alDopplerFactor, "alDopplerFactor", handle)) return false;
454 if (!linkit((void**)&_alDopplerVelocity, "alDopplerVelocity", handle)) return false;
455 if (!linkit((void**)&_alSpeedOfSound, "alSpeedOfSound", handle)) return false;
456 if (!linkit((void**)&_alDistanceModel, "alDistanceModel", handle)) return false;
457 if (!linkit((void**)&_alcCreateContext, "alcCreateContext", handle)) return false;
458 if (!linkit((void**)&_alcMakeContextCurrent, "alcMakeContextCurrent", handle)) return false;
459 if (!linkit((void**)&_alcProcessContext, "alcProcessContext", handle)) return false;
460 if (!linkit((void**)&_alcSuspendContext, "alcSuspendContext", handle)) return false;
461 if (!linkit((void**)&_alcDestroyContext, "alcDestroyContext", handle)) return false;
462 if (!linkit((void**)&_alcGetCurrentContext, "alcGetCurrentContext", handle)) return false;
463 if (!linkit((void**)&_alcGetContextsDevice, "alcGetContextsDevice", handle)) return false;
464 if (!linkit((void**)&_alcOpenDevice, "alcOpenDevice", handle)) return false;
465 if (!linkit((void**)&_alcCloseDevice, "alcCloseDevice", handle)) return false;
466 if (!linkit((void**)&_alcGetError, "alcGetError", handle)) return false;
467 if (!linkit((void**)&_alcIsExtensionPresent, "alcIsExtensionPresent", handle)) return false;
468 if (!linkit((void**)&_alcGetProcAddress, "alcGetProcAddress", handle)) return false;
469 if (!linkit((void**)&_alcGetEnumValue, "alcGetEnumValue", handle)) return false;
470 if (!linkit((void**)&_alcGetString, "alcGetString", handle)) return false;
471 if (!linkit((void**)&_alcGetIntegerv, "alcGetIntegerv", handle)) return false;
472 if (!linkit((void**)&_alcCaptureOpenDevice, "alcCaptureOpenDevice", handle)) return false;
473 if (!linkit((void**)&_alcCaptureCloseDevice, "alcCaptureCloseDevice", handle)) return false;
474 if (!linkit((void**)&_alcCaptureStart, "alcCaptureStart", handle)) return false;
475 if (!linkit((void**)&_alcCaptureStop, "alcCaptureStop", handle)) return false;
476 if (!linkit((void**)&_alcCaptureSamples, "alcCaptureSamples", handle)) return false;
478 return true;
482 void alEnable( ALenum capability ) { _alEnable(capability); }
483 void alDisable( ALenum capability ) { _alDisable(capability); }
484 ALboolean alIsEnabled( ALenum capability ) { _alIsEnabled(capability); }
485 const ALchar* alGetString( ALenum param ) { return _alGetString(param); }
486 void alGetBooleanv( ALenum param, ALboolean* data ) { _alGetBooleanv(param, data); }
487 void alGetIntegerv( ALenum param, ALint* data ) { _alGetIntegerv(param, data); }
488 void alGetFloatv( ALenum param, ALfloat* data ) { _alGetFloatv(param, data); }
489 void alGetDoublev( ALenum param, ALdouble* data ) { _alGetDoublev(param, data); }
490 ALboolean alGetBoolean( ALenum param ) { return _alGetBoolean(param); }
491 ALint alGetInteger( ALenum param ) { return _alGetInteger(param); }
492 ALfloat alGetFloat( ALenum param ) { return _alGetFloat(param); }
493 ALdouble alGetDouble( ALenum param ) { return _alGetDouble(param); }
494 ALenum alGetError( void ) { _alGetError(); }
495 ALboolean alIsExtensionPresent( const ALchar* extname ) { return _alIsExtensionPresent(extname); }
496 void* alGetProcAddress( const ALchar* fname ) { return _alGetProcAddress(fname); }
497 ALenum alGetEnumValue( const ALchar* ename ) { return _alGetEnumValue(ename); }
498 void alListenerf( ALenum param, ALfloat value ) { return _alListenerf(param, value); }
499 void alListener3f( ALenum param, ALfloat value1, ALfloat value2, ALfloat value3 ) { return _alListener3f(param, value1, value2, value3); }
500 void alListenerfv( ALenum param, const ALfloat* values ) { return _alListenerfv(param, values); }
501 void alListeneri( ALenum param, ALint value ) { return _alListeneri(param, value); }
502 void alListener3i( ALenum param, ALint value1, ALint value2, ALint value3 ) { return _alListener3i(param, value1, value2, value3); }
503 void alListeneriv( ALenum param, const ALint* values ) { return _alListeneriv(param, values); }
504 void alGetListenerf( ALenum param, ALfloat* value ) { return _alGetListenerf(param, value); }
505 void alGetListener3f( ALenum param, ALfloat *value1, ALfloat *value2, ALfloat *value3 ) { return _alGetListener3f(param, value1, value2, value3); }
506 void alGetListenerfv( ALenum param, ALfloat* values ) { return _alGetListenerfv(param, values); }
507 void alGetListeneri( ALenum param, ALint* value ) { return _alGetListeneri(param, value); }
508 void alGetListener3i( ALenum param, ALint *value1, ALint *value2, ALint *value3 ) { return _alGetListener3i(param, value1, value2, value3); }
509 void alGetListeneriv( ALenum param, ALint* values ) { return _alGetListeneriv(param, values); }
510 void alGenSources( ALsizei n, ALuint* sources ) { return _alGenSources(n, sources); }
511 void alDeleteSources( ALsizei n, const ALuint* sources ) { return _alDeleteSources(n, sources); }
512 ALboolean alIsSource( ALuint sid ) { return _alIsSource(sid); }
513 void alSourcef( ALuint sid, ALenum param, ALfloat value ) { return _alSourcef(sid, param, value); }
514 void alSource3f( ALuint sid, ALenum param, ALfloat value1, ALfloat value2, ALfloat value3 ) { return _alSource3f(sid, param, value1, value2, value3); }
515 void alSourcefv( ALuint sid, ALenum param, const ALfloat* values ) { return _alSourcefv(sid, param, values); }
516 void alSourcei( ALuint sid, ALenum param, ALint value ) { return _alSourcei(sid, param, value); }
517 void alSource3i( ALuint sid, ALenum param, ALint value1, ALint value2, ALint value3 ) { return _alSource3i(sid, param, value1, value2, value3); }
518 void alSourceiv( ALuint sid, ALenum param, const ALint* values ) { return _alSourceiv(sid, param, values); }
519 void alGetSourcef( ALuint sid, ALenum param, ALfloat* value ) { return _alGetSourcef(sid, param, value); }
520 void alGetSource3f( ALuint sid, ALenum param, ALfloat* value1, ALfloat* value2, ALfloat* value3) { return _alGetSource3f(sid, param, value1, value2, value3); }
521 void alGetSourcefv( ALuint sid, ALenum param, ALfloat* values ) { return _alGetSourcefv(sid, param, values); }
522 void alGetSourcei( ALuint sid, ALenum param, ALint* value ) { return _alGetSourcei(sid, param, value); }
523 void alGetSource3i( ALuint sid, ALenum param, ALint* value1, ALint* value2, ALint* value3) { return _alGetSource3i(sid, param, value1, value2, value3); }
524 void alGetSourceiv( ALuint sid, ALenum param, ALint* values ) { return _alGetSourceiv(sid, param, values); }
525 void alSourcePlayv( ALsizei ns, const ALuint *sids ) { return _alSourcePlayv(ns, sids); }
526 void alSourceStopv( ALsizei ns, const ALuint *sids ) { return _alSourceStopv(ns, sids); }
527 void alSourceRewindv( ALsizei ns, const ALuint *sids ) { return _alSourceRewindv(ns, sids); }
528 void alSourcePausev( ALsizei ns, const ALuint *sids ) { return _alSourcePausev(ns, sids); }
529 void alSourcePlay( ALuint sid ) { return _alSourcePlay(sid); }
530 void alSourceStop( ALuint sid ) { return _alSourceStop(sid); }
531 void alSourceRewind( ALuint sid ) { return _alSourceRewind(sid); }
532 void alSourcePause( ALuint sid ) { return _alSourcePause(sid); }
533 void alSourceQueueBuffers( ALuint sid, ALsizei numEntries, const ALuint *bids ) { return _alSourceQueueBuffers(sid, numEntries, bids); }
534 void alSourceUnqueueBuffers( ALuint sid, ALsizei numEntries, ALuint *bids ) { return _alSourceUnqueueBuffers(sid, numEntries, bids); }
535 void alGenBuffers( ALsizei n, ALuint* buffers ) { return _alGenBuffers(n, buffers); }
536 void alDeleteBuffers( ALsizei n, const ALuint* buffers ) { return _alDeleteBuffers(n, buffers); }
537 ALboolean alIsBuffer( ALuint bid ) { return _alIsBuffer(bid); }
538 void alBufferData( ALuint bid, ALenum format, const ALvoid* data, ALsizei size, ALsizei freq ) { return _alBufferData(bid, format, data, size, freq); }
539 void alBufferf( ALuint bid, ALenum param, ALfloat value ) { return _alBufferf(bid, param, value); }
540 void alBuffer3f( ALuint bid, ALenum param, ALfloat value1, ALfloat value2, ALfloat value3 ) { return _alBuffer3f(bid, param, value1, value2, value3); }
541 void alBufferfv( ALuint bid, ALenum param, const ALfloat* values ) { return _alBufferfv(bid, param, values); }
542 void alBufferi( ALuint bid, ALenum param, ALint value ) { return _alBufferi(bid, param, value); }
543 void alBuffer3i( ALuint bid, ALenum param, ALint value1, ALint value2, ALint value3 ) { return _alBuffer3i(bid, param, value1, value2, value3); }
544 void alBufferiv( ALuint bid, ALenum param, const ALint* values ) { return _alBufferiv(bid, param, values); }
545 void alGetBufferf( ALuint bid, ALenum param, ALfloat* value ) { return _alGetBufferf(bid, param, value); }
546 void alGetBuffer3f( ALuint bid, ALenum param, ALfloat* value1, ALfloat* value2, ALfloat* value3) { return _alGetBuffer3f(bid, param, value1, value2, value3); }
547 void alGetBufferfv( ALuint bid, ALenum param, ALfloat* values ) { return _alGetBufferfv(bid, param, values); }
548 void alGetBufferi( ALuint bid, ALenum param, ALint* value ) { return _alGetBufferi(bid, param, value); }
549 void alGetBuffer3i( ALuint bid, ALenum param, ALint* value1, ALint* value2, ALint* value3) { return _alGetBuffer3i(bid, param, value1, value2, value3); }
550 void alGetBufferiv( ALuint bid, ALenum param, ALint* values ) { return _alGetBufferiv(bid, param, values); }
551 void alDopplerFactor( ALfloat value ) { return _alDopplerFactor(value); }
552 void alDopplerVelocity( ALfloat value ) { return _alDopplerVelocity(value); }
553 void alSpeedOfSound( ALfloat value ) { return _alSpeedOfSound(value); }
554 void alDistanceModel( ALenum distanceModel ) { return _alDistanceModel(distanceModel); }
555 ALCcontext * alcCreateContext( ALCdevice *device, const ALCint* attrlist ) { return _alcCreateContext(device, attrlist); }
556 ALCboolean alcMakeContextCurrent( ALCcontext *context ) { return _alcMakeContextCurrent(context); }
557 void alcProcessContext( ALCcontext *context ) { return _alcProcessContext(context); }
558 void alcSuspendContext( ALCcontext *context ) { return _alcSuspendContext(context); }
559 void alcDestroyContext( ALCcontext *context ) { return _alcDestroyContext(context); }
560 ALCcontext * alcGetCurrentContext( void ) { return _alcGetCurrentContext(); }
561 ALCdevice* alcGetContextsDevice( ALCcontext *context ) { return _alcGetContextsDevice(context); }
562 ALCdevice * alcOpenDevice( const ALCchar *devicename ) { return _alcOpenDevice(devicename); }
563 ALCboolean alcCloseDevice( ALCdevice *device ) { return _alcCloseDevice(device); }
564 ALCenum alcGetError( ALCdevice *device ) { return _alcGetError(device); }
565 ALCboolean alcIsExtensionPresent( ALCdevice *device, const ALCchar *extname ) { return _alcIsExtensionPresent(device, extname); }
566 void * alcGetProcAddress( ALCdevice *device, const ALCchar *funcname ) { return _alcGetProcAddress(device, funcname); }
567 ALCenum alcGetEnumValue( ALCdevice *device, const ALCchar *enumname ) { return _alcGetEnumValue(device, enumname); }
568 const ALCchar * alcGetString( ALCdevice *device, ALCenum param ) { return _alcGetString(device, param); }
569 void alcGetIntegerv( ALCdevice *device, ALCenum param, ALCsizei size, ALCint *data ) { return _alcGetIntegerv(device, param, size, data); }
570 ALCdevice* alcCaptureOpenDevice( const ALCchar *devicename, ALCuint frequency, ALCenum format, ALCsizei buffersize ) { return _alcCaptureOpenDevice(devicename, frequency, format, buffersize); }
571 ALCboolean alcCaptureCloseDevice( ALCdevice *device ) { return _alcCaptureCloseDevice(device); }
572 void alcCaptureStart( ALCdevice *device ) { return _alcCaptureStart(device); }
573 void alcCaptureStop( ALCdevice *device ) { return _alcCaptureStop(device); }
574 void alcCaptureSamples( ALCdevice *device, ALCvoid *buffer, ALCsizei samples ) { return _alcCaptureSamples(device, buffer, samples); }
577 static SNDFILE* (*_sf_open) (const char *path, int mode, SF_INFO *sfinfo);
578 static SNDFILE* (*_sf_open_fd) (int fd, int mode, SF_INFO *sfinfo, int close_desc);
579 static SNDFILE* (*_sf_open_virtual) (SF_VIRTUAL_IO *sfvirtual, int mode, SF_INFO *sfinfo, void *user_data);
580 static int (*_sf_error) (SNDFILE *sndfile);
581 static const char* (*_sf_strerror) (SNDFILE *sndfile);
582 static const char* (*_sf_error_number) (int errnum);
583 static int (*_sf_perror) (SNDFILE *sndfile);
584 static int (*_sf_error_str) (SNDFILE *sndfile, char* str, size_t len);
585 static int (*_sf_command) (SNDFILE *sndfile, int command, void *data, int datasize);
586 static int (*_sf_format_check) (const SF_INFO *info);
587 static sf_count_t (*_sf_seek) (SNDFILE *sndfile, sf_count_t frames, int whence);
588 static int (*_sf_set_string) (SNDFILE *sndfile, int str_type, const char* str);
589 static const char* (*_sf_get_string) (SNDFILE *sndfile, int str_type);
590 static const char * (*_sf_version_string) (void);
591 static sf_count_t (*_sf_read_raw) (SNDFILE *sndfile, void *ptr, sf_count_t bytes);
592 static sf_count_t (*_sf_write_raw) (SNDFILE *sndfile, const void *ptr, sf_count_t bytes);
593 static sf_count_t (*_sf_readf_short) (SNDFILE *sndfile, short *ptr, sf_count_t frames);
594 static sf_count_t (*_sf_writef_short) (SNDFILE *sndfile, const short *ptr, sf_count_t frames);
595 static sf_count_t (*_sf_readf_int) (SNDFILE *sndfile, int *ptr, sf_count_t frames);
596 static sf_count_t (*_sf_writef_int) (SNDFILE *sndfile, const int *ptr, sf_count_t frames);
597 static sf_count_t (*_sf_readf_float) (SNDFILE *sndfile, float *ptr, sf_count_t frames);
598 static sf_count_t (*_sf_writef_float) (SNDFILE *sndfile, const float *ptr, sf_count_t frames);
599 static sf_count_t (*_sf_readf_double) (SNDFILE *sndfile, double *ptr, sf_count_t frames);
600 static sf_count_t (*_sf_writef_double) (SNDFILE *sndfile, const double *ptr, sf_count_t frames);
601 static sf_count_t (*_sf_read_short) (SNDFILE *sndfile, short *ptr, sf_count_t items);
602 static sf_count_t (*_sf_write_short) (SNDFILE *sndfile, const short *ptr, sf_count_t items);
603 static sf_count_t (*_sf_read_int) (SNDFILE *sndfile, int *ptr, sf_count_t items);
604 static sf_count_t (*_sf_write_int) (SNDFILE *sndfile, const int *ptr, sf_count_t items);
605 static sf_count_t (*_sf_read_float) (SNDFILE *sndfile, float *ptr, sf_count_t items);
606 static sf_count_t (*_sf_write_float) (SNDFILE *sndfile, const float *ptr, sf_count_t items);
607 static sf_count_t (*_sf_read_double) (SNDFILE *sndfile, double *ptr, sf_count_t items);
608 static sf_count_t (*_sf_write_double) (SNDFILE *sndfile, const double *ptr, sf_count_t items);
609 static int (*_sf_close) (SNDFILE *sndfile);
610 static void (*_sf_write_sync) (SNDFILE *sndfile);
612 static bool init_sndfile() {
613 void *handle = dlopen("libsndfile.so", RTLD_LAZY);
614 if (!handle) handle = dlopen("libsndfile.so.1", RTLD_LAZY);
615 if (!handle) return false;
617 if (!linkit((void**)&_sf_open, "sf_open", handle)) return false;
618 if (!linkit((void**)&_sf_open_fd, "sf_open_fd", handle)) return false;
619 if (!linkit((void**)&_sf_open_virtual, "sf_open_virtual", handle)) return false;
620 if (!linkit((void**)&_sf_error, "sf_error", handle)) return false;
621 if (!linkit((void**)&_sf_strerror, "sf_strerror", handle)) return false;
622 if (!linkit((void**)&_sf_error_number, "sf_error_number", handle)) return false;
623 if (!linkit((void**)&_sf_perror, "sf_perror", handle)) return false;
624 if (!linkit((void**)&_sf_error_str, "sf_error_str", handle)) return false;
625 if (!linkit((void**)&_sf_command, "sf_command", handle)) return false;
626 if (!linkit((void**)&_sf_format_check, "sf_format_check", handle)) return false;
627 if (!linkit((void**)&_sf_seek, "sf_seek", handle)) return false;
628 if (!linkit((void**)&_sf_set_string, "sf_set_string", handle)) return false;
629 if (!linkit((void**)&_sf_get_string, "sf_get_string", handle)) return false;
630 if (!linkit((void**)&_sf_version_string, "sf_version_string", handle)) return false;
631 if (!linkit((void**)&_sf_read_raw, "sf_read_raw", handle)) return false;
632 if (!linkit((void**)&_sf_write_raw, "sf_write_raw", handle)) return false;
633 if (!linkit((void**)&_sf_readf_short, "sf_readf_short", handle)) return false;
634 if (!linkit((void**)&_sf_writef_short, "sf_writef_short", handle)) return false;
635 if (!linkit((void**)&_sf_readf_int, "sf_readf_int", handle)) return false;
636 if (!linkit((void**)&_sf_writef_int, "sf_writef_int", handle)) return false;
637 if (!linkit((void**)&_sf_readf_float, "sf_readf_float", handle)) return false;
638 if (!linkit((void**)&_sf_writef_float, "sf_writef_float", handle)) return false;
639 if (!linkit((void**)&_sf_readf_double, "sf_readf_double", handle)) return false;
640 if (!linkit((void**)&_sf_writef_double, "sf_writef_double", handle)) return false;
641 if (!linkit((void**)&_sf_read_short, "sf_read_short", handle)) return false;
642 if (!linkit((void**)&_sf_write_short, "sf_write_short", handle)) return false;
643 if (!linkit((void**)&_sf_read_int, "sf_read_int", handle)) return false;
644 if (!linkit((void**)&_sf_write_int, "sf_write_int", handle)) return false;
645 if (!linkit((void**)&_sf_read_float, "sf_read_float", handle)) return false;
646 if (!linkit((void**)&_sf_write_float, "sf_write_float", handle)) return false;
647 if (!linkit((void**)&_sf_read_double, "sf_read_double", handle)) return false;
648 if (!linkit((void**)&_sf_write_double, "sf_write_double", handle)) return false;
649 if (!linkit((void**)&_sf_close, "sf_close", handle)) return false;
650 if (!linkit((void**)&_sf_write_sync, "sf_write_sync", handle)) return false;
652 return true;
656 SNDFILE* sf_open (const char *path, int mode, SF_INFO *sfinfo) { return _sf_open(path, mode, sfinfo); }
657 SNDFILE* sf_open_fd (int fd, int mode, SF_INFO *sfinfo, int close_desc) { return _sf_open_fd(fd, mode, sfinfo, close_desc); }
658 SNDFILE* sf_open_virtual (SF_VIRTUAL_IO *sfvirtual, int mode, SF_INFO *sfinfo, void *user_data) { return _sf_open_virtual(sfvirtual, mode, sfinfo, user_data); }
659 int sf_error (SNDFILE *sndfile) { return _sf_error(sndfile); }
660 const char* sf_strerror (SNDFILE *sndfile) { return _sf_strerror(sndfile); }
661 const char* sf_error_number (int errnum) { return _sf_error_number(errnum); }
662 int sf_perror (SNDFILE *sndfile) { return _sf_perror(sndfile); }
663 int sf_error_str (SNDFILE *sndfile, char* str, size_t len) { return _sf_error_str(sndfile, str, len); }
664 int sf_command (SNDFILE *sndfile, int command, void *data, int datasize) { return _sf_command(sndfile, command, data, datasize); }
665 int sf_format_check (const SF_INFO *info) { return _sf_format_check(info); }
666 sf_count_t sf_seek (SNDFILE *sndfile, sf_count_t frames, int whence) { return _sf_seek(sndfile, frames, whence); }
667 int sf_set_string (SNDFILE *sndfile, int str_type, const char* str) { return _sf_set_string(sndfile, str_type, str); }
668 const char* sf_get_string (SNDFILE *sndfile, int str_type) { return _sf_get_string(sndfile, str_type); }
669 const char * sf_version_string (void) { return _sf_version_string(); }
670 sf_count_t sf_read_raw (SNDFILE *sndfile, void *ptr, sf_count_t bytes) { return _sf_read_raw(sndfile, ptr, bytes); }
671 sf_count_t sf_write_raw (SNDFILE *sndfile, const void *ptr, sf_count_t bytes) { return _sf_write_raw(sndfile, ptr, bytes); }
672 sf_count_t sf_readf_short (SNDFILE *sndfile, short *ptr, sf_count_t frames) { return _sf_readf_short(sndfile, ptr, frames); }
673 sf_count_t sf_writef_short (SNDFILE *sndfile, const short *ptr, sf_count_t frames) { return _sf_writef_short(sndfile, ptr, frames); }
674 sf_count_t sf_readf_int (SNDFILE *sndfile, int *ptr, sf_count_t frames) { return _sf_readf_int(sndfile, ptr, frames); }
675 sf_count_t sf_writef_int (SNDFILE *sndfile, const int *ptr, sf_count_t frames) { return _sf_writef_int(sndfile, ptr, frames); }
676 sf_count_t sf_readf_float (SNDFILE *sndfile, float *ptr, sf_count_t frames) { return _sf_readf_float(sndfile, ptr, frames); }
677 sf_count_t sf_writef_float (SNDFILE *sndfile, const float *ptr, sf_count_t frames) { return _sf_writef_float(sndfile, ptr, frames); }
678 sf_count_t sf_readf_double (SNDFILE *sndfile, double *ptr, sf_count_t frames) { return _sf_readf_double(sndfile, ptr, frames); }
679 sf_count_t sf_writef_double (SNDFILE *sndfile, const double *ptr, sf_count_t frames) { return _sf_writef_double(sndfile, ptr, frames); }
680 sf_count_t sf_read_short (SNDFILE *sndfile, short *ptr, sf_count_t items) { return _sf_read_short(sndfile, ptr, items); }
681 sf_count_t sf_write_short (SNDFILE *sndfile, const short *ptr, sf_count_t items) { return _sf_write_short(sndfile, ptr, items); }
682 sf_count_t sf_read_int (SNDFILE *sndfile, int *ptr, sf_count_t items) { return _sf_read_int(sndfile, ptr, items); }
683 sf_count_t sf_write_int (SNDFILE *sndfile, const int *ptr, sf_count_t items) { return _sf_write_int(sndfile, ptr, items); }
684 sf_count_t sf_read_float (SNDFILE *sndfile, float *ptr, sf_count_t items) { return _sf_read_float(sndfile, ptr, items); }
685 sf_count_t sf_write_float (SNDFILE *sndfile, const float *ptr, sf_count_t items) { return _sf_write_float(sndfile, ptr, items); }
686 sf_count_t sf_read_double (SNDFILE *sndfile, double *ptr, sf_count_t items) { return _sf_read_double(sndfile, ptr, items); }
687 sf_count_t sf_write_double (SNDFILE *sndfile, const double *ptr, sf_count_t items) { return _sf_write_double(sndfile, ptr, items); }
688 int sf_close (SNDFILE *sndfile) { return _sf_close(sndfile); }
689 void sf_write_sync (SNDFILE *sndfile) { return _sf_write_sync(sndfile); }