audio_stream.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /**************************************************************************/
  2. /* audio_stream.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_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 AudioStream;
  40. class AudioStreamPlayback : public RefCounted {
  41. GDCLASS(AudioStreamPlayback, RefCounted);
  42. protected:
  43. static void _bind_methods();
  44. GDVIRTUAL1(_start, double)
  45. GDVIRTUAL0(_stop)
  46. GDVIRTUAL0RC(bool, _is_playing)
  47. GDVIRTUAL0RC(int, _get_loop_count)
  48. GDVIRTUAL0RC(double, _get_playback_position)
  49. GDVIRTUAL1(_seek, double)
  50. GDVIRTUAL3R(int, _mix, GDExtensionPtr<AudioFrame>, float, int)
  51. GDVIRTUAL0(_tag_used_streams)
  52. public:
  53. virtual void start(double p_from_pos = 0.0);
  54. virtual void stop();
  55. virtual bool is_playing() const;
  56. virtual int get_loop_count() const; //times it looped
  57. virtual double get_playback_position() const;
  58. virtual void seek(double p_time);
  59. virtual void tag_used_streams();
  60. virtual int mix(AudioFrame *p_buffer, float p_rate_scale, int p_frames);
  61. };
  62. class AudioStreamPlaybackResampled : public AudioStreamPlayback {
  63. GDCLASS(AudioStreamPlaybackResampled, AudioStreamPlayback);
  64. enum {
  65. FP_BITS = 16, //fixed point used for resampling
  66. FP_LEN = (1 << FP_BITS),
  67. FP_MASK = FP_LEN - 1,
  68. INTERNAL_BUFFER_LEN = 128, // 128 warrants 3ms positional jitter at much at 44100hz
  69. CUBIC_INTERP_HISTORY = 4
  70. };
  71. AudioFrame internal_buffer[INTERNAL_BUFFER_LEN + CUBIC_INTERP_HISTORY];
  72. unsigned int internal_buffer_end = -1;
  73. uint64_t mix_offset = 0;
  74. protected:
  75. void begin_resample();
  76. // Returns the number of frames that were mixed.
  77. virtual int _mix_internal(AudioFrame *p_buffer, int p_frames);
  78. virtual float get_stream_sampling_rate();
  79. GDVIRTUAL2R(int, _mix_resampled, GDExtensionPtr<AudioFrame>, int)
  80. GDVIRTUAL0RC(float, _get_stream_sampling_rate)
  81. static void _bind_methods();
  82. public:
  83. virtual int mix(AudioFrame *p_buffer, float p_rate_scale, int p_frames) override;
  84. AudioStreamPlaybackResampled() { mix_offset = 0; }
  85. };
  86. class AudioStream : public Resource {
  87. GDCLASS(AudioStream, Resource);
  88. OBJ_SAVE_TYPE(AudioStream); // Saves derived classes with common type so they can be interchanged.
  89. enum {
  90. MAX_TAGGED_OFFSETS = 8
  91. };
  92. uint64_t tagged_frame = 0;
  93. uint64_t offset_count = 0;
  94. float tagged_offsets[MAX_TAGGED_OFFSETS];
  95. protected:
  96. static void _bind_methods();
  97. GDVIRTUAL0RC(Ref<AudioStreamPlayback>, _instantiate_playback)
  98. GDVIRTUAL0RC(String, _get_stream_name)
  99. GDVIRTUAL0RC(double, _get_length)
  100. GDVIRTUAL0RC(bool, _is_monophonic)
  101. GDVIRTUAL0RC(double, _get_bpm)
  102. GDVIRTUAL0RC(bool, _has_loop)
  103. GDVIRTUAL0RC(int, _get_bar_beats)
  104. GDVIRTUAL0RC(int, _get_beat_count)
  105. public:
  106. virtual Ref<AudioStreamPlayback> instantiate_playback();
  107. virtual String get_stream_name() const;
  108. virtual double get_bpm() const;
  109. virtual bool has_loop() const;
  110. virtual int get_bar_beats() const;
  111. virtual int get_beat_count() const;
  112. virtual double get_length() const;
  113. virtual bool is_monophonic() const;
  114. void tag_used(float p_offset);
  115. uint64_t get_tagged_frame() const;
  116. uint32_t get_tagged_frame_count() const;
  117. float get_tagged_frame_offset(int p_index) const;
  118. };
  119. // Microphone
  120. class AudioStreamPlaybackMicrophone;
  121. class AudioStreamMicrophone : public AudioStream {
  122. GDCLASS(AudioStreamMicrophone, AudioStream);
  123. friend class AudioStreamPlaybackMicrophone;
  124. HashSet<AudioStreamPlaybackMicrophone *> playbacks;
  125. protected:
  126. static void _bind_methods();
  127. public:
  128. virtual Ref<AudioStreamPlayback> instantiate_playback() override;
  129. virtual String get_stream_name() const override;
  130. virtual double get_length() const override; //if supported, otherwise return 0
  131. virtual bool is_monophonic() const override;
  132. AudioStreamMicrophone();
  133. };
  134. class AudioStreamPlaybackMicrophone : public AudioStreamPlaybackResampled {
  135. GDCLASS(AudioStreamPlaybackMicrophone, AudioStreamPlaybackResampled);
  136. friend class AudioStreamMicrophone;
  137. bool active = false;
  138. unsigned int input_ofs = 0;
  139. Ref<AudioStreamMicrophone> microphone;
  140. protected:
  141. virtual int _mix_internal(AudioFrame *p_buffer, int p_frames) override;
  142. virtual float get_stream_sampling_rate() override;
  143. virtual double get_playback_position() const override;
  144. public:
  145. virtual int mix(AudioFrame *p_buffer, float p_rate_scale, int p_frames) override;
  146. virtual void start(double p_from_pos = 0.0) override;
  147. virtual void stop() override;
  148. virtual bool is_playing() const override;
  149. virtual int get_loop_count() const override; //times it looped
  150. virtual void seek(double p_time) override;
  151. virtual void tag_used_streams() override;
  152. ~AudioStreamPlaybackMicrophone();
  153. AudioStreamPlaybackMicrophone();
  154. };
  155. //
  156. class AudioStreamPlaybackRandomizer;
  157. class AudioStreamRandomizer : public AudioStream {
  158. GDCLASS(AudioStreamRandomizer, AudioStream);
  159. public:
  160. enum PlaybackMode {
  161. PLAYBACK_RANDOM_NO_REPEATS,
  162. PLAYBACK_RANDOM,
  163. PLAYBACK_SEQUENTIAL,
  164. };
  165. private:
  166. friend class AudioStreamPlaybackRandomizer;
  167. struct PoolEntry {
  168. Ref<AudioStream> stream;
  169. float weight;
  170. };
  171. HashSet<AudioStreamPlaybackRandomizer *> playbacks;
  172. Vector<PoolEntry> audio_stream_pool;
  173. float random_pitch_scale = 1.1f;
  174. float random_volume_offset_db = 5.0f;
  175. Ref<AudioStreamPlayback> instance_playback_random();
  176. Ref<AudioStreamPlayback> instance_playback_no_repeats();
  177. Ref<AudioStreamPlayback> instance_playback_sequential();
  178. Ref<AudioStream> last_playback = nullptr;
  179. PlaybackMode playback_mode = PLAYBACK_RANDOM_NO_REPEATS;
  180. protected:
  181. static void _bind_methods();
  182. bool _set(const StringName &p_name, const Variant &p_value);
  183. bool _get(const StringName &p_name, Variant &r_ret) const;
  184. void _get_property_list(List<PropertyInfo> *p_list) const;
  185. public:
  186. void add_stream(int p_index, Ref<AudioStream> p_stream, float p_weight = 1.0);
  187. void move_stream(int p_index_from, int p_index_to);
  188. void remove_stream(int p_index);
  189. void set_stream(int p_index, Ref<AudioStream> p_stream);
  190. Ref<AudioStream> get_stream(int p_index) const;
  191. void set_stream_probability_weight(int p_index, float p_weight);
  192. float get_stream_probability_weight(int p_index) const;
  193. void set_streams_count(int p_count);
  194. int get_streams_count() const;
  195. void set_random_pitch(float p_pitch_scale);
  196. float get_random_pitch() const;
  197. void set_random_volume_offset_db(float p_volume_offset_db);
  198. float get_random_volume_offset_db() const;
  199. void set_playback_mode(PlaybackMode p_playback_mode);
  200. PlaybackMode get_playback_mode() const;
  201. virtual Ref<AudioStreamPlayback> instantiate_playback() override;
  202. virtual String get_stream_name() const override;
  203. virtual double get_length() const override; //if supported, otherwise return 0
  204. virtual bool is_monophonic() const override;
  205. AudioStreamRandomizer();
  206. };
  207. class AudioStreamPlaybackRandomizer : public AudioStreamPlayback {
  208. GDCLASS(AudioStreamPlaybackRandomizer, AudioStreamPlayback);
  209. friend class AudioStreamRandomizer;
  210. Ref<AudioStreamRandomizer> randomizer;
  211. Ref<AudioStreamPlayback> playback;
  212. Ref<AudioStreamPlayback> playing;
  213. float pitch_scale;
  214. float volume_scale;
  215. public:
  216. virtual void start(double p_from_pos = 0.0) override;
  217. virtual void stop() override;
  218. virtual bool is_playing() const override;
  219. virtual int get_loop_count() const override; //times it looped
  220. virtual double get_playback_position() const override;
  221. virtual void seek(double p_time) override;
  222. virtual int mix(AudioFrame *p_buffer, float p_rate_scale, int p_frames) override;
  223. virtual void tag_used_streams() override;
  224. ~AudioStreamPlaybackRandomizer();
  225. };
  226. VARIANT_ENUM_CAST(AudioStreamRandomizer::PlaybackMode);
  227. #endif // AUDIO_STREAM_H