audio_stream_player_3d.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 "core/os/mutex.h"
  33. #include "scene/3d/area_3d.h"
  34. #include "scene/3d/node_3d.h"
  35. #include "scene/3d/velocity_tracker_3d.h"
  36. #include "servers/audio/audio_filter_sw.h"
  37. #include "servers/audio/audio_stream.h"
  38. #include "servers/audio_server.h"
  39. class Camera3D;
  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. Vector<Ref<AudioStreamPlayback>> stream_playbacks;
  60. Ref<AudioStream> stream;
  61. SafeFlag active{ false };
  62. SafeNumeric<float> setplay{ -1.0 };
  63. Ref<AudioStreamPlayback> setplayback;
  64. AttenuationModel attenuation_model = ATTENUATION_INVERSE_DISTANCE;
  65. float volume_db = 0.0;
  66. float unit_size = 10.0;
  67. float max_db = 3.0;
  68. float pitch_scale = 1.0;
  69. // Internally used to take doppler tracking into account.
  70. float actual_pitch_scale = 1.0;
  71. bool autoplay = false;
  72. StringName bus = SNAME("Master");
  73. int max_polyphony = 1;
  74. uint64_t last_mix_count = -1;
  75. bool force_update_panning = false;
  76. static void _calc_output_vol(const Vector3 &source_dir, real_t tightness, Vector<AudioFrame> &output);
  77. void _calc_reverb_vol(Area3D *area, Vector3 listener_area_pos, Vector<AudioFrame> direct_path_vol, Vector<AudioFrame> &reverb_vol);
  78. static void _listener_changed_cb(void *self) { reinterpret_cast<AudioStreamPlayer3D *>(self)->force_update_panning = true; }
  79. void _set_playing(bool p_enable);
  80. bool _is_active() const;
  81. StringName _get_actual_bus();
  82. Area3D *_get_overriding_area();
  83. Vector<AudioFrame> _update_panning();
  84. void _on_bus_layout_changed();
  85. void _on_bus_renamed(int p_bus_index, const StringName &p_old_name, const StringName &p_new_name);
  86. uint32_t area_mask = 1;
  87. bool emission_angle_enabled = false;
  88. float emission_angle = 45.0;
  89. float emission_angle_filter_attenuation_db = -12.0;
  90. float attenuation_filter_cutoff_hz = 5000.0;
  91. float attenuation_filter_db = -24.0;
  92. float linear_attenuation = 0;
  93. float max_distance = 0.0;
  94. Ref<VelocityTracker3D> velocity_tracker;
  95. DopplerTracking doppler_tracking = DOPPLER_TRACKING_DISABLED;
  96. float _get_attenuation_db(float p_distance) const;
  97. float panning_strength = 1.0f;
  98. float cached_global_panning_strength = 0.5f;
  99. protected:
  100. void _validate_property(PropertyInfo &p_property) const;
  101. void _notification(int p_what);
  102. static void _bind_methods();
  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_unit_size(float p_volume);
  109. float get_unit_size() const;
  110. void set_max_db(float p_boost);
  111. float get_max_db() const;
  112. void set_pitch_scale(float p_pitch_scale);
  113. float get_pitch_scale() const;
  114. void play(float p_from_pos = 0.0);
  115. void seek(float p_seconds);
  116. void stop();
  117. bool is_playing() const;
  118. float get_playback_position();
  119. void set_bus(const StringName &p_bus);
  120. StringName get_bus() const;
  121. void set_max_polyphony(int p_max_polyphony);
  122. int get_max_polyphony() const;
  123. void set_autoplay(bool p_enable);
  124. bool is_autoplay_enabled();
  125. void set_max_distance(float p_metres);
  126. float get_max_distance() const;
  127. void set_area_mask(uint32_t p_mask);
  128. uint32_t get_area_mask() const;
  129. void set_emission_angle_enabled(bool p_enable);
  130. bool is_emission_angle_enabled() const;
  131. void set_emission_angle(float p_angle);
  132. float get_emission_angle() const;
  133. void set_emission_angle_filter_attenuation_db(float p_angle_attenuation_db);
  134. float get_emission_angle_filter_attenuation_db() const;
  135. void set_attenuation_filter_cutoff_hz(float p_hz);
  136. float get_attenuation_filter_cutoff_hz() const;
  137. void set_attenuation_filter_db(float p_db);
  138. float get_attenuation_filter_db() const;
  139. void set_attenuation_model(AttenuationModel p_model);
  140. AttenuationModel get_attenuation_model() const;
  141. void set_doppler_tracking(DopplerTracking p_tracking);
  142. DopplerTracking get_doppler_tracking() const;
  143. void set_stream_paused(bool p_pause);
  144. bool get_stream_paused() const;
  145. void set_panning_strength(float p_panning_strength);
  146. float get_panning_strength() const;
  147. bool has_stream_playback();
  148. Ref<AudioStreamPlayback> get_stream_playback();
  149. AudioStreamPlayer3D();
  150. ~AudioStreamPlayer3D();
  151. };
  152. VARIANT_ENUM_CAST(AudioStreamPlayer3D::AttenuationModel)
  153. VARIANT_ENUM_CAST(AudioStreamPlayer3D::DopplerTracking)
  154. #endif // AUDIO_STREAM_PLAYER_3D_H