cp_player_data_events.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  1. /*************************************************************************/
  2. /* cp_player_data_events.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 "cp_player_data.h"
  31. #include "cp_sample_manager.h"
  32. #include "stdio.h"
  33. /*
  34. setup_voices():
  35. This will go throught all the REAL channels, if it finds a channel
  36. that needs to be restarted or assigned a new VIRTUAL channel, then it
  37. will just find one and do it.
  38. */
  39. #define C5FREQ 261.6255653006
  40. static const int32_t C5FREQ_MIXER = ((int32_t)(C5FREQ * (float)(1 << CPMixer::FREQUENCY_BITS)));
  41. void CPPlayer::setup_voices() {
  42. int i, voice_index;
  43. for (i = 0; i < CPPattern::WIDTH; i++) {
  44. voice_index = -1;
  45. if (control.channel[i].note_delay) continue;
  46. // check if we need a new empty voice
  47. if (control.channel[i].kick == KICK_NOTE) {
  48. /* if no channel was cut above, find an empty or quiet channel
  49. here */
  50. if (song->has_instruments() && !control.force_no_nna) {
  51. if (control.channel[i].slave_voice == NULL) { // no slave??
  52. int newchn;
  53. if ((newchn = find_empty_voice()) != -1) {
  54. control.channel[i].slave_voice_index = newchn;
  55. control.channel[i].slave_voice = &voice[newchn];
  56. }
  57. }
  58. } else {
  59. if (i < control.max_voices) {
  60. control.channel[i].slave_voice_index = i;
  61. control.channel[i].slave_voice = &voice[i];
  62. } else {
  63. //This is a _DIRTY_ hack, but i cant think a better way.
  64. control.channel[i].slave_voice_index = control.max_voices - 1;
  65. control.channel[i].slave_voice = &voice[control.max_voices - 1];
  66. }
  67. }
  68. /* assign parts of MP_VOICE only done for a KICK_NOTE */
  69. if ((control.channel[i].slave_voice != NULL)) {
  70. voice_index = control.channel[i].slave_voice_index;
  71. Voice_Control &v = voice[voice_index];
  72. if (v.has_master_channel && (v.master_channel != NULL)) {
  73. // If this voice already has a master channel, make sure to remove the reference to it.
  74. v.master_channel->slave_voice = NULL;
  75. }
  76. //notify the voice that the current channel is the master
  77. v.master_channel = &control.channel[i];
  78. //set the voice as slave of the current channel
  79. control.channel[i].slave_voice = &v;
  80. //master channel index of the voice
  81. v.master_channel_index = i;
  82. v.has_master_channel = true;
  83. }
  84. } else {
  85. // nope..
  86. // so if we DO have a slave voice then use it.
  87. if (control.channel[i].slave_voice != NULL) {
  88. voice_index = control.channel[i].slave_voice_index;
  89. }
  90. }
  91. //assuming this channel has a slave voice..
  92. if (voice_index >= 0) {
  93. // IMPROVE: Code a method for this:
  94. voice[voice_index].update_info_from_master_channel();
  95. }
  96. control.channel[i].kick = KICK_NOTHING;
  97. }
  98. }
  99. void CPPlayer::Voice_Control::reset() {
  100. cp_memzero(this, sizeof(*this));
  101. instrument_ptr = NULL;
  102. sample_ptr = NULL;
  103. has_master_channel = false;
  104. instrument_index = -1;
  105. reverb_send = 0;
  106. chorus_send = 0;
  107. filter.it_cutoff = 255;
  108. filter.it_reso = 0;
  109. display_volume = 0;
  110. }
  111. void CPPlayer::Channel_Control::reset() {
  112. int prev_gv = channel_global_volume;
  113. cp_memzero(this, sizeof(*this));
  114. slave_voice = NULL;
  115. slave_voice_index = 255;
  116. mute = false;
  117. old_note = 255;
  118. real_note = 255;
  119. instrument_index = 255;
  120. filter.it_cutoff = 255;
  121. filter.it_reso = 0;
  122. reverb_send = 0;
  123. chorus_send = 0;
  124. reserved = false;
  125. carry.maybe = false;
  126. last_event_usecs = -1;
  127. channel_global_volume = prev_gv;
  128. }
  129. void CPPlayer::Voice_Control::update_info_from_master_channel() {
  130. instrument_ptr = master_channel->instrument_ptr;
  131. sample_ptr = master_channel->sample_ptr;
  132. instrument_index = master_channel->instrument_index;
  133. sample_index = master_channel->sample_index;
  134. note = master_channel->note;
  135. output_volume = master_channel->output_volume;
  136. channel_volume = master_channel->channel_volume;
  137. panning = master_channel->panning;
  138. kick = master_channel->kick;
  139. note_end_flags = master_channel->note_end_flags;
  140. period = master_channel->period;
  141. volume_envelope_ctrl.active = master_channel->volume_envelope_on;
  142. panning_envelope_ctrl.active = master_channel->panning_envelope_on;
  143. pitch_envelope_ctrl.active = master_channel->pitch_envelope_on;
  144. NNA_type = master_channel->NNA_type;
  145. reverb_send = master_channel->reverb_send;
  146. chorus_send = master_channel->chorus_send;
  147. // last_note_type=master_channel->last_note_type;
  148. sample_start_index = master_channel->sample_start_index;
  149. filter = master_channel->filter;
  150. }
  151. void CPPlayer::update_mixer() {
  152. int tmp_mixer_period;
  153. int32_t tmp_vibrato_value, tmp_vibrato_depth, tmp_volenv_value;
  154. uint64_t tmpvol; // 64bits should be the only way to avoid getting notes raped out
  155. int i;
  156. control.voices_used = 0;
  157. for (i = 0; i < control.max_voices; i++) {
  158. int filter_env = -1;
  159. Voice_Control &v = voice[i];
  160. if (!((v.kick == KICK_NOTE) || (v.kick == KICK_NOTEOFF)) && !is_voice_active(i))
  161. continue;
  162. //if voice doesnt have a sample set or size is 0.. forget it
  163. if (v.sample_ptr == NULL) continue;
  164. //TODO set limits somewhere else
  165. if (v.period < 40) {
  166. v.period = 40;
  167. } else if (v.period > 50000) {
  168. v.period = 50000;
  169. }
  170. if ((v.kick == KICK_NOTE) || (v.kick == KICK_NOTEOFF)) {
  171. int real_start_index;
  172. if (v.sample_start_index == -1) {
  173. real_start_index = 0;
  174. } else {
  175. real_start_index = v.sample_start_index;
  176. }
  177. mixer->setup_voice(i, v.sample_ptr->get_sample_data(), real_start_index);
  178. v.fadeout_volume = 1024; //IT Docs it is 1024 internally
  179. v.auto_vibrato_sweep_pos = 0;
  180. }
  181. /* Start Envelopes */
  182. if (song->has_instruments() && ((v.kick == KICK_NOTE) || (v.kick == KICK_ENVELOPE))) {
  183. // Voice_Control *carry=0;
  184. if (v.has_master_channel && v.master_channel->carry.maybe) {
  185. v.start_envelope(v.instrument_ptr->get_volume_envelope(), &v.volume_envelope_ctrl, &v.master_channel->carry.vol);
  186. v.start_envelope(v.instrument_ptr->get_pan_envelope(), &v.panning_envelope_ctrl, &v.master_channel->carry.pan);
  187. v.start_envelope(v.instrument_ptr->get_pitch_filter_envelope(), &v.pitch_envelope_ctrl, &v.master_channel->carry.pitch);
  188. } else {
  189. v.start_envelope(v.instrument_ptr->get_volume_envelope(), &v.volume_envelope_ctrl, NULL);
  190. v.start_envelope(v.instrument_ptr->get_pan_envelope(), &v.panning_envelope_ctrl, NULL);
  191. v.start_envelope(v.instrument_ptr->get_pitch_filter_envelope(), &v.pitch_envelope_ctrl, NULL);
  192. }
  193. }
  194. v.kick = KICK_NOTHING;
  195. if (song->has_instruments()) {
  196. if (!v.process_envelope(v.instrument_ptr->get_volume_envelope(), &v.volume_envelope_ctrl))
  197. v.volume_envelope_ctrl.value = 64;
  198. if (!v.process_envelope(v.instrument_ptr->get_pan_envelope(), &v.panning_envelope_ctrl))
  199. v.panning_envelope_ctrl.value = 0;
  200. if (!v.process_envelope(v.instrument_ptr->get_pitch_filter_envelope(), &v.pitch_envelope_ctrl))
  201. v.pitch_envelope_ctrl.value = 0;
  202. if (v.volume_envelope_ctrl.terminated) {
  203. if (v.volume_envelope_ctrl.kill) {
  204. v.fadeout_volume = 0;
  205. } else {
  206. v.note_end_flags |= END_NOTE_FADE;
  207. }
  208. }
  209. }
  210. if (song->has_instruments()) {
  211. tmp_volenv_value = v.volume_envelope_ctrl.value;
  212. } else {
  213. tmp_volenv_value = 64;
  214. }
  215. /*printf("fadeout %i\n",(int)v.fadeout_volume);
  216. printf("channel %i\n",(int)v.channel_volume);
  217. printf("output %i\n",(int)v.output_volume);
  218. printf("env %i\n",(int)tmp_volenv_value);
  219. printf("cgb %i\n",(int)v.master_channel->channel_global_volume);
  220. */
  221. tmpvol = (uint64_t)v.fadeout_volume; /* max 1024 - 10 bits */
  222. tmpvol *= (uint64_t)v.channel_volume; /* * max 64 - 6 bits */
  223. tmpvol *= (uint64_t)v.output_volume; /* * max 256 - 8 bits */
  224. tmpvol *= (uint64_t)tmp_volenv_value; /* max 64 - 6 bits*/
  225. tmpvol *= (uint64_t)v.master_channel->channel_global_volume;
  226. v.display_volume = tmpvol >> 22; //volume used for display purposes , 0 -- 256
  227. tmpvol *= (uint64_t)song->get_mixing_volume(); /* max 128 - 7 bits */
  228. tmpvol *= (uint64_t)control.global_volume; /* max 128 - 7 bits*/
  229. /* total 10+6+8+6+7+7=44 bits */
  230. tmpvol >>= 43; /* Move back to 8 bits range , 44-19+8=43*/
  231. if (tmpvol > CP_VOL_MAX)
  232. tmpvol = CP_VOL_MAX;
  233. //printf("volume check - fade %i, channel %i, output %i, env %i, mix %i, global %i -- final %i\n",v.fadeout_volume, v.channel_volume,v.output_volume,tmp_volenv_value, song->get_mixing_volume(),control.global_volume,tmpvol);
  234. v.total_volume = tmpvol;
  235. if ((v.master_channel != NULL) && song->is_channel_mute(v.master_channel_index) && !v.master_channel->reserved) {
  236. mixer->set_voice_volume(i, 0);
  237. } else {
  238. mixer->set_voice_volume(i, tmpvol);
  239. if (v.fadeout_volume > 0) control.voices_used++;
  240. }
  241. if (!song->is_stereo()) {
  242. mixer->set_voice_panning(i, PAN_CENTER);
  243. } else if (v.panning == PAN_SURROUND) {
  244. mixer->set_voice_panning(i, PAN_SURROUND);
  245. } else if (song->has_instruments()) {
  246. int newpan, real_modifier;
  247. real_modifier = (v.panning_envelope_ctrl.value * (PAN_CENTER - cp_intabs(v.panning - PAN_CENTER))) / 32;
  248. newpan = v.panning + real_modifier;
  249. newpan = (newpan < PAN_LEFT) ? PAN_LEFT : (newpan > PAN_RIGHT) ? PAN_RIGHT : newpan;
  250. //printf("panenv val: %i, finalpan val %i\n",v.panning_envelope_ctrl.value,newpan);
  251. mixer->set_voice_panning(i, newpan);
  252. } else {
  253. mixer->set_voice_panning(i, v.panning);
  254. }
  255. /* VIBRATO */
  256. if ((v.period > 0) && (v.sample_ptr->get_vibrato_depth() > 0)) {
  257. switch (v.sample_ptr->get_vibrato_type()) {
  258. case CPSample::VIBRATO_SINE:
  259. tmp_vibrato_value = auto_vibrato_table[v.auto_vibrato_pos & 127];
  260. if (v.auto_vibrato_pos & 0x80) tmp_vibrato_value = -tmp_vibrato_value;
  261. break;
  262. case CPSample::VIBRATO_SQUARE:
  263. tmp_vibrato_value = 64;
  264. if (v.auto_vibrato_pos & 0x80) tmp_vibrato_value = -tmp_vibrato_value;
  265. break;
  266. case CPSample::VIBRATO_SAW:
  267. tmp_vibrato_value = 63 - (((v.auto_vibrato_pos + 128) & 255) >> 1);
  268. break;
  269. default:
  270. tmp_vibrato_value = (((v.auto_vibrato_pos + 128) & 255) >> 1) - 64;
  271. break;
  272. }
  273. } else {
  274. tmp_vibrato_value = 0;
  275. }
  276. if ((v.auto_vibrato_sweep_pos >> 8) < v.sample_ptr->get_vibrato_depth()) {
  277. v.auto_vibrato_sweep_pos += v.sample_ptr->get_vibrato_speed(); //FIXME - speed? i think so
  278. tmp_vibrato_depth = v.auto_vibrato_sweep_pos;
  279. } else {
  280. tmp_vibrato_depth = v.sample_ptr->get_vibrato_depth() << 8;
  281. }
  282. tmp_vibrato_value = (tmp_vibrato_value * tmp_vibrato_depth) >> 16;
  283. if (song->has_linear_slides())
  284. tmp_vibrato_value >>= 1;
  285. v.period -= tmp_vibrato_value;
  286. /* update vibrato position */
  287. v.auto_vibrato_pos = (v.auto_vibrato_pos + v.sample_ptr->get_vibrato_rate()) & 0xff;
  288. /* process pitch envelope */
  289. tmp_mixer_period = v.period;
  290. if (v.pitch_envelope_ctrl.active) {
  291. long aux_pitch_diff;
  292. int pe_value = v.pitch_envelope_ctrl.value;
  293. if (!v.instrument_ptr->is_pitch_use_as_filter()) {
  294. if (((uint16_t)v.note << 1) + pe_value <= 0)
  295. pe_value = -(v.note << 1);
  296. int smp_c5 = CPSampleManager::get_singleton()->get_c5_freq(v.sample_ptr->get_sample_data());
  297. int base = get_period(((uint16_t)v.note << 1), smp_c5);
  298. int env = get_period(((uint16_t)v.note << 1) + pe_value, smp_c5);
  299. /*
  300. int env_next=(pe_value<0)?get_period(((uint16_t)(v.note-1)<<1)+pe_value,smp_c5):get_period(((uint16_t)(v.note+1)<<1)+pe_value,smp_c5);
  301. env=env+(abs(v.pitch_envelope_ctrl.value)&((1<<CPEnvelope::FX_HEIGHT_BITS)-1))*(env_next-env)/(1<<CPEnvelope::FX_HEIGHT_BITS);
  302. printf("env %i\n",env);
  303. */
  304. aux_pitch_diff = env - base;
  305. if (((int)tmp_mixer_period - aux_pitch_diff) < 0) aux_pitch_diff = 0;
  306. tmp_mixer_period += aux_pitch_diff;
  307. } else {
  308. filter_env = pe_value + 32; //max 64
  309. // printf("pitch envelope at %i",filter_env);
  310. }
  311. }
  312. if (v.fadeout_volume == 0 || (v.note_end_flags & END_NOTE_KILL)) { /* check for a dead note (fadevol=0) */
  313. mixer->stop_voice(i);
  314. } else {
  315. int32_t freq = get_frequency(tmp_mixer_period);
  316. int32_t tracker_c5 = get_frequency(get_period(60 << 1, CPSampleManager::get_singleton()->get_c5_freq(v.sample_ptr->get_sample_data())));
  317. freq = (int32_t)((uint64_t)freq * (uint64_t)C5FREQ_MIXER / (uint64_t)tracker_c5); //numbers may become very high
  318. mixer->set_voice_frequency(i, freq);
  319. /* if keyfade, start substracting fadeoutspeed from fadevol: */
  320. if ((song->has_instruments()) && (v.note_end_flags & END_NOTE_FADE)) {
  321. if (v.fadeout_volume >= (v.instrument_ptr->get_volume_fadeout())) {
  322. v.fadeout_volume -= (v.instrument_ptr->get_volume_fadeout());
  323. } else {
  324. v.fadeout_volume = 0;
  325. }
  326. }
  327. /*FILTARSSSSSSSS*/
  328. v.filter.envelope_cutoff = filter_env;
  329. v.filter.process();
  330. if ((v.filter.final_cutoff < 0xFF) && (control.filters)) {
  331. //int final_cutoff;
  332. //uint8_t final_reso;
  333. //v.filter.set_filter_parameters( &final_cutoff, &final_reso );
  334. mixer->set_voice_filter(i, true, v.filter.final_cutoff, v.filter.it_reso);
  335. } else {
  336. mixer->set_voice_filter(i, false, 0, 0);
  337. }
  338. /* RAIVERV */
  339. mixer->set_voice_reverb_send(i, v.reverb_send);
  340. /* CHAURUZ */
  341. mixer->set_voice_chorus_send(i, v.chorus_send);
  342. }
  343. }
  344. switch (song->get_reverb_mode()) {
  345. case CPSong::REVERB_MODE_ROOM: {
  346. mixer->set_reverb_mode(CPMixer::REVERB_MODE_ROOM);
  347. } break;
  348. case CPSong::REVERB_MODE_STUDIO_SMALL: {
  349. mixer->set_reverb_mode(CPMixer::REVERB_MODE_STUDIO_SMALL);
  350. } break;
  351. case CPSong::REVERB_MODE_STUDIO_MEDIUM: {
  352. mixer->set_reverb_mode(CPMixer::REVERB_MODE_STUDIO_MEDIUM);
  353. } break;
  354. case CPSong::REVERB_MODE_STUDIO_LARGE: {
  355. mixer->set_reverb_mode(CPMixer::REVERB_MODE_STUDIO_LARGE);
  356. } break;
  357. case CPSong::REVERB_MODE_HALL: {
  358. mixer->set_reverb_mode(CPMixer::REVERB_MODE_HALL);
  359. } break;
  360. case CPSong::REVERB_MODE_SPACE_ECHO: {
  361. mixer->set_reverb_mode(CPMixer::REVERB_MODE_SPACE_ECHO);
  362. } break;
  363. case CPSong::REVERB_MODE_ECHO: {
  364. mixer->set_reverb_mode(CPMixer::REVERB_MODE_ECHO);
  365. } break;
  366. case CPSong::REVERB_MODE_DELAY: {
  367. mixer->set_reverb_mode(CPMixer::REVERB_MODE_DELAY);
  368. } break;
  369. case CPSong::REVERB_MODE_HALF_ECHO: {
  370. mixer->set_reverb_mode(CPMixer::REVERB_MODE_HALF_ECHO);
  371. } break;
  372. }
  373. mixer->set_chorus_params(song->get_chorus_delay_ms(), song->get_chorus_separation_ms(), song->get_chorus_depth_ms10(), song->get_chorus_speed_hz10());
  374. }
  375. void CPPlayer::handle_tick() {
  376. int i;
  377. if (mixer == NULL) return;
  378. if (song == NULL) return;
  379. /* update time counter (sngtime is in milliseconds (in fact 2^-10)) */
  380. if (control.ticks_counter >= control.speed) { // time to process... ***THE ROW***!
  381. /* process pattern-delay. pf->patdly2 is the counter and pf->patdly is
  382. the command memory. */
  383. // if (control.pattern_delay_1) {
  384. // control.pattern_delay_2=control.pattern_delay_1;
  385. // control.pattern_delay_1=0;
  386. // }
  387. // if (control.pattern_delay_2) {
  388. // patterndelay active
  389. // if (--control.pattern_delay_2)
  390. // so turn back pf->patpos by 1
  391. // if (pf->patpos) pf->patpos--;
  392. // }
  393. if (control.play_mode != PLAY_NOTHING) {
  394. control.ticks_counter = 0;
  395. if (control.position.force_next_order >= 0) {
  396. control.position.current_order = control.position.force_next_order;
  397. }
  398. control.position.force_next_order = -1;
  399. control.previous_position = control.position; // for those special cases...
  400. control.position.forbid_jump = false;
  401. for (i = 0; i < CPPattern::WIDTH; i++) {
  402. process_note(i, song->get_pattern(control.position.current_pattern)->get_note(i, control.position.current_row));
  403. }
  404. control.position.current_row++;
  405. if (control.position.current_row >= song->get_pattern(control.position.current_pattern)->get_length()) {
  406. if (control.play_mode == PLAY_SONG) {
  407. int next_order;
  408. next_order = get_song_next_order_idx(song, control.position.current_order);
  409. if (next_order != -1) {
  410. // Do we have a "next order?"
  411. control.position.current_pattern = song->get_order(next_order);
  412. if (next_order <= control.position.current_order)
  413. control.reached_end = true;
  414. control.position.current_order = next_order;
  415. } else {
  416. // no, probably the user deleted the orderlist.
  417. control.play_mode = PLAY_NOTHING;
  418. reset();
  419. control.reached_end = true;
  420. }
  421. }
  422. control.position.current_row = 0;
  423. }
  424. }
  425. }
  426. pre_process_effects();
  427. process_NNAs();
  428. setup_voices();
  429. /* now set up the actual hardware channel playback information */
  430. update_mixer();
  431. control.ticks_counter++;
  432. }