cp_player_data_notes.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /*************************************************************************/
  2. /* cp_player_data_notes.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. #define RANDOM_MAX 2147483647
  33. static inline int32_t cp_random_generate(int32_t *seed) {
  34. int32_t k;
  35. int32_t s = (int32_t)(*seed);
  36. if (s == 0)
  37. s = 0x12345987;
  38. k = s / 127773;
  39. s = 16807 * (s - k * 127773) - 2836 * k;
  40. if (s < 0)
  41. s += 2147483647;
  42. (*seed) = (int32_t)s;
  43. return (int32_t)(s & RANDOM_MAX);
  44. }
  45. void CPPlayer::process_new_note(int p_track, uint8_t p_note) { // if there's really a new note....
  46. if (control.channel[p_track].real_note != 255) {
  47. control.channel[p_track].old_note = control.channel[p_track].real_note;
  48. }
  49. control.channel[p_track].real_note = p_note;
  50. control.channel[p_track].kick = KICK_NOTE;
  51. control.channel[p_track].sample_start_index = -1;
  52. control.channel[p_track].sliding = 0;
  53. control.channel[p_track].row_has_note = true;
  54. control.channel[p_track].last_event_usecs = song_usecs;
  55. if (control.channel[p_track].panbrello_type) control.channel[p_track].panbrello_position = 0;
  56. }
  57. bool CPPlayer::process_new_instrument(int p_track, uint8_t p_instrument) {
  58. // bool different_instrument=false;
  59. ERR_FAIL_INDEX_V(p_instrument, CPSong::MAX_INSTRUMENTS, false);
  60. if (song->has_instruments()) {
  61. control.channel[p_track].instrument_ptr = song->get_instrument(p_instrument);
  62. } else {
  63. control.channel[p_track].instrument_ptr = NULL;
  64. }
  65. control.channel[p_track].retrig_counter = 0;
  66. control.channel[p_track].tremor_position = 0;
  67. control.channel[p_track].sample_offset_fine = 0;
  68. int old_instr_index = control.channel[p_track].instrument_index;
  69. control.channel[p_track].instrument_index = p_instrument;
  70. return (old_instr_index != p_instrument);
  71. }
  72. // returns if it was able to process
  73. bool CPPlayer::process_note_and_instrument(int p_track, int p_note, int p_instrument) {
  74. bool aux_result;
  75. aux_result = false;
  76. CPSample *aux_sample = 0; // current sample
  77. int dest_sample_index;
  78. bool new_instrument = false;
  79. control.channel[p_track].row_has_note = false; // wise man says.. "we dont have a note... until we really know we have a note".
  80. control.channel[p_track].new_instrument = false;
  81. if ((p_note < 0) && (p_instrument < 0)) return aux_result; // nothing to do here
  82. if ((p_note == 255) && (p_instrument == 255)) return aux_result;
  83. if ((p_note >= 0) && (p_note < 120)) {
  84. process_new_note(p_track, p_note);
  85. } else if (p_note == CPNote::CUT) {
  86. control.channel[p_track].aux_volume = 0;
  87. control.channel[p_track].note_end_flags |= END_NOTE_OFF;
  88. control.channel[p_track].note_end_flags |= END_NOTE_KILL;
  89. return aux_result;
  90. } else if ((p_note == CPNote::OFF) && (song->has_instruments())) {
  91. if (control.channel[p_track].instrument_ptr != NULL) {
  92. control.channel[p_track].note_end_flags |= END_NOTE_OFF;
  93. if (!control.channel[p_track].instrument_ptr->get_volume_envelope()->is_enabled() || control.channel[p_track].instrument_ptr->get_volume_envelope()->is_loop_enabled()) {
  94. control.channel[p_track].note_end_flags |= END_NOTE_FADE;
  95. }
  96. }
  97. return aux_result;
  98. } else
  99. return aux_result; // invalid note!
  100. if ((p_instrument >= 0) && (p_instrument < CPSong::MAX_INSTRUMENTS)) {
  101. new_instrument = process_new_instrument(p_track, p_instrument);
  102. if (song->has_instruments()) {
  103. // If we're in instrument mode...
  104. if (control.channel[p_track].instrument_ptr->get_sample_number(control.channel[p_track].real_note) >= CPSong::MAX_SAMPLES) {
  105. control.channel[p_track].kick = KICK_NOTHING;
  106. return aux_result;
  107. } else {
  108. dest_sample_index = control.channel[p_track].instrument_ptr->get_sample_number(control.channel[p_track].real_note);
  109. control.channel[p_track].note = control.channel[p_track].instrument_ptr->get_note_number(control.channel[p_track].real_note);
  110. }
  111. } else {
  112. // If we're in sample mode...
  113. dest_sample_index = control.channel[p_track].instrument_index;
  114. control.channel[p_track].note = control.channel[p_track].real_note;
  115. }
  116. control.channel[p_track].sample_index = dest_sample_index;
  117. aux_sample = song->get_sample(dest_sample_index);
  118. if (!CPSampleManager::get_singleton()->check(aux_sample->get_sample_data())) {
  119. /* INVALID SAMPLE */
  120. control.channel[p_track].kick = KICK_NOTHING;
  121. return aux_result;
  122. }
  123. aux_sample = song->get_sample(dest_sample_index);
  124. } else {
  125. if (!control.channel[p_track].sample_ptr)
  126. return aux_result;
  127. if (song->has_instruments()) {
  128. if (!control.channel[p_track].instrument_ptr)
  129. return aux_result;
  130. control.channel[p_track].note = control.channel[p_track].instrument_ptr->get_note_number(control.channel[p_track].real_note);
  131. } else {
  132. control.channel[p_track].note = control.channel[p_track].real_note;
  133. }
  134. aux_sample = control.channel[p_track].sample_ptr;
  135. }
  136. if (p_instrument >= CPSong::MAX_INSTRUMENTS && control.channel[p_track].sample_ptr != aux_sample) {
  137. control.channel[p_track].new_instrument = (control.channel[p_track].period > 0);
  138. }
  139. control.channel[p_track].sample_ptr = aux_sample;
  140. /* channel or instrument determined panning ? */
  141. control.channel[p_track].panning = control.channel[p_track].channel_panning;
  142. /* set filter,if any ? */
  143. if (aux_sample->is_pan_enabled()) {
  144. control.channel[p_track].panning = (int)aux_sample->get_pan() * 255 / 64;
  145. } else if (song->has_instruments() && (control.channel[p_track].instrument_ptr->is_pan_default_enabled())) {
  146. control.channel[p_track].panning = (int)control.channel[p_track].instrument_ptr->get_pan_default_amount() * 255 / 64;
  147. }
  148. if (song->has_instruments()) {
  149. /* Pitch-Pan Separation */
  150. if ((control.channel[p_track].instrument_ptr->get_pan_pitch_separation() != 0) && (control.channel[p_track].channel_panning != PAN_SURROUND)) {
  151. control.channel[p_track].panning += ((control.channel[p_track].real_note - control.channel[p_track].instrument_ptr->get_pan_pitch_center()) * control.channel[p_track].instrument_ptr->get_pan_pitch_separation()) / 8;
  152. if (control.channel[p_track].panning < PAN_LEFT) control.channel[p_track].panning = PAN_LEFT;
  153. if (control.channel[p_track].panning > PAN_RIGHT) control.channel[p_track].panning = PAN_RIGHT;
  154. }
  155. /* Random Volume Variation */
  156. if (control.channel[p_track].instrument_ptr->get_volume_random_variation() > 0) {
  157. control.channel[p_track].random_volume_variation = 100 - (cp_random_generate(&control.random_seed) % control.channel[p_track].instrument_ptr->get_volume_random_variation());
  158. } else {
  159. control.channel[p_track].random_volume_variation = 100;
  160. }
  161. /* Random Pan Variation */
  162. if ((control.channel[p_track].instrument_ptr->get_pan_random_variation() > 0) && (control.channel[p_track].panning != PAN_SURROUND)) {
  163. int aux_pan_modifier;
  164. aux_pan_modifier = (cp_random_generate(&control.random_seed) % (control.channel[p_track].instrument_ptr->get_pan_random_variation() << 2));
  165. if ((cp_random_generate(&control.random_seed) % 2) == 1) aux_pan_modifier = 0 - aux_pan_modifier; /* it's 5am, let me sleep :) */
  166. control.channel[p_track].panning += aux_pan_modifier;
  167. if (control.channel[p_track].panning < PAN_LEFT) control.channel[p_track].panning = PAN_LEFT;
  168. if (control.channel[p_track].panning > PAN_RIGHT) control.channel[p_track].panning = PAN_RIGHT;
  169. }
  170. /*filter*/
  171. if (control.channel[p_track].instrument_ptr->filter_use_default_cutoff()) {
  172. control.channel[p_track].filter.it_cutoff = control.channel[p_track].instrument_ptr->get_filter_default_cutoff() * 2;
  173. }
  174. if (control.channel[p_track].instrument_ptr->filter_use_default_resonance()) {
  175. control.channel[p_track].filter.it_reso = control.channel[p_track].instrument_ptr->get_filter_default_resonance() * 2;
  176. }
  177. /*envelopes*/
  178. control.channel[p_track].volume_envelope_on = control.channel[p_track].instrument_ptr->get_volume_envelope()->is_enabled();
  179. control.channel[p_track].panning_envelope_on = control.channel[p_track].instrument_ptr->get_pan_envelope()->is_enabled();
  180. control.channel[p_track].pitch_envelope_on = control.channel[p_track].instrument_ptr->get_pitch_filter_envelope()->is_enabled();
  181. control.channel[p_track].NNA_type = control.channel[p_track].instrument_ptr->get_NNA_type();
  182. control.channel[p_track].duplicate_check_type = control.channel[p_track].instrument_ptr->get_DC_type();
  183. control.channel[p_track].duplicate_check_action = control.channel[p_track].instrument_ptr->get_DC_action();
  184. } else {
  185. control.channel[p_track].NNA_type = CPInstrument::NNA_NOTE_CUT;
  186. control.channel[p_track].duplicate_check_type = CPInstrument::DCT_DISABLED;
  187. control.channel[p_track].duplicate_check_action = CPInstrument::DCA_NOTE_CUT;
  188. }
  189. if (p_instrument < CPSong::MAX_INSTRUMENTS) { // instrument change
  190. control.channel[p_track].volume = control.channel[p_track].aux_volume = aux_sample->get_default_volume();
  191. }
  192. control.channel[p_track].slide_to_period = control.channel[p_track].aux_period = get_period((uint16_t)(control.channel[p_track].note) << 1, CPSampleManager::get_singleton()->get_c5_freq((aux_sample->get_sample_data())));
  193. control.channel[p_track].note_end_flags = END_NOTE_NOTHING; /* clears flags */
  194. return true;
  195. }
  196. void CPPlayer::process_volume_column(int p_track, uint8_t p_volume) {
  197. control.channel[p_track].current_volume_command = CPNote::EMPTY;
  198. control.channel[p_track].current_volume_parameter = CPNote::EMPTY;
  199. if (p_volume < 65) { // VOLUME
  200. control.channel[p_track].aux_volume = p_volume;
  201. } else if (p_volume < 125) { // Volume Command
  202. control.channel[p_track].current_volume_command = (p_volume - 65) / 10;
  203. control.channel[p_track].current_volume_parameter = (p_volume - 65) % 10;
  204. } else if (p_volume < 193) { // PAN
  205. control.channel[p_track].channel_panning = (p_volume - 128) * PAN_RIGHT / 64;
  206. control.channel[p_track].panning = control.channel[p_track].channel_panning;
  207. } else if (p_volume < 213) { //More volume Commands
  208. control.channel[p_track].current_volume_command = ((p_volume - 193) / 10) + 6;
  209. control.channel[p_track].current_volume_parameter = (p_volume - 193) % 10;
  210. }
  211. }
  212. void CPPlayer::process_note(int p_track, CPNote p_note) {
  213. if (p_note.note != CPNote::SCRIPT) {
  214. process_note_and_instrument(p_track, p_note.note, p_note.instrument);
  215. process_volume_column(p_track, p_note.volume);
  216. control.channel[p_track].current_command = p_note.command;
  217. control.channel[p_track].current_parameter = p_note.parameter;
  218. } else {
  219. CPNote n = song->get_pattern(control.position.current_pattern)->get_transformed_script_note(p_track, control.position.current_row);
  220. process_note(p_track, n);
  221. song->get_pattern(control.position.current_pattern)->scripted_clone(p_track, control.position.current_row);
  222. }
  223. }