Skyscraper 2.0
reverb.cpp
Go to the documentation of this file.
1/*
2 Scalable Building Simulator - Reverb Object
3 The Skyscraper Project - Version 2.0
4 Copyright (C)2004-2024 Ryan Thoryk
5 https://www.skyscrapersim.net
6 https://sourceforge.net/projects/skyscraper/
7 Contact - ryan@skyscrapersim.net
8
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License
11 as published by the Free Software Foundation; either version 2
12 of the License, or (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22*/
23
24#ifndef DISABLE_SOUND
25 #include <fmod.hpp>
26 #include <fmod_errors.h>
27#endif
28#include "globals.h"
29#include "sbs.h"
30#include "floor.h"
31#include "elevatorcar.h"
32#include "soundsystem.h"
33#include "reverb.h"
34
35namespace SBS {
36
37Reverb::Reverb(Object *parent, const std::string &name, std::string type, const Vector3 &position, Real min_distance, Real max_distance, bool permanent) : Object(parent)
38{
39 //set up SBS object
40 SetValues("Reverb", name, permanent);
41
42 //first set default values
44 MaxDistance = max_distance;
45 MinDistance = min_distance;
46#ifndef DISABLE_SOUND
47 reverb = 0;
48#endif
49
50 //convert type to lowercase
51 SetCase(type, false);
52
53 //create reverb object
54
55#ifndef DISABLE_SOUND
56 FMOD_RESULT result = soundsys->GetFmodSystem()->createReverb3D(&reverb);
57 if (result != FMOD_OK)
58 {
59 std::string fmod_result = FMOD_ErrorString(result);
60 ReportError("Can't create reverb:\n" + fmod_result);
61 reverb = 0;
62 return;
63 }
64
65 //set reverb type
66 FMOD_REVERB_PROPERTIES prop;
67
68 if (type == "generic")
69 prop = FMOD_PRESET_GENERIC;
70 else if (type == "paddedcell")
71 prop = FMOD_PRESET_PADDEDCELL;
72 else if (type == "room")
73 prop = FMOD_PRESET_ROOM;
74 else if (type == "bathroom")
75 prop = FMOD_PRESET_BATHROOM;
76 else if (type == "livingroom")
77 prop = FMOD_PRESET_LIVINGROOM;
78 else if (type == "stoneroom")
79 prop = FMOD_PRESET_STONEROOM;
80 else if (type == "auditorium")
81 prop = FMOD_PRESET_AUDITORIUM;
82 else if (type == "concerthall")
83 prop = FMOD_PRESET_CONCERTHALL;
84 else if (type == "cave")
85 prop = FMOD_PRESET_CAVE;
86 else if (type == "arena")
87 prop = FMOD_PRESET_ARENA;
88 else if (type == "hangar")
89 prop = FMOD_PRESET_HANGAR;
90 else if (type == "carpetedhallway")
91 prop = FMOD_PRESET_CARPETTEDHALLWAY;
92 else if (type == "hallway")
93 prop = FMOD_PRESET_HALLWAY;
94 else if (type == "stonecorridor")
95 prop = FMOD_PRESET_STONECORRIDOR;
96 else if (type == "alley")
97 prop = FMOD_PRESET_ALLEY;
98 else if (type == "forest")
99 prop = FMOD_PRESET_FOREST;
100 else if (type == "city")
101 prop = FMOD_PRESET_CITY;
102 else if (type == "mountains")
103 prop = FMOD_PRESET_MOUNTAINS;
104 else if (type == "quarry")
105 prop = FMOD_PRESET_QUARRY;
106 else if (type == "plain")
107 prop = FMOD_PRESET_PLAIN;
108 else if (type == "parkinglot")
109 prop = FMOD_PRESET_PARKINGLOT;
110 else if (type == "sewerpipe")
111 prop = FMOD_PRESET_SEWERPIPE;
112 else if (type == "underwater")
113 prop = FMOD_PRESET_UNDERWATER;
114
115 reverb->setProperties(&prop);
116
117 //set reverb position
118 FMOD_VECTOR fmod_pos;
119 Vector3 global_pos = sbs->ToGlobal(position);
120 fmod_pos.x = global_pos.x;
121 fmod_pos.y = global_pos.y;
122 fmod_pos.z = global_pos.z;
123 Move(position);
124
125 FMOD_RESULT result2 = reverb->set3DAttributes(&fmod_pos, min_distance, max_distance);
126 if (result2 != FMOD_OK)
127 {
128 std::string fmod_result = FMOD_ErrorString(result);
129 ReportError("Can't set reverb attributes:\n" + fmod_result);
130 reverb->release();
131 reverb = 0;
132 return;
133 }
134
136 Report("Reverb '" + name + "' created with type: " + type);
137#endif
138}
139
141{
143
144#ifndef DISABLE_SOUND
145 //release reverb instance
146 if (reverb)
147 reverb->release();
148#endif
149
150 //unregister from parent
151 if (sbs->FastDelete == false)
152 {
153 if (parent_deleting == false)
154 {
155 std::string type = GetParent()->GetType();
156
157 if (type == "ElevatorCar")
158 static_cast<ElevatorCar*>(GetParent())->RemoveReverb();
159 else if (type == "Floor")
160 static_cast<Floor*>(GetParent())->RemoveReverb(this);
161 else if (type == "SBS")
162 sbs->RemoveReverb(this);
163 }
164 }
165}
166
167void Reverb::OnMove(bool parent)
168{
169 //reposition FMOD reverb on object move
170
171#ifndef DISABLE_SOUND
172 if (!reverb)
173 return;
174
175 Vector3 global_position = sbs->ToGlobal(GetPosition());
176
177 FMOD_VECTOR pos = {(float)global_position.x, (float)global_position.y, (float)global_position.z};
178
179 //note - do not use ToRemote for positioning
180 FMOD_RESULT result = reverb->set3DAttributes(&pos, MinDistance, MaxDistance);
181 if (result != FMOD_OK)
182 {
183 std::string fmod_result = FMOD_ErrorString(result);
184 ReportError("Can't set reverb attributes:\n" + fmod_result);
185 }
186#endif
187}
188
193
198
199void Reverb::Report(const std::string &message)
200{
201 Object::Report("Reverb '" + GetName() + "', parent '" + GetParent()->GetName() + "': " + message);
202}
203
204bool Reverb::ReportError(const std::string &message)
205{
206 return Object::ReportError("Reverb '" + GetName() + "', parent '" + GetParent()->GetName() + "': " + message);
207}
208
209void Reverb::Enable(bool value)
210{
211#ifndef DISABLE_SOUND
212 if (reverb)
213 reverb->setActive(value);
214#endif
215}
216
217}
const std::string & GetName()
Definition object.cpp:53
virtual bool ReportError(const std::string &message)
Definition object.cpp:84
Object * GetParent()
Definition object.cpp:42
virtual void Report(const std::string &message)
Definition object.cpp:78
bool parent_deleting
Definition object.h:64
virtual void Move(const Vector3 &vector, Real speed=1.0)
Definition object.cpp:253
virtual Vector3 GetPosition(bool relative=false)
Definition object.cpp:321
void SetValues(const std::string &type, const std::string &name, bool is_permanent, bool is_movable=true)
Definition object.cpp:144
const std::string & GetType()
Definition object.cpp:177
Real GetMaximumDistance()
Definition reverb.cpp:194
float MaxDistance
Definition reverb.h:54
void Report(const std::string &message)
Definition reverb.cpp:199
Reverb(Object *parent, const std::string &name, std::string type, const Vector3 &position, Real min_distance, Real max_distance, bool permanent)
Definition reverb.cpp:37
float MinDistance
Definition reverb.h:53
FMOD::Reverb3D * reverb
Definition reverb.h:52
SoundSystem * soundsys
Definition reverb.h:50
Real GetMinimumDistance()
Definition reverb.cpp:189
bool ReportError(const std::string &message)
Definition reverb.cpp:204
void Enable(bool value)
Definition reverb.cpp:209
void OnMove(bool parent)
Definition reverb.cpp:167
void RemoveReverb(Reverb *reverb)
Definition sbs.cpp:2816
Vector3 ToGlobal(const Vector3 &position)
Definition sbs.cpp:4409
void IncrementReverbCount()
Definition sbs.cpp:2357
void DecrementReverbCount()
Definition sbs.cpp:2362
bool FastDelete
Definition sbs.h:188
SoundSystem * GetSoundSystem()
Definition sbs.cpp:4518
FMOD::System * GetFmodSystem()
Ogre::Vector3 Vector3
Definition globals.h:58
Ogre::Real Real
Definition globals.h:57
void SetCase(std::string &string, bool uppercase)
Definition globals.cpp:172