| 1 | /* | 
|---|
| 2 | *   ORXONOX - the hottest 3D action shooter ever to exist | 
|---|
| 3 | * | 
|---|
| 4 | * | 
|---|
| 5 | *   License notice: | 
|---|
| 6 | * | 
|---|
| 7 | *   This program is free software; you can redistribute it and/or | 
|---|
| 8 | *   modify it under the terms of the GNU General Public License | 
|---|
| 9 | *   as published by the Free Software Foundation; either version 2 | 
|---|
| 10 | *   of the License, or (at your option) any later version. | 
|---|
| 11 | * | 
|---|
| 12 | *   This program is distributed in the hope that it will be useful, | 
|---|
| 13 | *   but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 14 | *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
| 15 | *   GNU General Public License for more details. | 
|---|
| 16 | * | 
|---|
| 17 | *   You should have received a copy of the GNU General Public License | 
|---|
| 18 | *   along with this program; if not, write to the Free Software | 
|---|
| 19 | *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA. | 
|---|
| 20 | * | 
|---|
| 21 | *   Author: | 
|---|
| 22 | *      ... | 
|---|
| 23 | *   Co-authors: | 
|---|
| 24 | *      ... | 
|---|
| 25 | * | 
|---|
| 26 | */ | 
|---|
| 27 |  | 
|---|
| 28 |  | 
|---|
| 29 | #include <iostream> | 
|---|
| 30 | #include <string> | 
|---|
| 31 |  | 
|---|
| 32 | #include "core/Debug.h" | 
|---|
| 33 | #include "AudioObject.h" | 
|---|
| 34 |  | 
|---|
| 35 | namespace audio | 
|---|
| 36 | { | 
|---|
| 37 |   AudioObject::AudioObject(std::string audioFile) | 
|---|
| 38 |   { | 
|---|
| 39 |     audioFile_ = audioFile; | 
|---|
| 40 |     SourcePos[0]=0; | 
|---|
| 41 |     SourcePos[1]=0; | 
|---|
| 42 |     SourcePos[2]=0; | 
|---|
| 43 |  | 
|---|
| 44 |     SourceVel[0]=0; | 
|---|
| 45 |     SourceVel[1]=0; | 
|---|
| 46 |     SourceVel[2]=0; | 
|---|
| 47 |  | 
|---|
| 48 |     ListenerPos[0]=0; | 
|---|
| 49 |     ListenerPos[1]=0; | 
|---|
| 50 |     ListenerPos[2]=0; | 
|---|
| 51 |  | 
|---|
| 52 |     ListenerVel[0]=0; | 
|---|
| 53 |     ListenerVel[1]=0; | 
|---|
| 54 |     ListenerVel[2]=0; | 
|---|
| 55 |  | 
|---|
| 56 |     ListenerOri[0]=0; | 
|---|
| 57 |     ListenerOri[1]=0; | 
|---|
| 58 |     ListenerOri[2]=-1; | 
|---|
| 59 |     ListenerOri[3]=0; | 
|---|
| 60 |     ListenerOri[4]=1; | 
|---|
| 61 |     ListenerOri[5]=0; | 
|---|
| 62 |  | 
|---|
| 63 |  | 
|---|
| 64 |     // Initialize OpenAL and clear the error bit. | 
|---|
| 65 |  | 
|---|
| 66 |     alutInit(NULL, 0); | 
|---|
| 67 |     alGetError(); | 
|---|
| 68 |  | 
|---|
| 69 |     // Load the wav data. | 
|---|
| 70 |  | 
|---|
| 71 |     if(LoadALData() == AL_FALSE) | 
|---|
| 72 |     { | 
|---|
| 73 |       printf("Error loading sound data."); | 
|---|
| 74 |  | 
|---|
| 75 |     } | 
|---|
| 76 |  | 
|---|
| 77 |     SetListenerValues(); | 
|---|
| 78 |     COUT(3) << "Info: Play sone ambient background sound"; | 
|---|
| 79 |   } | 
|---|
| 80 |  | 
|---|
| 81 |   AudioObject::~AudioObject() | 
|---|
| 82 |   { | 
|---|
| 83 |     KillALData(); | 
|---|
| 84 |   } | 
|---|
| 85 |  | 
|---|
| 86 |   ALboolean AudioObject::LoadALData() | 
|---|
| 87 |   { | 
|---|
| 88 |     ALenum format; | 
|---|
| 89 |     ALsizei size; | 
|---|
| 90 |     ALvoid* data; | 
|---|
| 91 |     ALsizei freq; | 
|---|
| 92 |     ALboolean loop; | 
|---|
| 93 |  | 
|---|
| 94 |  | 
|---|
| 95 |     alGenBuffers(1, &Buffer); | 
|---|
| 96 |  | 
|---|
| 97 |     if(alGetError() != AL_NO_ERROR) | 
|---|
| 98 |       return AL_FALSE; | 
|---|
| 99 |  | 
|---|
| 100 |     alutLoadWAVFile((ALbyte*)audioFile_.c_str(), &format, &data, &size, &freq, &loop); | 
|---|
| 101 |     alBufferData(Buffer, format, data, size, freq); | 
|---|
| 102 |     alutUnloadWAV(format, data, size, freq); | 
|---|
| 103 |  | 
|---|
| 104 |     alGenSources(1, &Source); | 
|---|
| 105 |  | 
|---|
| 106 |     if(alGetError() != AL_NO_ERROR) | 
|---|
| 107 |       return AL_FALSE; | 
|---|
| 108 |  | 
|---|
| 109 |     alSourcei (Source, AL_BUFFER,   Buffer   ); | 
|---|
| 110 |     alSourcef (Source, AL_PITCH,    1.0      ); | 
|---|
| 111 |     alSourcef (Source, AL_GAIN,     1.0      ); | 
|---|
| 112 |     alSourcefv(Source, AL_POSITION, SourcePos); | 
|---|
| 113 |     alSourcefv(Source, AL_VELOCITY, SourceVel); | 
|---|
| 114 |     alSourcei (Source, AL_LOOPING,  loop     ); | 
|---|
| 115 |  | 
|---|
| 116 |     if(alGetError() == AL_NO_ERROR) | 
|---|
| 117 |       return AL_TRUE; | 
|---|
| 118 |  | 
|---|
| 119 |  | 
|---|
| 120 |     return AL_FALSE; | 
|---|
| 121 |   } | 
|---|
| 122 |  | 
|---|
| 123 |   void AudioObject::SetListenerValues() | 
|---|
| 124 |   { | 
|---|
| 125 |     alListenerfv(AL_POSITION,    ListenerPos); | 
|---|
| 126 |     alListenerfv(AL_VELOCITY,    ListenerVel); | 
|---|
| 127 |     alListenerfv(AL_ORIENTATION, ListenerOri); | 
|---|
| 128 |   } | 
|---|
| 129 |  | 
|---|
| 130 |   void AudioObject::KillALData() | 
|---|
| 131 |   { | 
|---|
| 132 |     alDeleteBuffers(1, &Buffer); | 
|---|
| 133 |     alDeleteSources(1, &Source); | 
|---|
| 134 |     alutExit(); | 
|---|
| 135 |   } | 
|---|
| 136 |  | 
|---|
| 137 |   void AudioObject::play() | 
|---|
| 138 |   { | 
|---|
| 139 |     alSourcePlay(Source); | 
|---|
| 140 |   } | 
|---|
| 141 |  | 
|---|
| 142 | } | 
|---|