audio_stream_player_2d.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /**************************************************************************/
  2. /* audio_stream_player_2d.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_2D_H
  31. #define AUDIO_STREAM_PLAYER_2D_H
  32. #include "scene/2d/node_2d.h"
  33. #include "scene/scene_string_names.h"
  34. #include "servers/audio/audio_stream.h"
  35. #include "servers/audio_server.h"
  36. class AudioStreamPlayer2D : public Node2D {
  37. GDCLASS(AudioStreamPlayer2D, Node2D);
  38. private:
  39. enum {
  40. MAX_OUTPUTS = 8,
  41. MAX_INTERSECT_AREAS = 32
  42. };
  43. struct Output {
  44. AudioFrame vol;
  45. int bus_index = 0;
  46. Viewport *viewport = nullptr; //pointer only used for reference to previous mix
  47. };
  48. Vector<Ref<AudioStreamPlayback>> stream_playbacks;
  49. Ref<AudioStream> stream;
  50. SafeFlag active{ false };
  51. SafeNumeric<float> setplay{ -1.0 };
  52. Ref<AudioStreamPlayback> setplayback;
  53. Vector<AudioFrame> volume_vector;
  54. uint64_t last_mix_count = -1;
  55. bool force_update_panning = false;
  56. float volume_db = 0.0;
  57. float pitch_scale = 1.0;
  58. bool autoplay = false;
  59. StringName default_bus = SceneStringNames::get_singleton()->Master;
  60. int max_polyphony = 1;
  61. void _set_playing(bool p_enable);
  62. bool _is_active() const;
  63. StringName _get_actual_bus();
  64. void _update_panning();
  65. void _on_bus_layout_changed();
  66. void _on_bus_renamed(int p_bus_index, const StringName &p_old_name, const StringName &p_new_name);
  67. static void _listener_changed_cb(void *self) { reinterpret_cast<AudioStreamPlayer2D *>(self)->force_update_panning = true; }
  68. uint32_t area_mask = 1;
  69. float max_distance = 2000.0;
  70. float attenuation = 1.0;
  71. float panning_strength = 1.0f;
  72. float cached_global_panning_strength = 0.5f;
  73. protected:
  74. void _validate_property(PropertyInfo &p_property) const;
  75. void _notification(int p_what);
  76. static void _bind_methods();
  77. public:
  78. void set_stream(Ref<AudioStream> p_stream);
  79. Ref<AudioStream> get_stream() const;
  80. void set_volume_db(float p_volume);
  81. float get_volume_db() const;
  82. void set_pitch_scale(float p_pitch_scale);
  83. float get_pitch_scale() const;
  84. void play(float p_from_pos = 0.0);
  85. void seek(float p_seconds);
  86. void stop();
  87. bool is_playing() const;
  88. float get_playback_position();
  89. void set_bus(const StringName &p_bus);
  90. StringName get_bus() const;
  91. void set_autoplay(bool p_enable);
  92. bool is_autoplay_enabled();
  93. void set_max_distance(float p_pixels);
  94. float get_max_distance() const;
  95. void set_attenuation(float p_curve);
  96. float get_attenuation() const;
  97. void set_area_mask(uint32_t p_mask);
  98. uint32_t get_area_mask() const;
  99. void set_stream_paused(bool p_pause);
  100. bool get_stream_paused() const;
  101. void set_max_polyphony(int p_max_polyphony);
  102. int get_max_polyphony() const;
  103. void set_panning_strength(float p_panning_strength);
  104. float get_panning_strength() const;
  105. bool has_stream_playback();
  106. Ref<AudioStreamPlayback> get_stream_playback();
  107. AudioStreamPlayer2D();
  108. ~AudioStreamPlayer2D();
  109. };
  110. #endif // AUDIO_STREAM_PLAYER_2D_H