spatial_sample_player.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*************************************************************************/
  2. /* spatial_sample_player.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "spatial_sample_player.h"
  31. #include "camera.h"
  32. #include "scene/scene_string_names.h"
  33. #include "servers/audio_server.h"
  34. #include "servers/spatial_sound_server.h"
  35. bool SpatialSamplePlayer::_set(const StringName &p_name, const Variant &p_value) {
  36. String name = p_name;
  37. if (name == SceneStringNames::get_singleton()->play_play) {
  38. if (library.is_valid()) {
  39. String what = p_value;
  40. if (what == "")
  41. stop_all();
  42. else
  43. play(what);
  44. played_back = what;
  45. }
  46. return true;
  47. }
  48. return false;
  49. }
  50. bool SpatialSamplePlayer::_get(const StringName &p_name, Variant &r_ret) const {
  51. String name = p_name;
  52. if (name == SceneStringNames::get_singleton()->play_play) {
  53. r_ret = played_back;
  54. return true;
  55. }
  56. return false;
  57. }
  58. void SpatialSamplePlayer::_get_property_list(List<PropertyInfo> *p_list) const {
  59. String en = "";
  60. if (library.is_valid()) {
  61. List<StringName> samples;
  62. Ref<SampleLibrary> ncl = library;
  63. ncl->get_sample_list(&samples);
  64. for (List<StringName>::Element *E = samples.front(); E; E = E->next()) {
  65. en += ",";
  66. en += E->get();
  67. }
  68. }
  69. p_list->push_back(PropertyInfo(Variant::STRING, "play/play", PROPERTY_HINT_ENUM, en, PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_ANIMATE_AS_TRIGGER));
  70. }
  71. void SpatialSamplePlayer::_notification(int p_what) {
  72. switch (p_what) {
  73. case NOTIFICATION_ENTER_WORLD: {
  74. SpatialSoundServer::get_singleton()->source_set_polyphony(get_source_rid(), polyphony);
  75. } break;
  76. }
  77. }
  78. void SpatialSamplePlayer::set_sample_library(const Ref<SampleLibrary> &p_library) {
  79. library = p_library;
  80. _change_notify();
  81. update_configuration_warning();
  82. }
  83. Ref<SampleLibrary> SpatialSamplePlayer::get_sample_library() const {
  84. return library;
  85. }
  86. void SpatialSamplePlayer::set_polyphony(int p_voice_count) {
  87. ERR_FAIL_COND(p_voice_count < 0 || p_voice_count > 64);
  88. polyphony = p_voice_count;
  89. if (get_source_rid().is_valid())
  90. SpatialSoundServer::get_singleton()->source_set_polyphony(get_source_rid(), polyphony);
  91. }
  92. int SpatialSamplePlayer::get_polyphony() const {
  93. return polyphony;
  94. }
  95. SpatialSamplePlayer::VoiceID SpatialSamplePlayer::play(const String &p_sample, int p_voice) {
  96. if (!get_source_rid().is_valid())
  97. return INVALID_VOICE;
  98. if (library.is_null())
  99. return INVALID_VOICE;
  100. if (!library->has_sample(p_sample))
  101. return INVALID_VOICE;
  102. Ref<Sample> sample = library->get_sample(p_sample);
  103. float vol_change = library->sample_get_volume_db(p_sample);
  104. float pitch_change = library->sample_get_pitch_scale(p_sample);
  105. int priority = library->sample_get_priority(p_sample);
  106. VoiceID vid = SpatialSoundServer::get_singleton()->source_play_sample(get_source_rid(), sample->get_rid(), sample->get_mix_rate() * pitch_change, p_voice);
  107. if (vol_change)
  108. SpatialSoundServer::get_singleton()->source_voice_set_volume_scale_db(get_source_rid(), vid, vol_change);
  109. return vid;
  110. }
  111. //voices
  112. void SpatialSamplePlayer::voice_set_pitch_scale(VoiceID p_voice, float p_pitch_scale) {
  113. if (!get_source_rid().is_valid())
  114. return;
  115. SpatialSoundServer::get_singleton()->source_voice_set_pitch_scale(get_source_rid(), p_voice, p_pitch_scale);
  116. }
  117. void SpatialSamplePlayer::voice_set_volume_scale_db(VoiceID p_voice, float p_volume_db) {
  118. if (!get_source_rid().is_valid())
  119. return;
  120. SpatialSoundServer::get_singleton()->source_voice_set_volume_scale_db(get_source_rid(), p_voice, p_volume_db);
  121. }
  122. bool SpatialSamplePlayer::is_voice_active(VoiceID p_voice) const {
  123. if (!get_source_rid().is_valid())
  124. return false;
  125. return SpatialSoundServer::get_singleton()->source_is_voice_active(get_source_rid(), p_voice);
  126. }
  127. void SpatialSamplePlayer::stop_voice(VoiceID p_voice) {
  128. if (!get_source_rid().is_valid())
  129. return;
  130. SpatialSoundServer::get_singleton()->source_stop_voice(get_source_rid(), p_voice);
  131. }
  132. void SpatialSamplePlayer::stop_all() {
  133. if (!get_source_rid().is_valid())
  134. return;
  135. for (int i = 0; i < polyphony; i++) {
  136. SpatialSoundServer::get_singleton()->source_stop_voice(get_source_rid(), i);
  137. }
  138. }
  139. String SpatialSamplePlayer::get_configuration_warning() const {
  140. if (library.is_null()) {
  141. return TTR("A SampleLibrary resource must be created or set in the 'samples' property in order for SpatialSamplePlayer to play sound.");
  142. }
  143. return String();
  144. }
  145. void SpatialSamplePlayer::_bind_methods() {
  146. ObjectTypeDB::bind_method(_MD("set_sample_library", "library:SampleLibrary"), &SpatialSamplePlayer::set_sample_library);
  147. ObjectTypeDB::bind_method(_MD("get_sample_library:SampleLibrary"), &SpatialSamplePlayer::get_sample_library);
  148. ObjectTypeDB::bind_method(_MD("set_polyphony", "voices"), &SpatialSamplePlayer::set_polyphony);
  149. ObjectTypeDB::bind_method(_MD("get_polyphony"), &SpatialSamplePlayer::get_polyphony);
  150. ObjectTypeDB::bind_method(_MD("play", "sample", "voice"), &SpatialSamplePlayer::play, DEFVAL(NEXT_VOICE));
  151. //voices,DEV
  152. ObjectTypeDB::bind_method(_MD("voice_set_pitch_scale", "voice", "ratio"), &SpatialSamplePlayer::voice_set_pitch_scale);
  153. ObjectTypeDB::bind_method(_MD("voice_set_volume_scale_db", "voice", "db"), &SpatialSamplePlayer::voice_set_volume_scale_db);
  154. ObjectTypeDB::bind_method(_MD("is_voice_active", "voice"), &SpatialSamplePlayer::is_voice_active);
  155. ObjectTypeDB::bind_method(_MD("stop_voice", "voice"), &SpatialSamplePlayer::stop_voice);
  156. ObjectTypeDB::bind_method(_MD("stop_all"), &SpatialSamplePlayer::stop_all);
  157. BIND_CONSTANT(INVALID_VOICE);
  158. BIND_CONSTANT(NEXT_VOICE);
  159. ADD_PROPERTY(PropertyInfo(Variant::INT, "config/polyphony", PROPERTY_HINT_RANGE, "1,64,1"), _SCS("set_polyphony"), _SCS("get_polyphony"));
  160. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "config/samples", PROPERTY_HINT_RESOURCE_TYPE, "SampleLibrary"), _SCS("set_sample_library"), _SCS("get_sample_library"));
  161. }
  162. SpatialSamplePlayer::SpatialSamplePlayer() {
  163. polyphony = 1;
  164. }
  165. SpatialSamplePlayer::~SpatialSamplePlayer() {
  166. }