audio_stream_wav.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  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 <class Depth, bool is_stereo, bool is_ima_adpcm>
  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) {
  78. // this function will be compiled branchless by any decent compiler
  79. int32_t final, final_r, next, next_r;
  80. while (p_amount) {
  81. p_amount--;
  82. int64_t pos = p_offset >> MIX_FRAC_BITS;
  83. if (is_stereo && !is_ima_adpcm) {
  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. const uint8_t *src_ptr = (const uint8_t *)base->data;
  108. src_ptr += AudioStreamWAV::DATA_PAD;
  109. uint8_t nbb = src_ptr[(p_ima_adpcm[i].last_nibble >> 1) * (is_stereo ? 2 : 1) + i];
  110. nibble = (p_ima_adpcm[i].last_nibble & 1) ? (nbb >> 4) : (nbb & 0xF);
  111. step = _ima_adpcm_step_table[p_ima_adpcm[i].step_index];
  112. p_ima_adpcm[i].step_index += _ima_adpcm_index_table[nibble];
  113. if (p_ima_adpcm[i].step_index < 0) {
  114. p_ima_adpcm[i].step_index = 0;
  115. }
  116. if (p_ima_adpcm[i].step_index > 88) {
  117. p_ima_adpcm[i].step_index = 88;
  118. }
  119. diff = step >> 3;
  120. if (nibble & 1) {
  121. diff += step >> 2;
  122. }
  123. if (nibble & 2) {
  124. diff += step >> 1;
  125. }
  126. if (nibble & 4) {
  127. diff += step;
  128. }
  129. if (nibble & 8) {
  130. diff = -diff;
  131. }
  132. p_ima_adpcm[i].predictor += diff;
  133. if (p_ima_adpcm[i].predictor < -0x8000) {
  134. p_ima_adpcm[i].predictor = -0x8000;
  135. } else if (p_ima_adpcm[i].predictor > 0x7FFF) {
  136. p_ima_adpcm[i].predictor = 0x7FFF;
  137. }
  138. /* store loop if there */
  139. if (p_ima_adpcm[i].last_nibble == p_ima_adpcm[i].loop_pos) {
  140. p_ima_adpcm[i].loop_step_index = p_ima_adpcm[i].step_index;
  141. p_ima_adpcm[i].loop_predictor = p_ima_adpcm[i].predictor;
  142. }
  143. //printf("%i - %i - pred %i\n",int(p_ima_adpcm[i].last_nibble),int(nibble),int(p_ima_adpcm[i].predictor));
  144. }
  145. }
  146. final = p_ima_adpcm[0].predictor;
  147. if (is_stereo) {
  148. final_r = p_ima_adpcm[1].predictor;
  149. }
  150. } else {
  151. final = p_src[pos];
  152. if (is_stereo) {
  153. final_r = p_src[pos + 1];
  154. }
  155. if constexpr (sizeof(Depth) == 1) { /* conditions will not exist anymore when compiled! */
  156. final <<= 8;
  157. if (is_stereo) {
  158. final_r <<= 8;
  159. }
  160. }
  161. if (is_stereo) {
  162. next = p_src[pos + 2];
  163. next_r = p_src[pos + 3];
  164. } else {
  165. next = p_src[pos + 1];
  166. }
  167. if constexpr (sizeof(Depth) == 1) {
  168. next <<= 8;
  169. if (is_stereo) {
  170. next_r <<= 8;
  171. }
  172. }
  173. int32_t frac = int64_t(p_offset & MIX_FRAC_MASK);
  174. final = final + ((next - final) * frac >> MIX_FRAC_BITS);
  175. if (is_stereo) {
  176. final_r = final_r + ((next_r - final_r) * frac >> MIX_FRAC_BITS);
  177. }
  178. }
  179. if (!is_stereo) {
  180. final_r = final; //copy to right channel if stereo
  181. }
  182. p_dst->left = final / 32767.0;
  183. p_dst->right = final_r / 32767.0;
  184. p_dst++;
  185. p_offset += p_increment;
  186. }
  187. }
  188. int AudioStreamPlaybackWAV::mix(AudioFrame *p_buffer, float p_rate_scale, int p_frames) {
  189. if (!base->data || !active) {
  190. for (int i = 0; i < p_frames; i++) {
  191. p_buffer[i] = AudioFrame(0, 0);
  192. }
  193. return 0;
  194. }
  195. int len = base->data_bytes;
  196. switch (base->format) {
  197. case AudioStreamWAV::FORMAT_8_BITS:
  198. len /= 1;
  199. break;
  200. case AudioStreamWAV::FORMAT_16_BITS:
  201. len /= 2;
  202. break;
  203. case AudioStreamWAV::FORMAT_IMA_ADPCM:
  204. len *= 2;
  205. break;
  206. }
  207. if (base->stereo) {
  208. len /= 2;
  209. }
  210. /* some 64-bit fixed point precaches */
  211. int64_t loop_begin_fp = ((int64_t)base->loop_begin << MIX_FRAC_BITS);
  212. int64_t loop_end_fp = ((int64_t)base->loop_end << MIX_FRAC_BITS);
  213. int64_t length_fp = ((int64_t)len << MIX_FRAC_BITS);
  214. int64_t begin_limit = (base->loop_mode != AudioStreamWAV::LOOP_DISABLED) ? loop_begin_fp : 0;
  215. int64_t end_limit = (base->loop_mode != AudioStreamWAV::LOOP_DISABLED) ? loop_end_fp : length_fp;
  216. bool is_stereo = base->stereo;
  217. int32_t todo = p_frames;
  218. if (base->loop_mode == AudioStreamWAV::LOOP_BACKWARD) {
  219. sign = -1;
  220. }
  221. float base_rate = AudioServer::get_singleton()->get_mix_rate();
  222. float srate = base->mix_rate;
  223. srate *= p_rate_scale;
  224. float playback_speed_scale = AudioServer::get_singleton()->get_playback_speed_scale();
  225. float fincrement = (srate * playback_speed_scale) / base_rate;
  226. int32_t increment = int32_t(MAX(fincrement * MIX_FRAC_LEN, 1));
  227. increment *= sign;
  228. //looping
  229. AudioStreamWAV::LoopMode loop_format = base->loop_mode;
  230. AudioStreamWAV::Format format = base->format;
  231. /* audio data */
  232. uint8_t *dataptr = (uint8_t *)base->data;
  233. const void *data = dataptr + AudioStreamWAV::DATA_PAD;
  234. AudioFrame *dst_buff = p_buffer;
  235. if (format == AudioStreamWAV::FORMAT_IMA_ADPCM) {
  236. if (loop_format != AudioStreamWAV::LOOP_DISABLED) {
  237. ima_adpcm[0].loop_pos = loop_begin_fp >> MIX_FRAC_BITS;
  238. ima_adpcm[1].loop_pos = loop_begin_fp >> MIX_FRAC_BITS;
  239. loop_format = AudioStreamWAV::LOOP_FORWARD;
  240. }
  241. }
  242. while (todo > 0) {
  243. int64_t limit = 0;
  244. int32_t target = 0, aux = 0;
  245. /** LOOP CHECKING **/
  246. if (increment < 0) {
  247. /* going backwards */
  248. if (loop_format != AudioStreamWAV::LOOP_DISABLED && offset < loop_begin_fp) {
  249. /* loopstart reached */
  250. if (loop_format == AudioStreamWAV::LOOP_PINGPONG) {
  251. /* bounce ping pong */
  252. offset = loop_begin_fp + (loop_begin_fp - offset);
  253. increment = -increment;
  254. sign *= -1;
  255. } else {
  256. /* go to loop-end */
  257. offset = loop_end_fp - (loop_begin_fp - offset);
  258. }
  259. } else {
  260. /* check for sample not reaching beginning */
  261. if (offset < 0) {
  262. active = false;
  263. break;
  264. }
  265. }
  266. } else {
  267. /* going forward */
  268. if (loop_format != AudioStreamWAV::LOOP_DISABLED && offset >= loop_end_fp) {
  269. /* loopend reached */
  270. if (loop_format == AudioStreamWAV::LOOP_PINGPONG) {
  271. /* bounce ping pong */
  272. offset = loop_end_fp - (offset - loop_end_fp);
  273. increment = -increment;
  274. sign *= -1;
  275. } else {
  276. /* go to loop-begin */
  277. if (format == AudioStreamWAV::FORMAT_IMA_ADPCM) {
  278. for (int i = 0; i < 2; i++) {
  279. ima_adpcm[i].step_index = ima_adpcm[i].loop_step_index;
  280. ima_adpcm[i].predictor = ima_adpcm[i].loop_predictor;
  281. ima_adpcm[i].last_nibble = loop_begin_fp >> MIX_FRAC_BITS;
  282. }
  283. offset = loop_begin_fp;
  284. } else {
  285. offset = loop_begin_fp + (offset - loop_end_fp);
  286. }
  287. }
  288. } else {
  289. /* no loop, check for end of sample */
  290. if (offset >= length_fp) {
  291. active = false;
  292. break;
  293. }
  294. }
  295. }
  296. /** MIXCOUNT COMPUTING **/
  297. /* next possible limit (looppoints or sample begin/end */
  298. limit = (increment < 0) ? begin_limit : end_limit;
  299. /* compute what is shorter, the todo or the limit? */
  300. aux = (limit - offset) / increment + 1;
  301. target = (aux < todo) ? aux : todo; /* mix target is the shorter buffer */
  302. /* check just in case */
  303. if (target <= 0) {
  304. active = false;
  305. break;
  306. }
  307. todo -= target;
  308. switch (base->format) {
  309. case AudioStreamWAV::FORMAT_8_BITS: {
  310. if (is_stereo) {
  311. do_resample<int8_t, true, false>((int8_t *)data, dst_buff, offset, increment, target, ima_adpcm);
  312. } else {
  313. do_resample<int8_t, false, false>((int8_t *)data, dst_buff, offset, increment, target, ima_adpcm);
  314. }
  315. } break;
  316. case AudioStreamWAV::FORMAT_16_BITS: {
  317. if (is_stereo) {
  318. do_resample<int16_t, true, false>((int16_t *)data, dst_buff, offset, increment, target, ima_adpcm);
  319. } else {
  320. do_resample<int16_t, false, false>((int16_t *)data, dst_buff, offset, increment, target, ima_adpcm);
  321. }
  322. } break;
  323. case AudioStreamWAV::FORMAT_IMA_ADPCM: {
  324. if (is_stereo) {
  325. do_resample<int8_t, true, true>((int8_t *)data, dst_buff, offset, increment, target, ima_adpcm);
  326. } else {
  327. do_resample<int8_t, false, true>((int8_t *)data, dst_buff, offset, increment, target, ima_adpcm);
  328. }
  329. } break;
  330. }
  331. dst_buff += target;
  332. }
  333. if (todo) {
  334. int mixed_frames = p_frames - todo;
  335. //bit was missing from mix
  336. int todo_ofs = p_frames - todo;
  337. for (int i = todo_ofs; i < p_frames; i++) {
  338. p_buffer[i] = AudioFrame(0, 0);
  339. }
  340. return mixed_frames;
  341. }
  342. return p_frames;
  343. }
  344. void AudioStreamPlaybackWAV::tag_used_streams() {
  345. base->tag_used(get_playback_position());
  346. }
  347. AudioStreamPlaybackWAV::AudioStreamPlaybackWAV() {}
  348. /////////////////////
  349. void AudioStreamWAV::set_format(Format p_format) {
  350. format = p_format;
  351. }
  352. AudioStreamWAV::Format AudioStreamWAV::get_format() const {
  353. return format;
  354. }
  355. void AudioStreamWAV::set_loop_mode(LoopMode p_loop_mode) {
  356. loop_mode = p_loop_mode;
  357. }
  358. AudioStreamWAV::LoopMode AudioStreamWAV::get_loop_mode() const {
  359. return loop_mode;
  360. }
  361. void AudioStreamWAV::set_loop_begin(int p_frame) {
  362. loop_begin = p_frame;
  363. }
  364. int AudioStreamWAV::get_loop_begin() const {
  365. return loop_begin;
  366. }
  367. void AudioStreamWAV::set_loop_end(int p_frame) {
  368. loop_end = p_frame;
  369. }
  370. int AudioStreamWAV::get_loop_end() const {
  371. return loop_end;
  372. }
  373. void AudioStreamWAV::set_mix_rate(int p_hz) {
  374. ERR_FAIL_COND(p_hz == 0);
  375. mix_rate = p_hz;
  376. }
  377. int AudioStreamWAV::get_mix_rate() const {
  378. return mix_rate;
  379. }
  380. void AudioStreamWAV::set_stereo(bool p_enable) {
  381. stereo = p_enable;
  382. }
  383. bool AudioStreamWAV::is_stereo() const {
  384. return stereo;
  385. }
  386. double AudioStreamWAV::get_length() const {
  387. int len = data_bytes;
  388. switch (format) {
  389. case AudioStreamWAV::FORMAT_8_BITS:
  390. len /= 1;
  391. break;
  392. case AudioStreamWAV::FORMAT_16_BITS:
  393. len /= 2;
  394. break;
  395. case AudioStreamWAV::FORMAT_IMA_ADPCM:
  396. len *= 2;
  397. break;
  398. }
  399. if (stereo) {
  400. len /= 2;
  401. }
  402. return double(len) / mix_rate;
  403. }
  404. bool AudioStreamWAV::is_monophonic() const {
  405. return false;
  406. }
  407. void AudioStreamWAV::set_data(const Vector<uint8_t> &p_data) {
  408. AudioServer::get_singleton()->lock();
  409. if (data) {
  410. memfree(data);
  411. data = nullptr;
  412. data_bytes = 0;
  413. }
  414. int datalen = p_data.size();
  415. if (datalen) {
  416. const uint8_t *r = p_data.ptr();
  417. int alloc_len = datalen + DATA_PAD * 2;
  418. data = memalloc(alloc_len); //alloc with some padding for interpolation
  419. memset(data, 0, alloc_len);
  420. uint8_t *dataptr = (uint8_t *)data;
  421. memcpy(dataptr + DATA_PAD, r, datalen);
  422. data_bytes = datalen;
  423. }
  424. AudioServer::get_singleton()->unlock();
  425. }
  426. Vector<uint8_t> AudioStreamWAV::get_data() const {
  427. Vector<uint8_t> pv;
  428. if (data) {
  429. pv.resize(data_bytes);
  430. {
  431. uint8_t *w = pv.ptrw();
  432. uint8_t *dataptr = (uint8_t *)data;
  433. memcpy(w, dataptr + DATA_PAD, data_bytes);
  434. }
  435. }
  436. return pv;
  437. }
  438. Error AudioStreamWAV::save_to_wav(const String &p_path) {
  439. if (format == AudioStreamWAV::FORMAT_IMA_ADPCM) {
  440. WARN_PRINT("Saving IMA_ADPC samples are 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. byte_pr_sample = 2;
  457. break;
  458. case AudioStreamWAV::FORMAT_IMA_ADPCM:
  459. byte_pr_sample = 4;
  460. break;
  461. }
  462. String file_path = p_path;
  463. if (!(file_path.substr(file_path.length() - 4, 4) == ".wav")) {
  464. file_path += ".wav";
  465. }
  466. Ref<FileAccess> file = FileAccess::open(file_path, FileAccess::WRITE); //Overrides existing file if present
  467. ERR_FAIL_COND_V(file.is_null(), ERR_FILE_CANT_WRITE);
  468. // Create WAV Header
  469. file->store_string("RIFF"); //ChunkID
  470. file->store_32(sub_chunk_2_size + 36); //ChunkSize = 36 + SubChunk2Size (size of entire file minus the 8 bits for this and previous header)
  471. file->store_string("WAVE"); //Format
  472. file->store_string("fmt "); //Subchunk1ID
  473. file->store_32(16); //Subchunk1Size = 16
  474. file->store_16(format_code); //AudioFormat
  475. file->store_16(n_channels); //Number of Channels
  476. file->store_32(sample_rate); //SampleRate
  477. file->store_32(sample_rate * n_channels * byte_pr_sample); //ByteRate
  478. file->store_16(n_channels * byte_pr_sample); //BlockAlign = NumChannels * BytePrSample
  479. file->store_16(byte_pr_sample * 8); //BitsPerSample
  480. file->store_string("data"); //Subchunk2ID
  481. file->store_32(sub_chunk_2_size); //Subchunk2Size
  482. // Add data
  483. Vector<uint8_t> stream_data = get_data();
  484. const uint8_t *read_data = stream_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. for (unsigned int i = 0; i < data_bytes / 2; i++) {
  494. uint16_t data_point = decode_uint16(&read_data[i * 2]);
  495. file->store_16(data_point);
  496. }
  497. break;
  498. case AudioStreamWAV::FORMAT_IMA_ADPCM:
  499. //Unimplemented
  500. break;
  501. }
  502. return OK;
  503. }
  504. Ref<AudioStreamPlayback> AudioStreamWAV::instantiate_playback() {
  505. Ref<AudioStreamPlaybackWAV> sample;
  506. sample.instantiate();
  507. sample->base = Ref<AudioStreamWAV>(this);
  508. return sample;
  509. }
  510. String AudioStreamWAV::get_stream_name() const {
  511. return "";
  512. }
  513. void AudioStreamWAV::_bind_methods() {
  514. ClassDB::bind_method(D_METHOD("set_data", "data"), &AudioStreamWAV::set_data);
  515. ClassDB::bind_method(D_METHOD("get_data"), &AudioStreamWAV::get_data);
  516. ClassDB::bind_method(D_METHOD("set_format", "format"), &AudioStreamWAV::set_format);
  517. ClassDB::bind_method(D_METHOD("get_format"), &AudioStreamWAV::get_format);
  518. ClassDB::bind_method(D_METHOD("set_loop_mode", "loop_mode"), &AudioStreamWAV::set_loop_mode);
  519. ClassDB::bind_method(D_METHOD("get_loop_mode"), &AudioStreamWAV::get_loop_mode);
  520. ClassDB::bind_method(D_METHOD("set_loop_begin", "loop_begin"), &AudioStreamWAV::set_loop_begin);
  521. ClassDB::bind_method(D_METHOD("get_loop_begin"), &AudioStreamWAV::get_loop_begin);
  522. ClassDB::bind_method(D_METHOD("set_loop_end", "loop_end"), &AudioStreamWAV::set_loop_end);
  523. ClassDB::bind_method(D_METHOD("get_loop_end"), &AudioStreamWAV::get_loop_end);
  524. ClassDB::bind_method(D_METHOD("set_mix_rate", "mix_rate"), &AudioStreamWAV::set_mix_rate);
  525. ClassDB::bind_method(D_METHOD("get_mix_rate"), &AudioStreamWAV::get_mix_rate);
  526. ClassDB::bind_method(D_METHOD("set_stereo", "stereo"), &AudioStreamWAV::set_stereo);
  527. ClassDB::bind_method(D_METHOD("is_stereo"), &AudioStreamWAV::is_stereo);
  528. ClassDB::bind_method(D_METHOD("save_to_wav", "path"), &AudioStreamWAV::save_to_wav);
  529. ADD_PROPERTY(PropertyInfo(Variant::PACKED_BYTE_ARRAY, "data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_data", "get_data");
  530. ADD_PROPERTY(PropertyInfo(Variant::INT, "format", PROPERTY_HINT_ENUM, "8-Bit,16-Bit,IMA-ADPCM"), "set_format", "get_format");
  531. ADD_PROPERTY(PropertyInfo(Variant::INT, "loop_mode", PROPERTY_HINT_ENUM, "Disabled,Forward,Ping-Pong,Backward"), "set_loop_mode", "get_loop_mode");
  532. ADD_PROPERTY(PropertyInfo(Variant::INT, "loop_begin"), "set_loop_begin", "get_loop_begin");
  533. ADD_PROPERTY(PropertyInfo(Variant::INT, "loop_end"), "set_loop_end", "get_loop_end");
  534. ADD_PROPERTY(PropertyInfo(Variant::INT, "mix_rate"), "set_mix_rate", "get_mix_rate");
  535. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "stereo"), "set_stereo", "is_stereo");
  536. BIND_ENUM_CONSTANT(FORMAT_8_BITS);
  537. BIND_ENUM_CONSTANT(FORMAT_16_BITS);
  538. BIND_ENUM_CONSTANT(FORMAT_IMA_ADPCM);
  539. BIND_ENUM_CONSTANT(LOOP_DISABLED);
  540. BIND_ENUM_CONSTANT(LOOP_FORWARD);
  541. BIND_ENUM_CONSTANT(LOOP_PINGPONG);
  542. BIND_ENUM_CONSTANT(LOOP_BACKWARD);
  543. }
  544. AudioStreamWAV::AudioStreamWAV() {}
  545. AudioStreamWAV::~AudioStreamWAV() {
  546. if (data) {
  547. memfree(data);
  548. data = nullptr;
  549. data_bytes = 0;
  550. }
  551. }