audio_stream_polyphonic.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /**************************************************************************/
  2. /* audio_stream_polyphonic.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_POLYPHONIC_H
  31. #define AUDIO_STREAM_POLYPHONIC_H
  32. #include "core/templates/local_vector.h"
  33. #include "scene/scene_string_names.h"
  34. #include "servers/audio/audio_stream.h"
  35. #include "servers/audio_server.h"
  36. class AudioStreamPolyphonic : public AudioStream {
  37. GDCLASS(AudioStreamPolyphonic, AudioStream)
  38. int polyphony = 32;
  39. AudioServer::PlaybackType playback_type;
  40. static void _bind_methods();
  41. public:
  42. virtual Ref<AudioStreamPlayback> instantiate_playback() override;
  43. virtual String get_stream_name() const override;
  44. virtual bool is_monophonic() const override;
  45. void set_polyphony(int p_voices);
  46. int get_polyphony() const;
  47. virtual bool is_meta_stream() const override { return true; }
  48. AudioStreamPolyphonic();
  49. };
  50. class AudioStreamPlaybackPolyphonic : public AudioStreamPlayback {
  51. GDCLASS(AudioStreamPlaybackPolyphonic, AudioStreamPlayback)
  52. constexpr static uint32_t INTERNAL_BUFFER_LEN = 128;
  53. struct Stream {
  54. SafeFlag active;
  55. SafeFlag pending_play;
  56. SafeFlag finish_request;
  57. float play_offset = 0;
  58. float pitch_scale = 1.0;
  59. Ref<AudioStream> stream;
  60. Ref<AudioStreamPlayback> stream_playback;
  61. float prev_volume_db = 0;
  62. float volume_db = 0;
  63. uint32_t id = 0;
  64. Stream() :
  65. active(false), pending_play(false), finish_request(false) {}
  66. };
  67. LocalVector<Stream> streams;
  68. AudioFrame internal_buffer[INTERNAL_BUFFER_LEN];
  69. bool active = false;
  70. uint32_t id_counter = 1;
  71. bool _is_sample = false;
  72. Ref<AudioSamplePlayback> sample_playback;
  73. _FORCE_INLINE_ Stream *_find_stream(int64_t p_id);
  74. _FORCE_INLINE_ const Stream *_find_stream(int64_t p_id) const;
  75. friend class AudioStreamPolyphonic;
  76. protected:
  77. static void _bind_methods();
  78. public:
  79. typedef int64_t ID;
  80. enum {
  81. INVALID_ID = -1
  82. };
  83. virtual void start(double p_from_pos = 0.0) override;
  84. virtual void stop() override;
  85. virtual bool is_playing() const override;
  86. virtual int get_loop_count() const override; //times it looped
  87. virtual double get_playback_position() const override;
  88. virtual void seek(double p_time) override;
  89. virtual void tag_used_streams() override;
  90. virtual int mix(AudioFrame *p_buffer, float p_rate_scale, int p_frames) override;
  91. ID play_stream(const Ref<AudioStream> &p_stream, float p_from_offset = 0, float p_volume_db = 0, float p_pitch_scale = 1.0, AudioServer::PlaybackType p_playback_type = AudioServer::PlaybackType::PLAYBACK_TYPE_DEFAULT, const StringName &p_bus = SceneStringName(Master));
  92. void set_stream_volume(ID p_stream_id, float p_volume_db);
  93. void set_stream_pitch_scale(ID p_stream_id, float p_pitch_scale);
  94. bool is_stream_playing(ID p_stream_id) const;
  95. void stop_stream(ID p_stream_id);
  96. virtual void set_is_sample(bool p_is_sample) override;
  97. virtual bool get_is_sample() const override;
  98. virtual Ref<AudioSamplePlayback> get_sample_playback() const override;
  99. virtual void set_sample_playback(const Ref<AudioSamplePlayback> &p_playback) override;
  100. private:
  101. #ifndef DISABLE_DEPRECATED
  102. ID _play_stream_bind_compat_91382(const Ref<AudioStream> &p_stream, float p_from_offset = 0, float p_volume_db = 0, float p_pitch_scale = 1.0);
  103. static void _bind_compatibility_methods();
  104. #endif // DISABLE_DEPRECATED
  105. public:
  106. AudioStreamPlaybackPolyphonic();
  107. };
  108. #endif // AUDIO_STREAM_POLYPHONIC_H