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.2KB

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