Rigs of Rods 2023.09
Soft-body Physics Simulation
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Loading...
Searching...
No Matches
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
26using namespace Ogre;
27using namespace RoR;
28
29Sound::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
45void 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 {
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
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
125void Sound::setGain(float gain_)
126{
127 if (gain_ == this->gain)
128 return;
129
130 this->gain = gain_;
132}
133
134void Sound::setLoop(bool loop_)
135{
136 if (loop_ == this->loop)
137 return;
138
139 this->loop = loop_;
141}
142
143void Sound::setPitch(float pitch_)
144{
145 if (pitch_ == this->pitch)
146 return;
147
148 this->pitch = pitch_;
150}
151
152void Sound::setPosition(Ogre::Vector3 pos)
153{
154 if (pos == this->position)
155 return;
156
157 this->position = pos;
159}
160
161void Sound::setVelocity(Ogre::Vector3 vel)
162{
163 if (vel == this->velocity)
164 return;
165
166 this->velocity = vel;
168}
169
170#endif // USE_OPENAL
Ogre::Vector3 position
Definition Sound.h:93
void stop()
Definition Sound.cpp:119
void computeAudibility(Ogre::Vector3 pos)
Definition Sound.cpp:45
void setVelocity(Ogre::Vector3 vel)
Definition Sound.cpp:161
@ REASON_POSN
Definition Sound.h:75
@ REASON_STOP
Definition Sound.h:71
@ REASON_LOOP
Definition Sound.h:73
@ REASON_PTCH
Definition Sound.h:74
@ REASON_PLAY
Definition Sound.h:70
@ REASON_VLCT
Definition Sound.h:76
@ REASON_GAIN
Definition Sound.h:72
bool isPlaying()
Definition Sound.cpp:88
float gain
Definition Sound.h:83
void setPitch(float pitch)
Definition Sound.cpp:143
void setGain(float gain)
Definition Sound.cpp:125
int source_index
Definition Sound.h:98
void setPosition(Ogre::Vector3 pos)
Definition Sound.cpp:152
float audibility
Definition Sound.h:82
bool enabled
Definition Sound.h:86
void setEnabled(bool e)
Definition Sound.cpp:99
bool loop
Definition Sound.h:85
Sound(ALuint buffer, SoundManager *soundManager, int sourceIndex)
Definition Sound.cpp:29
bool getEnabled()
Definition Sound.cpp:108
void play()
Definition Sound.cpp:113
float pitch
Definition Sound.h:84
Ogre::Vector3 velocity
Definition Sound.h:94
void setLoop(bool loop)
Definition Sound.cpp:134
SoundManager * sound_manager
Definition Sound.h:96
bool should_play
Definition Sound.h:87
int hardware_index
Definition Sound.h:90
static const float MAX_DISTANCE
void recomputeSource(int source_index, int reason, float vfl, Ogre::Vector3 *vvec)
Computes audibility of an audio source and retires it if it is inaudible.
ALuint getHardwareSource(int hardware_index)
Returns the AL handle for the hardware source with the provided index.
static const float REFERENCE_DISTANCE
static const float ROLLOFF_FACTOR