spatial_sample_player.cpp 7.3 KB

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