sound.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. Minetest
  3. Copyright (C) 2023 DS
  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. #include "sound.h"
  17. #include "filesys.h"
  18. #include "log.h"
  19. #include "porting.h"
  20. #include "settings.h"
  21. #include "util/numeric.h"
  22. #include <algorithm>
  23. #include <string>
  24. #include <vector>
  25. std::vector<std::string> SoundFallbackPathProvider::
  26. getLocalFallbackPathsForSoundname(const std::string &name)
  27. {
  28. std::vector<std::string> paths;
  29. // only try each name once
  30. if (m_done_names.count(name))
  31. return paths;
  32. m_done_names.insert(name);
  33. addThePaths(name, paths);
  34. // remove duplicates
  35. std::sort(paths.begin(), paths.end());
  36. auto end = std::unique(paths.begin(), paths.end());
  37. paths.erase(end, paths.end());
  38. return paths;
  39. }
  40. void SoundFallbackPathProvider::addAllAlternatives(const std::string &common,
  41. std::vector<std::string> &paths)
  42. {
  43. paths.reserve(paths.size() + 11);
  44. for (auto &&ext : {".ogg", ".0.ogg", ".1.ogg", ".2.ogg", ".3.ogg", ".4.ogg",
  45. ".5.ogg", ".6.ogg", ".7.ogg", ".8.ogg", ".9.ogg", }) {
  46. paths.push_back(common + ext);
  47. }
  48. }
  49. void SoundFallbackPathProvider::addThePaths(const std::string &name,
  50. std::vector<std::string> &paths)
  51. {
  52. addAllAlternatives(porting::path_share + DIR_DELIM + "sounds" + DIR_DELIM + name, paths);
  53. addAllAlternatives(porting::path_user + DIR_DELIM + "sounds" + DIR_DELIM + name, paths);
  54. }
  55. void ISoundManager::reportRemovedSound(sound_handle_t id)
  56. {
  57. if (id <= 0)
  58. return;
  59. freeId(id);
  60. m_removed_sounds.push_back(id);
  61. }
  62. sound_handle_t ISoundManager::allocateId(u32 num_owners)
  63. {
  64. while (m_occupied_ids.find(m_next_id) != m_occupied_ids.end()
  65. || m_next_id == SOUND_HANDLE_T_MAX) {
  66. m_next_id = static_cast<sound_handle_t>(
  67. myrand() % static_cast<u32>(SOUND_HANDLE_T_MAX - 1) + 1);
  68. }
  69. sound_handle_t id = m_next_id++;
  70. m_occupied_ids.emplace(id, num_owners);
  71. return id;
  72. }
  73. void ISoundManager::freeId(sound_handle_t id, u32 num_owners)
  74. {
  75. auto it = m_occupied_ids.find(id);
  76. if (it == m_occupied_ids.end())
  77. return;
  78. if (it->second <= num_owners)
  79. m_occupied_ids.erase(it);
  80. else
  81. it->second -= num_owners;
  82. }
  83. void sound_volume_control(ISoundManager *sound_mgr, bool is_window_active)
  84. {
  85. bool mute_sound = g_settings->getBool("mute_sound");
  86. if (mute_sound) {
  87. sound_mgr->setListenerGain(0.0f);
  88. } else {
  89. // Check if volume is in the proper range, else fix it.
  90. float old_volume = g_settings->getFloat("sound_volume");
  91. float new_volume = rangelim(old_volume, 0.0f, 1.0f);
  92. if (old_volume != new_volume) {
  93. g_settings->setFloat("sound_volume", new_volume);
  94. }
  95. if (!is_window_active) {
  96. new_volume *= g_settings->getFloat("sound_volume_unfocused");
  97. new_volume = rangelim(new_volume, 0.0f, 1.0f);
  98. }
  99. sound_mgr->setListenerGain(new_volume);
  100. }
  101. }