audio_stream_wav.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /**************************************************************************/
  2. /* audio_stream_wav.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_WAV_H
  31. #define AUDIO_STREAM_WAV_H
  32. #define QOA_IMPLEMENTATION
  33. #define QOA_NO_STDIO
  34. #include "servers/audio/audio_stream.h"
  35. #include "thirdparty/misc/qoa.h"
  36. class AudioStreamWAV;
  37. class AudioStreamPlaybackWAV : public AudioStreamPlayback {
  38. GDCLASS(AudioStreamPlaybackWAV, AudioStreamPlayback);
  39. enum {
  40. MIX_FRAC_BITS = 13,
  41. MIX_FRAC_LEN = (1 << MIX_FRAC_BITS),
  42. MIX_FRAC_MASK = MIX_FRAC_LEN - 1,
  43. };
  44. struct IMA_ADPCM_State {
  45. int16_t step_index = 0;
  46. int32_t predictor = 0;
  47. /* values at loop point */
  48. int16_t loop_step_index = 0;
  49. int32_t loop_predictor = 0;
  50. int32_t last_nibble = 0;
  51. int32_t loop_pos = 0;
  52. int32_t window_ofs = 0;
  53. } ima_adpcm[2];
  54. struct QOA_State {
  55. qoa_desc desc = {};
  56. uint32_t data_ofs = 0;
  57. uint32_t frame_len = 0;
  58. LocalVector<int16_t> dec;
  59. uint32_t dec_len = 0;
  60. int64_t cache_pos = -1;
  61. int16_t cache[2] = { 0, 0 };
  62. int16_t cache_r[2] = { 0, 0 };
  63. } qoa;
  64. int64_t offset = 0;
  65. int sign = 1;
  66. bool active = false;
  67. friend class AudioStreamWAV;
  68. Ref<AudioStreamWAV> base;
  69. template <typename Depth, bool is_stereo, bool is_ima_adpcm, bool is_qoa>
  70. void do_resample(const Depth *p_src, AudioFrame *p_dst, int64_t &p_offset, int32_t &p_increment, uint32_t p_amount, IMA_ADPCM_State *p_ima_adpcm, QOA_State *p_qoa);
  71. bool _is_sample = false;
  72. Ref<AudioSamplePlayback> sample_playback;
  73. public:
  74. virtual void start(double p_from_pos = 0.0) override;
  75. virtual void stop() override;
  76. virtual bool is_playing() const override;
  77. virtual int get_loop_count() const override; //times it looped
  78. virtual double get_playback_position() const override;
  79. virtual void seek(double p_time) override;
  80. virtual int mix(AudioFrame *p_buffer, float p_rate_scale, int p_frames) override;
  81. virtual void tag_used_streams() override;
  82. virtual void set_is_sample(bool p_is_sample) override;
  83. virtual bool get_is_sample() const override;
  84. virtual Ref<AudioSamplePlayback> get_sample_playback() const override;
  85. virtual void set_sample_playback(const Ref<AudioSamplePlayback> &p_playback) override;
  86. AudioStreamPlaybackWAV();
  87. ~AudioStreamPlaybackWAV();
  88. };
  89. class AudioStreamWAV : public AudioStream {
  90. GDCLASS(AudioStreamWAV, AudioStream);
  91. RES_BASE_EXTENSION("sample")
  92. public:
  93. enum Format {
  94. FORMAT_8_BITS,
  95. FORMAT_16_BITS,
  96. FORMAT_IMA_ADPCM,
  97. FORMAT_QOA,
  98. };
  99. // Keep the ResourceImporterWAV `edit/loop_mode` enum hint in sync with these options.
  100. enum LoopMode {
  101. LOOP_DISABLED,
  102. LOOP_FORWARD,
  103. LOOP_PINGPONG,
  104. LOOP_BACKWARD
  105. };
  106. private:
  107. friend class AudioStreamPlaybackWAV;
  108. enum {
  109. DATA_PAD = 16 //padding for interpolation
  110. };
  111. Format format = FORMAT_8_BITS;
  112. LoopMode loop_mode = LOOP_DISABLED;
  113. bool stereo = false;
  114. int loop_begin = 0;
  115. int loop_end = 0;
  116. int mix_rate = 44100;
  117. LocalVector<uint8_t> data;
  118. uint32_t data_bytes = 0;
  119. protected:
  120. static void _bind_methods();
  121. public:
  122. void set_format(Format p_format);
  123. Format get_format() const;
  124. void set_loop_mode(LoopMode p_loop_mode);
  125. LoopMode get_loop_mode() const;
  126. void set_loop_begin(int p_frame);
  127. int get_loop_begin() const;
  128. void set_loop_end(int p_frame);
  129. int get_loop_end() const;
  130. void set_mix_rate(int p_hz);
  131. int get_mix_rate() const;
  132. void set_stereo(bool p_enable);
  133. bool is_stereo() const;
  134. virtual double get_length() const override; //if supported, otherwise return 0
  135. virtual bool is_monophonic() const override;
  136. void set_data(const Vector<uint8_t> &p_data);
  137. Vector<uint8_t> get_data() const;
  138. Error save_to_wav(const String &p_path);
  139. virtual Ref<AudioStreamPlayback> instantiate_playback() override;
  140. virtual String get_stream_name() const override;
  141. virtual bool can_be_sampled() const override {
  142. return true;
  143. }
  144. virtual Ref<AudioSample> generate_sample() const override;
  145. AudioStreamWAV();
  146. ~AudioStreamWAV();
  147. };
  148. VARIANT_ENUM_CAST(AudioStreamWAV::Format)
  149. VARIANT_ENUM_CAST(AudioStreamWAV::LoopMode)
  150. #endif // AUDIO_STREAM_WAV_H