00001 #include "../header/OpenAL.h"
00002
00003 STARTNAMESPACE
00004
00005 SoundBuffer::SoundBuffer(){
00006 buffer = 0;
00007 }
00008
00009 SoundBuffer::~SoundBuffer(){
00010 if(buffer != 0){
00011 alDeleteBuffers(1, &buffer);
00012 buffer = 0;
00013 }
00014 }
00015
00016 void Sound::update(float3* listener){
00017
00018 if(distance*distance < (*listener - *position).length2()){
00019
00020 if(m_playing == true){
00021
00022
00023 alSourceStop(source);
00024 m_playing = false;
00025 }
00026 }
00027
00028 else if(m_play == true){
00029
00030 if(m_playing == false){
00031
00032 alSourcePlay(source);
00033 m_playing = true;
00034 }
00035
00036 if(m_dynamic == true){
00037
00038 alSourcefv(source, AL_POSITION, (float*)position);
00039 alSourcefv(source, AL_VELOCITY, (float*)velocity);
00040 alSourcefv(source, AL_DIRECTION, (float*)direction);
00041 }
00042 }
00043 }
00044
00045 Sound::Sound(ArrayList<Sound*>& soundlist)
00046 : m_play(false),
00047 m_playing(false),
00048 m_dynamic(false),
00049 source(0),
00050 distance(0.0f),
00051 position(0),
00052 velocity(0),
00053 direction(0),
00054 list(soundlist)
00055 {
00056 }
00057
00058 Sound::~Sound(){
00059 if(source != 0){
00060 alDeleteSources(1, &source);
00061 source = 0;
00062 }
00063 }
00064
00065 void Sound::play(){
00066 if(m_play == false)
00067 list.add(this);
00068 m_play = true;
00069 alSourcePlay(source);
00070 }
00071
00072 void Sound::stop(){
00073 if(m_play)
00074 list.remove(this);
00075 m_play = false;
00076 m_playing = false;
00077 alSourceStop(source);
00078 }
00079
00080 void Sound::pause(){
00081 if(m_play)
00082 list.remove(this);
00083 m_play = false;
00084 m_playing = false;
00085 alSourcePause(source);
00086 }
00087
00088 void Sound::resume()
00089 {
00090 if(m_play == false)
00091 list.add(this);
00092 m_play = true;
00093 m_playing = true;
00094 alSourcePlay(source);
00095 }
00096
00097 bool Sound::load(::pBuffer buffer, bool dynamic,
00098 float3* position,
00099 float3* velocity,
00100 float3* direction,
00101 float distance, float coneAngle,
00102 bool loop, float volume)
00103 {
00104 alGenSources(1, &source);
00105 if(alGetError() != AL_NO_ERROR)
00106 return false;
00107
00108 alSourcei(source, AL_BUFFER, ((SoundBuffer*)buffer)->buffer);
00109 if(alGetError() != AL_NO_ERROR){
00110 alDeleteSources(1, &source);
00111 return false;
00112 }
00113
00114 m_dynamic = dynamic;
00115 this->position = position;
00116 this->velocity = velocity;
00117 this->direction = direction;
00118 this->distance = distance * 1.2f;
00119 if(m_dynamic == false){
00120 alSourcefv(source, AL_POSITION, (float*)position);
00121 alSourcefv(source, AL_VELOCITY, (float*)velocity);
00122 alSourcefv(source, AL_DIRECTION, (float*)direction);
00123 }
00124 if(coneAngle < 360.0f){
00125 alSourcef(source, AL_CONE_OUTER_ANGLE, coneAngle);
00126
00127 alSourcef(source, AL_CONE_OUTER_GAIN, volume*0.75f);
00128 }
00129 alSourcei(source, AL_LOOPING, loop == true ? AL_TRUE : AL_FALSE);
00130 alSourcef(source, AL_MAX_DISTANCE, distance);
00131 alSourcef(source, AL_GAIN, volume);
00132
00133 return true;
00134 }
00135
00136 ENDNAMESPACE