sound_player_2d.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*************************************************************************/
  2. /* sound_player_2d.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_player_2d.h"
  30. #include "servers/audio_server.h"
  31. #include "servers/spatial_sound_2d_server.h"
  32. #include "scene/resources/surface_tool.h"
  33. void SoundPlayer2D::_notification(int p_what) {
  34. switch(p_what) {
  35. case NOTIFICATION_ENTER_TREE: {
  36. //find the sound space
  37. source_rid = SpatialSound2DServer::get_singleton()->source_create(get_world_2d()->get_sound_space());
  38. for(int i=0;i<PARAM_MAX;i++)
  39. set_param(Param(i),params[i]);
  40. SpatialSound2DServer::get_singleton()->source_set_transform(source_rid,get_global_transform());
  41. } break;
  42. case NOTIFICATION_TRANSFORM_CHANGED: {
  43. SpatialSound2DServer::get_singleton()->source_set_transform(source_rid,get_global_transform());
  44. } break;
  45. case NOTIFICATION_EXIT_TREE: {
  46. if (source_rid.is_valid())
  47. SpatialSound2DServer::get_singleton()->free(source_rid);
  48. } break;
  49. }
  50. }
  51. void SoundPlayer2D::set_param( Param p_param, float p_value) {
  52. ERR_FAIL_INDEX(p_param,PARAM_MAX);
  53. params[p_param]=p_value;
  54. if (source_rid.is_valid())
  55. SpatialSound2DServer::get_singleton()->source_set_param(source_rid,(SpatialSound2DServer::SourceParam)p_param,p_value);
  56. }
  57. float SoundPlayer2D::get_param( Param p_param) const {
  58. ERR_FAIL_INDEX_V(p_param,PARAM_MAX,0);
  59. return params[p_param];
  60. }
  61. void SoundPlayer2D::_bind_methods() {
  62. ObjectTypeDB::bind_method(_MD("set_param","param","value"),&SoundPlayer2D::set_param);
  63. ObjectTypeDB::bind_method(_MD("get_param","param"),&SoundPlayer2D::get_param);
  64. BIND_CONSTANT( PARAM_VOLUME_DB );
  65. BIND_CONSTANT( PARAM_PITCH_SCALE );
  66. BIND_CONSTANT( PARAM_ATTENUATION_MIN_DISTANCE );
  67. BIND_CONSTANT( PARAM_ATTENUATION_MAX_DISTANCE );
  68. BIND_CONSTANT( PARAM_ATTENUATION_DISTANCE_EXP );
  69. BIND_CONSTANT( PARAM_MAX );
  70. ADD_PROPERTYI( PropertyInfo( Variant::REAL, "params/volume_db",PROPERTY_HINT_RANGE, "-80,24,0.01"),_SCS("set_param"),_SCS("get_param"),PARAM_VOLUME_DB);
  71. ADD_PROPERTYI( PropertyInfo( Variant::REAL, "params/pitch_scale",PROPERTY_HINT_RANGE, "0.001,32,0.001"),_SCS("set_param"),_SCS("get_param"),PARAM_PITCH_SCALE);
  72. ADD_PROPERTYI( PropertyInfo( Variant::REAL, "params/attenuation/min_distance",PROPERTY_HINT_EXP_RANGE, "16,16384,1"),_SCS("set_param"),_SCS("get_param"),PARAM_ATTENUATION_MIN_DISTANCE);
  73. ADD_PROPERTYI( PropertyInfo( Variant::REAL, "params/attenuation/max_distance",PROPERTY_HINT_EXP_RANGE, "16,16384,1"),_SCS("set_param"),_SCS("get_param"),PARAM_ATTENUATION_MAX_DISTANCE);
  74. ADD_PROPERTYI( PropertyInfo( Variant::REAL, "params/attenuation/distance_exp",PROPERTY_HINT_EXP_EASING, "attenuation"),_SCS("set_param"),_SCS("get_param"),PARAM_ATTENUATION_DISTANCE_EXP);
  75. }
  76. SoundPlayer2D::SoundPlayer2D() {
  77. params[PARAM_VOLUME_DB]=0.0;
  78. params[PARAM_PITCH_SCALE]=1.0;
  79. params[PARAM_ATTENUATION_MIN_DISTANCE]=1;
  80. params[PARAM_ATTENUATION_MAX_DISTANCE]=2048;
  81. params[PARAM_ATTENUATION_DISTANCE_EXP]=1.0; //linear (and not really good)
  82. }
  83. SoundPlayer2D::~SoundPlayer2D() {
  84. }