audio_stream_polyphonic.h 5.3 KB

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