Open Source Tomb Raider Engine
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

SoundManager.cpp 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. Sound::clear();
  20. }
  21. int SoundManager::prepareSources() {
  22. for (int i = 0; i < soundSources.size(); i++) {
  23. float vol;
  24. int index = getIndex(soundSources.at(i).id, &vol);
  25. int ret = Sound::addSource(index, vol, false, true);
  26. assert(ret == i);
  27. float pos[3] = { soundSources.at(i).x, soundSources.at(i).y, soundSources.at(i).z };
  28. ret = Sound::sourceAt(i, pos);
  29. assert(ret == 0);
  30. Sound::play(i, false);
  31. }
  32. for (int i = 0; i < soundMap.size(); i++) {
  33. float vol;
  34. int index = getIndex(i, &vol);
  35. if ((index >= 0) && (index < Sound::numBuffers())) {
  36. int ret = Sound::addSource(index, vol, true, false);
  37. assert(ret >= 0);
  38. }
  39. }
  40. }
  41. void SoundManager::addSoundSource(float x, float y, float z, int id, int flags) {
  42. soundSources.emplace_back(x, y, z, id, flags);
  43. }
  44. void SoundManager::addSoundMapEntry(int id) {
  45. soundMap.push_back(id);
  46. }
  47. void SoundManager::addSoundDetail(int sample, float volume) {
  48. soundDetails.emplace_back(sample, volume);
  49. }
  50. void SoundManager::addSampleIndex(int index) {
  51. sampleIndices.push_back(index);
  52. }
  53. int SoundManager::sizeSoundMap() {
  54. return soundMap.size();
  55. }
  56. int SoundManager::getIndex(int index, float* volume) {
  57. if (index <= -1)
  58. return -1;
  59. if (index >= soundMap.size())
  60. return -2; // SoundMap not big enough
  61. index = soundMap.at(index);
  62. if (index <= -1)
  63. return -3; // SoundMap has no entry here (-1)
  64. if (index >= soundDetails.size())
  65. return -4; // SoundMap entry is bigger than SoundDetails
  66. SoundDetail s = soundDetails.at(index);
  67. if (volume != nullptr)
  68. *volume = s.volume;
  69. if (s.sample <= -1)
  70. return -5; // SoundDetail has no entry here (-1)
  71. if (s.sample >= sampleIndices.size())
  72. return -6; // SoundDetail entry is bigger than SampleIndices
  73. index = sampleIndices.at(s.sample);
  74. if (index <= -1)
  75. return -7; // SampleIndices has no entry here (-1)
  76. return index;
  77. }
  78. int SoundManager::playSound(int index) {
  79. if ((index >= 0) && (index < soundMap.size())) {
  80. if (soundMap.at(index) == -1)
  81. return 0;
  82. int c = 1;
  83. for (int i = 0; i < index; i++)
  84. if (soundMap.at(i) != -1)
  85. c++;
  86. Sound::play(c, true);
  87. return 0;
  88. } else {
  89. return -1;
  90. }
  91. }