/* orxonox - the future of 3D-vertical-scrollers Copyright (C) 2004 orx This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. ### File Specific: main-programmer: Benjamin Grauer co-programmer: ... */ //#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD_ENTITY #include "sound_engine.h" #include "p_node.h" #include "list.h" using namespace std; /** \brief Creates a Soundbuffer out of an inputfile \param fileName The name of the File */ SoundBuffer::SoundBuffer(const char* fileName) { SoundEngine::getInstance()->addBuffer(this); ALenum format; ALvoid* data; ALsizei freq; alGenBuffers(1, &this->bufferID); alutLoadWAVFile((ALbyte*)fileName, &format, &data, &this->size, &freq, &this->loop); alBufferData(this->bufferID, format, data, this->size, freq); alutUnloadWAV(format, data, this->size, freq); } SoundBuffer::~SoundBuffer(void) { SoundEngine::getInstance()->removeBuffer(this); } /** \brief creates a SoundSource at position sourceNode with the SoundBuffer buffer */ SoundSource::SoundSource(SoundBuffer* buffer, PNode* sourceNode) { SoundEngine::getInstance()->addSource(this); this->buffer = buffer; this->sourceNode = sourceNode; } SoundSource::~SoundSource(void) { SoundEngine::getInstance()->removeSource(this); } /** \brief standard constructor */ SoundEngine::SoundEngine () { this->setClassName ("SoundEngine"); this->initAudio(); this->listener = NULL; this->bufferList = new tList; this->sourceList = new tList; } /** \brief the singleton reference to this class */ SoundEngine* SoundEngine::singletonRef = NULL; /** \returns a Pointer to this Class */ SoundEngine* SoundEngine::getInstance(void) { if (!SoundEngine::singletonRef) SoundEngine::singletonRef = new SoundEngine(); return SoundEngine::singletonRef; } /** \brief standard deconstructor */ SoundEngine::~SoundEngine () { SoundEngine::singletonRef = NULL; } /** \brief sets The listener (normaly the Camera) */ void SoundEngine::setListener(PNode* listener) { this->listener = listener; } void SoundEngine::addBuffer(SoundBuffer* buffer) { this->bufferList->add(buffer); } void SoundEngine::removeBuffer(SoundBuffer* buffer) { this->bufferList->remove(buffer); } void SoundEngine::addSource(SoundSource* source) { this->sourceList->add(source); } void SoundEngine::removeSource(SoundSource* source) { this->sourceList->remove(source); } /** \brief updates all The positions, Directions and Velocities of all Sounds */ void SoundEngine::update(void) { // updating the Listeners Position if (this->listener) { alListener3f(AL_POSITION, this->listener->getAbsCoor().x, this->listener->getAbsCoor().y, this->listener->getAbsCoor().z); alListener3f(AL_VELOCITY, this->listener->getVelocity().x, this->listener->getVelocity().y, this->listener->getVelocity().z); Vector absDirV = this->listener->getAbsDirV(); alListener3f(AL_ORIENTATION, absDirV.x, absDirV.y, absDirV.z); } // updating all the Sources positions tIterator* iterator = this->sourceList->getIterator(); SoundSource* enumSource = iterator->nextElement(); while (enumSource) { alSource3f(enumSource->getID(), AL_POSITION, enumSource->getNode()->getAbsCoor().x, enumSource->getNode()->getAbsCoor().y, enumSource->getNode()->getAbsCoor().z); alSource3f(enumSource->getID(), AL_VELOCITY, enumSource->getNode()->getVelocity().x, enumSource->getNode()->getVelocity().y, enumSource->getNode()->getVelocity().z); enumSource = iterator->nextElement(); } delete iterator; } /** \brief initializes Audio in general */ bool SoundEngine::initAudio(void) { alutInit(NULL, 0); alGetError(); }