event_stream_chibi.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813
  1. /*************************************************************************/
  2. /* event_stream_chibi.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 "event_stream_chibi.h"
  31. #include "cp_loader_it.h"
  32. #include "cp_loader_mod.h"
  33. #include "cp_loader_s3m.h"
  34. #include "cp_loader_xm.h"
  35. static CPSampleManagerImpl *sample_manager;
  36. static ResourceFormatLoaderChibi *resource_loader;
  37. CPSample_ID CPSampleManagerImpl::create(bool p_16bits, bool p_stereo, int32_t p_len) {
  38. AudioServer::SampleFormat sf = p_16bits ? AudioServer::SAMPLE_FORMAT_PCM16 : AudioServer::SAMPLE_FORMAT_PCM8;
  39. SampleData *sd = memnew(SampleData);
  40. sd->rid = AudioServer::get_singleton()->sample_create(sf, p_stereo, p_len);
  41. sd->stereo = p_stereo;
  42. sd->len = p_len;
  43. sd->is16 = p_16bits;
  44. sd->mixfreq = 44100;
  45. sd->loop_begin = 0;
  46. sd->loop_end = 0;
  47. sd->loop_type = CP_LOOP_NONE;
  48. sd->locks = 0;
  49. #ifdef DEBUG_ENABLED
  50. valid.insert(sd);
  51. #endif
  52. CPSample_ID sid;
  53. sid._private = sd;
  54. return sid;
  55. }
  56. void CPSampleManagerImpl::recreate(CPSample_ID p_id, bool p_16bits, bool p_stereo, int32_t p_len) {
  57. AudioServer::SampleFormat sf = p_16bits ? AudioServer::SAMPLE_FORMAT_PCM16 : AudioServer::SAMPLE_FORMAT_PCM8;
  58. SampleData *sd = _getsd(p_id);
  59. #ifdef DEBUG_ENABLED
  60. ERR_FAIL_COND(!valid.has(sd));
  61. #endif
  62. AudioServer::get_singleton()->free(sd->rid);
  63. sd->rid = AudioServer::get_singleton()->sample_create(sf, p_stereo, p_len);
  64. sd->stereo = p_stereo;
  65. sd->len = p_len;
  66. sd->is16 = p_16bits;
  67. sd->mixfreq = 44100;
  68. sd->loop_begin = 0;
  69. sd->loop_end = 0;
  70. sd->loop_type = CP_LOOP_NONE;
  71. }
  72. void CPSampleManagerImpl::destroy(CPSample_ID p_id) {
  73. SampleData *sd = _getsd(p_id);
  74. #ifdef DEBUG_ENABLED
  75. ERR_FAIL_COND(!valid.has(sd));
  76. valid.erase(sd);
  77. #endif
  78. AudioServer::get_singleton()->free(sd->rid);
  79. memdelete(sd);
  80. }
  81. bool CPSampleManagerImpl::check(CPSample_ID p_id) {
  82. SampleData *sd = _getsd(p_id);
  83. #ifdef DEBUG_ENABLED
  84. return valid.has(sd);
  85. #else
  86. return _getsd(p_id) != NULL;
  87. #endif
  88. }
  89. void CPSampleManagerImpl::set_c5_freq(CPSample_ID p_id, int32_t p_freq) {
  90. SampleData *sd = _getsd(p_id);
  91. #ifdef DEBUG_ENABLED
  92. ERR_FAIL_COND(!valid.has(sd));
  93. #endif
  94. sd->mixfreq = p_freq;
  95. AudioServer::get_singleton()->sample_set_mix_rate(sd->rid, p_freq);
  96. }
  97. void CPSampleManagerImpl::set_loop_begin(CPSample_ID p_id, int32_t p_begin) {
  98. SampleData *sd = _getsd(p_id);
  99. #ifdef DEBUG_ENABLED
  100. ERR_FAIL_COND(!valid.has(sd));
  101. #endif
  102. sd->loop_begin = p_begin;
  103. AudioServer::get_singleton()->sample_set_loop_begin(sd->rid, p_begin);
  104. }
  105. void CPSampleManagerImpl::set_loop_end(CPSample_ID p_id, int32_t p_end) {
  106. SampleData *sd = _getsd(p_id);
  107. #ifdef DEBUG_ENABLED
  108. ERR_FAIL_COND(!valid.has(sd));
  109. #endif
  110. sd->loop_end = p_end;
  111. AudioServer::get_singleton()->sample_set_loop_end(sd->rid, p_end);
  112. }
  113. void CPSampleManagerImpl::set_loop_type(CPSample_ID p_id, CPSample_Loop_Type p_type) {
  114. SampleData *sd = _getsd(p_id);
  115. #ifdef DEBUG_ENABLED
  116. ERR_FAIL_COND(!valid.has(sd));
  117. #endif
  118. sd->loop_type = p_type;
  119. AudioServer::get_singleton()->sample_set_loop_format(sd->rid, AudioServer::SampleLoopFormat(p_type));
  120. }
  121. void CPSampleManagerImpl::set_chunk(CPSample_ID p_id, int32_t p_index, void *p_data, int p_data_len) {
  122. SampleData *sd = _getsd(p_id);
  123. #ifdef DEBUG_ENABLED
  124. ERR_FAIL_COND(!valid.has(sd));
  125. #endif
  126. ERR_FAIL();
  127. }
  128. int32_t CPSampleManagerImpl::get_loop_begin(CPSample_ID p_id) {
  129. SampleData *sd = _getsd(p_id);
  130. #ifdef DEBUG_ENABLED
  131. ERR_FAIL_COND_V(!valid.has(sd), 0);
  132. #endif
  133. return sd->loop_begin;
  134. }
  135. int32_t CPSampleManagerImpl::get_loop_end(CPSample_ID p_id) {
  136. SampleData *sd = _getsd(p_id);
  137. #ifdef DEBUG_ENABLED
  138. ERR_FAIL_COND_V(!valid.has(sd), 0);
  139. #endif
  140. return sd->loop_end;
  141. }
  142. CPSample_Loop_Type CPSampleManagerImpl::get_loop_type(CPSample_ID p_id) {
  143. SampleData *sd = _getsd(p_id);
  144. #ifdef DEBUG_ENABLED
  145. ERR_FAIL_COND_V(!valid.has(sd), CP_LOOP_NONE);
  146. #endif
  147. return sd->loop_type;
  148. }
  149. int32_t CPSampleManagerImpl::get_c5_freq(CPSample_ID p_id) {
  150. SampleData *sd = _getsd(p_id);
  151. #ifdef DEBUG_ENABLED
  152. ERR_FAIL_COND_V(!valid.has(sd), 0);
  153. #endif
  154. return sd->mixfreq;
  155. }
  156. int32_t CPSampleManagerImpl::get_size(CPSample_ID p_id) {
  157. SampleData *sd = _getsd(p_id);
  158. #ifdef DEBUG_ENABLED
  159. ERR_FAIL_COND_V(!valid.has(sd), 0);
  160. #endif
  161. return sd->len;
  162. }
  163. bool CPSampleManagerImpl::is_16bits(CPSample_ID p_id) {
  164. SampleData *sd = _getsd(p_id);
  165. #ifdef DEBUG_ENABLED
  166. ERR_FAIL_COND_V(!valid.has(sd), false);
  167. #endif
  168. return sd->is16;
  169. }
  170. bool CPSampleManagerImpl::is_stereo(CPSample_ID p_id) {
  171. SampleData *sd = _getsd(p_id);
  172. #ifdef DEBUG_ENABLED
  173. ERR_FAIL_COND_V(!valid.has(sd), false);
  174. #endif
  175. return sd->stereo;
  176. }
  177. bool CPSampleManagerImpl::lock_data(CPSample_ID p_id) {
  178. SampleData *sd = _getsd(p_id);
  179. #ifdef DEBUG_ENABLED
  180. ERR_FAIL_COND_V(!valid.has(sd), 0);
  181. #endif
  182. sd->locks++;
  183. if (sd->locks == 1) {
  184. sd->lock = AudioServer::get_singleton()->sample_get_data(sd->rid);
  185. sd->w = sd->lock.write();
  186. }
  187. return true;
  188. }
  189. void *CPSampleManagerImpl::get_data(CPSample_ID p_id) {
  190. SampleData *sd = _getsd(p_id);
  191. #ifdef DEBUG_ENABLED
  192. ERR_FAIL_COND_V(!valid.has(sd), 0);
  193. #endif
  194. ERR_FAIL_COND_V(sd->locks == 0, 0);
  195. return sd->w.ptr();
  196. }
  197. int16_t CPSampleManagerImpl::get_data(CPSample_ID p_id, int p_sample, int p_channel) {
  198. SampleData *sd = _getsd(p_id);
  199. #ifdef DEBUG_ENABLED
  200. ERR_FAIL_COND_V(!valid.has(sd), 0);
  201. #endif
  202. ERR_FAIL_V(0);
  203. lock_data(p_id);
  204. int sofs = sd->stereo ? 2 : 1;
  205. uint16_t v = 0;
  206. if (sd->is16) {
  207. int16_t *p = (int16_t *)sd->w.ptr();
  208. v = p[p_sample * sofs + p_channel];
  209. } else {
  210. int8_t *p = (int8_t *)sd->w.ptr();
  211. v = p[p_sample * sofs + p_channel];
  212. }
  213. unlock_data(p_id);
  214. return v;
  215. }
  216. void CPSampleManagerImpl::set_data(CPSample_ID p_id, int p_sample, int16_t p_data, int p_channel) {
  217. SampleData *sd = _getsd(p_id);
  218. #ifdef DEBUG_ENABLED
  219. ERR_FAIL_COND(!valid.has(sd));
  220. #endif
  221. ERR_FAIL();
  222. lock_data(p_id);
  223. int sofs = sd->stereo ? 2 : 1;
  224. if (sd->is16) {
  225. int16_t *p = (int16_t *)sd->w.ptr();
  226. p[p_sample * sofs + p_channel] = p_data;
  227. } else {
  228. int8_t *p = (int8_t *)sd->w.ptr();
  229. p[p_sample * sofs + p_channel] = p_data;
  230. }
  231. unlock_data(p_id);
  232. }
  233. void CPSampleManagerImpl::unlock_data(CPSample_ID p_id) {
  234. SampleData *sd = _getsd(p_id);
  235. #ifdef DEBUG_ENABLED
  236. ERR_FAIL_COND(!valid.has(sd));
  237. #endif
  238. ERR_FAIL_COND(sd->locks == 0);
  239. sd->locks--;
  240. if (sd->locks == 0) {
  241. sd->w = DVector<uint8_t>::Write();
  242. AudioServer::get_singleton()->sample_set_data(sd->rid, sd->lock);
  243. sd->lock = DVector<uint8_t>();
  244. }
  245. }
  246. void CPSampleManagerImpl::get_chunk(CPSample_ID p_id, int32_t p_index, void *p_data, int p_data_len) {
  247. SampleData *sd = _getsd(p_id);
  248. #ifdef DEBUG_ENABLED
  249. ERR_FAIL_COND(!valid.has(sd));
  250. #endif
  251. ERR_FAIL();
  252. }
  253. /** MIXER **/
  254. void CPMixerImpl::set_callback_interval(int p_interval_us) {
  255. callback_interval = p_interval_us;
  256. }
  257. void CPMixerImpl::set_callback(void (*p_callback)(void *), void *p_userdata) {
  258. callback = p_callback;
  259. userdata = p_userdata;
  260. }
  261. void CPMixerImpl::setup_voice(int p_voice_index, CPSample_ID p_sample_id, int32_t p_start_index) {
  262. Voice &v = voices[p_voice_index];
  263. if (v.channel != AudioMixer::INVALID_CHANNEL) {
  264. mixer->channel_free(v.channel);
  265. }
  266. v.channel = mixer->channel_alloc(sample_manager->get_rid(p_sample_id));
  267. v.freq_mult = sample_manager->get_c5_freq(p_sample_id) / 261.6255653006;
  268. v.sample = p_sample_id;
  269. }
  270. void CPMixerImpl::stop_voice(int p_voice_index) {
  271. Voice &v = voices[p_voice_index];
  272. if (v.channel == AudioMixer::INVALID_CHANNEL)
  273. return;
  274. mixer->channel_free(v.channel);
  275. v.channel = AudioMixer::INVALID_CHANNEL;
  276. }
  277. void CPMixerImpl::set_voice_frequency(int p_voice_index, int32_t p_freq) {
  278. Voice &v = voices[p_voice_index];
  279. ERR_FAIL_COND(v.channel == AudioMixer::INVALID_CHANNEL);
  280. float f = p_freq / 256.0;
  281. f *= pitch_scale;
  282. mixer->channel_set_mix_rate(v.channel, f * v.freq_mult);
  283. }
  284. void CPMixerImpl::set_voice_panning(int p_voice_index, int p_pan) {
  285. Voice &v = voices[p_voice_index];
  286. ERR_FAIL_COND(v.channel == AudioMixer::INVALID_CHANNEL);
  287. if (p_pan == CP_PAN_SURROUND)
  288. p_pan = CP_PAN_CENTER;
  289. float p = p_pan / 256.0;
  290. mixer->channel_set_pan(v.channel, p);
  291. }
  292. void CPMixerImpl::set_voice_volume(int p_voice_index, int p_vol) {
  293. Voice &v = voices[p_voice_index];
  294. ERR_FAIL_COND(v.channel == AudioMixer::INVALID_CHANNEL);
  295. float vol = p_vol / 512.0;
  296. vol *= voice_scale;
  297. mixer->channel_set_volume(v.channel, vol);
  298. mixer->channel_set_reverb(v.channel, reverb_type, vol * v.reverb);
  299. }
  300. void CPMixerImpl::set_voice_filter(int p_voice_index, bool p_enabled, uint8_t p_cutoff, uint8_t p_resonance) {
  301. Voice &v = voices[p_voice_index];
  302. ERR_FAIL_COND(v.channel == AudioMixer::INVALID_CHANNEL);
  303. }
  304. void CPMixerImpl::set_voice_reverb_send(int p_voice_index, int p_reverb) {
  305. Voice &v = voices[p_voice_index];
  306. ERR_FAIL_COND(v.channel == AudioMixer::INVALID_CHANNEL);
  307. v.reverb = p_reverb / 255.0;
  308. //mixer->channel_set_reverb(v.channel,reverb_type,p_reverb/255.0);
  309. }
  310. void CPMixerImpl::set_voice_chorus_send(int p_voice_index, int p_chorus) {
  311. Voice &v = voices[p_voice_index];
  312. ERR_FAIL_COND(v.channel == AudioMixer::INVALID_CHANNEL);
  313. mixer->channel_set_chorus(v.channel, p_chorus / 255.0);
  314. }
  315. void CPMixerImpl::set_reverb_mode(ReverbMode p_mode) {
  316. // Voice &v=voices[p_voice_index];
  317. // ERR_FAIL_COND(v.channel==AudioMixer::INVALID_CHANNEL);
  318. switch (p_mode) {
  319. case CPMixer::REVERB_MODE_STUDIO_SMALL: reverb_type = AudioMixer::REVERB_SMALL; break;
  320. case CPMixer::REVERB_MODE_STUDIO_MEDIUM: reverb_type = AudioMixer::REVERB_MEDIUM; break;
  321. case CPMixer::REVERB_MODE_STUDIO_LARGE: reverb_type = AudioMixer::REVERB_LARGE; break;
  322. case CPMixer::REVERB_MODE_HALL: reverb_type = AudioMixer::REVERB_HALL; break;
  323. default: reverb_type = AudioMixer::REVERB_SMALL; break;
  324. }
  325. }
  326. void CPMixerImpl::set_chorus_params(unsigned int p_delay_ms, unsigned int p_separation_ms, unsigned int p_depth_ms10, unsigned int p_speed_hz10) {
  327. // Voice &v=voices[p_voice_index];
  328. // ERR_FAIL_COND(v.channel==AudioMixer::INVALID_CHANNEL);
  329. }
  330. /* Info retrieving */
  331. int32_t CPMixerImpl::get_voice_sample_pos_index(int p_voice_index) {
  332. Voice &v = voices[p_voice_index];
  333. ERR_FAIL_COND_V(v.channel == AudioMixer::INVALID_CHANNEL, 0);
  334. return 0;
  335. }
  336. int CPMixerImpl::get_voice_panning(int p_voice_index) {
  337. Voice &v = voices[p_voice_index];
  338. ERR_FAIL_COND_V(!is_voice_active(p_voice_index), 0);
  339. return mixer->channel_get_pan(v.channel) * CP_PAN_RIGHT;
  340. }
  341. int CPMixerImpl::get_voice_volume(int p_voice_index) {
  342. Voice &v = voices[p_voice_index];
  343. ERR_FAIL_COND_V(!is_voice_active(p_voice_index), 0);
  344. return mixer->channel_get_volume(v.channel);
  345. }
  346. CPSample_ID CPMixerImpl::get_voice_sample_id(int p_voice_index) {
  347. Voice &v = voices[p_voice_index];
  348. ERR_FAIL_COND_V(v.channel == AudioMixer::INVALID_CHANNEL, CPSample_ID());
  349. return v.sample;
  350. }
  351. bool CPMixerImpl::is_voice_active(int p_voice_index) {
  352. Voice &v = voices[p_voice_index];
  353. if (v.channel == AudioMixer::INVALID_CHANNEL)
  354. return false;
  355. if (!mixer->channel_is_valid(v.channel))
  356. v.channel = AudioMixer::INVALID_CHANNEL;
  357. return v.channel != AudioMixer::INVALID_CHANNEL;
  358. }
  359. void CPMixerImpl::process_usecs(int p_usec, float p_volume, float p_pitch_scale, float p_tempo_scale) {
  360. ERR_FAIL_COND(callback_interval == 0);
  361. //update this somewhere
  362. pitch_scale = p_pitch_scale;
  363. tempo_scale = p_tempo_scale;
  364. voice_scale = AudioServer::get_singleton()->get_event_voice_global_volume_scale() * p_volume;
  365. while (p_usec) {
  366. if (p_usec >= callback_timeout) {
  367. p_usec -= callback_timeout;
  368. callback_timeout = 0;
  369. if (callback) {
  370. callback(userdata);
  371. }
  372. callback_timeout = callback_interval * (1.0 / p_tempo_scale);
  373. } else {
  374. callback_timeout -= p_usec;
  375. p_usec = 0;
  376. }
  377. }
  378. }
  379. CPMixerImpl::CPMixerImpl(AudioMixer *p_mixer) {
  380. callback_interval = 1;
  381. callback_timeout = 0;
  382. userdata = 0;
  383. callback = 0;
  384. tempo_scale = 1.0;
  385. pitch_scale = 1.0;
  386. mixer = p_mixer;
  387. voice_scale = AudioServer::get_singleton()->get_event_voice_global_volume_scale();
  388. reverb_type = AudioMixer::REVERB_SMALL;
  389. }
  390. /** FILE ACCESS WRAPPER **/
  391. CPFileAccessWrapperImpl::Error CPFileAccessWrapperImpl::open(const char *p_filename, int p_mode_flags) {
  392. ERR_FAIL_COND_V(p_mode_flags & WRITE, ERROR_WRITING_FILE);
  393. close();
  394. f = FileAccess::open(String::utf8(p_filename), p_mode_flags);
  395. if (!f)
  396. return ERROR_FILE_NOT_FOUND;
  397. return OK;
  398. }
  399. void CPFileAccessWrapperImpl::close() {
  400. if (f)
  401. memdelete(f);
  402. f = NULL;
  403. }
  404. void CPFileAccessWrapperImpl::seek(uint32_t p_position) {
  405. f->seek(p_position);
  406. }
  407. void CPFileAccessWrapperImpl::seek_end() {
  408. f->seek_end();
  409. }
  410. uint32_t CPFileAccessWrapperImpl::get_pos() {
  411. return f->get_pos();
  412. }
  413. bool CPFileAccessWrapperImpl::eof_reached() {
  414. return f->eof_reached();
  415. }
  416. uint8_t CPFileAccessWrapperImpl::get_byte() {
  417. return f->get_8();
  418. }
  419. void CPFileAccessWrapperImpl::get_byte_array(uint8_t *p_dest, int p_elements) {
  420. f->get_buffer(p_dest, p_elements);
  421. }
  422. void CPFileAccessWrapperImpl::get_word_array(uint16_t *p_dest, int p_elements) {
  423. for (int i = 0; i < p_elements; i++) {
  424. p_dest[i] = f->get_16();
  425. }
  426. }
  427. uint16_t CPFileAccessWrapperImpl::get_word() {
  428. return f->get_16();
  429. }
  430. uint32_t CPFileAccessWrapperImpl::get_dword() {
  431. return f->get_32();
  432. }
  433. void CPFileAccessWrapperImpl::set_endian_conversion(bool p_swap) {
  434. f->set_endian_swap(p_swap);
  435. }
  436. bool CPFileAccessWrapperImpl::is_open() {
  437. return f != NULL;
  438. }
  439. CPFileAccessWrapperImpl::Error CPFileAccessWrapperImpl::get_error() {
  440. return (f->get_error() != ::OK) ? ERROR_READING_FILE : OK;
  441. }
  442. void CPFileAccessWrapperImpl::store_byte(uint8_t p_dest) {
  443. }
  444. void CPFileAccessWrapperImpl::store_byte_array(const uint8_t *p_dest, int p_elements) {
  445. }
  446. void CPFileAccessWrapperImpl::store_word(uint16_t p_dest) {
  447. }
  448. void CPFileAccessWrapperImpl::store_dword(uint32_t p_dest) {
  449. }
  450. ////////////////////////////////////////////////
  451. Error EventStreamPlaybackChibi::_play() {
  452. last_order = 0;
  453. loops = 0;
  454. player->play_start_song();
  455. total_usec = 0;
  456. return OK;
  457. }
  458. bool EventStreamPlaybackChibi::_update(AudioMixer *p_mixer, uint64_t p_usec) {
  459. total_usec += p_usec;
  460. mixer.process_usecs(p_usec, volume, pitch_scale, tempo_scale);
  461. int order = player->get_current_order();
  462. if (order < last_order) {
  463. if (!loop) {
  464. stop();
  465. } else {
  466. loops++;
  467. }
  468. }
  469. last_order = order;
  470. return false;
  471. }
  472. void EventStreamPlaybackChibi::_stop() {
  473. player->play_stop();
  474. }
  475. void EventStreamPlaybackChibi::set_paused(bool p_paused) {
  476. }
  477. bool EventStreamPlaybackChibi::is_paused() const {
  478. return false;
  479. }
  480. void EventStreamPlaybackChibi::set_loop(bool p_loop) {
  481. loop = p_loop;
  482. }
  483. bool EventStreamPlaybackChibi::is_loop_enabled() const {
  484. return loop;
  485. }
  486. int EventStreamPlaybackChibi::get_loop_count() const {
  487. //return player->is
  488. return loops;
  489. }
  490. float EventStreamPlaybackChibi::get_pos() const {
  491. return double(total_usec) / 1000000.0;
  492. }
  493. void EventStreamPlaybackChibi::seek_pos(float p_time) {
  494. WARN_PRINT("seek_pos unimplemented.");
  495. }
  496. void EventStreamPlaybackChibi::set_volume(float p_volume) {
  497. volume = p_volume;
  498. }
  499. float EventStreamPlaybackChibi::get_volume() const {
  500. return volume;
  501. }
  502. void EventStreamPlaybackChibi::set_pitch_scale(float p_pitch_scale) {
  503. pitch_scale = p_pitch_scale;
  504. }
  505. float EventStreamPlaybackChibi::get_pitch_scale() const {
  506. return pitch_scale;
  507. }
  508. void EventStreamPlaybackChibi::set_tempo_scale(float p_tempo_scale) {
  509. tempo_scale = p_tempo_scale;
  510. }
  511. float EventStreamPlaybackChibi::get_tempo_scale() const {
  512. return tempo_scale;
  513. }
  514. void EventStreamPlaybackChibi::set_channel_volume(int p_channel, float p_volume) {
  515. if (p_channel >= 64)
  516. return;
  517. player->set_channel_global_volume(p_channel, p_volume * 256);
  518. }
  519. float EventStreamPlaybackChibi::get_channel_volume(int p_channel) const {
  520. return player->get_channel_global_volume(p_channel) / 256.0;
  521. }
  522. float EventStreamPlaybackChibi::get_last_note_time(int p_channel) const {
  523. double v = (player->get_channel_last_note_time_usec(p_channel)) / 1000000.0;
  524. if (v < 0)
  525. v = -1;
  526. return v;
  527. }
  528. EventStreamPlaybackChibi::EventStreamPlaybackChibi(Ref<EventStreamChibi> p_stream) :
  529. mixer(_get_mixer()) {
  530. stream = p_stream;
  531. player = memnew(CPPlayer(&mixer, &p_stream->song));
  532. loop = false;
  533. last_order = 0;
  534. loops = 0;
  535. volume = 1.0;
  536. pitch_scale = 1.0;
  537. tempo_scale = 1.0;
  538. }
  539. EventStreamPlaybackChibi::~EventStreamPlaybackChibi() {
  540. player->play_stop();
  541. memdelete(player);
  542. }
  543. ////////////////////////////////////////////////////
  544. Ref<EventStreamPlayback> EventStreamChibi::instance_playback() {
  545. return Ref<EventStreamPlayback>(memnew(EventStreamPlaybackChibi(Ref<EventStreamChibi>(this))));
  546. }
  547. String EventStreamChibi::get_stream_name() const {
  548. return song.get_name();
  549. }
  550. float EventStreamChibi::get_length() const {
  551. return 1;
  552. }
  553. EventStreamChibi::EventStreamChibi() {
  554. }
  555. //////////////////////////////////////////////////////////////////
  556. RES ResourceFormatLoaderChibi::load(const String &p_path, const String &p_original_path, Error *r_error) {
  557. if (r_error)
  558. *r_error = ERR_FILE_CANT_OPEN;
  559. String el = p_path.extension().to_lower();
  560. CPFileAccessWrapperImpl f;
  561. if (el == "it") {
  562. Ref<EventStreamChibi> esc(memnew(EventStreamChibi));
  563. CPLoader_IT loader(&f);
  564. CPLoader::Error err = loader.load_song(p_path.utf8().get_data(), &esc->song, false);
  565. ERR_FAIL_COND_V(err != CPLoader::FILE_OK, RES());
  566. if (r_error)
  567. *r_error = OK;
  568. return esc;
  569. } else if (el == "xm") {
  570. Ref<EventStreamChibi> esc(memnew(EventStreamChibi));
  571. CPLoader_XM loader(&f);
  572. CPLoader::Error err = loader.load_song(p_path.utf8().get_data(), &esc->song, false);
  573. ERR_FAIL_COND_V(err != CPLoader::FILE_OK, RES());
  574. if (r_error)
  575. *r_error = OK;
  576. return esc;
  577. } else if (el == "s3m") {
  578. Ref<EventStreamChibi> esc(memnew(EventStreamChibi));
  579. CPLoader_S3M loader(&f);
  580. CPLoader::Error err = loader.load_song(p_path.utf8().get_data(), &esc->song, false);
  581. ERR_FAIL_COND_V(err != CPLoader::FILE_OK, RES());
  582. if (r_error)
  583. *r_error = OK;
  584. return esc;
  585. } else if (el == "mod") {
  586. Ref<EventStreamChibi> esc(memnew(EventStreamChibi));
  587. CPLoader_MOD loader(&f);
  588. CPLoader::Error err = loader.load_song(p_path.utf8().get_data(), &esc->song, false);
  589. ERR_FAIL_COND_V(err != CPLoader::FILE_OK, RES());
  590. if (r_error)
  591. *r_error = OK;
  592. return esc;
  593. }
  594. return RES();
  595. }
  596. void ResourceFormatLoaderChibi::get_recognized_extensions(List<String> *p_extensions) const {
  597. p_extensions->push_back("it");
  598. p_extensions->push_back("xm");
  599. p_extensions->push_back("s3m");
  600. p_extensions->push_back("mod");
  601. }
  602. bool ResourceFormatLoaderChibi::handles_type(const String &p_type) const {
  603. return (p_type == "EventStreamChibi" || p_type == "EventStream");
  604. }
  605. String ResourceFormatLoaderChibi::get_resource_type(const String &p_path) const {
  606. String el = p_path.extension().to_lower();
  607. if (el == "it" || el == "s3m" || el == "xm" || el == "mod")
  608. return "EventStreamChibi";
  609. return "";
  610. }
  611. /////////////////////////////////////////////////////////////////
  612. void initialize_chibi() {
  613. sample_manager = memnew(CPSampleManagerImpl);
  614. resource_loader = memnew(ResourceFormatLoaderChibi);
  615. ObjectTypeDB::register_type<EventStreamChibi>();
  616. ResourceLoader::add_resource_format_loader(resource_loader);
  617. }
  618. void finalize_chibi() {
  619. memdelete(sample_manager);
  620. memdelete(resource_loader);
  621. }