audio_stream.h 9.6 KB

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