audio_stream_sample.cpp 19 KB

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