audio_stream.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*************************************************************************/
  2. /* audio_stream.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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 "core/io/image.h"
  33. #include "core/io/resource.h"
  34. #include "servers/audio/audio_filter_sw.h"
  35. #include "servers/audio_server.h"
  36. #include "core/object/gdvirtual.gen.inc"
  37. #include "core/object/script_language.h"
  38. #include "core/variant/native_ptr.h"
  39. class AudioStreamPlayback : public RefCounted {
  40. GDCLASS(AudioStreamPlayback, RefCounted);
  41. protected:
  42. static void _bind_methods();
  43. GDVIRTUAL1(_start, float)
  44. GDVIRTUAL0(_stop)
  45. GDVIRTUAL0RC(bool, _is_playing)
  46. GDVIRTUAL0RC(int, _get_loop_count)
  47. GDVIRTUAL0RC(float, _get_playback_position)
  48. GDVIRTUAL1(_seek, float)
  49. GDVIRTUAL3R(int, _mix, GDNativePtr<AudioFrame>, float, int)
  50. public:
  51. virtual void start(float p_from_pos = 0.0);
  52. virtual void stop();
  53. virtual bool is_playing() const;
  54. virtual int get_loop_count() const; //times it looped
  55. virtual float get_playback_position() const;
  56. virtual void seek(float p_time);
  57. virtual int mix(AudioFrame *p_buffer, float p_rate_scale, int p_frames);
  58. };
  59. class AudioStreamPlaybackResampled : public AudioStreamPlayback {
  60. GDCLASS(AudioStreamPlaybackResampled, AudioStreamPlayback);
  61. enum {
  62. FP_BITS = 16, //fixed point used for resampling
  63. FP_LEN = (1 << FP_BITS),
  64. FP_MASK = FP_LEN - 1,
  65. INTERNAL_BUFFER_LEN = 256,
  66. CUBIC_INTERP_HISTORY = 4
  67. };
  68. AudioFrame internal_buffer[INTERNAL_BUFFER_LEN + CUBIC_INTERP_HISTORY];
  69. unsigned int internal_buffer_end = -1;
  70. uint64_t mix_offset;
  71. protected:
  72. void _begin_resample();
  73. // Returns the number of frames that were mixed.
  74. virtual int _mix_internal(AudioFrame *p_buffer, int p_frames) = 0;
  75. virtual float get_stream_sampling_rate() = 0;
  76. public:
  77. virtual int mix(AudioFrame *p_buffer, float p_rate_scale, int p_frames) override;
  78. AudioStreamPlaybackResampled() { mix_offset = 0; }
  79. };
  80. class AudioStream : public Resource {
  81. GDCLASS(AudioStream, Resource);
  82. OBJ_SAVE_TYPE(AudioStream); // Saves derived classes with common type so they can be interchanged.
  83. protected:
  84. static void _bind_methods();
  85. GDVIRTUAL0RC(Ref<AudioStreamPlayback>, _instance_playback)
  86. GDVIRTUAL0RC(String, _get_stream_name)
  87. GDVIRTUAL0RC(float, _get_length)
  88. GDVIRTUAL0RC(bool, _is_monophonic)
  89. public:
  90. virtual Ref<AudioStreamPlayback> instance_playback();
  91. virtual String get_stream_name() const;
  92. virtual float get_length() const;
  93. virtual bool is_monophonic() const;
  94. };
  95. // Microphone
  96. class AudioStreamPlaybackMicrophone;
  97. class AudioStreamMicrophone : public AudioStream {
  98. GDCLASS(AudioStreamMicrophone, AudioStream);
  99. friend class AudioStreamPlaybackMicrophone;
  100. Set<AudioStreamPlaybackMicrophone *> playbacks;
  101. protected:
  102. static void _bind_methods();
  103. public:
  104. virtual Ref<AudioStreamPlayback> instance_playback() override;
  105. virtual String get_stream_name() const override;
  106. virtual float get_length() const override; //if supported, otherwise return 0
  107. virtual bool is_monophonic() const override;
  108. AudioStreamMicrophone();
  109. };
  110. class AudioStreamPlaybackMicrophone : public AudioStreamPlaybackResampled {
  111. GDCLASS(AudioStreamPlaybackMicrophone, AudioStreamPlaybackResampled);
  112. friend class AudioStreamMicrophone;
  113. bool active;
  114. unsigned int input_ofs;
  115. Ref<AudioStreamMicrophone> microphone;
  116. protected:
  117. virtual int _mix_internal(AudioFrame *p_buffer, int p_frames) override;
  118. virtual float get_stream_sampling_rate() override;
  119. public:
  120. virtual int mix(AudioFrame *p_buffer, float p_rate_scale, int p_frames) override;
  121. virtual void start(float p_from_pos = 0.0) override;
  122. virtual void stop() override;
  123. virtual bool is_playing() const override;
  124. virtual int get_loop_count() const override; //times it looped
  125. virtual float get_playback_position() const override;
  126. virtual void seek(float p_time) override;
  127. ~AudioStreamPlaybackMicrophone();
  128. AudioStreamPlaybackMicrophone();
  129. };
  130. //
  131. class AudioStreamPlaybackRandomPitch;
  132. class AudioStreamRandomPitch : public AudioStream {
  133. GDCLASS(AudioStreamRandomPitch, AudioStream);
  134. friend class AudioStreamPlaybackRandomPitch;
  135. Set<AudioStreamPlaybackRandomPitch *> playbacks;
  136. Ref<AudioStream> audio_stream;
  137. float random_pitch;
  138. protected:
  139. static void _bind_methods();
  140. public:
  141. void set_audio_stream(const Ref<AudioStream> &p_audio_stream);
  142. Ref<AudioStream> get_audio_stream() const;
  143. void set_random_pitch(float p_pitch);
  144. float get_random_pitch() const;
  145. virtual Ref<AudioStreamPlayback> instance_playback() override;
  146. virtual String get_stream_name() const override;
  147. virtual float get_length() const override; //if supported, otherwise return 0
  148. virtual bool is_monophonic() const override;
  149. AudioStreamRandomPitch();
  150. };
  151. class AudioStreamPlaybackRandomPitch : public AudioStreamPlayback {
  152. GDCLASS(AudioStreamPlaybackRandomPitch, AudioStreamPlayback);
  153. friend class AudioStreamRandomPitch;
  154. Ref<AudioStreamRandomPitch> random_pitch;
  155. Ref<AudioStreamPlayback> playback;
  156. Ref<AudioStreamPlayback> playing;
  157. float pitch_scale;
  158. public:
  159. virtual void start(float p_from_pos = 0.0) override;
  160. virtual void stop() override;
  161. virtual bool is_playing() const override;
  162. virtual int get_loop_count() const override; //times it looped
  163. virtual float get_playback_position() const override;
  164. virtual void seek(float p_time) override;
  165. virtual int mix(AudioFrame *p_buffer, float p_rate_scale, int p_frames) override;
  166. ~AudioStreamPlaybackRandomPitch();
  167. };
  168. #endif // AUDIO_STREAM_H