sound_object.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // SuperTux
  2. // Copyright (C) 2023 mrkubax10 <mrkubax10@onet.pl>
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. #include "object/sound_object.hpp"
  17. #include <limits>
  18. #include "audio/sound_manager.hpp"
  19. #include "audio/sound_source.hpp"
  20. #include "editor/editor.hpp"
  21. #include "util/reader_mapping.hpp"
  22. SoundObject::SoundObject(const ReaderMapping& mapping) :
  23. GameObject(mapping),
  24. ExposedObject<SoundObject, scripting::SoundObject>(this),
  25. m_sample(),
  26. m_sound_source(),
  27. m_volume(),
  28. m_started(false)
  29. {
  30. mapping.get("sample", m_sample, "");
  31. mapping.get("volume", m_volume, 1.0f);
  32. prepare_sound_source();
  33. }
  34. SoundObject::SoundObject(float vol, const std::string& file) :
  35. ExposedObject<SoundObject, scripting::SoundObject>(this),
  36. m_sample(file),
  37. m_sound_source(),
  38. m_volume(vol),
  39. m_started(false)
  40. {
  41. prepare_sound_source();
  42. }
  43. SoundObject::~SoundObject()
  44. {
  45. stop_looping_sounds();
  46. }
  47. void
  48. SoundObject::update(float dt_sec)
  49. {
  50. if (m_started)
  51. return;
  52. m_sound_source->play();
  53. m_started = true;
  54. }
  55. ObjectSettings
  56. SoundObject::get_settings()
  57. {
  58. ObjectSettings result = GameObject::get_settings();
  59. result.add_sound(_("Sound"), &m_sample, "sample");
  60. result.add_float(_("Volume"), &m_volume, "volume");
  61. result.reorder({"sample", "volume", "name"});
  62. result.add_remove();
  63. return result;
  64. }
  65. void
  66. SoundObject::stop_looping_sounds()
  67. {
  68. if (m_sound_source)
  69. m_sound_source->pause();
  70. }
  71. void
  72. SoundObject::play_looping_sounds()
  73. {
  74. if (!Editor::is_active() && m_sound_source)
  75. m_sound_source->play();
  76. }
  77. void
  78. SoundObject::prepare_sound_source()
  79. {
  80. if (Editor::is_active())
  81. return;
  82. if (m_sample.empty())
  83. {
  84. remove_me();
  85. return;
  86. }
  87. try
  88. {
  89. m_sound_source = SoundManager::current()->create_sound_source(m_sample);
  90. if (!m_sound_source)
  91. throw std::runtime_error("file not found");
  92. m_sound_source->set_gain(0);
  93. m_sound_source->set_looping(true);
  94. m_sound_source->set_relative(true);
  95. m_sound_source->set_gain(m_volume);
  96. }
  97. catch(const std::exception& e)
  98. {
  99. log_warning << "Couldn't load '" << m_sample << "': " << e.what() << std::endl;
  100. m_sound_source.reset();
  101. remove_me();
  102. }
  103. }
  104. void
  105. SoundObject::set_volume(float volume)
  106. {
  107. m_volume = volume;
  108. m_sound_source->set_gain(volume);
  109. }
  110. /* EOF */