audio_stream_wav.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  1. /**************************************************************************/
  2. /* audio_stream_wav.cpp */
  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. #include "audio_stream_wav.h"
  31. #include "core/io/file_access.h"
  32. #include "core/io/marshalls.h"
  33. void AudioStreamPlaybackWAV::start(double p_from_pos) {
  34. if (base->format == AudioStreamWAV::FORMAT_IMA_ADPCM) {
  35. //no seeking in IMA_ADPCM
  36. for (int i = 0; i < 2; i++) {
  37. ima_adpcm[i].step_index = 0;
  38. ima_adpcm[i].predictor = 0;
  39. ima_adpcm[i].loop_step_index = 0;
  40. ima_adpcm[i].loop_predictor = 0;
  41. ima_adpcm[i].last_nibble = -1;
  42. ima_adpcm[i].loop_pos = 0x7FFFFFFF;
  43. ima_adpcm[i].window_ofs = 0;
  44. }
  45. offset = 0;
  46. } else {
  47. seek(p_from_pos);
  48. }
  49. sign = 1;
  50. active = true;
  51. }
  52. void AudioStreamPlaybackWAV::stop() {
  53. active = false;
  54. }
  55. bool AudioStreamPlaybackWAV::is_playing() const {
  56. return active;
  57. }
  58. int AudioStreamPlaybackWAV::get_loop_count() const {
  59. return 0;
  60. }
  61. double AudioStreamPlaybackWAV::get_playback_position() const {
  62. return float(offset >> MIX_FRAC_BITS) / base->mix_rate;
  63. }
  64. void AudioStreamPlaybackWAV::seek(double p_time) {
  65. if (base->format == AudioStreamWAV::FORMAT_IMA_ADPCM) {
  66. return; //no seeking in ima-adpcm
  67. }
  68. double max = base->get_length();
  69. if (p_time < 0) {
  70. p_time = 0;
  71. } else if (p_time >= max) {
  72. p_time = max - 0.001;
  73. }
  74. offset = uint64_t(p_time * base->mix_rate) << MIX_FRAC_BITS;
  75. }
  76. template <typename Depth, bool is_stereo, bool is_ima_adpcm, bool is_qoa>
  77. void AudioStreamPlaybackWAV::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) {
  78. // this function will be compiled branchless by any decent compiler
  79. int32_t final = 0, final_r = 0, next = 0, next_r = 0;
  80. while (p_amount) {
  81. p_amount--;
  82. int64_t pos = p_offset >> MIX_FRAC_BITS;
  83. if (is_stereo && !is_ima_adpcm && !is_qoa) {
  84. pos <<= 1;
  85. }
  86. if (is_ima_adpcm) {
  87. int64_t sample_pos = pos + p_ima_adpcm[0].window_ofs;
  88. while (sample_pos > p_ima_adpcm[0].last_nibble) {
  89. static const int16_t _ima_adpcm_step_table[89] = {
  90. 7, 8, 9, 10, 11, 12, 13, 14, 16, 17,
  91. 19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
  92. 50, 55, 60, 66, 73, 80, 88, 97, 107, 118,
  93. 130, 143, 157, 173, 190, 209, 230, 253, 279, 307,
  94. 337, 371, 408, 449, 494, 544, 598, 658, 724, 796,
  95. 876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066,
  96. 2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358,
  97. 5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
  98. 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
  99. };
  100. static const int8_t _ima_adpcm_index_table[16] = {
  101. -1, -1, -1, -1, 2, 4, 6, 8,
  102. -1, -1, -1, -1, 2, 4, 6, 8
  103. };
  104. for (int i = 0; i < (is_stereo ? 2 : 1); i++) {
  105. int16_t nibble, diff, step;
  106. p_ima_adpcm[i].last_nibble++;
  107. uint8_t nbb = p_src[(p_ima_adpcm[i].last_nibble >> 1) * (is_stereo ? 2 : 1) + i];
  108. nibble = (p_ima_adpcm[i].last_nibble & 1) ? (nbb >> 4) : (nbb & 0xF);
  109. step = _ima_adpcm_step_table[p_ima_adpcm[i].step_index];
  110. p_ima_adpcm[i].step_index += _ima_adpcm_index_table[nibble];
  111. if (p_ima_adpcm[i].step_index < 0) {
  112. p_ima_adpcm[i].step_index = 0;
  113. }
  114. if (p_ima_adpcm[i].step_index > 88) {
  115. p_ima_adpcm[i].step_index = 88;
  116. }
  117. diff = step >> 3;
  118. if (nibble & 1) {
  119. diff += step >> 2;
  120. }
  121. if (nibble & 2) {
  122. diff += step >> 1;
  123. }
  124. if (nibble & 4) {
  125. diff += step;
  126. }
  127. if (nibble & 8) {
  128. diff = -diff;
  129. }
  130. p_ima_adpcm[i].predictor += diff;
  131. if (p_ima_adpcm[i].predictor < -0x8000) {
  132. p_ima_adpcm[i].predictor = -0x8000;
  133. } else if (p_ima_adpcm[i].predictor > 0x7FFF) {
  134. p_ima_adpcm[i].predictor = 0x7FFF;
  135. }
  136. /* store loop if there */
  137. if (p_ima_adpcm[i].last_nibble == p_ima_adpcm[i].loop_pos) {
  138. p_ima_adpcm[i].loop_step_index = p_ima_adpcm[i].step_index;
  139. p_ima_adpcm[i].loop_predictor = p_ima_adpcm[i].predictor;
  140. }
  141. //printf("%i - %i - pred %i\n",int(p_ima_adpcm[i].last_nibble),int(nibble),int(p_ima_adpcm[i].predictor));
  142. }
  143. }
  144. final = p_ima_adpcm[0].predictor;
  145. if (is_stereo) {
  146. final_r = p_ima_adpcm[1].predictor;
  147. }
  148. } else {
  149. if (is_qoa) {
  150. if (pos != p_qoa->cache_pos) { // Prevents triple decoding on lower mix rates.
  151. for (int i = 0; i < 2; i++) {
  152. // Sign operations prevent triple decoding on backward loops, maxing prevents pop.
  153. uint32_t interp_pos = MIN(pos + (i * sign) + (sign < 0), p_qoa->desc.samples - 1);
  154. uint32_t new_data_ofs = 8 + interp_pos / QOA_FRAME_LEN * p_qoa->frame_len;
  155. if (p_qoa->data_ofs != new_data_ofs) {
  156. p_qoa->data_ofs = new_data_ofs;
  157. const uint8_t *ofs_src = (uint8_t *)p_src + p_qoa->data_ofs;
  158. qoa_decode_frame(ofs_src, p_qoa->frame_len, &p_qoa->desc, p_qoa->dec.ptr(), &p_qoa->dec_len);
  159. }
  160. uint32_t dec_idx = (interp_pos % QOA_FRAME_LEN) * p_qoa->desc.channels;
  161. if ((sign > 0 && i == 0) || (sign < 0 && i == 1)) {
  162. final = p_qoa->dec[dec_idx];
  163. p_qoa->cache[0] = final;
  164. if (is_stereo) {
  165. final_r = p_qoa->dec[dec_idx + 1];
  166. p_qoa->cache_r[0] = final_r;
  167. }
  168. } else {
  169. next = p_qoa->dec[dec_idx];
  170. p_qoa->cache[1] = next;
  171. if (is_stereo) {
  172. next_r = p_qoa->dec[dec_idx + 1];
  173. p_qoa->cache_r[1] = next_r;
  174. }
  175. }
  176. }
  177. p_qoa->cache_pos = pos;
  178. } else {
  179. final = p_qoa->cache[0];
  180. if (is_stereo) {
  181. final_r = p_qoa->cache_r[0];
  182. }
  183. next = p_qoa->cache[1];
  184. if (is_stereo) {
  185. next_r = p_qoa->cache_r[1];
  186. }
  187. }
  188. } else {
  189. final = p_src[pos];
  190. if (is_stereo) {
  191. final_r = p_src[pos + 1];
  192. }
  193. if constexpr (sizeof(Depth) == 1) { /* conditions will not exist anymore when compiled! */
  194. final <<= 8;
  195. if (is_stereo) {
  196. final_r <<= 8;
  197. }
  198. }
  199. if (is_stereo) {
  200. next = p_src[pos + 2];
  201. next_r = p_src[pos + 3];
  202. } else {
  203. next = p_src[pos + 1];
  204. }
  205. if constexpr (sizeof(Depth) == 1) {
  206. next <<= 8;
  207. if (is_stereo) {
  208. next_r <<= 8;
  209. }
  210. }
  211. }
  212. int32_t frac = int64_t(p_offset & MIX_FRAC_MASK);
  213. final = final + ((next - final) * frac >> MIX_FRAC_BITS);
  214. if (is_stereo) {
  215. final_r = final_r + ((next_r - final_r) * frac >> MIX_FRAC_BITS);
  216. }
  217. }
  218. if (!is_stereo) {
  219. final_r = final; //copy to right channel if stereo
  220. }
  221. p_dst->left = final / 32767.0;
  222. p_dst->right = final_r / 32767.0;
  223. p_dst++;
  224. p_offset += p_increment;
  225. }
  226. }
  227. int AudioStreamPlaybackWAV::mix(AudioFrame *p_buffer, float p_rate_scale, int p_frames) {
  228. if (base->data.is_empty() || !active) {
  229. for (int i = 0; i < p_frames; i++) {
  230. p_buffer[i] = AudioFrame(0, 0);
  231. }
  232. return 0;
  233. }
  234. int len = base->data_bytes;
  235. switch (base->format) {
  236. case AudioStreamWAV::FORMAT_8_BITS:
  237. len /= 1;
  238. break;
  239. case AudioStreamWAV::FORMAT_16_BITS:
  240. len /= 2;
  241. break;
  242. case AudioStreamWAV::FORMAT_IMA_ADPCM:
  243. len *= 2;
  244. break;
  245. case AudioStreamWAV::FORMAT_QOA:
  246. len = qoa.desc.samples * qoa.desc.channels;
  247. break;
  248. }
  249. if (base->stereo) {
  250. len /= 2;
  251. }
  252. /* some 64-bit fixed point precaches */
  253. int64_t loop_begin_fp = ((int64_t)base->loop_begin << MIX_FRAC_BITS);
  254. int64_t loop_end_fp = ((int64_t)base->loop_end << MIX_FRAC_BITS);
  255. int64_t length_fp = ((int64_t)len << MIX_FRAC_BITS);
  256. int64_t begin_limit = (base->loop_mode != AudioStreamWAV::LOOP_DISABLED) ? loop_begin_fp : 0;
  257. int64_t end_limit = (base->loop_mode != AudioStreamWAV::LOOP_DISABLED) ? loop_end_fp : length_fp - MIX_FRAC_LEN;
  258. bool is_stereo = base->stereo;
  259. int32_t todo = p_frames;
  260. if (base->loop_mode == AudioStreamWAV::LOOP_BACKWARD) {
  261. sign = -1;
  262. }
  263. float base_rate = AudioServer::get_singleton()->get_mix_rate();
  264. float srate = base->mix_rate;
  265. srate *= p_rate_scale;
  266. float playback_speed_scale = AudioServer::get_singleton()->get_playback_speed_scale();
  267. float fincrement = (srate * playback_speed_scale) / base_rate;
  268. int32_t increment = int32_t(MAX(fincrement * MIX_FRAC_LEN, 1));
  269. increment *= sign;
  270. //looping
  271. AudioStreamWAV::LoopMode loop_format = base->loop_mode;
  272. AudioStreamWAV::Format format = base->format;
  273. /* audio data */
  274. const uint8_t *data = base->data.ptr() + AudioStreamWAV::DATA_PAD;
  275. AudioFrame *dst_buff = p_buffer;
  276. if (format == AudioStreamWAV::FORMAT_IMA_ADPCM) {
  277. if (loop_format != AudioStreamWAV::LOOP_DISABLED) {
  278. ima_adpcm[0].loop_pos = loop_begin_fp >> MIX_FRAC_BITS;
  279. ima_adpcm[1].loop_pos = loop_begin_fp >> MIX_FRAC_BITS;
  280. loop_format = AudioStreamWAV::LOOP_FORWARD;
  281. }
  282. }
  283. while (todo > 0) {
  284. int64_t limit = 0;
  285. int32_t target = 0, aux = 0;
  286. /** LOOP CHECKING **/
  287. if (increment < 0) {
  288. /* going backwards */
  289. if (loop_format != AudioStreamWAV::LOOP_DISABLED && offset < loop_begin_fp) {
  290. /* loopstart reached */
  291. if (loop_format == AudioStreamWAV::LOOP_PINGPONG) {
  292. /* bounce ping pong */
  293. offset = loop_begin_fp + (loop_begin_fp - offset);
  294. increment = -increment;
  295. sign *= -1;
  296. } else {
  297. /* go to loop-end */
  298. offset = loop_end_fp - (loop_begin_fp - offset);
  299. }
  300. } else {
  301. /* check for sample not reaching beginning */
  302. if (offset < 0) {
  303. active = false;
  304. break;
  305. }
  306. }
  307. } else {
  308. /* going forward */
  309. if (loop_format != AudioStreamWAV::LOOP_DISABLED && offset >= loop_end_fp) {
  310. /* loopend reached */
  311. if (loop_format == AudioStreamWAV::LOOP_PINGPONG) {
  312. /* bounce ping pong */
  313. offset = loop_end_fp - (offset - loop_end_fp);
  314. increment = -increment;
  315. sign *= -1;
  316. } else {
  317. /* go to loop-begin */
  318. if (format == AudioStreamWAV::FORMAT_IMA_ADPCM) {
  319. for (int i = 0; i < 2; i++) {
  320. ima_adpcm[i].step_index = ima_adpcm[i].loop_step_index;
  321. ima_adpcm[i].predictor = ima_adpcm[i].loop_predictor;
  322. ima_adpcm[i].last_nibble = loop_begin_fp >> MIX_FRAC_BITS;
  323. }
  324. offset = loop_begin_fp;
  325. } else {
  326. offset = loop_begin_fp + (offset - loop_end_fp);
  327. }
  328. }
  329. } else {
  330. /* no loop, check for end of sample */
  331. if (offset >= length_fp) {
  332. active = false;
  333. break;
  334. }
  335. }
  336. }
  337. /** MIXCOUNT COMPUTING **/
  338. /* next possible limit (looppoints or sample begin/end */
  339. limit = (increment < 0) ? begin_limit : end_limit;
  340. /* compute what is shorter, the todo or the limit? */
  341. aux = (limit - offset) / increment + 1;
  342. target = (aux < todo) ? aux : todo; /* mix target is the shorter buffer */
  343. /* check just in case */
  344. if (target <= 0) {
  345. active = false;
  346. break;
  347. }
  348. todo -= target;
  349. switch (base->format) {
  350. case AudioStreamWAV::FORMAT_8_BITS: {
  351. if (is_stereo) {
  352. do_resample<int8_t, true, false, false>((int8_t *)data, dst_buff, offset, increment, target, ima_adpcm, &qoa);
  353. } else {
  354. do_resample<int8_t, false, false, false>((int8_t *)data, dst_buff, offset, increment, target, ima_adpcm, &qoa);
  355. }
  356. } break;
  357. case AudioStreamWAV::FORMAT_16_BITS: {
  358. if (is_stereo) {
  359. do_resample<int16_t, true, false, false>((int16_t *)data, dst_buff, offset, increment, target, ima_adpcm, &qoa);
  360. } else {
  361. do_resample<int16_t, false, false, false>((int16_t *)data, dst_buff, offset, increment, target, ima_adpcm, &qoa);
  362. }
  363. } break;
  364. case AudioStreamWAV::FORMAT_IMA_ADPCM: {
  365. if (is_stereo) {
  366. do_resample<int8_t, true, true, false>((int8_t *)data, dst_buff, offset, increment, target, ima_adpcm, &qoa);
  367. } else {
  368. do_resample<int8_t, false, true, false>((int8_t *)data, dst_buff, offset, increment, target, ima_adpcm, &qoa);
  369. }
  370. } break;
  371. case AudioStreamWAV::FORMAT_QOA: {
  372. if (is_stereo) {
  373. do_resample<uint8_t, true, false, true>((uint8_t *)data, dst_buff, offset, increment, target, ima_adpcm, &qoa);
  374. } else {
  375. do_resample<uint8_t, false, false, true>((uint8_t *)data, dst_buff, offset, increment, target, ima_adpcm, &qoa);
  376. }
  377. } break;
  378. }
  379. dst_buff += target;
  380. }
  381. if (todo) {
  382. int mixed_frames = p_frames - todo;
  383. //bit was missing from mix
  384. int todo_ofs = p_frames - todo;
  385. for (int i = todo_ofs; i < p_frames; i++) {
  386. p_buffer[i] = AudioFrame(0, 0);
  387. }
  388. return mixed_frames;
  389. }
  390. return p_frames;
  391. }
  392. void AudioStreamPlaybackWAV::tag_used_streams() {
  393. base->tag_used(get_playback_position());
  394. }
  395. void AudioStreamPlaybackWAV::set_is_sample(bool p_is_sample) {
  396. _is_sample = p_is_sample;
  397. }
  398. bool AudioStreamPlaybackWAV::get_is_sample() const {
  399. return _is_sample;
  400. }
  401. Ref<AudioSamplePlayback> AudioStreamPlaybackWAV::get_sample_playback() const {
  402. return sample_playback;
  403. }
  404. void AudioStreamPlaybackWAV::set_sample_playback(const Ref<AudioSamplePlayback> &p_playback) {
  405. sample_playback = p_playback;
  406. if (sample_playback.is_valid()) {
  407. sample_playback->stream_playback = Ref<AudioStreamPlayback>(this);
  408. }
  409. }
  410. AudioStreamPlaybackWAV::AudioStreamPlaybackWAV() {}
  411. AudioStreamPlaybackWAV::~AudioStreamPlaybackWAV() {}
  412. /////////////////////
  413. void AudioStreamWAV::set_format(Format p_format) {
  414. format = p_format;
  415. }
  416. AudioStreamWAV::Format AudioStreamWAV::get_format() const {
  417. return format;
  418. }
  419. void AudioStreamWAV::set_loop_mode(LoopMode p_loop_mode) {
  420. loop_mode = p_loop_mode;
  421. }
  422. AudioStreamWAV::LoopMode AudioStreamWAV::get_loop_mode() const {
  423. return loop_mode;
  424. }
  425. void AudioStreamWAV::set_loop_begin(int p_frame) {
  426. loop_begin = p_frame;
  427. }
  428. int AudioStreamWAV::get_loop_begin() const {
  429. return loop_begin;
  430. }
  431. void AudioStreamWAV::set_loop_end(int p_frame) {
  432. loop_end = p_frame;
  433. }
  434. int AudioStreamWAV::get_loop_end() const {
  435. return loop_end;
  436. }
  437. void AudioStreamWAV::set_mix_rate(int p_hz) {
  438. ERR_FAIL_COND(p_hz == 0);
  439. mix_rate = p_hz;
  440. }
  441. int AudioStreamWAV::get_mix_rate() const {
  442. return mix_rate;
  443. }
  444. void AudioStreamWAV::set_stereo(bool p_enable) {
  445. stereo = p_enable;
  446. }
  447. bool AudioStreamWAV::is_stereo() const {
  448. return stereo;
  449. }
  450. double AudioStreamWAV::get_length() const {
  451. int len = data_bytes;
  452. switch (format) {
  453. case AudioStreamWAV::FORMAT_8_BITS:
  454. len /= 1;
  455. break;
  456. case AudioStreamWAV::FORMAT_16_BITS:
  457. len /= 2;
  458. break;
  459. case AudioStreamWAV::FORMAT_IMA_ADPCM:
  460. len *= 2;
  461. break;
  462. case AudioStreamWAV::FORMAT_QOA:
  463. qoa_desc desc = {};
  464. qoa_decode_header(data.ptr() + DATA_PAD, data_bytes, &desc);
  465. len = desc.samples * desc.channels;
  466. break;
  467. }
  468. if (stereo) {
  469. len /= 2;
  470. }
  471. return double(len) / mix_rate;
  472. }
  473. bool AudioStreamWAV::is_monophonic() const {
  474. return false;
  475. }
  476. void AudioStreamWAV::set_data(const Vector<uint8_t> &p_data) {
  477. AudioServer::get_singleton()->lock();
  478. int src_data_len = p_data.size();
  479. data.clear();
  480. int alloc_len = src_data_len + DATA_PAD * 2;
  481. data.resize(alloc_len);
  482. memset(data.ptr(), 0, alloc_len);
  483. memcpy(data.ptr() + DATA_PAD, p_data.ptr(), src_data_len);
  484. data_bytes = src_data_len;
  485. AudioServer::get_singleton()->unlock();
  486. }
  487. Vector<uint8_t> AudioStreamWAV::get_data() const {
  488. Vector<uint8_t> pv;
  489. if (!data.is_empty()) {
  490. pv.resize(data_bytes);
  491. memcpy(pv.ptrw(), data.ptr() + DATA_PAD, data_bytes);
  492. }
  493. return pv;
  494. }
  495. Error AudioStreamWAV::save_to_wav(const String &p_path) {
  496. if (format == AudioStreamWAV::FORMAT_IMA_ADPCM || format == AudioStreamWAV::FORMAT_QOA) {
  497. WARN_PRINT("Saving IMA_ADPCM and QOA samples is not supported yet");
  498. return ERR_UNAVAILABLE;
  499. }
  500. int sub_chunk_2_size = data_bytes; //Subchunk2Size = Size of data in bytes
  501. // Format code
  502. // 1:PCM format (for 8 or 16 bit)
  503. // 3:IEEE float format
  504. int format_code = (format == FORMAT_IMA_ADPCM) ? 3 : 1;
  505. int n_channels = stereo ? 2 : 1;
  506. long sample_rate = mix_rate;
  507. int byte_pr_sample = 0;
  508. switch (format) {
  509. case AudioStreamWAV::FORMAT_8_BITS:
  510. byte_pr_sample = 1;
  511. break;
  512. case AudioStreamWAV::FORMAT_16_BITS:
  513. case AudioStreamWAV::FORMAT_QOA:
  514. byte_pr_sample = 2;
  515. break;
  516. case AudioStreamWAV::FORMAT_IMA_ADPCM:
  517. byte_pr_sample = 4;
  518. break;
  519. }
  520. String file_path = p_path;
  521. if (file_path.substr(file_path.length() - 4, 4).to_lower() != ".wav") {
  522. file_path += ".wav";
  523. }
  524. Ref<FileAccess> file = FileAccess::open(file_path, FileAccess::WRITE); //Overrides existing file if present
  525. ERR_FAIL_COND_V(file.is_null(), ERR_FILE_CANT_WRITE);
  526. // Create WAV Header
  527. file->store_string("RIFF"); //ChunkID
  528. file->store_32(sub_chunk_2_size + 36); //ChunkSize = 36 + SubChunk2Size (size of entire file minus the 8 bits for this and previous header)
  529. file->store_string("WAVE"); //Format
  530. file->store_string("fmt "); //Subchunk1ID
  531. file->store_32(16); //Subchunk1Size = 16
  532. file->store_16(format_code); //AudioFormat
  533. file->store_16(n_channels); //Number of Channels
  534. file->store_32(sample_rate); //SampleRate
  535. file->store_32(sample_rate * n_channels * byte_pr_sample); //ByteRate
  536. file->store_16(n_channels * byte_pr_sample); //BlockAlign = NumChannels * BytePrSample
  537. file->store_16(byte_pr_sample * 8); //BitsPerSample
  538. file->store_string("data"); //Subchunk2ID
  539. file->store_32(sub_chunk_2_size); //Subchunk2Size
  540. // Add data
  541. Vector<uint8_t> stream_data = get_data();
  542. const uint8_t *read_data = stream_data.ptr();
  543. switch (format) {
  544. case AudioStreamWAV::FORMAT_8_BITS:
  545. for (unsigned int i = 0; i < data_bytes; i++) {
  546. uint8_t data_point = (read_data[i] + 128);
  547. file->store_8(data_point);
  548. }
  549. break;
  550. case AudioStreamWAV::FORMAT_16_BITS:
  551. case AudioStreamWAV::FORMAT_QOA:
  552. for (unsigned int i = 0; i < data_bytes / 2; i++) {
  553. uint16_t data_point = decode_uint16(&read_data[i * 2]);
  554. file->store_16(data_point);
  555. }
  556. break;
  557. case AudioStreamWAV::FORMAT_IMA_ADPCM:
  558. //Unimplemented
  559. break;
  560. }
  561. return OK;
  562. }
  563. Ref<AudioStreamPlayback> AudioStreamWAV::instantiate_playback() {
  564. Ref<AudioStreamPlaybackWAV> sample;
  565. sample.instantiate();
  566. sample->base = Ref<AudioStreamWAV>(this);
  567. if (format == AudioStreamWAV::FORMAT_QOA) {
  568. uint32_t ffp = qoa_decode_header(data.ptr() + DATA_PAD, data_bytes, &sample->qoa.desc);
  569. ERR_FAIL_COND_V(ffp != 8, Ref<AudioStreamPlaybackWAV>());
  570. sample->qoa.frame_len = qoa_max_frame_size(&sample->qoa.desc);
  571. int samples_len = (sample->qoa.desc.samples > QOA_FRAME_LEN ? QOA_FRAME_LEN : sample->qoa.desc.samples);
  572. int dec_len = sample->qoa.desc.channels * samples_len;
  573. sample->qoa.dec.resize(dec_len);
  574. }
  575. return sample;
  576. }
  577. String AudioStreamWAV::get_stream_name() const {
  578. return "";
  579. }
  580. Ref<AudioSample> AudioStreamWAV::generate_sample() const {
  581. Ref<AudioSample> sample;
  582. sample.instantiate();
  583. sample->stream = this;
  584. switch (loop_mode) {
  585. case AudioStreamWAV::LoopMode::LOOP_DISABLED: {
  586. sample->loop_mode = AudioSample::LoopMode::LOOP_DISABLED;
  587. } break;
  588. case AudioStreamWAV::LoopMode::LOOP_FORWARD: {
  589. sample->loop_mode = AudioSample::LoopMode::LOOP_FORWARD;
  590. } break;
  591. case AudioStreamWAV::LoopMode::LOOP_PINGPONG: {
  592. sample->loop_mode = AudioSample::LoopMode::LOOP_PINGPONG;
  593. } break;
  594. case AudioStreamWAV::LoopMode::LOOP_BACKWARD: {
  595. sample->loop_mode = AudioSample::LoopMode::LOOP_BACKWARD;
  596. } break;
  597. }
  598. sample->loop_begin = loop_begin;
  599. sample->loop_end = loop_end;
  600. sample->sample_rate = mix_rate;
  601. return sample;
  602. }
  603. void AudioStreamWAV::_bind_methods() {
  604. ClassDB::bind_method(D_METHOD("set_data", "data"), &AudioStreamWAV::set_data);
  605. ClassDB::bind_method(D_METHOD("get_data"), &AudioStreamWAV::get_data);
  606. ClassDB::bind_method(D_METHOD("set_format", "format"), &AudioStreamWAV::set_format);
  607. ClassDB::bind_method(D_METHOD("get_format"), &AudioStreamWAV::get_format);
  608. ClassDB::bind_method(D_METHOD("set_loop_mode", "loop_mode"), &AudioStreamWAV::set_loop_mode);
  609. ClassDB::bind_method(D_METHOD("get_loop_mode"), &AudioStreamWAV::get_loop_mode);
  610. ClassDB::bind_method(D_METHOD("set_loop_begin", "loop_begin"), &AudioStreamWAV::set_loop_begin);
  611. ClassDB::bind_method(D_METHOD("get_loop_begin"), &AudioStreamWAV::get_loop_begin);
  612. ClassDB::bind_method(D_METHOD("set_loop_end", "loop_end"), &AudioStreamWAV::set_loop_end);
  613. ClassDB::bind_method(D_METHOD("get_loop_end"), &AudioStreamWAV::get_loop_end);
  614. ClassDB::bind_method(D_METHOD("set_mix_rate", "mix_rate"), &AudioStreamWAV::set_mix_rate);
  615. ClassDB::bind_method(D_METHOD("get_mix_rate"), &AudioStreamWAV::get_mix_rate);
  616. ClassDB::bind_method(D_METHOD("set_stereo", "stereo"), &AudioStreamWAV::set_stereo);
  617. ClassDB::bind_method(D_METHOD("is_stereo"), &AudioStreamWAV::is_stereo);
  618. ClassDB::bind_method(D_METHOD("save_to_wav", "path"), &AudioStreamWAV::save_to_wav);
  619. ADD_PROPERTY(PropertyInfo(Variant::PACKED_BYTE_ARRAY, "data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_data", "get_data");
  620. ADD_PROPERTY(PropertyInfo(Variant::INT, "format", PROPERTY_HINT_ENUM, "8-Bit,16-Bit,IMA ADPCM,Quite OK Audio"), "set_format", "get_format");
  621. ADD_PROPERTY(PropertyInfo(Variant::INT, "loop_mode", PROPERTY_HINT_ENUM, "Disabled,Forward,Ping-Pong,Backward"), "set_loop_mode", "get_loop_mode");
  622. ADD_PROPERTY(PropertyInfo(Variant::INT, "loop_begin"), "set_loop_begin", "get_loop_begin");
  623. ADD_PROPERTY(PropertyInfo(Variant::INT, "loop_end"), "set_loop_end", "get_loop_end");
  624. ADD_PROPERTY(PropertyInfo(Variant::INT, "mix_rate"), "set_mix_rate", "get_mix_rate");
  625. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "stereo"), "set_stereo", "is_stereo");
  626. BIND_ENUM_CONSTANT(FORMAT_8_BITS);
  627. BIND_ENUM_CONSTANT(FORMAT_16_BITS);
  628. BIND_ENUM_CONSTANT(FORMAT_IMA_ADPCM);
  629. BIND_ENUM_CONSTANT(FORMAT_QOA);
  630. BIND_ENUM_CONSTANT(LOOP_DISABLED);
  631. BIND_ENUM_CONSTANT(LOOP_FORWARD);
  632. BIND_ENUM_CONSTANT(LOOP_PINGPONG);
  633. BIND_ENUM_CONSTANT(LOOP_BACKWARD);
  634. }
  635. AudioStreamWAV::AudioStreamWAV() {}
  636. AudioStreamWAV::~AudioStreamWAV() {}