audio_stream_playlist.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /**************************************************************************/
  2. /* audio_stream_playlist.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_PLAYLIST_H
  31. #define AUDIO_STREAM_PLAYLIST_H
  32. #include "servers/audio/audio_stream.h"
  33. class AudioStreamPlaybackPlaylist;
  34. class AudioStreamPlaylist : public AudioStream {
  35. GDCLASS(AudioStreamPlaylist, AudioStream)
  36. OBJ_SAVE_TYPE(AudioStream)
  37. private:
  38. friend class AudioStreamPlaybackPlaylist;
  39. enum {
  40. MAX_STREAMS = 64
  41. };
  42. bool shuffle = false;
  43. bool loop = true;
  44. double fade_time = 0.3;
  45. int stream_count = 0;
  46. Ref<AudioStream> audio_streams[MAX_STREAMS];
  47. HashSet<AudioStreamPlaybackPlaylist *> playbacks;
  48. public:
  49. virtual double get_bpm() const override;
  50. void set_stream_count(int p_count);
  51. int get_stream_count() const;
  52. void set_fade_time(float p_time);
  53. float get_fade_time() const;
  54. void set_shuffle(bool p_shuffle);
  55. bool get_shuffle() const;
  56. void set_loop(bool p_loop);
  57. virtual bool has_loop() const override;
  58. void set_list_stream(int p_stream_index, Ref<AudioStream> p_stream);
  59. Ref<AudioStream> get_list_stream(int p_stream_index) const;
  60. virtual Ref<AudioStreamPlayback> instantiate_playback() override;
  61. virtual String get_stream_name() const override;
  62. virtual double get_length() const override;
  63. protected:
  64. static void _bind_methods();
  65. void _validate_property(PropertyInfo &r_property) const;
  66. };
  67. ///////////////////////////////////////
  68. class AudioStreamPlaybackPlaylist : public AudioStreamPlayback {
  69. GDCLASS(AudioStreamPlaybackPlaylist, AudioStreamPlayback)
  70. friend class AudioStreamPlaylist;
  71. private:
  72. enum {
  73. MIX_BUFFER_SIZE = 128
  74. };
  75. AudioFrame mix_buffer[MIX_BUFFER_SIZE];
  76. AudioFrame fade_buffer[MIX_BUFFER_SIZE];
  77. Ref<AudioStreamPlaylist> playlist;
  78. Ref<AudioStreamPlayback> playback[AudioStreamPlaylist::MAX_STREAMS];
  79. int play_order[AudioStreamPlaylist::MAX_STREAMS] = {};
  80. double stream_todo = 0.0;
  81. int fade_index = -1;
  82. double fade_volume = 1.0;
  83. int play_index = 0;
  84. double offset = 0.0;
  85. int loop_count = 0;
  86. bool active = false;
  87. void _update_order();
  88. void _update_playback_instances();
  89. public:
  90. virtual void start(double p_from_pos = 0.0) override;
  91. virtual void stop() override;
  92. virtual bool is_playing() const override;
  93. virtual int get_loop_count() const override; // times it looped
  94. virtual double get_playback_position() const override;
  95. virtual void seek(double p_time) override;
  96. virtual int mix(AudioFrame *p_buffer, float p_rate_scale, int p_frames) override;
  97. virtual void tag_used_streams() override;
  98. ~AudioStreamPlaybackPlaylist();
  99. };
  100. #endif // AUDIO_STREAM_PLAYLIST_H