audio_stream_sample.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  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-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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. void AudioStreamPlaybackSample::start(float p_from_pos) {
  32. for (int i = 0; i < 2; i++) {
  33. ima_adpcm[i].step_index = 0;
  34. ima_adpcm[i].predictor = 0;
  35. ima_adpcm[i].loop_step_index = 0;
  36. ima_adpcm[i].loop_predictor = 0;
  37. ima_adpcm[i].last_nibble = -1;
  38. ima_adpcm[i].loop_pos = 0x7FFFFFFF;
  39. ima_adpcm[i].window_ofs = 0;
  40. }
  41. seek(p_from_pos);
  42. sign = 1;
  43. active = true;
  44. }
  45. void AudioStreamPlaybackSample::stop() {
  46. active = false;
  47. }
  48. bool AudioStreamPlaybackSample::is_playing() const {
  49. return active;
  50. }
  51. int AudioStreamPlaybackSample::get_loop_count() const {
  52. return 0;
  53. }
  54. float AudioStreamPlaybackSample::get_playback_position() const {
  55. return float(offset >> MIX_FRAC_BITS) / base->mix_rate;
  56. }
  57. void AudioStreamPlaybackSample::seek(float p_time) {
  58. if (base->format == AudioStreamSample::FORMAT_IMA_ADPCM)
  59. return; //no seeking in ima-adpcm
  60. float max = get_length();
  61. if (p_time < 0) {
  62. p_time = 0;
  63. } else if (p_time >= max) {
  64. p_time = max - 0.001;
  65. }
  66. offset = uint64_t(p_time * base->mix_rate) << MIX_FRAC_BITS;
  67. }
  68. template <class Depth, bool is_stereo, bool is_ima_adpcm>
  69. 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) {
  70. // this function will be compiled branchless by any decent compiler
  71. int32_t final, final_r, next, next_r;
  72. while (amount--) {
  73. int64_t pos = offset >> MIX_FRAC_BITS;
  74. if (is_stereo && !is_ima_adpcm)
  75. pos <<= 1;
  76. if (is_ima_adpcm) {
  77. int64_t sample_pos = pos + ima_adpcm[0].window_ofs;
  78. while (sample_pos > ima_adpcm[0].last_nibble) {
  79. static const int16_t _ima_adpcm_step_table[89] = {
  80. 7, 8, 9, 10, 11, 12, 13, 14, 16, 17,
  81. 19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
  82. 50, 55, 60, 66, 73, 80, 88, 97, 107, 118,
  83. 130, 143, 157, 173, 190, 209, 230, 253, 279, 307,
  84. 337, 371, 408, 449, 494, 544, 598, 658, 724, 796,
  85. 876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066,
  86. 2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358,
  87. 5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
  88. 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
  89. };
  90. static const int8_t _ima_adpcm_index_table[16] = {
  91. -1, -1, -1, -1, 2, 4, 6, 8,
  92. -1, -1, -1, -1, 2, 4, 6, 8
  93. };
  94. for (int i = 0; i < (is_stereo ? 2 : 1); i++) {
  95. int16_t nibble, diff, step;
  96. ima_adpcm[i].last_nibble++;
  97. const uint8_t *src_ptr = (const uint8_t *)base->data;
  98. src_ptr += AudioStreamSample::DATA_PAD;
  99. uint8_t nbb = src_ptr[(ima_adpcm[i].last_nibble >> 1) * (is_stereo ? 2 : 1) + i];
  100. nibble = (ima_adpcm[i].last_nibble & 1) ? (nbb >> 4) : (nbb & 0xF);
  101. step = _ima_adpcm_step_table[ima_adpcm[i].step_index];
  102. ima_adpcm[i].step_index += _ima_adpcm_index_table[nibble];
  103. if (ima_adpcm[i].step_index < 0)
  104. ima_adpcm[i].step_index = 0;
  105. if (ima_adpcm[i].step_index > 88)
  106. ima_adpcm[i].step_index = 88;
  107. diff = step >> 3;
  108. if (nibble & 1)
  109. diff += step >> 2;
  110. if (nibble & 2)
  111. diff += step >> 1;
  112. if (nibble & 4)
  113. diff += step;
  114. if (nibble & 8)
  115. diff = -diff;
  116. ima_adpcm[i].predictor += diff;
  117. if (ima_adpcm[i].predictor < -0x8000)
  118. ima_adpcm[i].predictor = -0x8000;
  119. else if (ima_adpcm[i].predictor > 0x7FFF)
  120. ima_adpcm[i].predictor = 0x7FFF;
  121. /* store loop if there */
  122. if (ima_adpcm[i].last_nibble == ima_adpcm[i].loop_pos) {
  123. ima_adpcm[i].loop_step_index = ima_adpcm[i].step_index;
  124. ima_adpcm[i].loop_predictor = ima_adpcm[i].predictor;
  125. }
  126. //printf("%i - %i - pred %i\n",int(ima_adpcm[i].last_nibble),int(nibble),int(ima_adpcm[i].predictor));
  127. }
  128. }
  129. final = ima_adpcm[0].predictor;
  130. if (is_stereo) {
  131. final_r = ima_adpcm[1].predictor;
  132. }
  133. } else {
  134. final = p_src[pos];
  135. if (is_stereo)
  136. final_r = p_src[pos + 1];
  137. if (sizeof(Depth) == 1) { /* conditions will not exist anymore when compiled! */
  138. final <<= 8;
  139. if (is_stereo)
  140. final_r <<= 8;
  141. }
  142. if (is_stereo) {
  143. next = p_src[pos + 2];
  144. next_r = p_src[pos + 3];
  145. } else {
  146. next = p_src[pos + 1];
  147. }
  148. if (sizeof(Depth) == 1) {
  149. next <<= 8;
  150. if (is_stereo)
  151. next_r <<= 8;
  152. }
  153. int32_t frac = int64_t(offset & MIX_FRAC_MASK);
  154. final = final + ((next - final) * frac >> MIX_FRAC_BITS);
  155. if (is_stereo)
  156. final_r = final_r + ((next_r - final_r) * frac >> MIX_FRAC_BITS);
  157. }
  158. if (!is_stereo) {
  159. final_r = final; //copy to right channel if stereo
  160. }
  161. p_dst->l = final / 32767.0;
  162. p_dst->r = final_r / 32767.0;
  163. p_dst++;
  164. offset += increment;
  165. }
  166. }
  167. void AudioStreamPlaybackSample::mix(AudioFrame *p_buffer, float p_rate_scale, int p_frames) {
  168. if (!base->data || !active) {
  169. for (int i = 0; i < p_frames; i++) {
  170. p_buffer[i] = AudioFrame(0, 0);
  171. }
  172. return;
  173. }
  174. int len = base->data_bytes;
  175. switch (base->format) {
  176. case AudioStreamSample::FORMAT_8_BITS: len /= 1; break;
  177. case AudioStreamSample::FORMAT_16_BITS: len /= 2; break;
  178. case AudioStreamSample::FORMAT_IMA_ADPCM: len *= 2; break;
  179. }
  180. if (base->stereo) {
  181. len /= 2;
  182. }
  183. /* some 64-bit fixed point precaches */
  184. int64_t loop_begin_fp = ((int64_t)base->loop_begin << MIX_FRAC_BITS);
  185. int64_t loop_end_fp = ((int64_t)base->loop_end << MIX_FRAC_BITS);
  186. int64_t length_fp = ((int64_t)len << MIX_FRAC_BITS);
  187. int64_t begin_limit = (base->loop_mode != AudioStreamSample::LOOP_DISABLED) ? loop_begin_fp : 0;
  188. int64_t end_limit = (base->loop_mode != AudioStreamSample::LOOP_DISABLED) ? loop_end_fp : length_fp;
  189. bool is_stereo = base->stereo;
  190. int32_t todo = p_frames;
  191. float base_rate = AudioServer::get_singleton()->get_mix_rate();
  192. float srate = base->mix_rate;
  193. srate *= p_rate_scale;
  194. float fincrement = srate / base_rate;
  195. int32_t increment = int32_t(fincrement * MIX_FRAC_LEN);
  196. increment *= sign;
  197. //looping
  198. AudioStreamSample::LoopMode loop_format = base->loop_mode;
  199. AudioStreamSample::Format format = base->format;
  200. /* audio data */
  201. uint8_t *dataptr = (uint8_t *)base->data;
  202. const void *data = dataptr + AudioStreamSample::DATA_PAD;
  203. AudioFrame *dst_buff = p_buffer;
  204. if (format == AudioStreamSample::FORMAT_IMA_ADPCM) {
  205. if (loop_format != AudioStreamSample::LOOP_DISABLED) {
  206. ima_adpcm[0].loop_pos = loop_begin_fp >> MIX_FRAC_BITS;
  207. ima_adpcm[1].loop_pos = loop_begin_fp >> MIX_FRAC_BITS;
  208. loop_format = AudioStreamSample::LOOP_FORWARD;
  209. }
  210. }
  211. while (todo > 0) {
  212. int64_t limit = 0;
  213. int32_t target = 0, aux = 0;
  214. /** LOOP CHECKING **/
  215. if (increment < 0) {
  216. /* going backwards */
  217. if (loop_format != AudioStreamSample::LOOP_DISABLED && offset < loop_begin_fp) {
  218. /* loopstart reached */
  219. if (loop_format == AudioStreamSample::LOOP_PING_PONG) {
  220. /* bounce ping pong */
  221. offset = loop_begin_fp + (loop_begin_fp - offset);
  222. increment = -increment;
  223. sign *= -1;
  224. } else {
  225. /* go to loop-end */
  226. offset = loop_end_fp - (loop_begin_fp - offset);
  227. }
  228. } else {
  229. /* check for sample not reaching beginning */
  230. if (offset < 0) {
  231. active = false;
  232. break;
  233. }
  234. }
  235. } else {
  236. /* going forward */
  237. if (loop_format != AudioStreamSample::LOOP_DISABLED && offset >= loop_end_fp) {
  238. /* loopend reached */
  239. if (loop_format == AudioStreamSample::LOOP_PING_PONG) {
  240. /* bounce ping pong */
  241. offset = loop_end_fp - (offset - loop_end_fp);
  242. increment = -increment;
  243. sign *= -1;
  244. } else {
  245. /* go to loop-begin */
  246. if (format == AudioStreamSample::FORMAT_IMA_ADPCM) {
  247. for (int i = 0; i < 2; i++) {
  248. ima_adpcm[i].step_index = ima_adpcm[i].loop_step_index;
  249. ima_adpcm[i].predictor = ima_adpcm[i].loop_predictor;
  250. ima_adpcm[i].last_nibble = loop_begin_fp >> MIX_FRAC_BITS;
  251. }
  252. offset = loop_begin_fp;
  253. } else {
  254. offset = loop_begin_fp + (offset - loop_end_fp);
  255. }
  256. }
  257. } else {
  258. /* no loop, check for end of sample */
  259. if (offset >= length_fp) {
  260. active = false;
  261. break;
  262. }
  263. }
  264. }
  265. /** MIXCOUNT COMPUTING **/
  266. /* next possible limit (looppoints or sample begin/end */
  267. limit = (increment < 0) ? begin_limit : end_limit;
  268. /* compute what is shorter, the todo or the limit? */
  269. aux = (limit - offset) / increment + 1;
  270. target = (aux < todo) ? aux : todo; /* mix target is the shorter buffer */
  271. /* check just in case */
  272. if (target <= 0) {
  273. active = false;
  274. break;
  275. }
  276. todo -= target;
  277. switch (base->format) {
  278. case AudioStreamSample::FORMAT_8_BITS: {
  279. if (is_stereo)
  280. do_resample<int8_t, true, false>((int8_t *)data, dst_buff, offset, increment, target, ima_adpcm);
  281. else
  282. do_resample<int8_t, false, false>((int8_t *)data, dst_buff, offset, increment, target, ima_adpcm);
  283. } break;
  284. case AudioStreamSample::FORMAT_16_BITS: {
  285. if (is_stereo)
  286. do_resample<int16_t, true, false>((int16_t *)data, dst_buff, offset, increment, target, ima_adpcm);
  287. else
  288. do_resample<int16_t, false, false>((int16_t *)data, dst_buff, offset, increment, target, ima_adpcm);
  289. } break;
  290. case AudioStreamSample::FORMAT_IMA_ADPCM: {
  291. if (is_stereo)
  292. do_resample<int8_t, true, true>((int8_t *)data, dst_buff, offset, increment, target, ima_adpcm);
  293. else
  294. do_resample<int8_t, false, true>((int8_t *)data, dst_buff, offset, increment, target, ima_adpcm);
  295. } break;
  296. }
  297. dst_buff += target;
  298. }
  299. }
  300. float AudioStreamPlaybackSample::get_length() const {
  301. int len = base->data_bytes;
  302. switch (base->format) {
  303. case AudioStreamSample::FORMAT_8_BITS: len /= 1; break;
  304. case AudioStreamSample::FORMAT_16_BITS: len /= 2; break;
  305. case AudioStreamSample::FORMAT_IMA_ADPCM: len *= 2; break;
  306. }
  307. if (base->stereo) {
  308. len /= 2;
  309. }
  310. return float(len) / base->mix_rate;
  311. }
  312. AudioStreamPlaybackSample::AudioStreamPlaybackSample() {
  313. active = false;
  314. offset = 0;
  315. sign = 1;
  316. }
  317. /////////////////////
  318. void AudioStreamSample::set_format(Format p_format) {
  319. format = p_format;
  320. }
  321. AudioStreamSample::Format AudioStreamSample::get_format() const {
  322. return format;
  323. }
  324. void AudioStreamSample::set_loop_mode(LoopMode p_loop_mode) {
  325. loop_mode = p_loop_mode;
  326. }
  327. AudioStreamSample::LoopMode AudioStreamSample::get_loop_mode() const {
  328. return loop_mode;
  329. }
  330. void AudioStreamSample::set_loop_begin(int p_frame) {
  331. loop_begin = p_frame;
  332. }
  333. int AudioStreamSample::get_loop_begin() const {
  334. return loop_begin;
  335. }
  336. void AudioStreamSample::set_loop_end(int p_frame) {
  337. loop_end = p_frame;
  338. }
  339. int AudioStreamSample::get_loop_end() const {
  340. return loop_end;
  341. }
  342. void AudioStreamSample::set_mix_rate(int p_hz) {
  343. mix_rate = p_hz;
  344. }
  345. int AudioStreamSample::get_mix_rate() const {
  346. return mix_rate;
  347. }
  348. void AudioStreamSample::set_stereo(bool p_enable) {
  349. stereo = p_enable;
  350. }
  351. bool AudioStreamSample::is_stereo() const {
  352. return stereo;
  353. }
  354. void AudioStreamSample::set_data(const PoolVector<uint8_t> &p_data) {
  355. AudioServer::get_singleton()->lock();
  356. if (data) {
  357. AudioServer::get_singleton()->audio_data_free(data);
  358. data = NULL;
  359. data_bytes = 0;
  360. }
  361. int datalen = p_data.size();
  362. if (datalen) {
  363. PoolVector<uint8_t>::Read r = p_data.read();
  364. int alloc_len = datalen + DATA_PAD * 2;
  365. data = AudioServer::get_singleton()->audio_data_alloc(alloc_len); //alloc with some padding for interpolation
  366. zeromem(data, alloc_len);
  367. uint8_t *dataptr = (uint8_t *)data;
  368. copymem(dataptr + DATA_PAD, r.ptr(), datalen);
  369. data_bytes = datalen;
  370. }
  371. AudioServer::get_singleton()->unlock();
  372. }
  373. PoolVector<uint8_t> AudioStreamSample::get_data() const {
  374. PoolVector<uint8_t> pv;
  375. if (data) {
  376. pv.resize(data_bytes);
  377. {
  378. PoolVector<uint8_t>::Write w = pv.write();
  379. uint8_t *dataptr = (uint8_t *)data;
  380. copymem(w.ptr(), dataptr + DATA_PAD, data_bytes);
  381. }
  382. }
  383. return pv;
  384. }
  385. Ref<AudioStreamPlayback> AudioStreamSample::instance_playback() {
  386. Ref<AudioStreamPlaybackSample> sample;
  387. sample.instance();
  388. sample->base = Ref<AudioStreamSample>(this);
  389. return sample;
  390. }
  391. String AudioStreamSample::get_stream_name() const {
  392. return "";
  393. }
  394. void AudioStreamSample::_bind_methods() {
  395. ClassDB::bind_method(D_METHOD("set_format", "format"), &AudioStreamSample::set_format);
  396. ClassDB::bind_method(D_METHOD("get_format"), &AudioStreamSample::get_format);
  397. ClassDB::bind_method(D_METHOD("set_loop_mode", "loop_mode"), &AudioStreamSample::set_loop_mode);
  398. ClassDB::bind_method(D_METHOD("get_loop_mode"), &AudioStreamSample::get_loop_mode);
  399. ClassDB::bind_method(D_METHOD("set_loop_begin", "loop_begin"), &AudioStreamSample::set_loop_begin);
  400. ClassDB::bind_method(D_METHOD("get_loop_begin"), &AudioStreamSample::get_loop_begin);
  401. ClassDB::bind_method(D_METHOD("set_loop_end", "loop_end"), &AudioStreamSample::set_loop_end);
  402. ClassDB::bind_method(D_METHOD("get_loop_end"), &AudioStreamSample::get_loop_end);
  403. ClassDB::bind_method(D_METHOD("set_mix_rate", "mix_rate"), &AudioStreamSample::set_mix_rate);
  404. ClassDB::bind_method(D_METHOD("get_mix_rate"), &AudioStreamSample::get_mix_rate);
  405. ClassDB::bind_method(D_METHOD("set_stereo", "stereo"), &AudioStreamSample::set_stereo);
  406. ClassDB::bind_method(D_METHOD("is_stereo"), &AudioStreamSample::is_stereo);
  407. ClassDB::bind_method(D_METHOD("set_data", "data"), &AudioStreamSample::set_data);
  408. ClassDB::bind_method(D_METHOD("get_data"), &AudioStreamSample::get_data);
  409. ADD_PROPERTY(PropertyInfo(Variant::INT, "format", PROPERTY_HINT_ENUM, "8-Bit,16-Bit,IMA-ADPCM"), "set_format", "get_format");
  410. ADD_PROPERTY(PropertyInfo(Variant::INT, "loop_mode", PROPERTY_HINT_ENUM, "Disabled,Forward,Ping-Pong"), "set_loop_mode", "get_loop_mode");
  411. ADD_PROPERTY(PropertyInfo(Variant::INT, "loop_begin"), "set_loop_begin", "get_loop_begin");
  412. ADD_PROPERTY(PropertyInfo(Variant::INT, "loop_end"), "set_loop_end", "get_loop_end");
  413. ADD_PROPERTY(PropertyInfo(Variant::INT, "mix_rate"), "set_mix_rate", "get_mix_rate");
  414. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "stereo"), "set_stereo", "is_stereo");
  415. ADD_PROPERTY(PropertyInfo(Variant::POOL_BYTE_ARRAY, "data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "set_data", "get_data");
  416. BIND_ENUM_CONSTANT(FORMAT_8_BITS);
  417. BIND_ENUM_CONSTANT(FORMAT_16_BITS);
  418. BIND_ENUM_CONSTANT(FORMAT_IMA_ADPCM);
  419. BIND_ENUM_CONSTANT(LOOP_DISABLED);
  420. BIND_ENUM_CONSTANT(LOOP_FORWARD);
  421. BIND_ENUM_CONSTANT(LOOP_PING_PONG);
  422. }
  423. AudioStreamSample::AudioStreamSample() {
  424. format = FORMAT_8_BITS;
  425. loop_mode = LOOP_DISABLED;
  426. stereo = false;
  427. loop_begin = 0;
  428. loop_end = 0;
  429. mix_rate = 44100;
  430. data = NULL;
  431. data_bytes = 0;
  432. }
  433. AudioStreamSample::~AudioStreamSample() {
  434. if (data) {
  435. AudioServer::get_singleton()->audio_data_free(data);
  436. data = NULL;
  437. data_bytes = 0;
  438. }
  439. }