sound.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. Minetest
  3. Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #ifndef SOUND_HEADER
  17. #define SOUND_HEADER
  18. #include "irrlichttypes_bloated.h"
  19. #include <string>
  20. #include <set>
  21. class OnDemandSoundFetcher
  22. {
  23. public:
  24. virtual void fetchSounds(const std::string &name,
  25. std::set<std::string> &dst_paths,
  26. std::set<std::string> &dst_datas) = 0;
  27. };
  28. struct SimpleSoundSpec
  29. {
  30. std::string name;
  31. float gain;
  32. SimpleSoundSpec(std::string name="", float gain=1.0):
  33. name(name),
  34. gain(gain)
  35. {}
  36. bool exists() {return name != "";}
  37. // Serialization intentionally left out
  38. };
  39. class ISoundManager
  40. {
  41. public:
  42. virtual ~ISoundManager(){}
  43. // Multiple sounds can be loaded per name; when played, the sound
  44. // should be chosen randomly from alternatives
  45. // Return value determines success/failure
  46. virtual bool loadSoundFile(const std::string &name,
  47. const std::string &filepath) = 0;
  48. virtual bool loadSoundData(const std::string &name,
  49. const std::string &filedata) = 0;
  50. virtual void updateListener(v3f pos, v3f vel, v3f at, v3f up) = 0;
  51. virtual void setListenerGain(float gain) = 0;
  52. // playSound functions return -1 on failure, otherwise a handle to the
  53. // sound. If name=="", call should be ignored without error.
  54. virtual int playSound(const std::string &name, bool loop,
  55. float volume) = 0;
  56. virtual int playSoundAt(const std::string &name, bool loop,
  57. float volume, v3f pos) = 0;
  58. virtual void stopSound(int sound) = 0;
  59. virtual bool soundExists(int sound) = 0;
  60. virtual void updateSoundPosition(int sound, v3f pos) = 0;
  61. int playSound(const SimpleSoundSpec &spec, bool loop)
  62. { return playSound(spec.name, loop, spec.gain); }
  63. int playSoundAt(const SimpleSoundSpec &spec, bool loop, v3f pos)
  64. { return playSoundAt(spec.name, loop, spec.gain, pos); }
  65. };
  66. class DummySoundManager: public ISoundManager
  67. {
  68. public:
  69. virtual bool loadSoundFile(const std::string &name,
  70. const std::string &filepath) {return true;}
  71. virtual bool loadSoundData(const std::string &name,
  72. const std::string &filedata) {return true;}
  73. void updateListener(v3f pos, v3f vel, v3f at, v3f up) {}
  74. void setListenerGain(float gain) {}
  75. int playSound(const std::string &name, bool loop,
  76. float volume) {return 0;}
  77. int playSoundAt(const std::string &name, bool loop,
  78. float volume, v3f pos) {return 0;}
  79. void stopSound(int sound) {}
  80. bool soundExists(int sound) {return false;}
  81. void updateSoundPosition(int sound, v3f pos) {}
  82. };
  83. // Global DummySoundManager singleton
  84. extern DummySoundManager dummySoundManager;
  85. #endif