OpenRaider  0.1.4-dev
Open Source Tomb Raider Game Engine implementation
SoundManager.cpp
Go to the documentation of this file.
1 
8 #include "imgui/imgui.h"
9 
10 #include "global.h"
11 #include "Camera.h"
12 #include "Log.h"
13 #include "system/Sound.h"
14 #include "SoundManager.h"
15 
17  if (source != -1)
18  return;
19 
20  float vol;
21  int index = SoundManager::getIndex(id, &vol);
22  if ((index < 0) || (index > Sound::numBuffers())) {
23  Log::get(LOG_ERROR) << "Invalid SoundSource ID (" << index << ", "
24  << Sound::numBuffers() << ")!" << Log::endl;
25  source = -2;
26  } else {
27  source = Sound::addSource(index, vol, false, true);
28  if (source < 0) {
29  source = -2;
30  } else {
31  int ret = Sound::sourceAt(source, pos);
32  if (ret < 0) {
33  Log::get(LOG_ERROR) << "Error positioning SoundSource " << id << Log::endl;
34  }
35  }
36  }
37 }
38 
40  playing = true;
41 
42  if (source >= 0)
43  Sound::play(source, false);
44 }
45 
47  playing = false;
48 
49  if (source >= 0)
51 }
52 
53 // ----------------------------------------------------------------------------
54 
55 std::vector<SoundSource> SoundManager::soundSources;
56 std::vector<int> SoundManager::soundMap;
57 std::vector<SoundDetail> SoundManager::soundDetails;
58 std::vector<int> SoundManager::sampleIndices;
59 
61  soundSources.clear();
62  soundMap.clear();
63  soundDetails.clear();
64  sampleIndices.clear();
65 
66  Sound::clear();
67 }
68 
70  for (int i = 0; i < soundMap.size(); i++) {
71  float vol;
72  SoundDetail* sd;
73  int index = getIndex(i, &vol, &sd);
74  if ((index >= 0) && (index < Sound::numBuffers())) {
75  int ret = Sound::addSource(index, vol, true, false);
76  if (ret < 0) {
77  Log::get(LOG_ERROR) << "Error adding SoundSource " << index << Log::endl;
78  }
79  sd->setSource(ret);
80  }
81  }
82 
83  for (int i = 0; i < soundSources.size(); i++) {
84  soundSources.at(i).prepare();
85  soundSources.at(i).play();
86  }
87 
88  return 0;
89 }
90 
91 void SoundManager::addSoundSource(glm::vec3 p, int id, int flags) {
92  soundSources.emplace_back(p, id, flags);
93 }
94 
96  soundMap.push_back(id);
97 }
98 
99 void SoundManager::addSoundDetail(int sample, float volume) {
100  soundDetails.emplace_back(sample, volume);
101 }
102 
104  sampleIndices.push_back(index);
105 }
106 
107 int SoundManager::getIndex(int index, float* volume, SoundDetail** sd) {
108  if (index <= -1)
109  return -1;
110 
111  if (index >= soundMap.size())
112  return -2; // SoundMap not big enough
113 
114  index = soundMap.at(index);
115 
116  if (index <= -1)
117  return -3; // SoundMap has no entry here (-1)
118 
119  if (index >= soundDetails.size())
120  return -4; // SoundMap entry is bigger than SoundDetails
121 
122  SoundDetail s = soundDetails.at(index);
123 
124  if (sd != nullptr)
125  *sd = &soundDetails.at(index);
126 
127  if (volume != nullptr)
128  *volume = s.getVolume();
129 
130  if (s.getSample() <= -1)
131  return -5; // SoundDetail has no entry here (-1)
132 
133  if (s.getSample() >= sampleIndices.size())
134  return -6; // SoundDetail entry is bigger than SampleIndices
135 
136  index = sampleIndices.at(s.getSample());
137 
138  if (index <= -1)
139  return -7; // SampleIndices has no entry here (-1)
140 
141  return index;
142 }
143 
144 int SoundManager::playSound(int index) {
145  if ((index >= 0) && (index < soundMap.size())) {
146  SoundDetail* sd;
147  int i = getIndex(index, nullptr, &sd);
148  if ((i < 0) || (i >= Sound::numBuffers()))
149  return -2;
150 
151  Sound::play(sd->getSource(), true);
152  return 0;
153  } else {
154  return -1;
155  }
156 }
157 
159  static bool offsets = false;
160  if (ImGui::CollapsingHeader("Sound Sources")) {
161  ImGui::Columns(5, "soundsources");
162  ImGui::Text("No");
164  ImGui::Text("ID");
166  ImGui::Text("Flags");
168  ImGui::Text("Pos");
170  ImGui::Text("Tools");
173  if (!offsets) {
174  ImGui::SetColumnOffset(1, 40.0f);
175  ImGui::SetColumnOffset(2, 80.0f);
176  ImGui::SetColumnOffset(3, 130.0f);
177  ImGui::SetColumnOffset(4, 350.0f);
178  offsets = true;
179  }
180  for (int i = 0; i < soundSources.size(); i++) {
181  auto& ss = soundSources.at(i);
182  ImGui::Text("%03d", i);
184  ImGui::Text("%d", ss.getID());
186  ImGui::Text("0x%X", ss.getFlags());
188  ImGui::Text("%.1f %.1f %.1f", ss.getPos().x, ss.getPos().y, ss.getPos().z);
190  ImGui::PushID(i);
191  if (ImGui::Button("Warp")) {
192  Camera::setPosition(ss.getPos());
193  }
194  ImGui::SameLine();
195  if (ImGui::Button("Play")) {
196  playSound(soundSources.at(i).getID());
197  }
198  ImGui::SameLine();
199  if (ss.isPlaying()) {
200  if (ImGui::Button("Stop")) {
201  ss.stop();
202  }
203  } else {
204  if (ImGui::Button("Start")) {
205  ss.play();
206  }
207  }
208  ImGui::PopID();
210  }
211  ImGui::Columns(1);
212  }
213 
214  static bool offsets2 = false;
215  if (ImGui::CollapsingHeader("Sound Details")) {
216  ImGui::Columns(4, "sounddetails");
217  ImGui::Text("No");
219  ImGui::Text("Vol");
221  ImGui::Text("Sample");
223  ImGui::Text("Tools");
226  if (!offsets2) {
227  ImGui::SetColumnOffset(1, 40.0f);
228  ImGui::SetColumnOffset(2, 80.0f);
229  ImGui::SetColumnOffset(3, 180.0f);
230  offsets2 = true;
231  }
232  for (int i = 0; i < soundDetails.size(); i++) {
233  auto& sd = soundDetails.at(i);
234  ImGui::Text("%03d", i);
236  ImGui::Text("%.2f", sd.getVolume());
238  if ((sd.getSample() < 0) || (sd.getSample() >= sampleIndices.size())) {
239  ImGui::Text("%03d --> ???", sd.getSample());
240  } else {
241  ImGui::Text("%03d --> %03d", sd.getSample(), sampleIndices.at(sd.getSample()));
242  }
244  ImGui::PushID(i);
245  if (sd.getSource() >= 0) {
246  if (ImGui::Button("Play")) {
247  Sound::play(sd.getSource(), true);
248  }
249  }
250  ImGui::PopID();
252  }
253  ImGui::Columns(1);
254  }
255 
256  if (ImGui::CollapsingHeader("Sound Map Player")) {
257  if (!Sound::getEnabled()) {
258  ImGui::Text("Please enable Sound first!");
259  if (ImGui::Button("Enable Sound!")) {
260  Sound::setEnabled(true);
261  }
262  return;
263  } else if (Sound::numBuffers() == 0) {
264  ImGui::Text("No Sounds currently loaded!");
265  return;
266  }
267 
268  static int index = 0;
269  ImGui::Text("Map");
270  ImGui::SameLine();
272  ImGui::SliderInt("##soundslide", &index, 0, soundMap.size() - 1);
274  ImGui::SameLine();
275  if (ImGui::Button("+##soundplus", ImVec2(0, 0), true)) {
276  if (index < (soundMap.size() - 1))
277  index++;
278  else
279  index = 0;
280  }
281  ImGui::SameLine();
282  if (ImGui::Button("-##soundminus", ImVec2(0, 0), true)) {
283  if (index > 0)
284  index--;
285  else
286  index = soundMap.size() - 1;
287  }
288  ImGui::SameLine();
289  if (ImGui::Button("Play##soundplay")) {
290  playSound(index);
291  }
292  ImGui::SameLine();
293  ImGui::Text("%d", getIndex(index));
294  }
295 }
296 
IMGUI_API void PopItemWidth()
Definition: imgui.cpp:3256
IMGUI_API bool CollapsingHeader(const char *label, const char *str_id=NULL, bool display_frame=true, bool default_open=false)
Definition: imgui.cpp:4332
static int getIndex(int index, float *volume=nullptr, SoundDetail **sd=nullptr)
IMGUI_API void NextColumn()
Definition: imgui.cpp:6575
IMGUI_API void SetColumnOffset(int column_index, float offset_x)
Definition: imgui.cpp:6636
static std::vector< int > sampleIndices
Definition: SoundManager.h:66
static std::vector< SoundDetail > soundDetails
Definition: SoundManager.h:65
glm::vec3 pos
Definition: SoundManager.h:27
Sound Source Manager.
static void stop(int source)
Definition: Sound.cpp:87
Included everywhere.
float getVolume()
Definition: SoundManager.h:36
static LogLevel & get(int level)
Definition: Log.cpp:14
static int numBuffers()
Definition: Sound.cpp:35
Definition: imgui.h:50
Global Logging Utility.
int getSample()
Definition: SoundManager.h:35
static int sourceAt(int source, glm::vec3 pos)
Definition: Sound.cpp:67
static int prepareSources()
IMGUI_API bool Button(const char *label, const ImVec2 &size=ImVec2(0, 0), bool repeat_when_held=false)
Definition: imgui.cpp:4055
static void setEnabled(bool on=true)
Definition: Sound.cpp:99
static void addSampleIndex(int index)
IMGUI_API void PopID()
Definition: imgui.cpp:4588
static void addSoundSource(glm::vec3 p, int id, int flags)
static std::vector< int > soundMap
Definition: SoundManager.h:64
static void addSoundMapEntry(int id)
void setSource(int s)
Definition: SoundManager.h:38
static const char endl
Definition: Log.h:35
IMGUI_API void Columns(int count=1, const char *id=NULL, bool border=true)
Definition: imgui.cpp:6674
int getSource()
Definition: SoundManager.h:37
IMGUI_API void SameLine(int column_x=0, int spacing_w=-1)
Definition: imgui.cpp:6551
#define LOG_ERROR
Definition: Log.h:19
static int addSource(int buffer, float volume=1.0f, bool atListener=false, bool loop=false)
Definition: Sound.cpp:59
static bool getEnabled()
Definition: Sound.cpp:105
static void setPosition(glm::vec3 p)
Definition: Camera.h:26
IMGUI_API void Text(const char *fmt,...)
Definition: imgui.cpp:3802
static void clear()
IMGUI_API void Separator()
Definition: imgui.cpp:6448
Camera, View Frustum.
static std::vector< SoundSource > soundSources
Definition: SoundManager.h:63
void prepare()
static void clear()
Definition: Sound.cpp:29
IMGUI_API float GetWindowWidth()
Definition: imgui.cpp:3484
Sound Interface.
static void display()
static void play(int source, bool atListener=false)
Definition: Sound.cpp:81
IMGUI_API bool SliderInt(const char *label, int *v, int v_min, int v_max, const char *display_format="%.0f")
Definition: imgui.cpp:4878
IMGUI_API void PushItemWidth(float item_width)
Definition: imgui.cpp:3250
static int playSound(int index)
static void addSoundDetail(int sample, float volume)
IMGUI_API void PushID(const char *str_id)
Definition: imgui.cpp:4569