| [513] | 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 |  */ | 
|---|
| [560] | 27 |  | 
|---|
| [513] | 28 |  | 
|---|
| [339] | 29 | #include <iostream> | 
|---|
 | 30 | #include <string> | 
|---|
 | 31 | #include "AudioObject.h" | 
|---|
| [560] | 32 | #include "../orxonox/core/Debug.h" | 
|---|
| [339] | 33 |  | 
|---|
 | 34 | namespace audio | 
|---|
 | 35 | { | 
|---|
 | 36 |         AudioObject::AudioObject(std::string audioFile) | 
|---|
 | 37 |         { | 
|---|
 | 38 |                 audioFile_ = audioFile; | 
|---|
 | 39 |                 SourcePos[0]=0; | 
|---|
 | 40 |                 SourcePos[1]=0; | 
|---|
 | 41 |                 SourcePos[2]=0; | 
|---|
 | 42 |  | 
|---|
 | 43 |                  SourceVel[0]=0; | 
|---|
 | 44 |                  SourceVel[1]=0; | 
|---|
 | 45 |                  SourceVel[2]=0; | 
|---|
 | 46 |  | 
|---|
 | 47 |                  ListenerPos[0]=0; | 
|---|
 | 48 |                  ListenerPos[1]=0; | 
|---|
 | 49 |                  ListenerPos[2]=0; | 
|---|
 | 50 |  | 
|---|
 | 51 |                  ListenerVel[0]=0; | 
|---|
 | 52 |                  ListenerVel[1]=0; | 
|---|
 | 53 |                  ListenerVel[2]=0; | 
|---|
 | 54 |  | 
|---|
 | 55 |                  ListenerOri[0]=0; | 
|---|
 | 56 |                  ListenerOri[1]=0; | 
|---|
 | 57 |                  ListenerOri[2]=-1; | 
|---|
 | 58 |                  ListenerOri[3]=0; | 
|---|
 | 59 |                  ListenerOri[4]=1; | 
|---|
 | 60 |                  ListenerOri[5]=0; | 
|---|
 | 61 |  | 
|---|
 | 62 |  | 
|---|
 | 63 |                 // Initialize OpenAL and clear the error bit. | 
|---|
| [513] | 64 |  | 
|---|
| [339] | 65 |                 alutInit(NULL, 0); | 
|---|
 | 66 |                 alGetError(); | 
|---|
| [513] | 67 |  | 
|---|
| [339] | 68 |                 // Load the wav data. | 
|---|
| [513] | 69 |  | 
|---|
| [339] | 70 |                 if(LoadALData() == AL_FALSE) | 
|---|
 | 71 |                 { | 
|---|
 | 72 |                     printf("Error loading sound data."); | 
|---|
| [513] | 73 |  | 
|---|
| [339] | 74 |                 } | 
|---|
 | 75 |  | 
|---|
 | 76 |                 SetListenerValues(); | 
|---|
| [560] | 77 |                 COUT(3) << "Info: Play sone ambient background sound"; | 
|---|
| [339] | 78 |         } | 
|---|
| [513] | 79 |  | 
|---|
| [339] | 80 |         AudioObject::~AudioObject() | 
|---|
| [513] | 81 |         { | 
|---|
| [339] | 82 |                 KillALData(); | 
|---|
 | 83 |         } | 
|---|
| [513] | 84 |  | 
|---|
| [339] | 85 |         ALboolean AudioObject::LoadALData() | 
|---|
 | 86 |         { | 
|---|
 | 87 |                 ALenum format; | 
|---|
 | 88 |                 ALsizei size; | 
|---|
 | 89 |                 ALvoid* data; | 
|---|
 | 90 |                 ALsizei freq; | 
|---|
 | 91 |                 ALboolean loop; | 
|---|
| [513] | 92 |  | 
|---|
 | 93 |  | 
|---|
| [339] | 94 |                 alGenBuffers(1, &Buffer); | 
|---|
| [513] | 95 |  | 
|---|
| [339] | 96 |                 if(alGetError() != AL_NO_ERROR) | 
|---|
 | 97 |                         return AL_FALSE; | 
|---|
| [513] | 98 |  | 
|---|
| [339] | 99 |                 alutLoadWAVFile((ALbyte*)audioFile_.c_str(), &format, &data, &size, &freq, &loop); | 
|---|
 | 100 |                 alBufferData(Buffer, format, data, size, freq); | 
|---|
 | 101 |                 alutUnloadWAV(format, data, size, freq); | 
|---|
| [513] | 102 |  | 
|---|
| [339] | 103 |                 alGenSources(1, &Source); | 
|---|
| [513] | 104 |  | 
|---|
| [339] | 105 |                 if(alGetError() != AL_NO_ERROR) | 
|---|
 | 106 |                         return AL_FALSE; | 
|---|
| [513] | 107 |  | 
|---|
| [339] | 108 |                 alSourcei (Source, AL_BUFFER,   Buffer   ); | 
|---|
 | 109 |                 alSourcef (Source, AL_PITCH,    1.0      ); | 
|---|
 | 110 |                 alSourcef (Source, AL_GAIN,     1.0      ); | 
|---|
 | 111 |                 alSourcefv(Source, AL_POSITION, SourcePos); | 
|---|
 | 112 |                 alSourcefv(Source, AL_VELOCITY, SourceVel); | 
|---|
 | 113 |                 alSourcei (Source, AL_LOOPING,  loop     ); | 
|---|
| [513] | 114 |  | 
|---|
| [339] | 115 |                 if(alGetError() == AL_NO_ERROR) | 
|---|
 | 116 |                         return AL_TRUE; | 
|---|
| [513] | 117 |  | 
|---|
 | 118 |  | 
|---|
| [339] | 119 |                 return AL_FALSE; | 
|---|
| [513] | 120 |         } | 
|---|
| [339] | 121 |  | 
|---|
 | 122 |         void AudioObject::SetListenerValues() | 
|---|
 | 123 |         { | 
|---|
 | 124 |                 alListenerfv(AL_POSITION,    ListenerPos); | 
|---|
 | 125 |                 alListenerfv(AL_VELOCITY,    ListenerVel); | 
|---|
 | 126 |                 alListenerfv(AL_ORIENTATION, ListenerOri); | 
|---|
 | 127 |         } | 
|---|
| [513] | 128 |  | 
|---|
| [339] | 129 |         void AudioObject::KillALData() | 
|---|
 | 130 |         { | 
|---|
 | 131 |                 alDeleteBuffers(1, &Buffer); | 
|---|
 | 132 |                 alDeleteSources(1, &Source); | 
|---|
 | 133 |                 alutExit(); | 
|---|
 | 134 |         } | 
|---|
 | 135 |  | 
|---|
 | 136 |         void AudioObject::play() | 
|---|
 | 137 |         { | 
|---|
 | 138 |                 alSourcePlay(Source); | 
|---|
| [513] | 139 |  | 
|---|
| [339] | 140 |         } | 
|---|
 | 141 | } | 
|---|
 | 142 |  | 
|---|