audio_stream_wav.cpp 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242
  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_memory.h"
  32. #include "core/io/marshalls.h"
  33. const float TRIM_DB_LIMIT = -50;
  34. const int TRIM_FADE_OUT_FRAMES = 500;
  35. void AudioStreamPlaybackWAV::start(double p_from_pos) {
  36. if (base->format == AudioStreamWAV::FORMAT_IMA_ADPCM) {
  37. //no seeking in IMA_ADPCM
  38. for (int i = 0; i < 2; i++) {
  39. ima_adpcm[i].step_index = 0;
  40. ima_adpcm[i].predictor = 0;
  41. ima_adpcm[i].loop_step_index = 0;
  42. ima_adpcm[i].loop_predictor = 0;
  43. ima_adpcm[i].last_nibble = -1;
  44. ima_adpcm[i].loop_pos = 0x7FFFFFFF;
  45. ima_adpcm[i].window_ofs = 0;
  46. }
  47. offset = 0;
  48. } else {
  49. seek(p_from_pos);
  50. }
  51. sign = 1;
  52. active = true;
  53. begin_resample();
  54. }
  55. void AudioStreamPlaybackWAV::stop() {
  56. active = false;
  57. }
  58. bool AudioStreamPlaybackWAV::is_playing() const {
  59. return active;
  60. }
  61. int AudioStreamPlaybackWAV::get_loop_count() const {
  62. return 0;
  63. }
  64. double AudioStreamPlaybackWAV::get_playback_position() const {
  65. return double(offset) / base->mix_rate;
  66. }
  67. void AudioStreamPlaybackWAV::seek(double p_time) {
  68. if (base->format == AudioStreamWAV::FORMAT_IMA_ADPCM) {
  69. return; //no seeking in ima-adpcm
  70. }
  71. double max = base->get_length();
  72. if (p_time < 0) {
  73. p_time = 0;
  74. } else if (p_time >= max) {
  75. p_time = max - 0.001;
  76. }
  77. offset = int64_t(p_time * base->mix_rate);
  78. }
  79. template <typename Depth, bool is_stereo, bool is_ima_adpcm, bool is_qoa>
  80. void AudioStreamPlaybackWAV::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) {
  81. // this function will be compiled branchless by any decent compiler
  82. int32_t final = 0, final_r = 0;
  83. while (p_amount) {
  84. p_amount--;
  85. int64_t pos = p_offset << (is_stereo && !is_ima_adpcm && !is_qoa ? 1 : 0);
  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 if (is_qoa) {
  149. uint32_t new_data_ofs = 8 + pos / QOA_FRAME_LEN * p_qoa->frame_len;
  150. if (p_qoa->data_ofs != new_data_ofs) {
  151. p_qoa->data_ofs = new_data_ofs;
  152. const uint8_t *ofs_src = (uint8_t *)p_src + p_qoa->data_ofs;
  153. qoa_decode_frame(ofs_src, p_qoa->frame_len, &p_qoa->desc, p_qoa->dec.ptr(), &p_qoa->dec_len);
  154. }
  155. uint32_t dec_idx = pos % QOA_FRAME_LEN << (is_stereo ? 1 : 0);
  156. final = p_qoa->dec[dec_idx];
  157. if (is_stereo) {
  158. final_r = p_qoa->dec[dec_idx + 1];
  159. }
  160. } else {
  161. final = p_src[pos];
  162. if (is_stereo) {
  163. final_r = p_src[pos + 1];
  164. }
  165. if constexpr (sizeof(Depth) == 1) { /* conditions will not exist anymore when compiled! */
  166. final <<= 8;
  167. if (is_stereo) {
  168. final_r <<= 8;
  169. }
  170. }
  171. }
  172. if (!is_stereo) {
  173. final_r = final; //copy to right channel if stereo
  174. }
  175. p_dst->left = final / 32767.0;
  176. p_dst->right = final_r / 32767.0;
  177. p_dst++;
  178. p_offset += p_increment;
  179. }
  180. }
  181. int AudioStreamPlaybackWAV::_mix_internal(AudioFrame *p_buffer, int p_frames) {
  182. if (base->data.is_empty() || !active) {
  183. for (int i = 0; i < p_frames; i++) {
  184. p_buffer[i] = AudioFrame(0, 0);
  185. }
  186. return 0;
  187. }
  188. uint32_t len = base->data_bytes;
  189. switch (base->format) {
  190. case AudioStreamWAV::FORMAT_8_BITS:
  191. len /= 1;
  192. break;
  193. case AudioStreamWAV::FORMAT_16_BITS:
  194. len /= 2;
  195. break;
  196. case AudioStreamWAV::FORMAT_IMA_ADPCM:
  197. len *= 2;
  198. break;
  199. case AudioStreamWAV::FORMAT_QOA:
  200. len = qoa.desc.samples * qoa.desc.channels;
  201. break;
  202. }
  203. if (base->stereo) {
  204. len /= 2;
  205. }
  206. int64_t loop_begin = base->loop_begin;
  207. int64_t loop_end = base->loop_end;
  208. int64_t begin_limit = (base->loop_mode != AudioStreamWAV::LOOP_DISABLED) ? loop_begin : 0;
  209. int64_t end_limit = (base->loop_mode != AudioStreamWAV::LOOP_DISABLED) ? loop_end : len - 1;
  210. bool is_stereo = base->stereo;
  211. int32_t todo = p_frames;
  212. if (base->loop_mode == AudioStreamWAV::LOOP_BACKWARD) {
  213. sign = -1;
  214. }
  215. int8_t increment = sign;
  216. //looping
  217. AudioStreamWAV::LoopMode loop_format = base->loop_mode;
  218. AudioStreamWAV::Format format = base->format;
  219. /* audio data */
  220. const uint8_t *data = base->data.ptr();
  221. AudioFrame *dst_buff = p_buffer;
  222. if (format == AudioStreamWAV::FORMAT_IMA_ADPCM) {
  223. if (loop_format != AudioStreamWAV::LOOP_DISABLED) {
  224. ima_adpcm[0].loop_pos = loop_begin;
  225. ima_adpcm[1].loop_pos = loop_begin;
  226. loop_format = AudioStreamWAV::LOOP_FORWARD;
  227. }
  228. }
  229. while (todo > 0) {
  230. int64_t limit = 0;
  231. int32_t target = 0, aux = 0;
  232. /** LOOP CHECKING **/
  233. if (increment < 0) {
  234. /* going backwards */
  235. if (loop_format != AudioStreamWAV::LOOP_DISABLED && offset < loop_begin) {
  236. /* loopstart reached */
  237. if (loop_format == AudioStreamWAV::LOOP_PINGPONG) {
  238. /* bounce ping pong */
  239. offset = loop_begin + (loop_begin - offset);
  240. increment = -increment;
  241. sign *= -1;
  242. } else {
  243. /* go to loop-end */
  244. offset = loop_end - (loop_begin - offset);
  245. }
  246. } else {
  247. /* check for sample not reaching beginning */
  248. if (offset < 0) {
  249. active = false;
  250. break;
  251. }
  252. }
  253. } else {
  254. /* going forward */
  255. if (loop_format != AudioStreamWAV::LOOP_DISABLED && offset >= loop_end) {
  256. /* loopend reached */
  257. if (loop_format == AudioStreamWAV::LOOP_PINGPONG) {
  258. /* bounce ping pong */
  259. offset = loop_end - (offset - loop_end);
  260. increment = -increment;
  261. sign *= -1;
  262. } else {
  263. /* go to loop-begin */
  264. if (format == AudioStreamWAV::FORMAT_IMA_ADPCM) {
  265. for (int i = 0; i < 2; i++) {
  266. ima_adpcm[i].step_index = ima_adpcm[i].loop_step_index;
  267. ima_adpcm[i].predictor = ima_adpcm[i].loop_predictor;
  268. ima_adpcm[i].last_nibble = loop_begin;
  269. }
  270. offset = loop_begin;
  271. } else {
  272. offset = loop_begin + (offset - loop_end);
  273. }
  274. }
  275. } else {
  276. /* no loop, check for end of sample */
  277. if (offset >= len) {
  278. active = false;
  279. break;
  280. }
  281. }
  282. }
  283. /** MIXCOUNT COMPUTING **/
  284. /* next possible limit (looppoints or sample begin/end */
  285. limit = (increment < 0) ? begin_limit : end_limit;
  286. /* compute what is shorter, the todo or the limit? */
  287. aux = (limit - offset) / increment + 1;
  288. target = (aux < todo) ? aux : todo; /* mix target is the shorter buffer */
  289. /* check just in case */
  290. if (target <= 0) {
  291. active = false;
  292. break;
  293. }
  294. todo -= target;
  295. switch (base->format) {
  296. case AudioStreamWAV::FORMAT_8_BITS: {
  297. if (is_stereo) {
  298. decode_samples<int8_t, true, false, false>((int8_t *)data, dst_buff, offset, increment, target, ima_adpcm, &qoa);
  299. } else {
  300. decode_samples<int8_t, false, false, false>((int8_t *)data, dst_buff, offset, increment, target, ima_adpcm, &qoa);
  301. }
  302. } break;
  303. case AudioStreamWAV::FORMAT_16_BITS: {
  304. if (is_stereo) {
  305. decode_samples<int16_t, true, false, false>((int16_t *)data, dst_buff, offset, increment, target, ima_adpcm, &qoa);
  306. } else {
  307. decode_samples<int16_t, false, false, false>((int16_t *)data, dst_buff, offset, increment, target, ima_adpcm, &qoa);
  308. }
  309. } break;
  310. case AudioStreamWAV::FORMAT_IMA_ADPCM: {
  311. if (is_stereo) {
  312. decode_samples<int8_t, true, true, false>((int8_t *)data, dst_buff, offset, increment, target, ima_adpcm, &qoa);
  313. } else {
  314. decode_samples<int8_t, false, true, false>((int8_t *)data, dst_buff, offset, increment, target, ima_adpcm, &qoa);
  315. }
  316. } break;
  317. case AudioStreamWAV::FORMAT_QOA: {
  318. if (is_stereo) {
  319. decode_samples<uint8_t, true, false, true>((uint8_t *)data, dst_buff, offset, increment, target, ima_adpcm, &qoa);
  320. } else {
  321. decode_samples<uint8_t, false, false, true>((uint8_t *)data, dst_buff, offset, increment, target, ima_adpcm, &qoa);
  322. }
  323. } break;
  324. }
  325. dst_buff += target;
  326. }
  327. if (todo) {
  328. int mixed_frames = p_frames - todo;
  329. //bit was missing from mix
  330. int todo_ofs = p_frames - todo;
  331. for (int i = todo_ofs; i < p_frames; i++) {
  332. p_buffer[i] = AudioFrame(0, 0);
  333. }
  334. return mixed_frames;
  335. }
  336. return p_frames;
  337. }
  338. float AudioStreamPlaybackWAV::get_stream_sampling_rate() {
  339. return base->mix_rate;
  340. }
  341. void AudioStreamPlaybackWAV::tag_used_streams() {
  342. base->tag_used(get_playback_position());
  343. }
  344. void AudioStreamPlaybackWAV::set_is_sample(bool p_is_sample) {
  345. _is_sample = p_is_sample;
  346. }
  347. bool AudioStreamPlaybackWAV::get_is_sample() const {
  348. return _is_sample;
  349. }
  350. Ref<AudioSamplePlayback> AudioStreamPlaybackWAV::get_sample_playback() const {
  351. return sample_playback;
  352. }
  353. void AudioStreamPlaybackWAV::set_sample_playback(const Ref<AudioSamplePlayback> &p_playback) {
  354. sample_playback = p_playback;
  355. if (sample_playback.is_valid()) {
  356. sample_playback->stream_playback = Ref<AudioStreamPlayback>(this);
  357. }
  358. }
  359. /////////////////////
  360. void AudioStreamWAV::set_format(Format p_format) {
  361. format = p_format;
  362. }
  363. AudioStreamWAV::Format AudioStreamWAV::get_format() const {
  364. return format;
  365. }
  366. void AudioStreamWAV::set_loop_mode(LoopMode p_loop_mode) {
  367. loop_mode = p_loop_mode;
  368. }
  369. AudioStreamWAV::LoopMode AudioStreamWAV::get_loop_mode() const {
  370. return loop_mode;
  371. }
  372. void AudioStreamWAV::set_loop_begin(int p_frame) {
  373. loop_begin = p_frame;
  374. }
  375. int AudioStreamWAV::get_loop_begin() const {
  376. return loop_begin;
  377. }
  378. void AudioStreamWAV::set_loop_end(int p_frame) {
  379. loop_end = p_frame;
  380. }
  381. int AudioStreamWAV::get_loop_end() const {
  382. return loop_end;
  383. }
  384. void AudioStreamWAV::set_mix_rate(int p_hz) {
  385. ERR_FAIL_COND(p_hz == 0);
  386. mix_rate = p_hz;
  387. }
  388. int AudioStreamWAV::get_mix_rate() const {
  389. return mix_rate;
  390. }
  391. void AudioStreamWAV::set_stereo(bool p_enable) {
  392. stereo = p_enable;
  393. }
  394. bool AudioStreamWAV::is_stereo() const {
  395. return stereo;
  396. }
  397. void AudioStreamWAV::set_tags(const Dictionary &p_tags) {
  398. tags = p_tags;
  399. }
  400. Dictionary AudioStreamWAV::get_tags() const {
  401. return tags;
  402. }
  403. double AudioStreamWAV::get_length() const {
  404. int len = data_bytes;
  405. switch (format) {
  406. case AudioStreamWAV::FORMAT_8_BITS:
  407. len /= 1;
  408. break;
  409. case AudioStreamWAV::FORMAT_16_BITS:
  410. len /= 2;
  411. break;
  412. case AudioStreamWAV::FORMAT_IMA_ADPCM:
  413. len *= 2;
  414. break;
  415. case AudioStreamWAV::FORMAT_QOA:
  416. qoa_desc desc = {};
  417. qoa_decode_header(data.ptr(), data_bytes, &desc);
  418. len = desc.samples * desc.channels;
  419. break;
  420. }
  421. if (stereo) {
  422. len /= 2;
  423. }
  424. return double(len) / mix_rate;
  425. }
  426. bool AudioStreamWAV::is_monophonic() const {
  427. return false;
  428. }
  429. void AudioStreamWAV::set_data(const Vector<uint8_t> &p_data) {
  430. AudioServer::get_singleton()->lock();
  431. data = p_data;
  432. data_bytes = p_data.size();
  433. AudioServer::get_singleton()->unlock();
  434. }
  435. Vector<uint8_t> AudioStreamWAV::get_data() const {
  436. return data;
  437. }
  438. Error AudioStreamWAV::save_to_wav(const String &p_path) {
  439. if (format == AudioStreamWAV::FORMAT_IMA_ADPCM || format == AudioStreamWAV::FORMAT_QOA) {
  440. WARN_PRINT("Saving IMA_ADPCM and QOA samples is not supported yet");
  441. return ERR_UNAVAILABLE;
  442. }
  443. int sub_chunk_2_size = data_bytes; //Subchunk2Size = Size of data in bytes
  444. // Format code
  445. // 1:PCM format (for 8 or 16 bit)
  446. // 3:IEEE float format
  447. int format_code = (format == FORMAT_IMA_ADPCM) ? 3 : 1;
  448. int n_channels = stereo ? 2 : 1;
  449. long sample_rate = mix_rate;
  450. int byte_pr_sample = 0;
  451. switch (format) {
  452. case AudioStreamWAV::FORMAT_8_BITS:
  453. byte_pr_sample = 1;
  454. break;
  455. case AudioStreamWAV::FORMAT_16_BITS:
  456. case AudioStreamWAV::FORMAT_QOA:
  457. byte_pr_sample = 2;
  458. break;
  459. case AudioStreamWAV::FORMAT_IMA_ADPCM:
  460. byte_pr_sample = 4;
  461. break;
  462. }
  463. String file_path = p_path;
  464. if (file_path.substr(file_path.length() - 4, 4).to_lower() != ".wav") {
  465. file_path += ".wav";
  466. }
  467. Ref<FileAccess> file = FileAccess::open(file_path, FileAccess::WRITE); //Overrides existing file if present
  468. ERR_FAIL_COND_V(file.is_null(), ERR_FILE_CANT_WRITE);
  469. // Create WAV Header
  470. file->store_string("RIFF"); //ChunkID
  471. file->store_32(sub_chunk_2_size + 36); //ChunkSize = 36 + SubChunk2Size (size of entire file minus the 8 bits for this and previous header)
  472. file->store_string("WAVE"); //Format
  473. file->store_string("fmt "); //Subchunk1ID
  474. file->store_32(16); //Subchunk1Size = 16
  475. file->store_16(format_code); //AudioFormat
  476. file->store_16(n_channels); //Number of Channels
  477. file->store_32(sample_rate); //SampleRate
  478. file->store_32(sample_rate * n_channels * byte_pr_sample); //ByteRate
  479. file->store_16(n_channels * byte_pr_sample); //BlockAlign = NumChannels * BytePrSample
  480. file->store_16(byte_pr_sample * 8); //BitsPerSample
  481. file->store_string("data"); //Subchunk2ID
  482. file->store_32(sub_chunk_2_size); //Subchunk2Size
  483. // Add data
  484. const uint8_t *read_data = data.ptr();
  485. switch (format) {
  486. case AudioStreamWAV::FORMAT_8_BITS:
  487. for (unsigned int i = 0; i < data_bytes; i++) {
  488. uint8_t data_point = (read_data[i] + 128);
  489. file->store_8(data_point);
  490. }
  491. break;
  492. case AudioStreamWAV::FORMAT_16_BITS:
  493. case AudioStreamWAV::FORMAT_QOA:
  494. for (unsigned int i = 0; i < data_bytes / 2; i++) {
  495. uint16_t data_point = decode_uint16(&read_data[i * 2]);
  496. file->store_16(data_point);
  497. }
  498. break;
  499. case AudioStreamWAV::FORMAT_IMA_ADPCM:
  500. //Unimplemented
  501. break;
  502. }
  503. return OK;
  504. }
  505. Ref<AudioStreamPlayback> AudioStreamWAV::instantiate_playback() {
  506. Ref<AudioStreamPlaybackWAV> sample;
  507. sample.instantiate();
  508. sample->base = Ref<AudioStreamWAV>(this);
  509. if (format == AudioStreamWAV::FORMAT_QOA) {
  510. uint32_t ffp = qoa_decode_header(data.ptr(), data_bytes, &sample->qoa.desc);
  511. ERR_FAIL_COND_V(ffp != 8, Ref<AudioStreamPlaybackWAV>());
  512. sample->qoa.frame_len = qoa_max_frame_size(&sample->qoa.desc);
  513. int samples_len = (sample->qoa.desc.samples > QOA_FRAME_LEN ? QOA_FRAME_LEN : sample->qoa.desc.samples);
  514. int dec_len = sample->qoa.desc.channels * samples_len;
  515. sample->qoa.dec.resize(dec_len);
  516. }
  517. return sample;
  518. }
  519. String AudioStreamWAV::get_stream_name() const {
  520. return "";
  521. }
  522. Ref<AudioSample> AudioStreamWAV::generate_sample() const {
  523. Ref<AudioSample> sample;
  524. sample.instantiate();
  525. sample->stream = this;
  526. switch (loop_mode) {
  527. case AudioStreamWAV::LoopMode::LOOP_DISABLED: {
  528. sample->loop_mode = AudioSample::LoopMode::LOOP_DISABLED;
  529. } break;
  530. case AudioStreamWAV::LoopMode::LOOP_FORWARD: {
  531. sample->loop_mode = AudioSample::LoopMode::LOOP_FORWARD;
  532. } break;
  533. case AudioStreamWAV::LoopMode::LOOP_PINGPONG: {
  534. sample->loop_mode = AudioSample::LoopMode::LOOP_PINGPONG;
  535. } break;
  536. case AudioStreamWAV::LoopMode::LOOP_BACKWARD: {
  537. sample->loop_mode = AudioSample::LoopMode::LOOP_BACKWARD;
  538. } break;
  539. }
  540. sample->loop_begin = loop_begin;
  541. sample->loop_end = loop_end;
  542. sample->sample_rate = mix_rate;
  543. return sample;
  544. }
  545. Ref<AudioStreamWAV> AudioStreamWAV::load_from_buffer(const Vector<uint8_t> &p_stream_data, const Dictionary &p_options) {
  546. // /* STEP 1, READ WAVE FILE */
  547. Ref<FileAccessMemory> file;
  548. file.instantiate();
  549. Error err = file->open_custom(p_stream_data.ptr(), p_stream_data.size());
  550. ERR_FAIL_COND_V_MSG(err != OK, Ref<AudioStreamWAV>(), "Cannot create memfile for WAV file buffer.");
  551. /* CHECK RIFF */
  552. char riff[5];
  553. riff[4] = 0;
  554. file->get_buffer((uint8_t *)&riff, 4); //RIFF
  555. if (riff[0] != 'R' || riff[1] != 'I' || riff[2] != 'F' || riff[3] != 'F') {
  556. ERR_FAIL_V_MSG(Ref<AudioStreamWAV>(), vformat("Not a WAV file. File should start with 'RIFF', but found '%s', in file of size %d bytes", riff, file->get_length()));
  557. }
  558. /* GET FILESIZE */
  559. // The file size in header is 8 bytes less than the actual size.
  560. // See https://docs.fileformat.com/audio/wav/
  561. const int FILE_SIZE_HEADER_OFFSET = 8;
  562. uint32_t file_size_header = file->get_32() + FILE_SIZE_HEADER_OFFSET;
  563. uint64_t file_size = file->get_length();
  564. if (file_size != file_size_header) {
  565. WARN_PRINT(vformat("File size %d is %s than the expected size %d.", file_size, file_size > file_size_header ? "larger" : "smaller", file_size_header));
  566. }
  567. /* CHECK WAVE */
  568. char wave[5];
  569. wave[4] = 0;
  570. file->get_buffer((uint8_t *)&wave, 4); //WAVE
  571. if (wave[0] != 'W' || wave[1] != 'A' || wave[2] != 'V' || wave[3] != 'E') {
  572. ERR_FAIL_V_MSG(Ref<AudioStreamWAV>(), vformat("Not a WAV file. Header should contain 'WAVE', but found '%s', in file of size %d bytes", wave, file->get_length()));
  573. }
  574. // Let users override potential loop points from the WAV.
  575. // We parse the WAV loop points only with "Detect From WAV" (0).
  576. int import_loop_mode = p_options["edit/loop_mode"];
  577. int format_bits = 0;
  578. int format_channels = 0;
  579. AudioStreamWAV::LoopMode loop_mode = AudioStreamWAV::LOOP_DISABLED;
  580. uint16_t compression_code = 1;
  581. bool format_found = false;
  582. bool data_found = false;
  583. int format_freq = 0;
  584. int loop_begin = 0;
  585. int loop_end = 0;
  586. int frames = 0;
  587. Vector<float> data;
  588. HashMap<String, String> tag_map;
  589. while (!file->eof_reached()) {
  590. /* chunk */
  591. char chunk_id[4];
  592. file->get_buffer((uint8_t *)&chunk_id, 4); //RIFF
  593. /* chunk size */
  594. uint32_t chunksize = file->get_32();
  595. uint32_t file_pos = file->get_position(); //save file pos, so we can skip to next chunk safely
  596. if (file->eof_reached()) {
  597. //ERR_PRINT("EOF REACH");
  598. break;
  599. }
  600. if (chunk_id[0] == 'f' && chunk_id[1] == 'm' && chunk_id[2] == 't' && chunk_id[3] == ' ' && !format_found) {
  601. /* IS FORMAT CHUNK */
  602. //Issue: #7755 : Not a bug - usage of other formats (format codes) are unsupported in current importer version.
  603. //Consider revision for engine version 3.0
  604. compression_code = file->get_16();
  605. if (compression_code != 1 && compression_code != 3) {
  606. ERR_FAIL_V_MSG(Ref<AudioStreamWAV>(), "Format not supported for WAVE file (not PCM). Save WAVE files as uncompressed PCM or IEEE float instead.");
  607. }
  608. format_channels = file->get_16();
  609. if (format_channels != 1 && format_channels != 2) {
  610. ERR_FAIL_V_MSG(Ref<AudioStreamWAV>(), "Format not supported for WAVE file (not stereo or mono).");
  611. }
  612. format_freq = file->get_32(); //sampling rate
  613. file->get_32(); // average bits/second (unused)
  614. file->get_16(); // block align (unused)
  615. format_bits = file->get_16(); // bits per sample
  616. if (format_bits % 8 || format_bits == 0) {
  617. ERR_FAIL_V_MSG(Ref<AudioStreamWAV>(), "Invalid amount of bits in the sample (should be one of 8, 16, 24 or 32).");
  618. }
  619. if (compression_code == 3 && format_bits % 32) {
  620. ERR_FAIL_V_MSG(Ref<AudioStreamWAV>(), "Invalid amount of bits in the IEEE float sample (should be 32 or 64).");
  621. }
  622. /* Don't need anything else, continue */
  623. format_found = true;
  624. }
  625. if (chunk_id[0] == 'd' && chunk_id[1] == 'a' && chunk_id[2] == 't' && chunk_id[3] == 'a' && !data_found) {
  626. /* IS DATA CHUNK */
  627. data_found = true;
  628. if (!format_found) {
  629. ERR_PRINT("'data' chunk before 'format' chunk found.");
  630. break;
  631. }
  632. uint64_t remaining_bytes = file_size - file_pos;
  633. frames = chunksize;
  634. if (remaining_bytes < chunksize) {
  635. WARN_PRINT("Data chunk size is smaller than expected. Proceeding with actual data size.");
  636. frames = remaining_bytes;
  637. }
  638. ERR_FAIL_COND_V(format_channels == 0, Ref<AudioStreamWAV>());
  639. frames /= format_channels;
  640. frames /= (format_bits >> 3);
  641. /*print_line("chunksize: "+itos(chunksize));
  642. print_line("channels: "+itos(format_channels));
  643. print_line("bits: "+itos(format_bits));
  644. */
  645. data.resize(frames * format_channels);
  646. if (compression_code == 1) {
  647. if (format_bits == 8) {
  648. for (int i = 0; i < frames * format_channels; i++) {
  649. // 8 bit samples are UNSIGNED
  650. data.write[i] = int8_t(file->get_8() - 128) / 128.f;
  651. }
  652. } else if (format_bits == 16) {
  653. for (int i = 0; i < frames * format_channels; i++) {
  654. //16 bit SIGNED
  655. data.write[i] = int16_t(file->get_16()) / 32768.f;
  656. }
  657. } else {
  658. for (int i = 0; i < frames * format_channels; i++) {
  659. //16+ bits samples are SIGNED
  660. // if sample is > 16 bits, just read extra bytes
  661. uint32_t s = 0;
  662. for (int b = 0; b < (format_bits >> 3); b++) {
  663. s |= ((uint32_t)file->get_8()) << (b * 8);
  664. }
  665. s <<= (32 - format_bits);
  666. data.write[i] = (int32_t(s) >> 16) / 32768.f;
  667. }
  668. }
  669. } else if (compression_code == 3) {
  670. if (format_bits == 32) {
  671. for (int i = 0; i < frames * format_channels; i++) {
  672. //32 bit IEEE Float
  673. data.write[i] = file->get_float();
  674. }
  675. } else if (format_bits == 64) {
  676. for (int i = 0; i < frames * format_channels; i++) {
  677. //64 bit IEEE Float
  678. data.write[i] = file->get_double();
  679. }
  680. }
  681. }
  682. // This is commented out due to some weird edge case seemingly in FileAccessMemory, doesn't seem to have any side effects though.
  683. // if (file->eof_reached()) {
  684. // ERR_FAIL_V_MSG(Ref<AudioStreamWAV>(), "Premature end of file.");
  685. // }
  686. }
  687. if (import_loop_mode == 0 && chunk_id[0] == 's' && chunk_id[1] == 'm' && chunk_id[2] == 'p' && chunk_id[3] == 'l') {
  688. // Loop point info!
  689. /**
  690. * Consider exploring next document:
  691. * http://www-mmsp.ece.mcgill.ca/Documents/AudioFormats/WAVE/Docs/RIFFNEW.pdf
  692. * Especially on page:
  693. * 16 - 17
  694. * Timestamp:
  695. * 22:38 06.07.2017 GMT
  696. **/
  697. for (int i = 0; i < 10; i++) {
  698. file->get_32(); // i wish to know why should i do this... no doc!
  699. }
  700. // only read 0x00 (loop forward), 0x01 (loop ping-pong) and 0x02 (loop backward)
  701. // Skip anything else because it's not supported, reserved for future uses or sampler specific
  702. // from https://sites.google.com/site/musicgapi/technical-documents/wav-file-format#smpl (loop type values table)
  703. int loop_type = file->get_32();
  704. if (loop_type == 0x00 || loop_type == 0x01 || loop_type == 0x02) {
  705. if (loop_type == 0x00) {
  706. loop_mode = AudioStreamWAV::LOOP_FORWARD;
  707. } else if (loop_type == 0x01) {
  708. loop_mode = AudioStreamWAV::LOOP_PINGPONG;
  709. } else if (loop_type == 0x02) {
  710. loop_mode = AudioStreamWAV::LOOP_BACKWARD;
  711. }
  712. loop_begin = file->get_32();
  713. loop_end = file->get_32();
  714. }
  715. }
  716. if (chunk_id[0] == 'L' && chunk_id[1] == 'I' && chunk_id[2] == 'S' && chunk_id[3] == 'T') {
  717. // RIFF 'LIST' chunk.
  718. // See https://www.recordingblogs.com/wiki/list-chunk-of-a-wave-file
  719. char list_id[4];
  720. file->get_buffer((uint8_t *)&list_id, 4);
  721. uint32_t end_of_chunk = file_pos + chunksize - 8;
  722. if (list_id[0] == 'I' && list_id[1] == 'N' && list_id[2] == 'F' && list_id[3] == 'O') {
  723. // 'INFO' list type.
  724. // The size of an entry can be arbitrary.
  725. while (file->get_position() < end_of_chunk) {
  726. char info_id[4];
  727. file->get_buffer((uint8_t *)&info_id, 4);
  728. uint32_t text_size = file->get_32();
  729. if (text_size == 0) {
  730. continue;
  731. }
  732. Vector<char> text;
  733. text.resize(text_size);
  734. file->get_buffer((uint8_t *)&text[0], text_size);
  735. // Skip padding byte if text_size is odd
  736. if (text_size & 1) {
  737. file->get_8();
  738. }
  739. // The data is always an ASCII string. ASCII is a subset of UTF-8.
  740. String tag;
  741. tag.append_utf8(&info_id[0], 4);
  742. String tag_value;
  743. tag_value.append_utf8(&text[0], text_size);
  744. tag_map[tag] = tag_value;
  745. }
  746. }
  747. }
  748. // Move to the start of the next chunk. Note that RIFF requires a padding byte for odd
  749. // chunk sizes.
  750. file->seek(file_pos + chunksize + (chunksize & 1));
  751. }
  752. // STEP 2, APPLY CONVERSIONS
  753. bool is16 = format_bits != 8;
  754. int rate = format_freq;
  755. /*
  756. print_line("Input Sample: ");
  757. print_line("\tframes: " + itos(frames));
  758. print_line("\tformat_channels: " + itos(format_channels));
  759. print_line("\t16bits: " + itos(is16));
  760. print_line("\trate: " + itos(rate));
  761. print_line("\tloop: " + itos(loop));
  762. print_line("\tloop begin: " + itos(loop_begin));
  763. print_line("\tloop end: " + itos(loop_end));
  764. */
  765. //apply frequency limit
  766. bool limit_rate = p_options["force/max_rate"];
  767. int limit_rate_hz = p_options["force/max_rate_hz"];
  768. if (limit_rate && rate > limit_rate_hz && rate > 0 && frames > 0) {
  769. // resample!
  770. int new_data_frames = (int)(frames * (float)limit_rate_hz / (float)rate);
  771. Vector<float> new_data;
  772. new_data.resize(new_data_frames * format_channels);
  773. for (int c = 0; c < format_channels; c++) {
  774. float frac = 0.0;
  775. int ipos = 0;
  776. for (int i = 0; i < new_data_frames; i++) {
  777. // Cubic interpolation should be enough.
  778. float y0 = data[MAX(0, ipos - 1) * format_channels + c];
  779. float y1 = data[ipos * format_channels + c];
  780. float y2 = data[MIN(frames - 1, ipos + 1) * format_channels + c];
  781. float y3 = data[MIN(frames - 1, ipos + 2) * format_channels + c];
  782. new_data.write[i * format_channels + c] = Math::cubic_interpolate(y1, y2, y0, y3, frac);
  783. // update position and always keep fractional part within ]0...1]
  784. // in order to avoid 32bit floating point precision errors
  785. frac += (float)rate / (float)limit_rate_hz;
  786. int tpos = (int)Math::floor(frac);
  787. ipos += tpos;
  788. frac -= tpos;
  789. }
  790. }
  791. if (loop_mode) {
  792. loop_begin = (int)(loop_begin * (float)new_data_frames / (float)frames);
  793. loop_end = (int)(loop_end * (float)new_data_frames / (float)frames);
  794. }
  795. data = new_data;
  796. rate = limit_rate_hz;
  797. frames = new_data_frames;
  798. }
  799. bool normalize = p_options["edit/normalize"];
  800. if (normalize) {
  801. float max = 0.0;
  802. for (int i = 0; i < data.size(); i++) {
  803. float amp = Math::abs(data[i]);
  804. if (amp > max) {
  805. max = amp;
  806. }
  807. }
  808. if (max > 0) {
  809. float mult = 1.0 / max;
  810. for (int i = 0; i < data.size(); i++) {
  811. data.write[i] *= mult;
  812. }
  813. }
  814. }
  815. bool trim = p_options["edit/trim"];
  816. if (trim && (loop_mode == AudioStreamWAV::LOOP_DISABLED) && format_channels > 0) {
  817. int first = 0;
  818. int last = (frames / format_channels) - 1;
  819. bool found = false;
  820. float limit = Math::db_to_linear(TRIM_DB_LIMIT);
  821. for (int i = 0; i < data.size() / format_channels; i++) {
  822. float amp_channel_sum = 0.0;
  823. for (int j = 0; j < format_channels; j++) {
  824. amp_channel_sum += Math::abs(data[(i * format_channels) + j]);
  825. }
  826. float amp = Math::abs(amp_channel_sum / (float)format_channels);
  827. if (!found && amp > limit) {
  828. first = i;
  829. found = true;
  830. }
  831. if (found && amp > limit) {
  832. last = i;
  833. }
  834. }
  835. if (first < last) {
  836. Vector<float> new_data;
  837. new_data.resize((last - first) * format_channels);
  838. for (int i = first; i < last; i++) {
  839. float fade_out_mult = 1.0;
  840. if (last - i < TRIM_FADE_OUT_FRAMES) {
  841. fade_out_mult = ((float)(last - i - 1) / (float)TRIM_FADE_OUT_FRAMES);
  842. }
  843. for (int j = 0; j < format_channels; j++) {
  844. new_data.write[((i - first) * format_channels) + j] = data[(i * format_channels) + j] * fade_out_mult;
  845. }
  846. }
  847. data = new_data;
  848. frames = data.size() / format_channels;
  849. }
  850. }
  851. if (import_loop_mode >= 2) {
  852. loop_mode = (AudioStreamWAV::LoopMode)(import_loop_mode - 1);
  853. loop_begin = p_options["edit/loop_begin"];
  854. loop_end = p_options["edit/loop_end"];
  855. // Wrap around to max frames, so `-1` can be used to select the end, etc.
  856. if (loop_begin < 0) {
  857. loop_begin = CLAMP(loop_begin + frames, 0, frames - 1);
  858. }
  859. if (loop_end < 0) {
  860. loop_end = CLAMP(loop_end + frames, 0, frames - 1);
  861. }
  862. }
  863. int compression = p_options["compress/mode"];
  864. bool force_mono = p_options["force/mono"];
  865. if (force_mono && format_channels == 2) {
  866. Vector<float> new_data;
  867. new_data.resize(data.size() / 2);
  868. for (int i = 0; i < frames; i++) {
  869. new_data.write[i] = (data[i * 2 + 0] + data[i * 2 + 1]) / 2.0;
  870. }
  871. data = new_data;
  872. format_channels = 1;
  873. }
  874. bool force_8_bit = p_options["force/8_bit"];
  875. if (force_8_bit) {
  876. is16 = false;
  877. }
  878. Vector<uint8_t> dst_data;
  879. AudioStreamWAV::Format dst_format;
  880. if (compression == 1) {
  881. dst_format = AudioStreamWAV::FORMAT_IMA_ADPCM;
  882. if (format_channels == 1) {
  883. _compress_ima_adpcm(data, dst_data);
  884. } else {
  885. //byte interleave
  886. Vector<float> left;
  887. Vector<float> right;
  888. int tframes = data.size() / 2;
  889. left.resize(tframes);
  890. right.resize(tframes);
  891. for (int i = 0; i < tframes; i++) {
  892. left.write[i] = data[i * 2 + 0];
  893. right.write[i] = data[i * 2 + 1];
  894. }
  895. Vector<uint8_t> bleft;
  896. Vector<uint8_t> bright;
  897. _compress_ima_adpcm(left, bleft);
  898. _compress_ima_adpcm(right, bright);
  899. int dl = bleft.size();
  900. dst_data.resize(dl * 2);
  901. uint8_t *w = dst_data.ptrw();
  902. const uint8_t *rl = bleft.ptr();
  903. const uint8_t *rr = bright.ptr();
  904. for (int i = 0; i < dl; i++) {
  905. w[i * 2 + 0] = rl[i];
  906. w[i * 2 + 1] = rr[i];
  907. }
  908. }
  909. } else if (compression == 2) {
  910. dst_format = AudioStreamWAV::FORMAT_QOA;
  911. qoa_desc desc = {};
  912. desc.samplerate = rate;
  913. desc.samples = frames;
  914. desc.channels = format_channels;
  915. _compress_qoa(data, dst_data, &desc);
  916. } else {
  917. dst_format = is16 ? AudioStreamWAV::FORMAT_16_BITS : AudioStreamWAV::FORMAT_8_BITS;
  918. dst_data.resize(data.size() * (is16 ? 2 : 1));
  919. {
  920. uint8_t *w = dst_data.ptrw();
  921. int ds = data.size();
  922. for (int i = 0; i < ds; i++) {
  923. if (is16) {
  924. int16_t v = CLAMP(data[i] * 32768, -32768, 32767);
  925. encode_uint16(v, &w[i * 2]);
  926. } else {
  927. int8_t v = CLAMP(data[i] * 128, -128, 127);
  928. w[i] = v;
  929. }
  930. }
  931. }
  932. }
  933. Ref<AudioStreamWAV> sample;
  934. sample.instantiate();
  935. sample->set_data(dst_data);
  936. sample->set_format(dst_format);
  937. sample->set_mix_rate(rate);
  938. sample->set_loop_mode(loop_mode);
  939. sample->set_loop_begin(loop_begin);
  940. sample->set_loop_end(loop_end);
  941. sample->set_stereo(format_channels == 2);
  942. if (!tag_map.is_empty()) {
  943. // Used to make the metadata tags more unified across different AudioStreams.
  944. // See https://www.recordingblogs.com/wiki/list-chunk-of-a-wave-file
  945. HashMap<String, String> tag_id_remaps;
  946. tag_id_remaps.reserve(15);
  947. tag_id_remaps["IARL"] = "location";
  948. tag_id_remaps["IART"] = "artist";
  949. tag_id_remaps["ICMS"] = "organization";
  950. tag_id_remaps["ICMT"] = "comments";
  951. tag_id_remaps["ICOP"] = "copyright";
  952. tag_id_remaps["ICRD"] = "date";
  953. tag_id_remaps["IGNR"] = "genre";
  954. tag_id_remaps["IKEY"] = "keywords";
  955. tag_id_remaps["IMED"] = "medium";
  956. tag_id_remaps["INAM"] = "title";
  957. tag_id_remaps["IPRD"] = "album";
  958. tag_id_remaps["ISBJ"] = "description";
  959. tag_id_remaps["ISFT"] = "software";
  960. tag_id_remaps["ITRK"] = "tracknumber";
  961. Dictionary tag_dictionary;
  962. for (const KeyValue<String, String> &E : tag_map) {
  963. HashMap<String, String>::ConstIterator remap = tag_id_remaps.find(E.key);
  964. String tag_key = E.key;
  965. if (remap) {
  966. tag_key = remap->value;
  967. }
  968. tag_dictionary[tag_key] = E.value;
  969. }
  970. sample->set_tags(tag_dictionary);
  971. }
  972. return sample;
  973. }
  974. Ref<AudioStreamWAV> AudioStreamWAV::load_from_file(const String &p_path, const Dictionary &p_options) {
  975. const Vector<uint8_t> stream_data = FileAccess::get_file_as_bytes(p_path);
  976. ERR_FAIL_COND_V_MSG(stream_data.is_empty(), Ref<AudioStreamWAV>(), vformat("Cannot open file '%s'.", p_path));
  977. return load_from_buffer(stream_data, p_options);
  978. }
  979. void AudioStreamWAV::_bind_methods() {
  980. ClassDB::bind_static_method("AudioStreamWAV", D_METHOD("load_from_buffer", "stream_data", "options"), &AudioStreamWAV::load_from_buffer, DEFVAL(Dictionary()));
  981. ClassDB::bind_static_method("AudioStreamWAV", D_METHOD("load_from_file", "path", "options"), &AudioStreamWAV::load_from_file, DEFVAL(Dictionary()));
  982. ClassDB::bind_method(D_METHOD("set_data", "data"), &AudioStreamWAV::set_data);
  983. ClassDB::bind_method(D_METHOD("get_data"), &AudioStreamWAV::get_data);
  984. ClassDB::bind_method(D_METHOD("set_format", "format"), &AudioStreamWAV::set_format);
  985. ClassDB::bind_method(D_METHOD("get_format"), &AudioStreamWAV::get_format);
  986. ClassDB::bind_method(D_METHOD("set_loop_mode", "loop_mode"), &AudioStreamWAV::set_loop_mode);
  987. ClassDB::bind_method(D_METHOD("get_loop_mode"), &AudioStreamWAV::get_loop_mode);
  988. ClassDB::bind_method(D_METHOD("set_loop_begin", "loop_begin"), &AudioStreamWAV::set_loop_begin);
  989. ClassDB::bind_method(D_METHOD("get_loop_begin"), &AudioStreamWAV::get_loop_begin);
  990. ClassDB::bind_method(D_METHOD("set_loop_end", "loop_end"), &AudioStreamWAV::set_loop_end);
  991. ClassDB::bind_method(D_METHOD("get_loop_end"), &AudioStreamWAV::get_loop_end);
  992. ClassDB::bind_method(D_METHOD("set_mix_rate", "mix_rate"), &AudioStreamWAV::set_mix_rate);
  993. ClassDB::bind_method(D_METHOD("get_mix_rate"), &AudioStreamWAV::get_mix_rate);
  994. ClassDB::bind_method(D_METHOD("set_stereo", "stereo"), &AudioStreamWAV::set_stereo);
  995. ClassDB::bind_method(D_METHOD("is_stereo"), &AudioStreamWAV::is_stereo);
  996. ClassDB::bind_method(D_METHOD("set_tags", "tags"), &AudioStreamWAV::set_tags);
  997. ClassDB::bind_method(D_METHOD("get_tags"), &AudioStreamWAV::get_tags);
  998. ClassDB::bind_method(D_METHOD("save_to_wav", "path"), &AudioStreamWAV::save_to_wav);
  999. ADD_PROPERTY(PropertyInfo(Variant::PACKED_BYTE_ARRAY, "data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_data", "get_data");
  1000. ADD_PROPERTY(PropertyInfo(Variant::INT, "format", PROPERTY_HINT_ENUM, "8-Bit,16-Bit,IMA ADPCM,Quite OK Audio"), "set_format", "get_format");
  1001. ADD_PROPERTY(PropertyInfo(Variant::INT, "loop_mode", PROPERTY_HINT_ENUM, "Disabled,Forward,Ping-Pong,Backward"), "set_loop_mode", "get_loop_mode");
  1002. ADD_PROPERTY(PropertyInfo(Variant::INT, "loop_begin"), "set_loop_begin", "get_loop_begin");
  1003. ADD_PROPERTY(PropertyInfo(Variant::INT, "loop_end"), "set_loop_end", "get_loop_end");
  1004. ADD_PROPERTY(PropertyInfo(Variant::INT, "mix_rate"), "set_mix_rate", "get_mix_rate");
  1005. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "stereo"), "set_stereo", "is_stereo");
  1006. ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "tags", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_tags", "get_tags");
  1007. BIND_ENUM_CONSTANT(FORMAT_8_BITS);
  1008. BIND_ENUM_CONSTANT(FORMAT_16_BITS);
  1009. BIND_ENUM_CONSTANT(FORMAT_IMA_ADPCM);
  1010. BIND_ENUM_CONSTANT(FORMAT_QOA);
  1011. BIND_ENUM_CONSTANT(LOOP_DISABLED);
  1012. BIND_ENUM_CONSTANT(LOOP_FORWARD);
  1013. BIND_ENUM_CONSTANT(LOOP_PINGPONG);
  1014. BIND_ENUM_CONSTANT(LOOP_BACKWARD);
  1015. }