sound.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. #pragma once
  17. #include <set>
  18. #include <string>
  19. #include "irrlichttypes_bloated.h"
  20. class OnDemandSoundFetcher
  21. {
  22. public:
  23. virtual void fetchSounds(const std::string &name,
  24. std::set<std::string> &dst_paths,
  25. std::set<std::string> &dst_datas) = 0;
  26. };
  27. struct SimpleSoundSpec
  28. {
  29. SimpleSoundSpec(const std::string &name = "", float gain = 1.0f,
  30. float fade = 0.0f, float pitch = 1.0f)
  31. : name(name), gain(gain), fade(fade), pitch(pitch)
  32. {
  33. }
  34. bool exists() const { return !name.empty(); }
  35. std::string name = "";
  36. float gain = 1.0f;
  37. float fade = 0.0f;
  38. float pitch = 1.0f;
  39. };
  40. class ISoundManager
  41. {
  42. public:
  43. virtual ~ISoundManager() = default;
  44. // Multiple sounds can be loaded per name; when played, the sound
  45. // should be chosen randomly from alternatives
  46. // Return value determines success/failure
  47. virtual bool loadSoundFile(
  48. const std::string &name, const std::string &filepath) = 0;
  49. virtual bool loadSoundData(
  50. const std::string &name, const std::string &filedata) = 0;
  51. virtual void updateListener(v3f pos, v3f vel, v3f at, v3f up) = 0;
  52. virtual void setListenerGain(float gain) = 0;
  53. // playSound functions return -1 on failure, otherwise a handle to the
  54. // sound. If name=="", call should be ignored without error.
  55. virtual int playSound(const std::string &name, bool loop, float volume,
  56. float fade = 0.0f, float pitch = 1.0f) = 0;
  57. virtual int playSoundAt(const std::string &name, bool loop, float volume, v3f pos,
  58. float pitch = 1.0f) = 0;
  59. virtual void stopSound(int sound) = 0;
  60. virtual bool soundExists(int sound) = 0;
  61. virtual void updateSoundPosition(int sound, v3f pos) = 0;
  62. virtual bool updateSoundGain(int id, float gain) = 0;
  63. virtual float getSoundGain(int id) = 0;
  64. virtual void step(float dtime) = 0;
  65. virtual void fadeSound(int sound, float step, float gain) = 0;
  66. int playSound(const SimpleSoundSpec &spec, bool loop)
  67. {
  68. return playSound(spec.name, loop, spec.gain, spec.fade, spec.pitch);
  69. }
  70. int playSoundAt(const SimpleSoundSpec &spec, bool loop, const v3f &pos)
  71. {
  72. return playSoundAt(spec.name, loop, spec.gain, pos, spec.pitch);
  73. }
  74. };
  75. class DummySoundManager : public ISoundManager
  76. {
  77. public:
  78. virtual bool loadSoundFile(const std::string &name, const std::string &filepath)
  79. {
  80. return true;
  81. }
  82. virtual bool loadSoundData(const std::string &name, const std::string &filedata)
  83. {
  84. return true;
  85. }
  86. void updateListener(v3f pos, v3f vel, v3f at, v3f up) {}
  87. void setListenerGain(float gain) {}
  88. int playSound(const std::string &name, bool loop, float volume, float fade,
  89. float pitch)
  90. {
  91. return 0;
  92. }
  93. int playSoundAt(const std::string &name, bool loop, float volume, v3f pos,
  94. float pitch)
  95. {
  96. return 0;
  97. }
  98. void stopSound(int sound) {}
  99. bool soundExists(int sound) { return false; }
  100. void updateSoundPosition(int sound, v3f pos) {}
  101. bool updateSoundGain(int id, float gain) { return false; }
  102. float getSoundGain(int id) { return 0; }
  103. void step(float dtime) {}
  104. void fadeSound(int sound, float step, float gain) {}
  105. };
  106. // Global DummySoundManager singleton
  107. extern DummySoundManager dummySoundManager;