dbf-halloween2015

annotate src/audio/audio.cc @ 0:50683c78264e

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 01 Nov 2015 00:09:12 +0200
parents
children
rev   line source
nuclear@0 1 #include <stdio.h>
nuclear@0 2
nuclear@0 3 #include "openal.h"
nuclear@0 4 #include "audio.h"
nuclear@0 5
nuclear@0 6 static ALCdevice *dev;
nuclear@0 7 static ALCcontext *ctx;
nuclear@0 8
nuclear@0 9 bool init_audio()
nuclear@0 10 {
nuclear@0 11 if (dev) {
nuclear@0 12 // Already initiated.
nuclear@0 13 return true;
nuclear@0 14 }
nuclear@0 15 else if (!(dev = alcOpenDevice(0))) {
nuclear@0 16 fprintf(stderr, "failed to open OpenAL device\n");
nuclear@0 17 return false;
nuclear@0 18 }
nuclear@0 19
nuclear@0 20 if (ctx)
nuclear@0 21 {
nuclear@0 22 return true;
nuclear@0 23 }
nuclear@0 24 else if (!(ctx = alcCreateContext(dev, 0)))
nuclear@0 25 {
nuclear@0 26 fprintf(stderr, "failed to create context\n");
nuclear@0 27 alcCloseDevice(dev);
nuclear@0 28 return false;
nuclear@0 29 }
nuclear@0 30
nuclear@0 31 alcMakeContextCurrent(ctx);
nuclear@0 32
nuclear@0 33 // Clear error state.
nuclear@0 34 alGetError();
nuclear@0 35
nuclear@0 36 return true;
nuclear@0 37 }
nuclear@0 38
nuclear@0 39 void destroy_audio()
nuclear@0 40 {
nuclear@0 41 alcMakeContextCurrent(0);
nuclear@0 42
nuclear@0 43 if (ctx) {
nuclear@0 44 alcDestroyContext(ctx);
nuclear@0 45 }
nuclear@0 46
nuclear@0 47 if (dev) {
nuclear@0 48 alcCloseDevice(dev);
nuclear@0 49 }
nuclear@0 50 }
nuclear@0 51
nuclear@0 52 void set_audio_listener(const Matrix4x4 &xform)
nuclear@0 53 {
nuclear@0 54 float pos[3], orient[6];
nuclear@0 55
nuclear@0 56 pos[0] = xform[0][3];
nuclear@0 57 pos[1] = xform[1][3];
nuclear@0 58 pos[2] = xform[2][3];
nuclear@0 59
nuclear@0 60 orient[0] = xform[0][2];
nuclear@0 61 orient[1] = xform[1][2];
nuclear@0 62 orient[2] = -xform[2][2];
nuclear@0 63
nuclear@0 64 orient[3] = xform[0][1];
nuclear@0 65 orient[4] = xform[1][1];
nuclear@0 66 orient[5] = xform[2][1];
nuclear@0 67
nuclear@0 68 alListenerfv(AL_POSITION, pos);
nuclear@0 69 alListenerfv(AL_ORIENTATION, orient);
nuclear@0 70 }