audio_stream_generator.h 4.4 KB

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