Open Source Tomb Raider Engine
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SoundManager.cpp 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*!
  2. * \file src/SoundManager.cpp
  3. * \brief This is the audio manager Implementation
  4. *
  5. * \author xythobuz
  6. */
  7. #include "imgui/imgui.h"
  8. #include "global.h"
  9. #include "Camera.h"
  10. #include "Log.h"
  11. #include "system/Sound.h"
  12. #include "SoundManager.h"
  13. void SoundSource::prepare() {
  14. if (source != -1)
  15. return;
  16. float vol;
  17. int index = SoundManager::getIndex(id, &vol);
  18. if ((index < 0) || (index > Sound::numBuffers())) {
  19. Log::get(LOG_ERROR) << "Invalid SoundSource ID (" << index << ", "
  20. << Sound::numBuffers() << ")!" << Log::endl;
  21. source = -2;
  22. } else {
  23. source = Sound::addSource(index, vol, false, true);
  24. if (source < 0) {
  25. source = -2;
  26. } else {
  27. int ret = Sound::sourceAt(source, pos);
  28. if (ret < 0) {
  29. Log::get(LOG_ERROR) << "Error positioning SoundSource " << id << Log::endl;
  30. }
  31. Sound::play(source, false);
  32. }
  33. }
  34. }
  35. // ----------------------------------------------------------------------------
  36. std::vector<SoundSource> SoundManager::soundSources;
  37. std::vector<int> SoundManager::soundMap;
  38. std::vector<SoundDetail> SoundManager::soundDetails;
  39. std::vector<int> SoundManager::sampleIndices;
  40. void SoundManager::clear() {
  41. soundSources.clear();
  42. soundMap.clear();
  43. soundDetails.clear();
  44. sampleIndices.clear();
  45. Sound::clear();
  46. }
  47. int SoundManager::prepareSources() {
  48. for (int i = 0; i < soundMap.size(); i++) {
  49. float vol;
  50. SoundDetail* sd;
  51. int index = getIndex(i, &vol, &sd);
  52. if ((index >= 0) && (index < Sound::numBuffers())) {
  53. int ret = Sound::addSource(index, vol, true, false);
  54. if (ret < 0) {
  55. Log::get(LOG_ERROR) << "Error adding SoundSource " << index << Log::endl;
  56. }
  57. sd->setSource(ret);
  58. }
  59. }
  60. for (int i = 0; i < soundSources.size(); i++) {
  61. soundSources.at(i).prepare();
  62. }
  63. return 0;
  64. }
  65. void SoundManager::addSoundSource(glm::vec3 p, int id, int flags) {
  66. soundSources.emplace_back(p, id, flags);
  67. }
  68. void SoundManager::addSoundMapEntry(int id) {
  69. soundMap.push_back(id);
  70. }
  71. void SoundManager::addSoundDetail(int sample, float volume) {
  72. soundDetails.emplace_back(sample, volume);
  73. }
  74. void SoundManager::addSampleIndex(int index) {
  75. sampleIndices.push_back(index);
  76. }
  77. int SoundManager::getIndex(int index, float* volume, SoundDetail** sd) {
  78. if (index <= -1)
  79. return -1;
  80. if (index >= soundMap.size())
  81. return -2; // SoundMap not big enough
  82. index = soundMap.at(index);
  83. if (index <= -1)
  84. return -3; // SoundMap has no entry here (-1)
  85. if (index >= soundDetails.size())
  86. return -4; // SoundMap entry is bigger than SoundDetails
  87. SoundDetail s = soundDetails.at(index);
  88. if (sd != nullptr)
  89. *sd = &soundDetails.at(index);
  90. if (volume != nullptr)
  91. *volume = s.getVolume();
  92. if (s.getSample() <= -1)
  93. return -5; // SoundDetail has no entry here (-1)
  94. if (s.getSample() >= sampleIndices.size())
  95. return -6; // SoundDetail entry is bigger than SampleIndices
  96. index = sampleIndices.at(s.getSample());
  97. if (index <= -1)
  98. return -7; // SampleIndices has no entry here (-1)
  99. return index;
  100. }
  101. int SoundManager::playSound(int index) {
  102. if ((index >= 0) && (index < soundMap.size())) {
  103. SoundDetail* sd;
  104. int i = getIndex(index, nullptr, &sd);
  105. if ((i < 0) || (i >= Sound::numBuffers()))
  106. return -2;
  107. Sound::play(sd->getSource(), true);
  108. return 0;
  109. } else {
  110. return -1;
  111. }
  112. }
  113. void SoundManager::display() {
  114. if (ImGui::CollapsingHeader("Sound Sources")) {
  115. ImGui::Columns(5, "soundsources");
  116. ImGui::Text("No"); ImGui::NextColumn();
  117. ImGui::Text("ID"); ImGui::NextColumn();
  118. ImGui::Text("Flags"); ImGui::NextColumn();
  119. ImGui::Text("Pos"); ImGui::NextColumn();
  120. ImGui::Text("Go"); ImGui::NextColumn();
  121. ImGui::Separator();
  122. for (int i = 0; i < soundSources.size(); i++) {
  123. auto& ss = soundSources.at(i);
  124. ImGui::Text("%03d", i);
  125. ImGui::NextColumn();
  126. ImGui::Text("%d", ss.getID());
  127. ImGui::NextColumn();
  128. ImGui::Text("%X", ss.getFlags());
  129. ImGui::NextColumn();
  130. ImGui::Text("%.1f %.1f %.1f", ss.getPos().x, ss.getPos().y, ss.getPos().z);
  131. ImGui::NextColumn();
  132. ImGui::PushID(i);
  133. if (ImGui::Button("Go!")) {
  134. Camera::setPosition(ss.getPos());
  135. }
  136. ImGui::PopID();
  137. ImGui::NextColumn();
  138. }
  139. ImGui::Columns(1);
  140. }
  141. if (ImGui::CollapsingHeader("Sound Player")) {
  142. if (!Sound::getEnabled()) {
  143. ImGui::Text("Please enable Sound first!");
  144. if (ImGui::Button("Enable Sound!")) {
  145. Sound::setEnabled(true);
  146. }
  147. return;
  148. } else if (Sound::numBuffers() == 0) {
  149. ImGui::Text("No Sounds currently loaded!");
  150. return;
  151. }
  152. static int index = 0;
  153. ImGui::Text("Map");
  154. ImGui::SameLine();
  155. ImGui::PushItemWidth(ImGui::GetWindowWidth() * 0.5f);
  156. ImGui::SliderInt("##soundslide", &index, 0, soundMap.size() - 1);
  157. ImGui::PopItemWidth();
  158. ImGui::SameLine();
  159. if (ImGui::Button("+##soundplus", ImVec2(0, 0), true)) {
  160. if (index < (soundMap.size() - 1))
  161. index++;
  162. else
  163. index = 0;
  164. }
  165. ImGui::SameLine();
  166. if (ImGui::Button("-##soundminus", ImVec2(0, 0), true)) {
  167. if (index > 0)
  168. index--;
  169. else
  170. index = soundMap.size() - 1;
  171. }
  172. ImGui::SameLine();
  173. if (ImGui::Button("Play##soundplay")) {
  174. playSound(index);
  175. }
  176. ImGui::SameLine();
  177. ImGui::Text("%d", getIndex(index));
  178. }
  179. }