audio_stream_player_2d.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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) 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_2D_H
  31. #define AUDIO_STREAM_PLAYER_2D_H
  32. #include "scene/2d/node_2d.h"
  33. #include "servers/audio/audio_stream.h"
  34. #include "servers/audio_server.h"
  35. class AudioStreamPlayer2D : public Node2D {
  36. GDCLASS(AudioStreamPlayer2D, Node2D)
  37. private:
  38. enum {
  39. MAX_OUTPUTS = 8,
  40. MAX_INTERSECT_AREAS = 32
  41. };
  42. struct Output {
  43. AudioFrame vol;
  44. int bus_index;
  45. Viewport *viewport; //pointer only used for reference to previous mix
  46. };
  47. Output outputs[MAX_OUTPUTS];
  48. volatile int output_count;
  49. volatile bool output_ready;
  50. //these are used by audio thread to have a reference of previous volumes (for ramping volume and avoiding clicks)
  51. Output prev_outputs[MAX_OUTPUTS];
  52. int prev_output_count;
  53. Ref<AudioStreamPlayback> stream_playback;
  54. Ref<AudioStream> stream;
  55. Vector<AudioFrame> mix_buffer;
  56. volatile float setseek;
  57. volatile bool active;
  58. volatile float setplay;
  59. float volume_db;
  60. float pitch_scale;
  61. bool autoplay;
  62. bool stream_paused;
  63. bool stream_paused_fade_in;
  64. bool stream_paused_fade_out;
  65. StringName bus;
  66. void _mix_audio();
  67. static void _mix_audios(void *self) { reinterpret_cast<AudioStreamPlayer2D *>(self)->_mix_audio(); }
  68. void _set_playing(bool p_enable);
  69. bool _is_active() const;
  70. void _bus_layout_changed();
  71. uint32_t area_mask;
  72. float max_distance;
  73. float attenuation;
  74. protected:
  75. void _validate_property(PropertyInfo &property) const;
  76. void _notification(int p_what);
  77. static void _bind_methods();
  78. public:
  79. void set_stream(Ref<AudioStream> p_stream);
  80. Ref<AudioStream> get_stream() const;
  81. void set_volume_db(float p_volume);
  82. float get_volume_db() const;
  83. void set_pitch_scale(float p_pitch_scale);
  84. float get_pitch_scale() const;
  85. void play(float p_from_pos = 0.0);
  86. void seek(float p_seconds);
  87. void stop();
  88. bool is_playing() const;
  89. float get_playback_position();
  90. void set_bus(const StringName &p_bus);
  91. StringName get_bus() const;
  92. void set_autoplay(bool p_enable);
  93. bool is_autoplay_enabled();
  94. void set_max_distance(float p_pixels);
  95. float get_max_distance() const;
  96. void set_attenuation(float p_curve);
  97. float get_attenuation() const;
  98. void set_area_mask(uint32_t p_mask);
  99. uint32_t get_area_mask() const;
  100. void set_stream_paused(bool p_pause);
  101. bool get_stream_paused() const;
  102. AudioStreamPlayer2D();
  103. ~AudioStreamPlayer2D();
  104. };
  105. #endif