audio_stream.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 "core/image.h"
  33. #include "core/resource.h"
  34. #include "servers/audio/audio_filter_sw.h"
  35. #include "servers/audio_server.h"
  36. class AudioStreamPlayback : public Reference {
  37. GDCLASS(AudioStreamPlayback, Reference)
  38. public:
  39. virtual void start(float p_from_pos = 0.0) = 0;
  40. virtual void stop() = 0;
  41. virtual bool is_playing() const = 0;
  42. virtual int get_loop_count() const = 0; //times it looped
  43. virtual float get_playback_position() const = 0;
  44. virtual void seek(float p_time) = 0;
  45. virtual void mix(AudioFrame *p_buffer, float p_rate_scale, int p_frames) = 0;
  46. };
  47. class AudioStreamPlaybackResampled : public AudioStreamPlayback {
  48. GDCLASS(AudioStreamPlaybackResampled, AudioStreamPlayback)
  49. enum {
  50. FP_BITS = 16, //fixed point used for resampling
  51. FP_LEN = (1 << FP_BITS),
  52. FP_MASK = FP_LEN - 1,
  53. INTERNAL_BUFFER_LEN = 256,
  54. CUBIC_INTERP_HISTORY = 4
  55. };
  56. AudioFrame internal_buffer[INTERNAL_BUFFER_LEN + CUBIC_INTERP_HISTORY];
  57. uint64_t mix_offset;
  58. protected:
  59. void _begin_resample();
  60. virtual void _mix_internal(AudioFrame *p_buffer, int p_frames) = 0;
  61. virtual float get_stream_sampling_rate() = 0;
  62. public:
  63. virtual void mix(AudioFrame *p_buffer, float p_rate_scale, int p_frames);
  64. AudioStreamPlaybackResampled() { mix_offset = 0; }
  65. };
  66. class AudioStream : public Resource {
  67. GDCLASS(AudioStream, Resource)
  68. OBJ_SAVE_TYPE(AudioStream) //children are all saved as AudioStream, so they can be exchanged
  69. protected:
  70. static void _bind_methods();
  71. public:
  72. virtual Ref<AudioStreamPlayback> instance_playback() = 0;
  73. virtual String get_stream_name() const = 0;
  74. virtual float get_length() const = 0; //if supported, otherwise return 0
  75. };
  76. // Microphone
  77. class AudioStreamPlaybackMicrophone;
  78. class AudioStreamMicrophone : public AudioStream {
  79. GDCLASS(AudioStreamMicrophone, AudioStream)
  80. friend class AudioStreamPlaybackMicrophone;
  81. Set<AudioStreamPlaybackMicrophone *> playbacks;
  82. protected:
  83. static void _bind_methods();
  84. public:
  85. virtual Ref<AudioStreamPlayback> instance_playback();
  86. virtual String get_stream_name() const;
  87. virtual float get_length() const; //if supported, otherwise return 0
  88. AudioStreamMicrophone();
  89. };
  90. class AudioStreamPlaybackMicrophone : public AudioStreamPlaybackResampled {
  91. GDCLASS(AudioStreamPlaybackMicrophone, AudioStreamPlayback)
  92. friend class AudioStreamMicrophone;
  93. bool active;
  94. unsigned int input_ofs;
  95. Ref<AudioStreamMicrophone> microphone;
  96. protected:
  97. virtual void _mix_internal(AudioFrame *p_buffer, int p_frames);
  98. virtual float get_stream_sampling_rate();
  99. public:
  100. virtual void mix(AudioFrame *p_buffer, float p_rate_scale, int p_frames);
  101. virtual void start(float p_from_pos = 0.0);
  102. virtual void stop();
  103. virtual bool is_playing() const;
  104. virtual int get_loop_count() const; //times it looped
  105. virtual float get_playback_position() const;
  106. virtual void seek(float p_time);
  107. ~AudioStreamPlaybackMicrophone();
  108. AudioStreamPlaybackMicrophone();
  109. };
  110. //
  111. class AudioStreamPlaybackRandomPitch;
  112. class AudioStreamRandomPitch : public AudioStream {
  113. GDCLASS(AudioStreamRandomPitch, AudioStream)
  114. friend class AudioStreamPlaybackRandomPitch;
  115. Set<AudioStreamPlaybackRandomPitch *> playbacks;
  116. Ref<AudioStream> audio_stream;
  117. float random_pitch;
  118. protected:
  119. static void _bind_methods();
  120. public:
  121. void set_audio_stream(const Ref<AudioStream> &p_audio_stream);
  122. Ref<AudioStream> get_audio_stream() const;
  123. void set_random_pitch(float p_pitch);
  124. float get_random_pitch() const;
  125. virtual Ref<AudioStreamPlayback> instance_playback();
  126. virtual String get_stream_name() const;
  127. virtual float get_length() const; //if supported, otherwise return 0
  128. AudioStreamRandomPitch();
  129. };
  130. class AudioStreamPlaybackRandomPitch : public AudioStreamPlayback {
  131. GDCLASS(AudioStreamPlaybackRandomPitch, AudioStreamPlayback)
  132. friend class AudioStreamRandomPitch;
  133. Ref<AudioStreamRandomPitch> random_pitch;
  134. Ref<AudioStreamPlayback> playback;
  135. Ref<AudioStreamPlayback> playing;
  136. float pitch_scale;
  137. public:
  138. virtual void start(float p_from_pos = 0.0);
  139. virtual void stop();
  140. virtual bool is_playing() const;
  141. virtual int get_loop_count() const; //times it looped
  142. virtual float get_playback_position() const;
  143. virtual void seek(float p_time);
  144. virtual void mix(AudioFrame *p_buffer, float p_rate_scale, int p_frames);
  145. ~AudioStreamPlaybackRandomPitch();
  146. };
  147. #endif // AUDIO_STREAM_H