audio_stream_generator.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. float mix_rate;
  37. float buffer_len;
  38. protected:
  39. static void _bind_methods();
  40. public:
  41. void set_mix_rate(float p_mix_rate);
  42. float get_mix_rate() const;
  43. void set_buffer_length(float p_seconds);
  44. float get_buffer_length() const;
  45. virtual Ref<AudioStreamPlayback> instantiate_playback() override;
  46. virtual String get_stream_name() const override;
  47. virtual double get_length() const override;
  48. virtual bool is_monophonic() const override;
  49. AudioStreamGenerator();
  50. };
  51. class AudioStreamGeneratorPlayback : public AudioStreamPlaybackResampled {
  52. GDCLASS(AudioStreamGeneratorPlayback, AudioStreamPlaybackResampled);
  53. friend class AudioStreamGenerator;
  54. RingBuffer<AudioFrame> buffer;
  55. int skips;
  56. bool active;
  57. float mixed;
  58. AudioStreamGenerator *generator = nullptr;
  59. protected:
  60. virtual int _mix_internal(AudioFrame *p_buffer, int p_frames) override;
  61. virtual float get_stream_sampling_rate() override;
  62. static void _bind_methods();
  63. public:
  64. virtual void start(double p_from_pos = 0.0) override;
  65. virtual void stop() override;
  66. virtual bool is_playing() const override;
  67. virtual int get_loop_count() const override; //times it looped
  68. virtual double get_playback_position() const override;
  69. virtual void seek(double p_time) override;
  70. bool push_frame(const Vector2 &p_frame);
  71. bool can_push_buffer(int p_frames) const;
  72. bool push_buffer(const PackedVector2Array &p_frames);
  73. int get_frames_available() const;
  74. int get_skips() const;
  75. virtual void tag_used_streams() override;
  76. void clear_buffer();
  77. AudioStreamGeneratorPlayback();
  78. };
  79. #endif // AUDIO_STREAM_GENERATOR_H