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 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 "system/Sound.h"
  10. #include "SoundManager.h"
  11. std::vector<SoundSource> SoundManager::soundSources;
  12. std::vector<int> SoundManager::soundMap;
  13. std::vector<SoundDetail> SoundManager::soundDetails;
  14. std::vector<int> SoundManager::sampleIndices;
  15. void SoundManager::clear() {
  16. soundSources.clear();
  17. soundMap.clear();
  18. soundDetails.clear();
  19. sampleIndices.clear();
  20. Sound::clear();
  21. }
  22. int SoundManager::prepareSources() {
  23. for (int i = 0; i < soundSources.size(); i++) {
  24. float vol;
  25. int index = getIndex(soundSources.at(i).id, &vol);
  26. int ret = Sound::addSource(index, vol, false, true);
  27. assertEqual(ret, i);
  28. glm::vec3 pos = soundSources.at(i).pos;
  29. ret = Sound::sourceAt(i, pos);
  30. assertEqual(ret, 0);
  31. Sound::play(i, false);
  32. }
  33. for (int i = 0; i < soundMap.size(); i++) {
  34. float vol;
  35. int index = getIndex(i, &vol);
  36. if ((index >= 0) && (index < Sound::numBuffers())) {
  37. int ret = Sound::addSource(index, vol, true, false);
  38. assertGreaterThanEqual(ret, 0);
  39. }
  40. }
  41. return 0;
  42. }
  43. void SoundManager::addSoundSource(glm::vec3 p, int id, int flags) {
  44. soundSources.emplace_back(p, id, flags);
  45. }
  46. void SoundManager::addSoundMapEntry(int id) {
  47. soundMap.push_back(id);
  48. }
  49. void SoundManager::addSoundDetail(int sample, float volume) {
  50. soundDetails.emplace_back(sample, volume);
  51. }
  52. void SoundManager::addSampleIndex(int index) {
  53. sampleIndices.push_back(index);
  54. }
  55. int SoundManager::getIndex(int index, float* volume) {
  56. if (index <= -1)
  57. return -1;
  58. if (index >= soundMap.size())
  59. return -2; // SoundMap not big enough
  60. index = soundMap.at(index);
  61. if (index <= -1)
  62. return -3; // SoundMap has no entry here (-1)
  63. if (index >= soundDetails.size())
  64. return -4; // SoundMap entry is bigger than SoundDetails
  65. SoundDetail s = soundDetails.at(index);
  66. if (volume != nullptr)
  67. *volume = s.volume;
  68. if (s.sample <= -1)
  69. return -5; // SoundDetail has no entry here (-1)
  70. if (s.sample >= sampleIndices.size())
  71. return -6; // SoundDetail entry is bigger than SampleIndices
  72. index = sampleIndices.at(s.sample);
  73. if (index <= -1)
  74. return -7; // SampleIndices has no entry here (-1)
  75. return index;
  76. }
  77. int SoundManager::playSound(int index) {
  78. if ((index >= 0) && (index < soundMap.size())) {
  79. if (soundMap.at(index) == -1)
  80. return 0;
  81. int c = 1;
  82. for (int i = 0; i < index; i++)
  83. if (soundMap.at(i) != -1)
  84. c++;
  85. Sound::play(c, true);
  86. return 0;
  87. } else {
  88. return -1;
  89. }
  90. }
  91. void SoundManager::display() {
  92. if (ImGui::CollapsingHeader("Sound Player")) {
  93. if (!Sound::getEnabled()) {
  94. ImGui::Text("Please enable Sound first!");
  95. if (ImGui::Button("Enable Sound!")) {
  96. Sound::setEnabled(true);
  97. }
  98. return;
  99. } else if (Sound::numBuffers() == 0) {
  100. ImGui::Text("No Sounds currently loaded!");
  101. return;
  102. }
  103. static int index = 0;
  104. ImGui::Text("Map");
  105. ImGui::SameLine();
  106. ImGui::PushItemWidth(ImGui::GetWindowWidth() * 0.5f);
  107. ImGui::SliderInt("##soundslide", &index, 0, soundMap.size() - 1);
  108. ImGui::PopItemWidth();
  109. ImGui::SameLine();
  110. if (ImGui::Button("+##soundplus", ImVec2(0, 0), true)) {
  111. if (index < (soundMap.size() - 1))
  112. index++;
  113. else
  114. index = 0;
  115. }
  116. ImGui::SameLine();
  117. if (ImGui::Button("-##soundminus", ImVec2(0, 0), true)) {
  118. if (index > 0)
  119. index--;
  120. else
  121. index = soundMap.size() - 1;
  122. }
  123. ImGui::SameLine();
  124. if (ImGui::Button("Play##soundplay")) {
  125. playSound(index);
  126. }
  127. ImGui::SameLine();
  128. ImGui::Text("%d", getIndex(index));
  129. }
  130. }