audio_stream_synchronized.h 4.7 KB

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