2
0

audio_stream_player_3d.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  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/templates/safe_refcount.h"
  33. #include "scene/3d/node_3d.h"
  34. #include "scene/3d/velocity_tracker_3d.h"
  35. #include "servers/audio/audio_filter_sw.h"
  36. #include "servers/audio/audio_stream.h"
  37. #include "servers/audio_server.h"
  38. class Camera3D;
  39. class AudioStreamPlayer3D : public Node3D {
  40. GDCLASS(AudioStreamPlayer3D, Node3D);
  41. public:
  42. enum AttenuationModel {
  43. ATTENUATION_INVERSE_DISTANCE,
  44. ATTENUATION_INVERSE_SQUARE_DISTANCE,
  45. ATTENUATION_LOGARITHMIC,
  46. ATTENUATION_DISABLED,
  47. };
  48. enum OutOfRangeMode {
  49. OUT_OF_RANGE_MIX,
  50. OUT_OF_RANGE_PAUSE,
  51. };
  52. enum DopplerTracking {
  53. DOPPLER_TRACKING_DISABLED,
  54. DOPPLER_TRACKING_IDLE_STEP,
  55. DOPPLER_TRACKING_PHYSICS_STEP
  56. };
  57. private:
  58. enum {
  59. MAX_OUTPUTS = 8,
  60. MAX_INTERSECT_AREAS = 32
  61. };
  62. struct Output {
  63. AudioFilterSW filter;
  64. AudioFilterSW::Processor filter_process[8];
  65. AudioFrame vol[4];
  66. float filter_gain = 0.0;
  67. float pitch_scale = 0.0;
  68. int bus_index = -1;
  69. int reverb_bus_index = -1;
  70. AudioFrame reverb_vol[4];
  71. Viewport *viewport = nullptr; //pointer only used for reference to previous mix
  72. };
  73. Output outputs[MAX_OUTPUTS];
  74. SafeNumeric<int> output_count;
  75. SafeFlag output_ready;
  76. //these are used by audio thread to have a reference of previous volumes (for ramping volume and avoiding clicks)
  77. Output prev_outputs[MAX_OUTPUTS];
  78. int prev_output_count = 0;
  79. Ref<AudioStreamPlayback> stream_playback;
  80. Ref<AudioStream> stream;
  81. Vector<AudioFrame> mix_buffer;
  82. SafeNumeric<float> setseek{ -1.0 };
  83. SafeFlag active;
  84. SafeNumeric<float> setplay{ -1.0 };
  85. AttenuationModel attenuation_model = ATTENUATION_INVERSE_DISTANCE;
  86. float unit_db = 0.0;
  87. float unit_size = 1.0;
  88. float max_db = 3.0;
  89. float pitch_scale = 1.0;
  90. bool autoplay = false;
  91. bool stream_paused = false;
  92. bool stream_paused_fade_in = false;
  93. bool stream_paused_fade_out = false;
  94. StringName bus;
  95. static void _calc_output_vol(const Vector3 &source_dir, real_t tightness, Output &output);
  96. void _mix_audio();
  97. static void _mix_audios(void *self) { reinterpret_cast<AudioStreamPlayer3D *>(self)->_mix_audio(); }
  98. void _set_playing(bool p_enable);
  99. bool _is_active() const;
  100. void _bus_layout_changed();
  101. uint32_t area_mask = 1;
  102. bool emission_angle_enabled = false;
  103. float emission_angle = 45.0;
  104. float emission_angle_filter_attenuation_db = -12.0;
  105. float attenuation_filter_cutoff_hz = 5000.0;
  106. float attenuation_filter_db = -24.0;
  107. float max_distance = 0.0;
  108. Ref<VelocityTracker3D> velocity_tracker;
  109. DopplerTracking doppler_tracking = DOPPLER_TRACKING_DISABLED;
  110. OutOfRangeMode out_of_range_mode = OUT_OF_RANGE_MIX;
  111. float _get_attenuation_db(float p_distance) const;
  112. protected:
  113. void _validate_property(PropertyInfo &property) const override;
  114. void _notification(int p_what);
  115. static void _bind_methods();
  116. public:
  117. void set_stream(Ref<AudioStream> p_stream);
  118. Ref<AudioStream> get_stream() const;
  119. void set_unit_db(float p_volume);
  120. float get_unit_db() const;
  121. void set_unit_size(float p_volume);
  122. float get_unit_size() const;
  123. void set_max_db(float p_boost);
  124. float get_max_db() const;
  125. void set_pitch_scale(float p_pitch_scale);
  126. float get_pitch_scale() const;
  127. void play(float p_from_pos = 0.0);
  128. void seek(float p_seconds);
  129. void stop();
  130. bool is_playing() const;
  131. float get_playback_position();
  132. void set_bus(const StringName &p_bus);
  133. StringName get_bus() const;
  134. void set_autoplay(bool p_enable);
  135. bool is_autoplay_enabled();
  136. void set_max_distance(float p_metres);
  137. float get_max_distance() const;
  138. void set_area_mask(uint32_t p_mask);
  139. uint32_t get_area_mask() const;
  140. void set_emission_angle_enabled(bool p_enable);
  141. bool is_emission_angle_enabled() const;
  142. void set_emission_angle(float p_angle);
  143. float get_emission_angle() const;
  144. void set_emission_angle_filter_attenuation_db(float p_angle_attenuation_db);
  145. float get_emission_angle_filter_attenuation_db() const;
  146. void set_attenuation_filter_cutoff_hz(float p_hz);
  147. float get_attenuation_filter_cutoff_hz() const;
  148. void set_attenuation_filter_db(float p_db);
  149. float get_attenuation_filter_db() const;
  150. void set_attenuation_model(AttenuationModel p_model);
  151. AttenuationModel get_attenuation_model() const;
  152. void set_out_of_range_mode(OutOfRangeMode p_mode);
  153. OutOfRangeMode get_out_of_range_mode() const;
  154. void set_doppler_tracking(DopplerTracking p_tracking);
  155. DopplerTracking get_doppler_tracking() const;
  156. void set_stream_paused(bool p_pause);
  157. bool get_stream_paused() const;
  158. Ref<AudioStreamPlayback> get_stream_playback();
  159. AudioStreamPlayer3D();
  160. ~AudioStreamPlayer3D();
  161. };
  162. VARIANT_ENUM_CAST(AudioStreamPlayer3D::AttenuationModel)
  163. VARIANT_ENUM_CAST(AudioStreamPlayer3D::OutOfRangeMode)
  164. VARIANT_ENUM_CAST(AudioStreamPlayer3D::DopplerTracking)
  165. #endif // AUDIO_STREAM_PLAYER_3D_H