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