Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/branches/openAL: patch, now it loads the wav from where it should

File size: 4.9 KB
Line 
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:
12   main-programmer: Benjamin Grauer
13   co-programmer: ...
14*/
15
16//#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD_ENTITY
17
18#include "sound_engine.h"
19
20#include "p_node.h"
21#include "list.h"
22
23using namespace std;
24
25
26//////////////////
27/* SOUND-BUFFER */
28//////////////////
29/**
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);
50  alDeleteBuffers(1, &this->bufferID);
51}
52
53//////////////////
54/* SOUND-SOURCE */
55//////////////////
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;
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);
76   alSourcei (sourceID, AL_LOOPING,  true     );
77 
78
79}
80
81SoundSource::~SoundSource(void)
82{
83  SoundEngine::getInstance()->removeSource(this);
84  alDeleteSources(1, &this->sourceID);
85}
86
87
88void SoundSource::play()
89{
90  alSourcePlay(this->sourceID);
91}
92void SoundSource::stop()
93{
94  alSourceStop(this->sourceID);
95}
96void SoundSource::pause()
97{
98  alSourcePause(this->sourceID);
99}
100
101
102//////////////////
103/* SOUND-ENGINE */
104//////////////////
105/**
106   \brief standard constructor
107*/
108SoundEngine::SoundEngine () 
109{
110  this->setClassName ("SoundEngine");
111   
112   this->initAudio();
113
114   this->listener = NULL;
115   this->bufferList = new tList<SoundBuffer>;
116   this->sourceList = new tList<SoundSource>;
117}
118
119/**
120   \brief the singleton reference to this class
121*/
122SoundEngine* SoundEngine::singletonRef = NULL;
123
124/**
125   \returns a Pointer to this Class
126*/
127SoundEngine* SoundEngine::getInstance(void)
128{
129  if (!SoundEngine::singletonRef)
130    SoundEngine::singletonRef = new SoundEngine();
131  return SoundEngine::singletonRef;
132}
133
134/**
135   \brief standard deconstructor
136
137*/
138SoundEngine::~SoundEngine () 
139{
140  SoundEngine::singletonRef = NULL;
141
142}
143
144/**
145   \brief sets The listener (normaly the Camera)
146*/
147void SoundEngine::setListener(PNode* listener)
148{
149  this->listener = listener;
150}
151
152
153void SoundEngine::addBuffer(SoundBuffer* buffer)
154{
155  this->bufferList->add(buffer);
156}
157
158void SoundEngine::removeBuffer(SoundBuffer* buffer)
159{
160  this->bufferList->remove(buffer);
161}
162
163void SoundEngine::addSource(SoundSource* source)
164{
165  this->sourceList->add(source);
166}
167void SoundEngine::removeSource(SoundSource* source)
168{
169  this->sourceList->remove(source);
170}
171
172
173/**
174   \brief updates all The positions, Directions and Velocities of all Sounds
175*/
176void SoundEngine::update(void)
177{
178
179  // updating the Listeners Position
180  if (this->listener)
181    {
182      alListener3f(AL_POSITION,
183                   this->listener->getAbsCoor().x,
184                   this->listener->getAbsCoor().y,
185                   this->listener->getAbsCoor().z);
186      alListener3f(AL_VELOCITY,
187                   this->listener->getVelocity().x,
188                   this->listener->getVelocity().y,
189                   this->listener->getVelocity().z);
190      Vector absDirV = this->listener->getAbsDirV();
191      ALfloat orientation [6] = {1,0,0, absDirV.x, absDirV.y, absDirV.z};
192      alListenerfv(AL_ORIENTATION, orientation);
193    }
194  else
195    PRINTF(2)("no listener defined\n");
196
197  // updating all the Sources positions
198  tIterator<SoundSource>* iterator = this->sourceList->getIterator();
199  SoundSource* enumSource = iterator->nextElement();
200  while (enumSource)
201    {
202      alSource3f(enumSource->getID(), AL_POSITION,
203                 enumSource->getNode()->getAbsCoor().x,
204                 enumSource->getNode()->getAbsCoor().y,
205                 enumSource->getNode()->getAbsCoor().z);
206      alSource3f(enumSource->getID(), AL_VELOCITY,
207                 enumSource->getNode()->getVelocity().x,
208                 enumSource->getNode()->getVelocity().y,
209                 enumSource->getNode()->getVelocity().z);
210
211      enumSource = iterator->nextElement();
212    }
213  delete iterator;
214}
215
216/**
217   \brief initializes Audio in general
218*/
219bool SoundEngine::initAudio(void)
220{
221  alutInit(NULL, 0);
222  alGetError();
223}
224
Note: See TracBrowser for help on using the repository browser.