sound_room_params.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*************************************************************************/
  2. /* sound_room_params.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 "sound_room_params.h"
  30. #include "scene/main/viewport.h"
  31. #ifndef _3D_DISABLED
  32. void SoundRoomParams::_update_sound_room() {
  33. if (!room.is_valid())
  34. return;
  35. for(int i=0;i<PARAM_MAX;i++) {
  36. SpatialSoundServer::get_singleton()->room_set_param(room,SpatialSoundServer::RoomParam(i),params[i]);
  37. }
  38. SpatialSoundServer::get_singleton()->room_set_reverb(room,SpatialSoundServer::RoomReverb(reverb));
  39. SpatialSoundServer::get_singleton()->room_set_force_params_to_all_sources(room,force_params_for_all_sources);
  40. }
  41. void SoundRoomParams::_notification(int p_what) {
  42. switch(p_what) {
  43. case NOTIFICATION_ENTER_TREE: {
  44. //#if 0
  45. Node *n=this;
  46. Room *room_instance=NULL;
  47. while(n) {
  48. room_instance=n->cast_to<Room>();
  49. if (room_instance) {
  50. break;
  51. }
  52. if (n->cast_to<Viewport>())
  53. break;
  54. n=n->get_parent();
  55. }
  56. if (room_instance) {
  57. room=room_instance->get_sound_room();
  58. } else {
  59. room=get_viewport()->find_world()->get_sound_space();
  60. }
  61. _update_sound_room();
  62. //#endif
  63. } break;
  64. case NOTIFICATION_EXIT_TREE: {
  65. room=RID();
  66. } break;
  67. }
  68. }
  69. void SoundRoomParams::set_param(Params p_param, float p_value) {
  70. ERR_FAIL_INDEX(p_param,PARAM_MAX);
  71. params[p_param]=p_value;
  72. if (room.is_valid())
  73. SpatialSoundServer::get_singleton()->room_set_param(room,SpatialSoundServer::RoomParam(p_param),p_value);
  74. }
  75. float SoundRoomParams::get_param(Params p_param) const {
  76. ERR_FAIL_INDEX_V(p_param,PARAM_MAX,0);
  77. return params[p_param];
  78. }
  79. void SoundRoomParams::set_reverb_mode(Reverb p_mode) {
  80. ERR_FAIL_INDEX(p_mode,4);
  81. reverb=p_mode;
  82. if (room.is_valid())
  83. SpatialSoundServer::get_singleton()->room_set_reverb(room,SpatialSoundServer::RoomReverb(p_mode));
  84. }
  85. SoundRoomParams::Reverb SoundRoomParams::get_reverb_mode() const {
  86. return reverb;
  87. }
  88. void SoundRoomParams::set_force_params_to_all_sources(bool p_force) {
  89. force_params_for_all_sources=p_force;
  90. if (room.is_valid())
  91. SpatialSoundServer::get_singleton()->room_set_force_params_to_all_sources(room,p_force);
  92. }
  93. bool SoundRoomParams::is_forcing_params_to_all_sources() {
  94. return force_params_for_all_sources;
  95. }
  96. void SoundRoomParams::_bind_methods() {
  97. ObjectTypeDB::bind_method(_MD("set_param","param","value"),&SoundRoomParams::set_param );
  98. ObjectTypeDB::bind_method(_MD("get_param","param"),&SoundRoomParams::get_param );
  99. ObjectTypeDB::bind_method(_MD("set_reverb_mode","reverb_mode","value"),&SoundRoomParams::set_reverb_mode );
  100. ObjectTypeDB::bind_method(_MD("get_reverb_mode","reverb_mode"),&SoundRoomParams::get_reverb_mode );
  101. ObjectTypeDB::bind_method(_MD("set_force_params_to_all_sources","enabled"),&SoundRoomParams::set_force_params_to_all_sources );
  102. ObjectTypeDB::bind_method(_MD("is_forcing_params_to_all_sources"),&SoundRoomParams::is_forcing_params_to_all_sources );
  103. ADD_PROPERTY( PropertyInfo( Variant::INT, "reverb/mode", PROPERTY_HINT_ENUM, "Small,Medium,Large,Hall"), _SCS("set_reverb_mode"), _SCS("get_reverb_mode") );
  104. ADD_PROPERTYI( PropertyInfo( Variant::REAL, "params/speed_of_scale", PROPERTY_HINT_RANGE, "0.01,16,0.01"), _SCS("set_param"), _SCS("get_param"), PARAM_SPEED_OF_SOUND_SCALE);
  105. ADD_PROPERTYI( PropertyInfo( Variant::REAL, "params/doppler_factor",PROPERTY_HINT_RANGE, "0.01,16,0.01"), _SCS("set_param"), _SCS("get_param"), PARAM_DOPPLER_FACTOR );
  106. ADD_PROPERTYI( PropertyInfo( Variant::REAL, "params/pitch_scale",PROPERTY_HINT_RANGE, "0.01,32,0.01"), _SCS("set_param"), _SCS("get_param"), PARAM_PITCH_SCALE );
  107. ADD_PROPERTYI( PropertyInfo( Variant::REAL, "params/volume_scale_db",PROPERTY_HINT_RANGE, "-80,24,0.01"), _SCS("set_param"), _SCS("get_param"), PARAM_VOLUME_SCALE_DB );
  108. ADD_PROPERTYI( PropertyInfo( Variant::REAL, "params/reverb_send",PROPERTY_HINT_RANGE, "0,1,0.01"), _SCS("set_param"), _SCS("get_param"), PARAM_REVERB_SEND );
  109. ADD_PROPERTYI( PropertyInfo( Variant::REAL, "params/chorus_send",PROPERTY_HINT_RANGE, "0,1,0.01"), _SCS("set_param"), _SCS("get_param"), PARAM_CHORUS_SEND );
  110. ADD_PROPERTYI( PropertyInfo( Variant::REAL, "params/attenuation_scale",PROPERTY_HINT_RANGE, "0.01,32,0.01"), _SCS("set_param"), _SCS("get_param"), PARAM_ATTENUATION_SCALE );
  111. ADD_PROPERTYI( PropertyInfo( Variant::REAL, "params/attenuation_hf_cutoff",PROPERTY_HINT_RANGE, "30,16384,1"), _SCS("set_param"), _SCS("get_param"), PARAM_ATTENUATION_HF_CUTOFF );
  112. ADD_PROPERTYI( PropertyInfo( Variant::REAL, "params/attenuation_hf_floor_db",PROPERTY_HINT_RANGE, "-80,24,0.01"), _SCS("set_param"), _SCS("get_param"), PARAM_ATTENUATION_HF_FLOOR_DB );
  113. ADD_PROPERTYI( PropertyInfo( Variant::REAL, "params/attenuation_hf_ratio_exp",PROPERTY_HINT_RANGE, "0.01,32,0.01"), _SCS("set_param"), _SCS("get_param"), PARAM_ATTENUATION_HF_RATIO_EXP );
  114. ADD_PROPERTYI( PropertyInfo( Variant::REAL, "params/attenuation_reverb_scale",PROPERTY_HINT_RANGE, "0.01,32,0.01"), _SCS("set_param"), _SCS("get_param"), PARAM_ATTENUATION_REVERB_SCALE );
  115. ADD_PROPERTY( PropertyInfo( Variant::BOOL, "force_to_all_sources"),_SCS("set_force_params_to_all_sources"),_SCS("is_forcing_params_to_all_sources") );
  116. }
  117. SoundRoomParams::SoundRoomParams() {
  118. reverb=REVERB_HALL;
  119. params[PARAM_SPEED_OF_SOUND_SCALE]=1;
  120. params[PARAM_DOPPLER_FACTOR]=1.0;
  121. params[PARAM_PITCH_SCALE]=1.0;
  122. params[PARAM_VOLUME_SCALE_DB]=0;
  123. params[PARAM_REVERB_SEND]=0;
  124. params[PARAM_CHORUS_SEND]=0;
  125. params[PARAM_ATTENUATION_SCALE]=1.0;
  126. params[PARAM_ATTENUATION_HF_CUTOFF]=5000;
  127. params[PARAM_ATTENUATION_HF_FLOOR_DB]=-24.0;
  128. params[PARAM_ATTENUATION_HF_RATIO_EXP]=1.0;
  129. params[PARAM_ATTENUATION_REVERB_SCALE]=0.0;
  130. force_params_for_all_sources=false;
  131. }
  132. #endif