Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/branches/openAL/src/lib/sound/sound_engine.cc @ 4200

Last change on this file since 4200 was 4200, checked in by bensch, 19 years ago

orxonox/branches/openAL: rolloffFactor

File size: 5.1 KB
RevLine 
[3655]1/*
2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 orx
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
10
11   ### File Specific:
[4195]12   main-programmer: Benjamin Grauer
[3655]13   co-programmer: ...
14*/
15
16//#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD_ENTITY
17
[4195]18#include "sound_engine.h"
[4197]19
[4196]20#include "p_node.h"
21#include "list.h"
[3655]22
23using namespace std;
24
25
[4197]26//////////////////
27/* SOUND-BUFFER */
28//////////////////
[3655]29/**
[4196]30   \brief Creates a Soundbuffer out of an inputfile
31   \param fileName The name of the File
32*/
33SoundBuffer::SoundBuffer(const char* fileName)
34{
35  SoundEngine::getInstance()->addBuffer(this);
36
37  ALenum format;
38  ALvoid* data;
39  ALsizei freq;
40
41  alGenBuffers(1, &this->bufferID);
42  alutLoadWAVFile((ALbyte*)fileName, &format, &data, &this->size, &freq, &this->loop);
43  alBufferData(this->bufferID, format, data, this->size, freq);
44  alutUnloadWAV(format, data, this->size, freq);
45}
46
47SoundBuffer::~SoundBuffer(void)
48{
49  SoundEngine::getInstance()->removeBuffer(this);
[4198]50  alDeleteBuffers(1, &this->bufferID);
[4196]51}
52
[4197]53//////////////////
54/* SOUND-SOURCE */
55//////////////////
[4196]56/**
57   \brief creates a SoundSource at position sourceNode with the SoundBuffer buffer
58*/
59SoundSource::SoundSource(SoundBuffer* buffer, PNode* sourceNode)
60{
61  SoundEngine::getInstance()->addSource(this);
62
63  this->buffer = buffer;
64  this->sourceNode = sourceNode;
[4197]65
66  alGenSources(1, &this->sourceID);
67
68  if(alGetError() != AL_NO_ERROR)
69    PRINTF(1)("error initializing SoundSource\n"); //return AL_FALSE;
70 
71  alSourcei (this->sourceID, AL_BUFFER,   this->buffer->getID());
72  alSourcef (this->sourceID, AL_PITCH,    1.0      );
73  alSourcef (this->sourceID, AL_GAIN,     1.0      );
74  //  alSourcefv(sourceID, AL_POSITION, SourcePos);
75  //  alSourcefv(sourceID, AL_VELOCITY, SourceVel);
[4200]76  alSourcei (sourceID, AL_LOOPING,  true     );
[4196]77}
78
79SoundSource::~SoundSource(void)
80{
81  SoundEngine::getInstance()->removeSource(this);
[4198]82  alDeleteSources(1, &this->sourceID);
[4196]83}
84
[4197]85
86void SoundSource::play()
87{
88  alSourcePlay(this->sourceID);
89}
90void SoundSource::stop()
91{
92  alSourceStop(this->sourceID);
93}
94void SoundSource::pause()
95{
96  alSourcePause(this->sourceID);
97}
98
[4200]99void SoundSource::setRolloffFactor(ALfloat rolloffFactor)
100{
101  alSourcef(this->sourceID, AL_ROLLOFF_FACTOR, rolloffFactor);
102}
[4197]103
[4200]104
105
[4197]106//////////////////
107/* SOUND-ENGINE */
108//////////////////
[4196]109/**
[3655]110   \brief standard constructor
111*/
[4195]112SoundEngine::SoundEngine () 
[3655]113{
[4196]114  this->setClassName ("SoundEngine");
115   
116   this->initAudio();
[3655]117
[4196]118   this->listener = NULL;
119   this->bufferList = new tList<SoundBuffer>;
120   this->sourceList = new tList<SoundSource>;
[3655]121}
122
123/**
124   \brief the singleton reference to this class
125*/
[4195]126SoundEngine* SoundEngine::singletonRef = NULL;
[3655]127
128/**
129   \returns a Pointer to this Class
130*/
[4195]131SoundEngine* SoundEngine::getInstance(void)
[3655]132{
[4195]133  if (!SoundEngine::singletonRef)
134    SoundEngine::singletonRef = new SoundEngine();
135  return SoundEngine::singletonRef;
[3655]136}
137
138/**
139   \brief standard deconstructor
140
141*/
[4195]142SoundEngine::~SoundEngine () 
[3655]143{
[4195]144  SoundEngine::singletonRef = NULL;
[3655]145
146}
[4196]147
148/**
149   \brief sets The listener (normaly the Camera)
150*/
151void SoundEngine::setListener(PNode* listener)
152{
153  this->listener = listener;
154}
155
156
157void SoundEngine::addBuffer(SoundBuffer* buffer)
158{
159  this->bufferList->add(buffer);
160}
161
162void SoundEngine::removeBuffer(SoundBuffer* buffer)
163{
164  this->bufferList->remove(buffer);
165}
166
167void SoundEngine::addSource(SoundSource* source)
168{
169  this->sourceList->add(source);
170}
171void SoundEngine::removeSource(SoundSource* source)
172{
173  this->sourceList->remove(source);
174}
175
176
177/**
178   \brief updates all The positions, Directions and Velocities of all Sounds
179*/
180void SoundEngine::update(void)
181{
182
183  // updating the Listeners Position
184  if (this->listener)
185    {
186      alListener3f(AL_POSITION,
187                   this->listener->getAbsCoor().x,
188                   this->listener->getAbsCoor().y,
189                   this->listener->getAbsCoor().z);
190      alListener3f(AL_VELOCITY,
191                   this->listener->getVelocity().x,
192                   this->listener->getVelocity().y,
193                   this->listener->getVelocity().z);
194      Vector absDirV = this->listener->getAbsDirV();
[4198]195      ALfloat orientation [6] = {1,0,0, absDirV.x, absDirV.y, absDirV.z};
196      alListenerfv(AL_ORIENTATION, orientation);
[4196]197    }
[4197]198  else
199    PRINTF(2)("no listener defined\n");
200
[4196]201  // updating all the Sources positions
202  tIterator<SoundSource>* iterator = this->sourceList->getIterator();
203  SoundSource* enumSource = iterator->nextElement();
204  while (enumSource)
205    {
206      alSource3f(enumSource->getID(), AL_POSITION,
207                 enumSource->getNode()->getAbsCoor().x,
208                 enumSource->getNode()->getAbsCoor().y,
209                 enumSource->getNode()->getAbsCoor().z);
210      alSource3f(enumSource->getID(), AL_VELOCITY,
211                 enumSource->getNode()->getVelocity().x,
212                 enumSource->getNode()->getVelocity().y,
213                 enumSource->getNode()->getVelocity().z);
214
215      enumSource = iterator->nextElement();
216    }
217  delete iterator;
218}
219
220/**
221   \brief initializes Audio in general
222*/
223bool SoundEngine::initAudio(void)
224{
225  alutInit(NULL, 0);
226  alGetError();
[4200]227
228  alDopplerFactor(SOUND_DOPPLER_FACTOR);
229  alDopplerVelocity(SOUND_DOPPLER_VELOCITY);
[4196]230}
231
Note: See TracBrowser for help on using the repository browser.