audio_stream_player_3d.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /**************************************************************************/
  2. /* audio_stream_player_3d.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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. #ifndef AUDIO_STREAM_PLAYER_3D_H
  31. #define AUDIO_STREAM_PLAYER_3D_H
  32. #include "scene/3d/node_3d.h"
  33. #include "servers/audio_server.h"
  34. class Area3D;
  35. struct AudioFrame;
  36. class AudioStream;
  37. class AudioStreamPlayback;
  38. class AudioStreamPlayerInternal;
  39. class VelocityTracker3D;
  40. class AudioStreamPlayer3D : public Node3D {
  41. GDCLASS(AudioStreamPlayer3D, Node3D);
  42. public:
  43. enum AttenuationModel {
  44. ATTENUATION_INVERSE_DISTANCE,
  45. ATTENUATION_INVERSE_SQUARE_DISTANCE,
  46. ATTENUATION_LOGARITHMIC,
  47. ATTENUATION_DISABLED,
  48. };
  49. enum DopplerTracking {
  50. DOPPLER_TRACKING_DISABLED,
  51. DOPPLER_TRACKING_IDLE_STEP,
  52. DOPPLER_TRACKING_PHYSICS_STEP
  53. };
  54. private:
  55. enum {
  56. MAX_OUTPUTS = 8,
  57. MAX_INTERSECT_AREAS = 32
  58. };
  59. AudioStreamPlayerInternal *internal = nullptr;
  60. SafeNumeric<float> setplay{ -1.0 };
  61. Ref<AudioStreamPlayback> setplayback;
  62. AttenuationModel attenuation_model = ATTENUATION_INVERSE_DISTANCE;
  63. float unit_size = 10.0;
  64. float max_db = 3.0;
  65. // Internally used to take doppler tracking into account.
  66. float actual_pitch_scale = 1.0;
  67. uint64_t last_mix_count = -1;
  68. bool force_update_panning = false;
  69. static void _calc_output_vol(const Vector3 &source_dir, real_t tightness, Vector<AudioFrame> &output);
  70. void _calc_reverb_vol(Area3D *area, Vector3 listener_area_pos, Vector<AudioFrame> direct_path_vol, Vector<AudioFrame> &reverb_vol);
  71. static void _listener_changed_cb(void *self) { reinterpret_cast<AudioStreamPlayer3D *>(self)->force_update_panning = true; }
  72. void _set_playing(bool p_enable);
  73. bool _is_active() const;
  74. StringName _get_actual_bus();
  75. Area3D *_get_overriding_area();
  76. Vector<AudioFrame> _update_panning();
  77. uint32_t area_mask = 1;
  78. AudioServer::PlaybackType playback_type = AudioServer::PlaybackType::PLAYBACK_TYPE_DEFAULT;
  79. bool emission_angle_enabled = false;
  80. float emission_angle = 45.0;
  81. float emission_angle_filter_attenuation_db = -12.0;
  82. float attenuation_filter_cutoff_hz = 5000.0;
  83. float attenuation_filter_db = -24.0;
  84. float linear_attenuation = 0;
  85. float max_distance = 0.0;
  86. bool was_further_than_max_distance_last_frame = false;
  87. Ref<VelocityTracker3D> velocity_tracker;
  88. DopplerTracking doppler_tracking = DOPPLER_TRACKING_DISABLED;
  89. float _get_attenuation_db(float p_distance) const;
  90. float panning_strength = 1.0f;
  91. float cached_global_panning_strength = 0.5f;
  92. protected:
  93. void _validate_property(PropertyInfo &p_property) const;
  94. void _notification(int p_what);
  95. static void _bind_methods();
  96. bool _set(const StringName &p_name, const Variant &p_value);
  97. bool _get(const StringName &p_name, Variant &r_ret) const;
  98. void _get_property_list(List<PropertyInfo> *p_list) const;
  99. #ifndef DISABLE_DEPRECATED
  100. bool _is_autoplay_enabled_bind_compat_86907();
  101. static void _bind_compatibility_methods();
  102. #endif // DISABLE_DEPRECATED
  103. public:
  104. void set_stream(Ref<AudioStream> p_stream);
  105. Ref<AudioStream> get_stream() const;
  106. void set_volume_db(float p_volume);
  107. float get_volume_db() const;
  108. void set_volume_linear(float p_volume);
  109. float get_volume_linear() const;
  110. void set_unit_size(float p_volume);
  111. float get_unit_size() const;
  112. void set_max_db(float p_boost);
  113. float get_max_db() const;
  114. void set_pitch_scale(float p_pitch_scale);
  115. float get_pitch_scale() const;
  116. void play(float p_from_pos = 0.0);
  117. void seek(float p_seconds);
  118. void stop();
  119. bool is_playing() const;
  120. float get_playback_position();
  121. void set_bus(const StringName &p_bus);
  122. StringName get_bus() const;
  123. void set_max_polyphony(int p_max_polyphony);
  124. int get_max_polyphony() const;
  125. void set_autoplay(bool p_enable);
  126. bool is_autoplay_enabled() const;
  127. void set_max_distance(float p_metres);
  128. float get_max_distance() const;
  129. void set_area_mask(uint32_t p_mask);
  130. uint32_t get_area_mask() const;
  131. void set_emission_angle_enabled(bool p_enable);
  132. bool is_emission_angle_enabled() const;
  133. void set_emission_angle(float p_angle);
  134. float get_emission_angle() const;
  135. void set_emission_angle_filter_attenuation_db(float p_angle_attenuation_db);
  136. float get_emission_angle_filter_attenuation_db() const;
  137. void set_attenuation_filter_cutoff_hz(float p_hz);
  138. float get_attenuation_filter_cutoff_hz() const;
  139. void set_attenuation_filter_db(float p_db);
  140. float get_attenuation_filter_db() const;
  141. void set_attenuation_model(AttenuationModel p_model);
  142. AttenuationModel get_attenuation_model() const;
  143. void set_doppler_tracking(DopplerTracking p_tracking);
  144. DopplerTracking get_doppler_tracking() const;
  145. void set_stream_paused(bool p_pause);
  146. bool get_stream_paused() const;
  147. void set_panning_strength(float p_panning_strength);
  148. float get_panning_strength() const;
  149. bool has_stream_playback();
  150. Ref<AudioStreamPlayback> get_stream_playback();
  151. AudioServer::PlaybackType get_playback_type() const;
  152. void set_playback_type(AudioServer::PlaybackType p_playback_type);
  153. AudioStreamPlayer3D();
  154. ~AudioStreamPlayer3D();
  155. };
  156. VARIANT_ENUM_CAST(AudioStreamPlayer3D::AttenuationModel)
  157. VARIANT_ENUM_CAST(AudioStreamPlayer3D::DopplerTracking)
  158. #endif // AUDIO_STREAM_PLAYER_3D_H