audio_stream_sample.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  1. /*************************************************************************/
  2. /* audio_stream_sample.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  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_sample.h"
  31. #include "core/io/marshalls.h"
  32. #include "core/os/file_access.h"
  33. void AudioStreamPlaybackSample::start(float p_from_pos) {
  34. if (base->format == AudioStreamSample::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 AudioStreamPlaybackSample::stop() {
  53. active = false;
  54. }
  55. bool AudioStreamPlaybackSample::is_playing() const {
  56. return active;
  57. }
  58. int AudioStreamPlaybackSample::get_loop_count() const {
  59. return 0;
  60. }
  61. float AudioStreamPlaybackSample::get_playback_position() const {
  62. return float(offset >> MIX_FRAC_BITS) / base->mix_rate;
  63. }
  64. void AudioStreamPlaybackSample::seek(float p_time) {
  65. if (base->format == AudioStreamSample::FORMAT_IMA_ADPCM)
  66. return; //no seeking in ima-adpcm
  67. float max = base->get_length();
  68. if (p_time < 0) {
  69. p_time = 0;
  70. } else if (p_time >= max) {
  71. p_time = max - 0.001;
  72. }
  73. offset = uint64_t(p_time * base->mix_rate) << MIX_FRAC_BITS;
  74. }
  75. template <class Depth, bool is_stereo, bool is_ima_adpcm>
  76. void AudioStreamPlaybackSample::do_resample(const Depth *p_src, AudioFrame *p_dst, int64_t &offset, int32_t &increment, uint32_t amount, IMA_ADPCM_State *ima_adpcm) {
  77. // this function will be compiled branchless by any decent compiler
  78. int32_t final, final_r, next, next_r;
  79. while (amount) {
  80. amount--;
  81. int64_t pos = offset >> MIX_FRAC_BITS;
  82. if (is_stereo && !is_ima_adpcm)
  83. pos <<= 1;
  84. if (is_ima_adpcm) {
  85. int64_t sample_pos = pos + ima_adpcm[0].window_ofs;
  86. while (sample_pos > ima_adpcm[0].last_nibble) {
  87. static const int16_t _ima_adpcm_step_table[89] = {
  88. 7, 8, 9, 10, 11, 12, 13, 14, 16, 17,
  89. 19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
  90. 50, 55, 60, 66, 73, 80, 88, 97, 107, 118,
  91. 130, 143, 157, 173, 190, 209, 230, 253, 279, 307,
  92. 337, 371, 408, 449, 494, 544, 598, 658, 724, 796,
  93. 876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066,
  94. 2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358,
  95. 5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
  96. 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
  97. };
  98. static const int8_t _ima_adpcm_index_table[16] = {
  99. -1, -1, -1, -1, 2, 4, 6, 8,
  100. -1, -1, -1, -1, 2, 4, 6, 8
  101. };
  102. for (int i = 0; i < (is_stereo ? 2 : 1); i++) {
  103. int16_t nibble, diff, step;
  104. ima_adpcm[i].last_nibble++;
  105. const uint8_t *src_ptr = (const uint8_t *)base->data;
  106. src_ptr += AudioStreamSample::DATA_PAD;
  107. uint8_t nbb = src_ptr[(ima_adpcm[i].last_nibble >> 1) * (is_stereo ? 2 : 1) + i];
  108. nibble = (ima_adpcm[i].last_nibble & 1) ? (nbb >> 4) : (nbb & 0xF);
  109. step = _ima_adpcm_step_table[ima_adpcm[i].step_index];
  110. ima_adpcm[i].step_index += _ima_adpcm_index_table[nibble];
  111. if (ima_adpcm[i].step_index < 0)
  112. ima_adpcm[i].step_index = 0;
  113. if (ima_adpcm[i].step_index > 88)
  114. ima_adpcm[i].step_index = 88;
  115. diff = step >> 3;
  116. if (nibble & 1)
  117. diff += step >> 2;
  118. if (nibble & 2)
  119. diff += step >> 1;
  120. if (nibble & 4)
  121. diff += step;
  122. if (nibble & 8)
  123. diff = -diff;
  124. ima_adpcm[i].predictor += diff;
  125. if (ima_adpcm[i].predictor < -0x8000)
  126. ima_adpcm[i].predictor = -0x8000;
  127. else if (ima_adpcm[i].predictor > 0x7FFF)
  128. ima_adpcm[i].predictor = 0x7FFF;
  129. /* store loop if there */
  130. if (ima_adpcm[i].last_nibble == ima_adpcm[i].loop_pos) {
  131. ima_adpcm[i].loop_step_index = ima_adpcm[i].step_index;
  132. ima_adpcm[i].loop_predictor = ima_adpcm[i].predictor;
  133. }
  134. //printf("%i - %i - pred %i\n",int(ima_adpcm[i].last_nibble),int(nibble),int(ima_adpcm[i].predictor));
  135. }
  136. }
  137. final = ima_adpcm[0].predictor;
  138. if (is_stereo) {
  139. final_r = ima_adpcm[1].predictor;
  140. }
  141. } else {
  142. final = p_src[pos];
  143. if (is_stereo)
  144. final_r = p_src[pos + 1];
  145. if (sizeof(Depth) == 1) { /* conditions will not exist anymore when compiled! */
  146. final <<= 8;
  147. if (is_stereo)
  148. final_r <<= 8;
  149. }
  150. if (is_stereo) {
  151. next = p_src[pos + 2];
  152. next_r = p_src[pos + 3];
  153. } else {
  154. next = p_src[pos + 1];
  155. }
  156. if (sizeof(Depth) == 1) {
  157. next <<= 8;
  158. if (is_stereo)
  159. next_r <<= 8;
  160. }
  161. int32_t frac = int64_t(offset & MIX_FRAC_MASK);
  162. final = final + ((next - final) * frac >> MIX_FRAC_BITS);
  163. if (is_stereo)
  164. final_r = final_r + ((next_r - final_r) * frac >> MIX_FRAC_BITS);
  165. }
  166. if (!is_stereo) {
  167. final_r = final; //copy to right channel if stereo
  168. }
  169. p_dst->l = final / 32767.0;
  170. p_dst->r = final_r / 32767.0;
  171. p_dst++;
  172. offset += increment;
  173. }
  174. }
  175. void AudioStreamPlaybackSample::mix(AudioFrame *p_buffer, float p_rate_scale, int p_frames) {
  176. if (!base->data || !active) {
  177. for (int i = 0; i < p_frames; i++) {
  178. p_buffer[i] = AudioFrame(0, 0);
  179. }
  180. return;
  181. }
  182. int len = base->data_bytes;
  183. switch (base->format) {
  184. case AudioStreamSample::FORMAT_8_BITS: len /= 1; break;
  185. case AudioStreamSample::FORMAT_16_BITS: len /= 2; break;
  186. case AudioStreamSample::FORMAT_IMA_ADPCM: len *= 2; break;
  187. }
  188. if (base->stereo) {
  189. len /= 2;
  190. }
  191. /* some 64-bit fixed point precaches */
  192. int64_t loop_begin_fp = ((int64_t)base->loop_begin << MIX_FRAC_BITS);
  193. int64_t loop_end_fp = ((int64_t)base->loop_end << MIX_FRAC_BITS);
  194. int64_t length_fp = ((int64_t)len << MIX_FRAC_BITS);
  195. int64_t begin_limit = (base->loop_mode != AudioStreamSample::LOOP_DISABLED) ? loop_begin_fp : 0;
  196. int64_t end_limit = (base->loop_mode != AudioStreamSample::LOOP_DISABLED) ? loop_end_fp : length_fp;
  197. bool is_stereo = base->stereo;
  198. int32_t todo = p_frames;
  199. if (base->loop_mode == AudioStreamSample::LOOP_BACKWARD) {
  200. sign = -1;
  201. }
  202. float base_rate = AudioServer::get_singleton()->get_mix_rate();
  203. float srate = base->mix_rate;
  204. srate *= p_rate_scale;
  205. float fincrement = srate / base_rate;
  206. int32_t increment = int32_t(MAX(fincrement * MIX_FRAC_LEN, 1));
  207. increment *= sign;
  208. //looping
  209. AudioStreamSample::LoopMode loop_format = base->loop_mode;
  210. AudioStreamSample::Format format = base->format;
  211. /* audio data */
  212. uint8_t *dataptr = (uint8_t *)base->data;
  213. const void *data = dataptr + AudioStreamSample::DATA_PAD;
  214. AudioFrame *dst_buff = p_buffer;
  215. if (format == AudioStreamSample::FORMAT_IMA_ADPCM) {
  216. if (loop_format != AudioStreamSample::LOOP_DISABLED) {
  217. ima_adpcm[0].loop_pos = loop_begin_fp >> MIX_FRAC_BITS;
  218. ima_adpcm[1].loop_pos = loop_begin_fp >> MIX_FRAC_BITS;
  219. loop_format = AudioStreamSample::LOOP_FORWARD;
  220. }
  221. }
  222. while (todo > 0) {
  223. int64_t limit = 0;
  224. int32_t target = 0, aux = 0;
  225. /** LOOP CHECKING **/
  226. if (increment < 0) {
  227. /* going backwards */
  228. if (loop_format != AudioStreamSample::LOOP_DISABLED && offset < loop_begin_fp) {
  229. /* loopstart reached */
  230. if (loop_format == AudioStreamSample::LOOP_PING_PONG) {
  231. /* bounce ping pong */
  232. offset = loop_begin_fp + (loop_begin_fp - offset);
  233. increment = -increment;
  234. sign *= -1;
  235. } else {
  236. /* go to loop-end */
  237. offset = loop_end_fp - (loop_begin_fp - offset);
  238. }
  239. } else {
  240. /* check for sample not reaching beginning */
  241. if (offset < 0) {
  242. active = false;
  243. break;
  244. }
  245. }
  246. } else {
  247. /* going forward */
  248. if (loop_format != AudioStreamSample::LOOP_DISABLED && offset >= loop_end_fp) {
  249. /* loopend reached */
  250. if (loop_format == AudioStreamSample::LOOP_PING_PONG) {
  251. /* bounce ping pong */
  252. offset = loop_end_fp - (offset - loop_end_fp);
  253. increment = -increment;
  254. sign *= -1;
  255. } else {
  256. /* go to loop-begin */
  257. if (format == AudioStreamSample::FORMAT_IMA_ADPCM) {
  258. for (int i = 0; i < 2; i++) {
  259. ima_adpcm[i].step_index = ima_adpcm[i].loop_step_index;
  260. ima_adpcm[i].predictor = ima_adpcm[i].loop_predictor;
  261. ima_adpcm[i].last_nibble = loop_begin_fp >> MIX_FRAC_BITS;
  262. }
  263. offset = loop_begin_fp;
  264. } else {
  265. offset = loop_begin_fp + (offset - loop_end_fp);
  266. }
  267. }
  268. } else {
  269. /* no loop, check for end of sample */
  270. if (offset >= length_fp) {
  271. active = false;
  272. break;
  273. }
  274. }
  275. }
  276. /** MIXCOUNT COMPUTING **/
  277. /* next possible limit (looppoints or sample begin/end */
  278. limit = (increment < 0) ? begin_limit : end_limit;
  279. /* compute what is shorter, the todo or the limit? */
  280. aux = (limit - offset) / increment + 1;
  281. target = (aux < todo) ? aux : todo; /* mix target is the shorter buffer */
  282. /* check just in case */
  283. if (target <= 0) {
  284. active = false;
  285. break;
  286. }
  287. todo -= target;
  288. switch (base->format) {
  289. case AudioStreamSample::FORMAT_8_BITS: {
  290. if (is_stereo)
  291. do_resample<int8_t, true, false>((int8_t *)data, dst_buff, offset, increment, target, ima_adpcm);
  292. else
  293. do_resample<int8_t, false, false>((int8_t *)data, dst_buff, offset, increment, target, ima_adpcm);
  294. } break;
  295. case AudioStreamSample::FORMAT_16_BITS: {
  296. if (is_stereo)
  297. do_resample<int16_t, true, false>((int16_t *)data, dst_buff, offset, increment, target, ima_adpcm);
  298. else
  299. do_resample<int16_t, false, false>((int16_t *)data, dst_buff, offset, increment, target, ima_adpcm);
  300. } break;
  301. case AudioStreamSample::FORMAT_IMA_ADPCM: {
  302. if (is_stereo)
  303. do_resample<int8_t, true, true>((int8_t *)data, dst_buff, offset, increment, target, ima_adpcm);
  304. else
  305. do_resample<int8_t, false, true>((int8_t *)data, dst_buff, offset, increment, target, ima_adpcm);
  306. } break;
  307. }
  308. dst_buff += target;
  309. }
  310. if (todo) {
  311. //bit was missing from mix
  312. int todo_ofs = p_frames - todo;
  313. for (int i = todo_ofs; i < p_frames; i++) {
  314. p_buffer[i] = AudioFrame(0, 0);
  315. }
  316. }
  317. }
  318. AudioStreamPlaybackSample::AudioStreamPlaybackSample() {
  319. active = false;
  320. offset = 0;
  321. sign = 1;
  322. }
  323. /////////////////////
  324. void AudioStreamSample::set_format(Format p_format) {
  325. format = p_format;
  326. }
  327. AudioStreamSample::Format AudioStreamSample::get_format() const {
  328. return format;
  329. }
  330. void AudioStreamSample::set_loop_mode(LoopMode p_loop_mode) {
  331. loop_mode = p_loop_mode;
  332. }
  333. AudioStreamSample::LoopMode AudioStreamSample::get_loop_mode() const {
  334. return loop_mode;
  335. }
  336. void AudioStreamSample::set_loop_begin(int p_frame) {
  337. loop_begin = p_frame;
  338. }
  339. int AudioStreamSample::get_loop_begin() const {
  340. return loop_begin;
  341. }
  342. void AudioStreamSample::set_loop_end(int p_frame) {
  343. loop_end = p_frame;
  344. }
  345. int AudioStreamSample::get_loop_end() const {
  346. return loop_end;
  347. }
  348. void AudioStreamSample::set_mix_rate(int p_hz) {
  349. ERR_FAIL_COND(p_hz == 0);
  350. mix_rate = p_hz;
  351. }
  352. int AudioStreamSample::get_mix_rate() const {
  353. return mix_rate;
  354. }
  355. void AudioStreamSample::set_stereo(bool p_enable) {
  356. stereo = p_enable;
  357. }
  358. bool AudioStreamSample::is_stereo() const {
  359. return stereo;
  360. }
  361. float AudioStreamSample::get_length() const {
  362. int len = data_bytes;
  363. switch (format) {
  364. case AudioStreamSample::FORMAT_8_BITS: len /= 1; break;
  365. case AudioStreamSample::FORMAT_16_BITS: len /= 2; break;
  366. case AudioStreamSample::FORMAT_IMA_ADPCM: len *= 2; break;
  367. }
  368. if (stereo) {
  369. len /= 2;
  370. }
  371. return float(len) / mix_rate;
  372. }
  373. void AudioStreamSample::set_data(const PoolVector<uint8_t> &p_data) {
  374. AudioServer::get_singleton()->lock();
  375. if (data) {
  376. AudioServer::get_singleton()->audio_data_free(data);
  377. data = NULL;
  378. data_bytes = 0;
  379. }
  380. int datalen = p_data.size();
  381. if (datalen) {
  382. PoolVector<uint8_t>::Read r = p_data.read();
  383. int alloc_len = datalen + DATA_PAD * 2;
  384. data = AudioServer::get_singleton()->audio_data_alloc(alloc_len); //alloc with some padding for interpolation
  385. zeromem(data, alloc_len);
  386. uint8_t *dataptr = (uint8_t *)data;
  387. copymem(dataptr + DATA_PAD, r.ptr(), datalen);
  388. data_bytes = datalen;
  389. }
  390. AudioServer::get_singleton()->unlock();
  391. }
  392. PoolVector<uint8_t> AudioStreamSample::get_data() const {
  393. PoolVector<uint8_t> pv;
  394. if (data) {
  395. pv.resize(data_bytes);
  396. {
  397. PoolVector<uint8_t>::Write w = pv.write();
  398. uint8_t *dataptr = (uint8_t *)data;
  399. copymem(w.ptr(), dataptr + DATA_PAD, data_bytes);
  400. }
  401. }
  402. return pv;
  403. }
  404. Error AudioStreamSample::save_to_wav(const String &p_path) {
  405. if (format == AudioStreamSample::FORMAT_IMA_ADPCM) {
  406. WARN_PRINTS("Saving IMA_ADPC samples are not supported yet");
  407. return ERR_UNAVAILABLE;
  408. }
  409. int sub_chunk_2_size = data_bytes; //Subchunk2Size = Size of data in bytes
  410. // Format code
  411. // 1:PCM format (for 8 or 16 bit)
  412. // 3:IEEE float format
  413. int format_code = (format == FORMAT_IMA_ADPCM) ? 3 : 1;
  414. int n_channels = stereo ? 2 : 1;
  415. long sample_rate = mix_rate;
  416. int byte_pr_sample = 0;
  417. switch (format) {
  418. case AudioStreamSample::FORMAT_8_BITS: byte_pr_sample = 1; break;
  419. case AudioStreamSample::FORMAT_16_BITS: byte_pr_sample = 2; break;
  420. case AudioStreamSample::FORMAT_IMA_ADPCM: byte_pr_sample = 4; break;
  421. }
  422. String file_path = p_path;
  423. if (!(file_path.substr(file_path.length() - 4, 4) == ".wav")) {
  424. file_path += ".wav";
  425. }
  426. FileAccessRef file = FileAccess::open(file_path, FileAccess::WRITE); //Overrides existing file if present
  427. ERR_FAIL_COND_V(!file, ERR_FILE_CANT_WRITE);
  428. // Create WAV Header
  429. file->store_string("RIFF"); //ChunkID
  430. file->store_32(sub_chunk_2_size + 36); //ChunkSize = 36 + SubChunk2Size (size of entire file minus the 8 bits for this and previous header)
  431. file->store_string("WAVE"); //Format
  432. file->store_string("fmt "); //Subchunk1ID
  433. file->store_32(16); //Subchunk1Size = 16
  434. file->store_16(format_code); //AudioFormat
  435. file->store_16(n_channels); //Number of Channels
  436. file->store_32(sample_rate); //SampleRate
  437. file->store_32(sample_rate * n_channels * byte_pr_sample); //ByteRate
  438. file->store_16(n_channels * byte_pr_sample); //BlockAlign = NumChannels * BytePrSample
  439. file->store_16(byte_pr_sample * 8); //BitsPerSample
  440. file->store_string("data"); //Subchunk2ID
  441. file->store_32(sub_chunk_2_size); //Subchunk2Size
  442. // Add data
  443. PoolVector<uint8_t> data = get_data();
  444. PoolVector<uint8_t>::Read read_data = data.read();
  445. switch (format) {
  446. case AudioStreamSample::FORMAT_8_BITS:
  447. for (unsigned int i = 0; i < data_bytes; i++) {
  448. uint8_t data_point = (read_data[i] + 128);
  449. file->store_8(data_point);
  450. }
  451. break;
  452. case AudioStreamSample::FORMAT_16_BITS:
  453. for (unsigned int i = 0; i < data_bytes / 2; i++) {
  454. uint16_t data_point = decode_uint16(&read_data[i * 2]);
  455. file->store_16(data_point);
  456. }
  457. break;
  458. case AudioStreamSample::FORMAT_IMA_ADPCM:
  459. //Unimplemented
  460. break;
  461. }
  462. file->close();
  463. return OK;
  464. }
  465. Ref<AudioStreamPlayback> AudioStreamSample::instance_playback() {
  466. Ref<AudioStreamPlaybackSample> sample;
  467. sample.instance();
  468. sample->base = Ref<AudioStreamSample>(this);
  469. return sample;
  470. }
  471. String AudioStreamSample::get_stream_name() const {
  472. return "";
  473. }
  474. void AudioStreamSample::_bind_methods() {
  475. ClassDB::bind_method(D_METHOD("set_data", "data"), &AudioStreamSample::set_data);
  476. ClassDB::bind_method(D_METHOD("get_data"), &AudioStreamSample::get_data);
  477. ClassDB::bind_method(D_METHOD("set_format", "format"), &AudioStreamSample::set_format);
  478. ClassDB::bind_method(D_METHOD("get_format"), &AudioStreamSample::get_format);
  479. ClassDB::bind_method(D_METHOD("set_loop_mode", "loop_mode"), &AudioStreamSample::set_loop_mode);
  480. ClassDB::bind_method(D_METHOD("get_loop_mode"), &AudioStreamSample::get_loop_mode);
  481. ClassDB::bind_method(D_METHOD("set_loop_begin", "loop_begin"), &AudioStreamSample::set_loop_begin);
  482. ClassDB::bind_method(D_METHOD("get_loop_begin"), &AudioStreamSample::get_loop_begin);
  483. ClassDB::bind_method(D_METHOD("set_loop_end", "loop_end"), &AudioStreamSample::set_loop_end);
  484. ClassDB::bind_method(D_METHOD("get_loop_end"), &AudioStreamSample::get_loop_end);
  485. ClassDB::bind_method(D_METHOD("set_mix_rate", "mix_rate"), &AudioStreamSample::set_mix_rate);
  486. ClassDB::bind_method(D_METHOD("get_mix_rate"), &AudioStreamSample::get_mix_rate);
  487. ClassDB::bind_method(D_METHOD("set_stereo", "stereo"), &AudioStreamSample::set_stereo);
  488. ClassDB::bind_method(D_METHOD("is_stereo"), &AudioStreamSample::is_stereo);
  489. ClassDB::bind_method(D_METHOD("save_to_wav", "path"), &AudioStreamSample::save_to_wav);
  490. ADD_PROPERTY(PropertyInfo(Variant::POOL_BYTE_ARRAY, "data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "set_data", "get_data");
  491. ADD_PROPERTY(PropertyInfo(Variant::INT, "format", PROPERTY_HINT_ENUM, "8-Bit,16-Bit,IMA-ADPCM"), "set_format", "get_format");
  492. ADD_PROPERTY(PropertyInfo(Variant::INT, "loop_mode", PROPERTY_HINT_ENUM, "Disabled,Forward,Ping-Pong,Backward"), "set_loop_mode", "get_loop_mode");
  493. ADD_PROPERTY(PropertyInfo(Variant::INT, "loop_begin"), "set_loop_begin", "get_loop_begin");
  494. ADD_PROPERTY(PropertyInfo(Variant::INT, "loop_end"), "set_loop_end", "get_loop_end");
  495. ADD_PROPERTY(PropertyInfo(Variant::INT, "mix_rate"), "set_mix_rate", "get_mix_rate");
  496. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "stereo"), "set_stereo", "is_stereo");
  497. BIND_ENUM_CONSTANT(FORMAT_8_BITS);
  498. BIND_ENUM_CONSTANT(FORMAT_16_BITS);
  499. BIND_ENUM_CONSTANT(FORMAT_IMA_ADPCM);
  500. BIND_ENUM_CONSTANT(LOOP_DISABLED);
  501. BIND_ENUM_CONSTANT(LOOP_FORWARD);
  502. BIND_ENUM_CONSTANT(LOOP_PING_PONG);
  503. BIND_ENUM_CONSTANT(LOOP_BACKWARD);
  504. }
  505. AudioStreamSample::AudioStreamSample() {
  506. format = FORMAT_8_BITS;
  507. loop_mode = LOOP_DISABLED;
  508. stereo = false;
  509. loop_begin = 0;
  510. loop_end = 0;
  511. mix_rate = 44100;
  512. data = NULL;
  513. data_bytes = 0;
  514. }
  515. AudioStreamSample::~AudioStreamSample() {
  516. if (data) {
  517. AudioServer::get_singleton()->audio_data_free(data);
  518. data = NULL;
  519. data_bytes = 0;
  520. }
  521. }