audio_stream_wav.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. #include "servers/audio/audio_stream.h"
  33. #include "thirdparty/misc/qoa.h"
  34. class AudioStreamWAV;
  35. class AudioStreamPlaybackWAV : public AudioStreamPlayback {
  36. GDCLASS(AudioStreamPlaybackWAV, AudioStreamPlayback);
  37. enum {
  38. MIX_FRAC_BITS = 13,
  39. MIX_FRAC_LEN = (1 << MIX_FRAC_BITS),
  40. MIX_FRAC_MASK = MIX_FRAC_LEN - 1,
  41. };
  42. struct IMA_ADPCM_State {
  43. int16_t step_index = 0;
  44. int32_t predictor = 0;
  45. /* values at loop point */
  46. int16_t loop_step_index = 0;
  47. int32_t loop_predictor = 0;
  48. int32_t last_nibble = 0;
  49. int32_t loop_pos = 0;
  50. int32_t window_ofs = 0;
  51. } ima_adpcm[2];
  52. struct QOA_State {
  53. qoa_desc desc = {};
  54. uint32_t data_ofs = 0;
  55. uint32_t frame_len = 0;
  56. LocalVector<int16_t> dec;
  57. uint32_t dec_len = 0;
  58. int64_t cache_pos = -1;
  59. int16_t cache[2] = { 0, 0 };
  60. int16_t cache_r[2] = { 0, 0 };
  61. } qoa;
  62. int64_t offset = 0;
  63. int sign = 1;
  64. bool active = false;
  65. friend class AudioStreamWAV;
  66. Ref<AudioStreamWAV> base;
  67. template <typename Depth, bool is_stereo, bool is_ima_adpcm, bool is_qoa>
  68. 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);
  69. bool _is_sample = false;
  70. Ref<AudioSamplePlayback> sample_playback;
  71. public:
  72. virtual void start(double p_from_pos = 0.0) override;
  73. virtual void stop() override;
  74. virtual bool is_playing() const override;
  75. virtual int get_loop_count() const override; //times it looped
  76. virtual double get_playback_position() const override;
  77. virtual void seek(double p_time) override;
  78. virtual int mix(AudioFrame *p_buffer, float p_rate_scale, int p_frames) override;
  79. virtual void tag_used_streams() override;
  80. virtual void set_is_sample(bool p_is_sample) override;
  81. virtual bool get_is_sample() const override;
  82. virtual Ref<AudioSamplePlayback> get_sample_playback() const override;
  83. virtual void set_sample_playback(const Ref<AudioSamplePlayback> &p_playback) override;
  84. AudioStreamPlaybackWAV();
  85. ~AudioStreamPlaybackWAV();
  86. };
  87. class AudioStreamWAV : public AudioStream {
  88. GDCLASS(AudioStreamWAV, AudioStream);
  89. RES_BASE_EXTENSION("sample")
  90. public:
  91. enum Format {
  92. FORMAT_8_BITS,
  93. FORMAT_16_BITS,
  94. FORMAT_IMA_ADPCM,
  95. FORMAT_QOA,
  96. };
  97. // Keep the ResourceImporterWAV `edit/loop_mode` enum hint in sync with these options.
  98. enum LoopMode {
  99. LOOP_DISABLED,
  100. LOOP_FORWARD,
  101. LOOP_PINGPONG,
  102. LOOP_BACKWARD
  103. };
  104. private:
  105. friend class AudioStreamPlaybackWAV;
  106. enum {
  107. DATA_PAD = 16 //padding for interpolation
  108. };
  109. Format format = FORMAT_8_BITS;
  110. LoopMode loop_mode = LOOP_DISABLED;
  111. bool stereo = false;
  112. int loop_begin = 0;
  113. int loop_end = 0;
  114. int mix_rate = 44100;
  115. LocalVector<uint8_t> data;
  116. uint32_t data_bytes = 0;
  117. protected:
  118. static void _bind_methods();
  119. public:
  120. static Ref<AudioStreamWAV> load_from_buffer(const Vector<uint8_t> &p_stream_data, const Dictionary &p_options);
  121. static Ref<AudioStreamWAV> load_from_file(const String &p_path, const Dictionary &p_options);
  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. static void _compress_ima_adpcm(const Vector<float> &p_data, Vector<uint8_t> &r_dst_data) {
  146. static const int16_t _ima_adpcm_step_table[89] = {
  147. 7, 8, 9, 10, 11, 12, 13, 14, 16, 17,
  148. 19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
  149. 50, 55, 60, 66, 73, 80, 88, 97, 107, 118,
  150. 130, 143, 157, 173, 190, 209, 230, 253, 279, 307,
  151. 337, 371, 408, 449, 494, 544, 598, 658, 724, 796,
  152. 876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066,
  153. 2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358,
  154. 5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
  155. 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
  156. };
  157. static const int8_t _ima_adpcm_index_table[16] = {
  158. -1, -1, -1, -1, 2, 4, 6, 8,
  159. -1, -1, -1, -1, 2, 4, 6, 8
  160. };
  161. int datalen = p_data.size();
  162. int datamax = datalen;
  163. if (datalen & 1) {
  164. datalen++;
  165. }
  166. r_dst_data.resize(datalen / 2 + 4);
  167. uint8_t *w = r_dst_data.ptrw();
  168. int i, step_idx = 0, prev = 0;
  169. uint8_t *out = w;
  170. const float *in = p_data.ptr();
  171. // Initial value is zero.
  172. *(out++) = 0;
  173. *(out++) = 0;
  174. // Table index initial value.
  175. *(out++) = 0;
  176. // Unused.
  177. *(out++) = 0;
  178. for (i = 0; i < datalen; i++) {
  179. int step, diff, vpdiff, mask;
  180. uint8_t nibble;
  181. int16_t xm_sample;
  182. if (i >= datamax) {
  183. xm_sample = 0;
  184. } else {
  185. xm_sample = CLAMP(in[i] * 32767.0, -32768, 32767);
  186. }
  187. diff = (int)xm_sample - prev;
  188. nibble = 0;
  189. step = _ima_adpcm_step_table[step_idx];
  190. vpdiff = step >> 3;
  191. if (diff < 0) {
  192. nibble = 8;
  193. diff = -diff;
  194. }
  195. mask = 4;
  196. while (mask) {
  197. if (diff >= step) {
  198. nibble |= mask;
  199. diff -= step;
  200. vpdiff += step;
  201. }
  202. step >>= 1;
  203. mask >>= 1;
  204. }
  205. if (nibble & 8) {
  206. prev -= vpdiff;
  207. } else {
  208. prev += vpdiff;
  209. }
  210. prev = CLAMP(prev, -32768, 32767);
  211. step_idx += _ima_adpcm_index_table[nibble];
  212. step_idx = CLAMP(step_idx, 0, 88);
  213. if (i & 1) {
  214. *out |= nibble << 4;
  215. out++;
  216. } else {
  217. *out = nibble;
  218. }
  219. }
  220. }
  221. static void _compress_qoa(const Vector<float> &p_data, Vector<uint8_t> &dst_data, qoa_desc *p_desc) {
  222. uint32_t frames_len = (p_desc->samples + QOA_FRAME_LEN - 1) / QOA_FRAME_LEN * (QOA_LMS_LEN * 4 * p_desc->channels + 8);
  223. uint32_t slices_len = (p_desc->samples + QOA_SLICE_LEN - 1) / QOA_SLICE_LEN * 8 * p_desc->channels;
  224. dst_data.resize(8 + frames_len + slices_len);
  225. for (uint32_t c = 0; c < p_desc->channels; c++) {
  226. memset(p_desc->lms[c].history, 0, sizeof(p_desc->lms[c].history));
  227. memset(p_desc->lms[c].weights, 0, sizeof(p_desc->lms[c].weights));
  228. p_desc->lms[c].weights[2] = -(1 << 13);
  229. p_desc->lms[c].weights[3] = (1 << 14);
  230. }
  231. LocalVector<int16_t> data16;
  232. data16.resize(QOA_FRAME_LEN * p_desc->channels);
  233. uint8_t *dst_ptr = dst_data.ptrw();
  234. dst_ptr += qoa_encode_header(p_desc, dst_data.ptrw());
  235. uint32_t frame_len = QOA_FRAME_LEN;
  236. for (uint32_t s = 0; s < p_desc->samples; s += frame_len) {
  237. frame_len = MIN(frame_len, p_desc->samples - s);
  238. for (uint32_t i = 0; i < frame_len * p_desc->channels; i++) {
  239. data16[i] = CLAMP(p_data[s * p_desc->channels + i] * 32767.0, -32768, 32767);
  240. }
  241. dst_ptr += qoa_encode_frame(data16.ptr(), p_desc, frame_len, dst_ptr);
  242. }
  243. }
  244. AudioStreamWAV();
  245. ~AudioStreamWAV();
  246. };
  247. VARIANT_ENUM_CAST(AudioStreamWAV::Format)
  248. VARIANT_ENUM_CAST(AudioStreamWAV::LoopMode)
  249. #endif // AUDIO_STREAM_WAV_H