audio_stream_player_3d.h 6.5 KB

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