sound.h 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 "util/serialize.h"
  20. #include "irrlichttypes_bloated.h"
  21. /**
  22. * Describes the sound information for playback.
  23. * Positional handling is done separately.
  24. *
  25. * `SimpleSoundSpec`, as used by modding, is a `SoundSpec` with only name, fain,
  26. * pitch and fade.
  27. */
  28. struct SoundSpec
  29. {
  30. SoundSpec(std::string_view name = "", float gain = 1.0f,
  31. bool loop = false, float fade = 0.0f, float pitch = 1.0f,
  32. float start_time = 0.0f) :
  33. name(name), gain(gain), fade(fade), pitch(pitch), start_time(start_time),
  34. loop(loop)
  35. {
  36. }
  37. bool exists() const { return !name.empty(); }
  38. /**
  39. * Serialize a `SimpleSoundSpec`.
  40. */
  41. void serializeSimple(std::ostream &os, u16 protocol_version) const
  42. {
  43. os << serializeString16(name);
  44. writeF32(os, gain);
  45. writeF32(os, pitch);
  46. writeF32(os, fade);
  47. }
  48. /**
  49. * Deserialize a `SimpleSoundSpec`.
  50. */
  51. void deSerializeSimple(std::istream &is, u16 protocol_version)
  52. {
  53. name = deSerializeString16(is);
  54. gain = readF32(is);
  55. pitch = readF32(is);
  56. fade = readF32(is);
  57. }
  58. // Name of the sound-group
  59. std::string name;
  60. float gain = 1.0f;
  61. float fade = 0.0f;
  62. float pitch = 1.0f;
  63. float start_time = 0.0f;
  64. bool loop = false;
  65. // If true, a local fallback (ie. from the user's sound pack) is used if the
  66. // sound-group does not exist.
  67. bool use_local_fallback = true;
  68. };
  69. // The order must not be changed. This is sent over the network.
  70. enum class SoundLocation : u8 {
  71. Local,
  72. Position,
  73. Object
  74. };