RigsofRods
Soft-body Physics Simulation
Sound.cpp
Go to the documentation of this file.
1 /*
2  This source file is part of Rigs of Rods
3  Copyright 2005-2012 Pierre-Michel Ricordel
4  Copyright 2007-2012 Thomas Fischer
5 
6  For more information, see http://www.rigsofrods.org/
7 
8  Rigs of Rods is free software: you can redistribute it and/or modify
9  it under the terms of the GNU General Public License version 3, as
10  published by the Free Software Foundation.
11 
12  Rigs of Rods 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 Rigs of Rods. If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 #ifdef USE_OPENAL
22 
23 #include "Sound.h"
24 #include "SoundManager.h"
25 
26 using namespace Ogre;
27 using namespace RoR;
28 
29 Sound::Sound(ALuint buffer, SoundManager* soundManager, int sourceIndex) :
30  buffer(buffer)
31  , sound_manager(soundManager)
32  , source_index(sourceIndex)
33  , audibility(0.0f)
34  , gain(0.0f)
35  , pitch(1.0f)
36  , position(Vector3::ZERO)
37  , velocity(Vector3::ZERO)
38  , enabled(true)
39  , loop(false)
40  , should_play(false)
41  , hardware_index(-1)
42 {
43 }
44 
45 void Sound::computeAudibility(Vector3 pos)
46 {
47  // disable sound?
48  if (!enabled)
49  {
50  audibility = 0.0f;
51  return;
52  }
53 
54  // first check if the sound is finished!
55  if (!loop && should_play && hardware_index != -1)
56  {
57  int value = 0;
58  alGetSourcei((ALuint)sound_manager->getHardwareSource(hardware_index), AL_SOURCE_STATE, &value);
59  if (value != AL_PLAYING)
60  {
61  should_play = false;
62  }
63  }
64 
65  // should it play at all?
66  if (!should_play || gain == 0.0f)
67  {
68  audibility = 0.0f;
69  return;
70  }
71 
72  float distance = (pos - position).length();
73 
74  if (distance > sound_manager->MAX_DISTANCE)
75  {
76  audibility = 0.0f;
77  }
78  else if (distance < sound_manager->REFERENCE_DISTANCE)
79  {
80  audibility = gain;
81  }
82  else
83  {
85  }
86 }
87 
89 {
90  if (hardware_index != -1)
91  {
92  int value = 0;
93  alGetSourcei((ALuint)sound_manager->getHardwareSource(hardware_index), AL_SOURCE_STATE, &value);
94  return (value == AL_PLAYING);
95  }
96  return false;
97 }
98 
99 void Sound::setEnabled(bool e)
100 {
101  if (e == this->enabled)
102  return;
103 
104  this->enabled = e;
106 }
107 
109 {
110  return enabled;
111 }
112 
114 {
115  should_play = true;
117 }
118 
120 {
121  should_play = false;
123 }
124 
125 void Sound::setGain(float gain)
126 {
127  if (gain == this->gain)
128  return;
129 
130  this->gain = gain;
132 }
133 
135 {
136  if (loop == this->loop)
137  return;
138 
139  this->loop = loop;
140  sound_manager->recomputeSource(source_index, REASON_LOOP, (loop) ? 1.0f : 0.0f, NULL);
141 }
142 
143 void Sound::setPitch(float pitch)
144 {
145  if (pitch == this->pitch)
146  return;
147 
148  this->pitch = pitch;
150 }
151 
152 void Sound::setPosition(Ogre::Vector3 pos)
153 {
154  if (pos == this->position)
155  return;
156 
157  this->position = pos;
159 }
160 
161 void Sound::setVelocity(Ogre::Vector3 vel)
162 {
163  if (vel == this->velocity)
164  return;
165 
166  this->velocity = vel;
168 }
169 
170 #endif // USE_OPENAL
Sound.h
RoR::Sound::REASON_PTCH
@ REASON_PTCH
Definition: Sound.h:74
RoR::SoundManager
Definition: SoundManager.h:44
RoR::Sound::getEnabled
bool getEnabled()
Definition: Sound.cpp:108
RoR::Sound::setEnabled
void setEnabled(bool e)
Definition: Sound.cpp:99
RoR::Sound::enabled
bool enabled
Definition: Sound.h:86
RoR::SoundManager::recomputeSource
void recomputeSource(int source_index, int reason, float vfl, Ogre::Vector3 *vvec)
Definition: SoundManager.cpp:208
RoR::SoundManager::getHardwareSource
ALuint getHardwareSource(int hardware_index)
Definition: SoundManager.h:76
RoR::Sound::pitch
float pitch
Definition: Sound.h:84
RoR::Sound::loop
bool loop
Definition: Sound.h:85
SoundManager.h
RoR::SoundManager::REFERENCE_DISTANCE
static const float REFERENCE_DISTANCE
Definition: SoundManager.h:69
RoR::Sound::position
Ogre::Vector3 position
Definition: Sound.h:93
RoR::Sound::audibility
float audibility
Definition: Sound.h:82
RoR::Sound::setPosition
void setPosition(Ogre::Vector3 pos)
Definition: Sound.cpp:152
RoR::Sound::setLoop
void setLoop(bool loop)
Definition: Sound.cpp:134
RoR::Sound::source_index
int source_index
Definition: Sound.h:98
RoR::Sound::should_play
bool should_play
Definition: Sound.h:87
RoR::Sound::isPlaying
bool isPlaying()
Definition: Sound.cpp:88
RoR::Sound::setPitch
void setPitch(float pitch)
Definition: Sound.cpp:143
RoR::Sound::REASON_LOOP
@ REASON_LOOP
Definition: Sound.h:73
RoR::Sound::REASON_PLAY
@ REASON_PLAY
Definition: Sound.h:70
RoR::Sound::REASON_STOP
@ REASON_STOP
Definition: Sound.h:71
RoR::Sound::setGain
void setGain(float gain)
Definition: Sound.cpp:125
RoR::SoundManager::MAX_DISTANCE
static const float MAX_DISTANCE
Definition: SoundManager.h:67
RoR::Sound::stop
void stop()
Definition: Sound.cpp:119
RoR::Sound::setVelocity
void setVelocity(Ogre::Vector3 vel)
Definition: Sound.cpp:161
RoR::Sound::computeAudibility
void computeAudibility(Ogre::Vector3 pos)
Definition: Sound.cpp:45
RoR::Sound::play
void play()
Definition: Sound.cpp:113
RoR::Sound::hardware_index
int hardware_index
Definition: Sound.h:90
Ogre
Definition: ExtinguishableFireAffector.cpp:35
RoR::Sound::REASON_POSN
@ REASON_POSN
Definition: Sound.h:75
RoR::Sound::REASON_GAIN
@ REASON_GAIN
Definition: Sound.h:72
RoR::Sound::REASON_VLCT
@ REASON_VLCT
Definition: Sound.h:76
RoR
Definition: AppContext.h:36
RoR::Sound::sound_manager
SoundManager * sound_manager
Definition: Sound.h:96
RoR::Sound::velocity
Ogre::Vector3 velocity
Definition: Sound.h:94
RoR::Sound::gain
float gain
Definition: Sound.h:83
RoR::SoundManager::ROLLOFF_FACTOR
static const float ROLLOFF_FACTOR
Definition: SoundManager.h:68
loop
Then in your render loop
Definition: README-OgreImGui.txt:62