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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*!
  2. * \file src/SoundManager.cpp
  3. * \brief This is the audio manager Implementation
  4. *
  5. * \author xythobuz
  6. */
  7. #include "global.h"
  8. #include "system/Sound.h"
  9. #include "SoundManager.h"
  10. std::vector<SoundSource> SoundManager::soundSources;
  11. std::vector<int> SoundManager::soundMap;
  12. std::vector<SoundDetail> SoundManager::soundDetails;
  13. std::vector<int> SoundManager::sampleIndices;
  14. void SoundManager::clear() {
  15. soundSources.clear();
  16. soundMap.clear();
  17. soundDetails.clear();
  18. sampleIndices.clear();
  19. }
  20. int SoundManager::prepareSources() {
  21. for (int i = 0; i < soundSources.size(); i++) {
  22. float vol;
  23. int index = getIndex(soundSources.at(i).id, &vol);
  24. int ret = Sound::addSource(index, vol, false, true);
  25. assert(ret == i);
  26. float pos[3] = { soundSources.at(i).x, soundSources.at(i).y, soundSources.at(i).z };
  27. ret = Sound::sourceAt(i, pos);
  28. assert(ret == 0);
  29. Sound::play(i, false);
  30. }
  31. for (int i = 0; i < soundMap.size(); i++) {
  32. float vol;
  33. int index = getIndex(i, &vol);
  34. if ((index >= 0) && (index < Sound::numBuffers())) {
  35. int ret = Sound::addSource(index, vol, true, false);
  36. assert(ret >= 0);
  37. }
  38. }
  39. }
  40. void SoundManager::addSoundSource(float x, float y, float z, int id, int flags) {
  41. soundSources.emplace_back(x, y, z, id, flags);
  42. }
  43. void SoundManager::addSoundMapEntry(int id) {
  44. soundMap.push_back(id);
  45. }
  46. void SoundManager::addSoundDetail(int sample, float volume) {
  47. soundDetails.emplace_back(sample, volume);
  48. }
  49. void SoundManager::addSampleIndex(int index) {
  50. sampleIndices.push_back(index);
  51. }
  52. int SoundManager::sizeSoundMap() {
  53. return soundMap.size();
  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. }