audio_stream_wav.h 9.1 KB

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