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_fade_in;
  97. bool stream_fade_out;
  98. bool stream_stop;
  99. StringName bus;
  100. void _mix_audio();
  101. static void _mix_audios(void *self) { reinterpret_cast<AudioStreamPlayer3D *>(self)->_mix_audio(); }
  102. void _set_playing(bool p_enable);
  103. bool _is_active() const;
  104. void _bus_layout_changed();
  105. uint32_t area_mask;
  106. bool emission_angle_enabled;
  107. float emission_angle;
  108. float emission_angle_filter_attenuation_db;
  109. float attenuation_filter_cutoff_hz;
  110. float attenuation_filter_db;
  111. float max_distance;
  112. Ref<SpatialVelocityTracker> velocity_tracker;
  113. DopplerTracking doppler_tracking;
  114. OutOfRangeMode out_of_range_mode;
  115. float _get_attenuation_db(float p_distance) const;
  116. protected:
  117. void _validate_property(PropertyInfo &property) const;
  118. void _notification(int p_what);
  119. static void _bind_methods();
  120. public:
  121. void set_stream(Ref<AudioStream> p_stream);
  122. Ref<AudioStream> get_stream() const;
  123. void set_unit_db(float p_volume);
  124. float get_unit_db() const;
  125. void set_unit_size(float p_volume);
  126. float get_unit_size() const;
  127. void set_max_db(float p_boost);
  128. float get_max_db() const;
  129. void set_pitch_scale(float p_pitch_scale);
  130. float get_pitch_scale() const;
  131. void play(float p_from_pos = 0.0);
  132. void seek(float p_seconds);
  133. void stop();
  134. bool is_playing() const;
  135. float get_playback_position();
  136. void set_bus(const StringName &p_bus);
  137. StringName get_bus() const;
  138. void set_autoplay(bool p_enable);
  139. bool is_autoplay_enabled();
  140. void set_max_distance(float p_metres);
  141. float get_max_distance() const;
  142. void set_area_mask(uint32_t p_mask);
  143. uint32_t get_area_mask() const;
  144. void set_emission_angle_enabled(bool p_enable);
  145. bool is_emission_angle_enabled() const;
  146. void set_emission_angle(float p_angle);
  147. float get_emission_angle() const;
  148. void set_emission_angle_filter_attenuation_db(float p_angle_attenuation_db);
  149. float get_emission_angle_filter_attenuation_db() const;
  150. void set_attenuation_filter_cutoff_hz(float p_hz);
  151. float get_attenuation_filter_cutoff_hz() const;
  152. void set_attenuation_filter_db(float p_db);
  153. float get_attenuation_filter_db() const;
  154. void set_attenuation_model(AttenuationModel p_model);
  155. AttenuationModel get_attenuation_model() const;
  156. void set_out_of_range_mode(OutOfRangeMode p_mode);
  157. OutOfRangeMode get_out_of_range_mode() const;
  158. void set_doppler_tracking(DopplerTracking p_tracking);
  159. DopplerTracking get_doppler_tracking() const;
  160. void set_stream_paused(bool p_pause);
  161. bool get_stream_paused() const;
  162. AudioStreamPlayer3D();
  163. ~AudioStreamPlayer3D();
  164. };
  165. VARIANT_ENUM_CAST(AudioStreamPlayer3D::AttenuationModel)
  166. VARIANT_ENUM_CAST(AudioStreamPlayer3D::OutOfRangeMode)
  167. VARIANT_ENUM_CAST(AudioStreamPlayer3D::DopplerTracking)
  168. #endif // AUDIO_STREAM_PLAYER_3D_H