audio_stream.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*************************************************************************/
  2. /* audio_stream.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  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_H
  31. #define AUDIO_STREAM_H
  32. #include "resource.h"
  33. #include "servers/audio/audio_filter_sw.h"
  34. #include "servers/audio_server.h"
  35. class AudioStreamPlayback : public Reference {
  36. GDCLASS(AudioStreamPlayback, Reference)
  37. public:
  38. virtual void start(float p_from_pos = 0.0) = 0;
  39. virtual void stop() = 0;
  40. virtual bool is_playing() const = 0;
  41. virtual int get_loop_count() const = 0; //times it looped
  42. virtual float get_playback_position() const = 0;
  43. virtual void seek(float p_time) = 0;
  44. virtual void mix(AudioFrame *p_buffer, float p_rate_scale, int p_frames) = 0;
  45. };
  46. class AudioStreamPlaybackResampled : public AudioStreamPlayback {
  47. GDCLASS(AudioStreamPlaybackResampled, AudioStreamPlayback)
  48. enum {
  49. FP_BITS = 16, //fixed point used for resampling
  50. FP_LEN = (1 << FP_BITS),
  51. FP_MASK = FP_LEN - 1,
  52. INTERNAL_BUFFER_LEN = 256,
  53. CUBIC_INTERP_HISTORY = 4
  54. };
  55. AudioFrame internal_buffer[INTERNAL_BUFFER_LEN + CUBIC_INTERP_HISTORY];
  56. uint64_t mix_offset;
  57. protected:
  58. void _begin_resample();
  59. virtual void _mix_internal(AudioFrame *p_buffer, int p_frames) = 0;
  60. virtual float get_stream_sampling_rate() = 0;
  61. public:
  62. virtual void mix(AudioFrame *p_buffer, float p_rate_scale, int p_frames);
  63. AudioStreamPlaybackResampled() { mix_offset = 0; }
  64. };
  65. class AudioStream : public Resource {
  66. GDCLASS(AudioStream, Resource)
  67. OBJ_SAVE_TYPE(AudioStream) //children are all saved as AudioStream, so they can be exchanged
  68. protected:
  69. static void _bind_methods();
  70. public:
  71. virtual Ref<AudioStreamPlayback> instance_playback() = 0;
  72. virtual String get_stream_name() const = 0;
  73. virtual float get_length() const = 0; //if supported, otherwise return 0
  74. };
  75. class AudioStreamPlaybackRandomPitch;
  76. class AudioStreamRandomPitch : public AudioStream {
  77. GDCLASS(AudioStreamRandomPitch, AudioStream)
  78. friend class AudioStreamPlaybackRandomPitch;
  79. Set<AudioStreamPlaybackRandomPitch *> playbacks;
  80. Ref<AudioStream> audio_stream;
  81. float random_pitch;
  82. protected:
  83. static void _bind_methods();
  84. public:
  85. void set_audio_stream(const Ref<AudioStream> &p_audio_stream);
  86. Ref<AudioStream> get_audio_stream() const;
  87. void set_random_pitch(float p_pitch);
  88. float get_random_pitch() const;
  89. virtual Ref<AudioStreamPlayback> instance_playback();
  90. virtual String get_stream_name() const;
  91. virtual float get_length() const; //if supported, otherwise return 0
  92. AudioStreamRandomPitch();
  93. };
  94. class AudioStreamPlaybackRandomPitch : public AudioStreamPlayback {
  95. GDCLASS(AudioStreamPlaybackRandomPitch, AudioStreamPlayback)
  96. friend class AudioStreamRandomPitch;
  97. Ref<AudioStreamRandomPitch> random_pitch;
  98. Ref<AudioStreamPlayback> playback;
  99. Ref<AudioStreamPlayback> playing;
  100. float pitch_scale;
  101. public:
  102. virtual void start(float p_from_pos = 0.0);
  103. virtual void stop();
  104. virtual bool is_playing() const;
  105. virtual int get_loop_count() const; //times it looped
  106. virtual float get_playback_position() const;
  107. virtual void seek(float p_time);
  108. virtual void mix(AudioFrame *p_buffer, float p_rate_scale, int p_frames);
  109. ~AudioStreamPlaybackRandomPitch();
  110. };
  111. #endif // AUDIO_STREAM_H