sound.h 3.8 KB

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