audio_player.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*************************************************************************/
  2. /* audio_player.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 AUDIOPLAYER_H
  31. #define AUDIOPLAYER_H
  32. #include "scene/main/node.h"
  33. #include "servers/audio/audio_stream.h"
  34. class AudioStreamPlayer : public Node {
  35. GDCLASS(AudioStreamPlayer, Node)
  36. public:
  37. enum MixTarget {
  38. MIX_TARGET_STEREO,
  39. MIX_TARGET_SURROUND,
  40. MIX_TARGET_CENTER
  41. };
  42. private:
  43. Ref<AudioStreamPlayback> stream_playback;
  44. Ref<AudioStream> stream;
  45. Vector<AudioFrame> mix_buffer;
  46. volatile float setseek;
  47. volatile bool active;
  48. float mix_volume_db;
  49. float pitch_scale;
  50. float volume_db;
  51. bool autoplay;
  52. bool stream_paused;
  53. bool stream_paused_fade;
  54. StringName bus;
  55. MixTarget mix_target;
  56. void _mix_internal(bool p_fadeout);
  57. void _mix_audio();
  58. static void _mix_audios(void *self) { reinterpret_cast<AudioStreamPlayer *>(self)->_mix_audio(); }
  59. void _set_playing(bool p_enable);
  60. bool _is_active() const;
  61. void _bus_layout_changed();
  62. protected:
  63. void _validate_property(PropertyInfo &property) const;
  64. void _notification(int p_what);
  65. static void _bind_methods();
  66. public:
  67. void set_stream(Ref<AudioStream> p_stream);
  68. Ref<AudioStream> get_stream() const;
  69. void set_volume_db(float p_volume);
  70. float get_volume_db() const;
  71. void set_pitch_scale(float p_pitch_scale);
  72. float get_pitch_scale() const;
  73. void play(float p_from_pos = 0.0);
  74. void seek(float p_seconds);
  75. void stop();
  76. bool is_playing() const;
  77. float get_playback_position();
  78. void set_bus(const StringName &p_bus);
  79. StringName get_bus() const;
  80. void set_autoplay(bool p_enable);
  81. bool is_autoplay_enabled();
  82. void set_mix_target(MixTarget p_target);
  83. MixTarget get_mix_target() const;
  84. void set_stream_paused(bool p_pause);
  85. bool get_stream_paused() const;
  86. AudioStreamPlayer();
  87. ~AudioStreamPlayer();
  88. };
  89. VARIANT_ENUM_CAST(AudioStreamPlayer::MixTarget)
  90. #endif // AUDIOPLAYER_H