audio_stream_player_3d.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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/safe_refcount.h"
  33. #include "scene/3d/spatial.h"
  34. #include "scene/3d/spatial_velocity_tracker.h"
  35. #include "servers/audio/audio_filter_sw.h"
  36. #include "servers/audio/audio_stream.h"
  37. #include "servers/audio_server.h"
  38. class Camera;
  39. class AudioStreamPlayer3D : public Spatial {
  40. GDCLASS(AudioStreamPlayer3D, Spatial);
  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;
  67. float pitch_scale;
  68. int bus_index;
  69. int reverb_bus_index;
  70. AudioFrame reverb_vol[4];
  71. Viewport *viewport; //pointer only used for reference to previous mix
  72. Output() {
  73. filter_gain = 0;
  74. viewport = nullptr;
  75. reverb_bus_index = -1;
  76. bus_index = -1;
  77. }
  78. };
  79. Output outputs[MAX_OUTPUTS];
  80. SafeNumeric<int> output_count;
  81. SafeFlag output_ready;
  82. //these are used by audio thread to have a reference of previous volumes (for ramping volume and avoiding clicks)
  83. Output prev_outputs[MAX_OUTPUTS];
  84. int prev_output_count;
  85. Ref<AudioStreamPlayback> stream_playback;
  86. Ref<AudioStream> stream;
  87. Vector<AudioFrame> mix_buffer;
  88. SafeNumeric<float> setseek;
  89. SafeFlag active;
  90. SafeNumeric<float> setplay;
  91. AttenuationModel attenuation_model;
  92. float unit_db;
  93. float unit_size;
  94. float max_db;
  95. float pitch_scale;
  96. bool autoplay;
  97. bool stream_paused;
  98. bool stream_paused_fade_in;
  99. bool stream_paused_fade_out;
  100. StringName bus;
  101. static void _calc_output_vol(const Vector3 &source_dir, real_t tightness, Output &output);
  102. void _mix_audio();
  103. static void _mix_audios(void *self) { reinterpret_cast<AudioStreamPlayer3D *>(self)->_mix_audio(); }
  104. void _set_playing(bool p_enable);
  105. bool _is_active() const;
  106. void _bus_layout_changed();
  107. uint32_t area_mask;
  108. bool emission_angle_enabled;
  109. float emission_angle;
  110. float emission_angle_filter_attenuation_db;
  111. float attenuation_filter_cutoff_hz;
  112. float attenuation_filter_db;
  113. float max_distance;
  114. Ref<SpatialVelocityTracker> velocity_tracker;
  115. DopplerTracking doppler_tracking;
  116. OutOfRangeMode out_of_range_mode;
  117. float _get_attenuation_db(float p_distance) const;
  118. protected:
  119. void _validate_property(PropertyInfo &property) const;
  120. void _notification(int p_what);
  121. static void _bind_methods();
  122. public:
  123. void set_stream(Ref<AudioStream> p_stream);
  124. Ref<AudioStream> get_stream() const;
  125. void set_unit_db(float p_volume);
  126. float get_unit_db() const;
  127. void set_unit_size(float p_volume);
  128. float get_unit_size() const;
  129. void set_max_db(float p_boost);
  130. float get_max_db() const;
  131. void set_pitch_scale(float p_pitch_scale);
  132. float get_pitch_scale() const;
  133. void play(float p_from_pos = 0.0);
  134. void seek(float p_seconds);
  135. void stop();
  136. bool is_playing() const;
  137. float get_playback_position();
  138. void set_bus(const StringName &p_bus);
  139. StringName get_bus() const;
  140. void set_autoplay(bool p_enable);
  141. bool is_autoplay_enabled();
  142. void set_max_distance(float p_metres);
  143. float get_max_distance() const;
  144. void set_area_mask(uint32_t p_mask);
  145. uint32_t get_area_mask() const;
  146. void set_emission_angle_enabled(bool p_enable);
  147. bool is_emission_angle_enabled() const;
  148. void set_emission_angle(float p_angle);
  149. float get_emission_angle() const;
  150. void set_emission_angle_filter_attenuation_db(float p_angle_attenuation_db);
  151. float get_emission_angle_filter_attenuation_db() const;
  152. void set_attenuation_filter_cutoff_hz(float p_hz);
  153. float get_attenuation_filter_cutoff_hz() const;
  154. void set_attenuation_filter_db(float p_db);
  155. float get_attenuation_filter_db() const;
  156. void set_attenuation_model(AttenuationModel p_model);
  157. AttenuationModel get_attenuation_model() const;
  158. void set_out_of_range_mode(OutOfRangeMode p_mode);
  159. OutOfRangeMode get_out_of_range_mode() const;
  160. void set_doppler_tracking(DopplerTracking p_tracking);
  161. DopplerTracking get_doppler_tracking() const;
  162. void set_stream_paused(bool p_pause);
  163. bool get_stream_paused() const;
  164. Ref<AudioStreamPlayback> get_stream_playback();
  165. AudioStreamPlayer3D();
  166. ~AudioStreamPlayer3D();
  167. };
  168. VARIANT_ENUM_CAST(AudioStreamPlayer3D::AttenuationModel)
  169. VARIANT_ENUM_CAST(AudioStreamPlayer3D::OutOfRangeMode)
  170. VARIANT_ENUM_CAST(AudioStreamPlayer3D::DopplerTracking)
  171. #endif // AUDIO_STREAM_PLAYER_3D_H