audio_stream_generator.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /**************************************************************************/
  2. /* audio_stream_generator.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/ring_buffer.h"
  32. #include "servers/audio/audio_stream.h"
  33. class AudioStreamGenerator : public AudioStream {
  34. GDCLASS(AudioStreamGenerator, AudioStream);
  35. public:
  36. enum AudioStreamGeneratorMixRate {
  37. MIX_RATE_OUTPUT,
  38. MIX_RATE_INPUT,
  39. MIX_RATE_CUSTOM,
  40. MIX_RATE_MAX,
  41. };
  42. private:
  43. AudioStreamGeneratorMixRate mix_rate_mode = MIX_RATE_CUSTOM;
  44. float mix_rate = 44100;
  45. float buffer_len = 0.5;
  46. protected:
  47. static void _bind_methods();
  48. public:
  49. float _get_target_rate() const;
  50. void set_mix_rate(float p_mix_rate);
  51. float get_mix_rate() const;
  52. void set_mix_rate_mode(AudioStreamGeneratorMixRate p_mix_rate_mode);
  53. AudioStreamGeneratorMixRate get_mix_rate_mode() const;
  54. void set_buffer_length(float p_seconds);
  55. float get_buffer_length() const;
  56. virtual Ref<AudioStreamPlayback> instantiate_playback() override;
  57. virtual String get_stream_name() const override;
  58. virtual double get_length() const override;
  59. virtual bool is_monophonic() const override;
  60. AudioStreamGenerator() {}
  61. };
  62. class AudioStreamGeneratorPlayback : public AudioStreamPlaybackResampled {
  63. GDCLASS(AudioStreamGeneratorPlayback, AudioStreamPlaybackResampled);
  64. friend class AudioStreamGenerator;
  65. RingBuffer<AudioFrame> buffer;
  66. int skips;
  67. bool active;
  68. float mixed;
  69. AudioStreamGenerator *generator = nullptr;
  70. protected:
  71. virtual int _mix_internal(AudioFrame *p_buffer, int p_frames) override;
  72. virtual float get_stream_sampling_rate() override;
  73. static void _bind_methods();
  74. public:
  75. virtual void start(double p_from_pos = 0.0) override;
  76. virtual void stop() override;
  77. virtual bool is_playing() const override;
  78. virtual int get_loop_count() const override; //times it looped
  79. virtual double get_playback_position() const override;
  80. virtual void seek(double p_time) override;
  81. bool push_frame(const Vector2 &p_frame);
  82. bool can_push_buffer(int p_frames) const;
  83. bool push_buffer(const PackedVector2Array &p_frames);
  84. int get_frames_available() const;
  85. int get_skips() const;
  86. virtual void tag_used_streams() override;
  87. void clear_buffer();
  88. AudioStreamGeneratorPlayback();
  89. };
  90. VARIANT_ENUM_CAST(AudioStreamGenerator::AudioStreamGeneratorMixRate);