cp_player_data_notes.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /*************************************************************************/
  2. /* cp_player_data_notes.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "cp_player_data.h"
  30. #include "cp_sample_manager.h"
  31. #define RANDOM_MAX 2147483647
  32. static inline int32_t cp_random_generate(int32_t *seed) {
  33. int32_t k;
  34. int32_t s = (int32_t)(*seed);
  35. if (s == 0)
  36. s = 0x12345987;
  37. k = s / 127773;
  38. s = 16807 * (s - k * 127773) - 2836 * k;
  39. if (s < 0)
  40. s += 2147483647;
  41. (*seed) = (int32_t)s;
  42. return (int32_t)(s & RANDOM_MAX);
  43. }
  44. void CPPlayer::process_new_note(int p_track,uint8_t p_note) { // if there's really a new note....
  45. if (control.channel[p_track].real_note!=255) {
  46. control.channel[p_track].old_note=control.channel[p_track].real_note;
  47. }
  48. control.channel[p_track].real_note=p_note;
  49. control.channel[p_track].kick=KICK_NOTE;
  50. control.channel[p_track].sample_start_index=-1;
  51. control.channel[p_track].sliding=0;
  52. control.channel[p_track].row_has_note=true;
  53. control.channel[p_track].last_event_usecs=song_usecs;
  54. if (control.channel[p_track].panbrello_type) control.channel[p_track].panbrello_position=0;
  55. }
  56. bool CPPlayer::process_new_instrument(int p_track,uint8_t p_instrument) {
  57. // bool different_instrument=false;
  58. ERR_FAIL_INDEX_V(p_instrument,CPSong::MAX_INSTRUMENTS,false);
  59. if ( song->has_instruments() ) {
  60. control.channel[p_track].instrument_ptr=song->get_instrument(p_instrument);
  61. } else {
  62. control.channel[p_track].instrument_ptr=NULL;
  63. }
  64. control.channel[p_track].retrig_counter=0;
  65. control.channel[p_track].tremor_position=0;
  66. control.channel[p_track].sample_offset_fine=0;
  67. int old_instr_index=control.channel[p_track].instrument_index;
  68. control.channel[p_track].instrument_index=p_instrument;
  69. return (old_instr_index!=p_instrument);
  70. }
  71. // returns if it was able to process
  72. bool CPPlayer::process_note_and_instrument(int p_track,int p_note,int p_instrument) {
  73. bool aux_result;
  74. aux_result=false;
  75. CPSample *aux_sample=0; // current sample
  76. int dest_sample_index;
  77. bool new_instrument=false;
  78. control.channel[p_track].row_has_note=false; // wise man says.. "we dont have a note... until we really know we have a note".
  79. control.channel[p_track].new_instrument=false;
  80. if ( (p_note<0) && (p_instrument<0) ) return aux_result; // nothing to do here
  81. if ( (p_note==255) && (p_instrument==255) ) return aux_result;
  82. if ( (p_note>=0) && (p_note<120) ) {
  83. process_new_note(p_track,p_note);
  84. } else if (p_note==CPNote::CUT) {
  85. control.channel[p_track].aux_volume=0;
  86. control.channel[p_track].note_end_flags|=END_NOTE_OFF;
  87. control.channel[p_track].note_end_flags|=END_NOTE_KILL;
  88. return aux_result;
  89. } else if ((p_note==CPNote::OFF) && (song->has_instruments())) {
  90. if (control.channel[p_track].instrument_ptr!=NULL) {
  91. control.channel[p_track].note_end_flags|=END_NOTE_OFF;
  92. if (!control.channel[p_track].instrument_ptr->get_volume_envelope()->is_enabled() || control.channel[p_track].instrument_ptr->get_volume_envelope()->is_loop_enabled()) {
  93. control.channel[p_track].note_end_flags|=END_NOTE_FADE;
  94. }
  95. }
  96. return aux_result;
  97. } else return aux_result; // invalid note!
  98. if ( (p_instrument>=0) && (p_instrument<CPSong::MAX_INSTRUMENTS)) {
  99. new_instrument=process_new_instrument(p_track,p_instrument);
  100. if ( song->has_instruments() ) {
  101. // If we're in instrument mode...
  102. if ( control.channel[p_track].instrument_ptr->get_sample_number(control.channel[p_track].real_note) >= CPSong::MAX_SAMPLES) {
  103. control.channel[p_track].kick=KICK_NOTHING;
  104. return aux_result;
  105. } else {
  106. dest_sample_index=control.channel[p_track].instrument_ptr->get_sample_number(control.channel[p_track].real_note);
  107. control.channel[p_track].note=control.channel[p_track].instrument_ptr->get_note_number(control.channel[p_track].real_note);
  108. }
  109. } else {
  110. // If we're in sample mode...
  111. dest_sample_index=control.channel[p_track].instrument_index;
  112. control.channel[p_track].note=control.channel[p_track].real_note;
  113. }
  114. control.channel[p_track].sample_index=dest_sample_index;
  115. aux_sample=song->get_sample(dest_sample_index);
  116. if (!CPSampleManager::get_singleton()->check( aux_sample->get_sample_data() )) {
  117. /* INVALID SAMPLE */
  118. control.channel[p_track].kick=KICK_NOTHING;
  119. return aux_result;
  120. }
  121. aux_sample=song->get_sample(dest_sample_index);
  122. } else {
  123. if (!control.channel[p_track].sample_ptr)
  124. return aux_result;
  125. if (song->has_instruments()) {
  126. if (!control.channel[p_track].instrument_ptr)
  127. return aux_result;
  128. control.channel[p_track].note=control.channel[p_track].instrument_ptr->get_note_number(control.channel[p_track].real_note);
  129. } else {
  130. control.channel[p_track].note=control.channel[p_track].real_note;
  131. }
  132. aux_sample=control.channel[p_track].sample_ptr;
  133. }
  134. if (p_instrument>=CPSong::MAX_INSTRUMENTS && control.channel[p_track].sample_ptr!=aux_sample) {
  135. control.channel[p_track].new_instrument=(control.channel[p_track].period>0);
  136. }
  137. control.channel[p_track].sample_ptr=aux_sample;
  138. /* channel or instrument determined panning ? */
  139. control.channel[p_track].panning=control.channel[p_track].channel_panning;
  140. /* set filter,if any ? */
  141. if (aux_sample->is_pan_enabled()) {
  142. control.channel[p_track].panning=(int)aux_sample->get_pan()*255/64;
  143. } else if ( song->has_instruments() && (control.channel[p_track].instrument_ptr->is_pan_default_enabled()) ) {
  144. control.channel[p_track].panning=(int)control.channel[p_track].instrument_ptr->get_pan_default_amount()*255/64;
  145. }
  146. if (song->has_instruments()) {
  147. /* Pitch-Pan Separation */
  148. if ((control.channel[p_track].instrument_ptr->get_pan_pitch_separation()!=0) && (control.channel[p_track].channel_panning!=PAN_SURROUND)){
  149. 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;
  150. if (control.channel[p_track].panning<PAN_LEFT) control.channel[p_track].panning=PAN_LEFT;
  151. if (control.channel[p_track].panning>PAN_RIGHT) control.channel[p_track].panning=PAN_RIGHT;
  152. }
  153. /* Random Volume Variation */
  154. if (control.channel[p_track].instrument_ptr->get_volume_random_variation()>0) {
  155. control.channel[p_track].random_volume_variation=100-(cp_random_generate(&control.random_seed) % control.channel[p_track].instrument_ptr->get_volume_random_variation());
  156. } else {
  157. control.channel[p_track].random_volume_variation=100;
  158. }
  159. /* Random Pan Variation */
  160. if ((control.channel[p_track].instrument_ptr->get_pan_random_variation()>0) && (control.channel[p_track].panning!=PAN_SURROUND)){
  161. int aux_pan_modifier;
  162. aux_pan_modifier=(cp_random_generate(&control.random_seed) % (control.channel[p_track].instrument_ptr->get_pan_random_variation() << 2));
  163. if ((cp_random_generate(&control.random_seed) % 2)==1) aux_pan_modifier=0-aux_pan_modifier; /* it's 5am, let me sleep :) */
  164. control.channel[p_track].panning+=aux_pan_modifier;
  165. if (control.channel[p_track].panning<PAN_LEFT) control.channel[p_track].panning=PAN_LEFT;
  166. if (control.channel[p_track].panning>PAN_RIGHT) control.channel[p_track].panning=PAN_RIGHT;
  167. }
  168. /*filter*/
  169. if (control.channel[p_track].instrument_ptr->filter_use_default_cutoff()) {
  170. control.channel[p_track].filter.it_cutoff=control.channel[p_track].instrument_ptr->get_filter_default_cutoff()*2;
  171. }
  172. if (control.channel[p_track].instrument_ptr->filter_use_default_resonance()) {
  173. control.channel[p_track].filter.it_reso=control.channel[p_track].instrument_ptr->get_filter_default_resonance()*2;
  174. }
  175. /*envelopes*/
  176. control.channel[p_track].volume_envelope_on=control.channel[p_track].instrument_ptr->get_volume_envelope()->is_enabled();
  177. control.channel[p_track].panning_envelope_on=control.channel[p_track].instrument_ptr->get_pan_envelope()->is_enabled();
  178. control.channel[p_track].pitch_envelope_on=control.channel[p_track].instrument_ptr->get_pitch_filter_envelope()->is_enabled();
  179. control.channel[p_track].NNA_type=control.channel[p_track].instrument_ptr->get_NNA_type();
  180. control.channel[p_track].duplicate_check_type=control.channel[p_track].instrument_ptr->get_DC_type();
  181. control.channel[p_track].duplicate_check_action=control.channel[p_track].instrument_ptr->get_DC_action();
  182. } else {
  183. control.channel[p_track].NNA_type=CPInstrument::NNA_NOTE_CUT;
  184. control.channel[p_track].duplicate_check_type=CPInstrument::DCT_DISABLED;
  185. control.channel[p_track].duplicate_check_action=CPInstrument::DCA_NOTE_CUT;
  186. }
  187. if (p_instrument<CPSong::MAX_INSTRUMENTS) { // instrument change
  188. control.channel[p_track].volume=control.channel[p_track].aux_volume=aux_sample->get_default_volume();
  189. }
  190. 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())));
  191. control.channel[p_track].note_end_flags=END_NOTE_NOTHING; /* clears flags */
  192. return true;
  193. }
  194. void CPPlayer::process_volume_column(int p_track,uint8_t p_volume) {
  195. control.channel[p_track].current_volume_command=CPNote::EMPTY;
  196. control.channel[p_track].current_volume_parameter=CPNote::EMPTY;
  197. if (p_volume<65) { // VOLUME
  198. control.channel[p_track].aux_volume=p_volume;
  199. } else if (p_volume<125) { // Volume Command
  200. control.channel[p_track].current_volume_command=(p_volume-65) / 10;
  201. control.channel[p_track].current_volume_parameter=(p_volume-65) % 10;
  202. } else if (p_volume<193) { // PAN
  203. control.channel[p_track].channel_panning=(p_volume-128)*PAN_RIGHT/64;
  204. control.channel[p_track].panning=control.channel[p_track].channel_panning;
  205. } else if (p_volume<213) { //More volume Commands
  206. control.channel[p_track].current_volume_command=((p_volume-193) / 10)+6;
  207. control.channel[p_track].current_volume_parameter=(p_volume-193) % 10;
  208. }
  209. }
  210. void CPPlayer::process_note(int p_track,CPNote p_note) {
  211. if ( p_note.note!=CPNote::SCRIPT ) {
  212. process_note_and_instrument(p_track,p_note.note,p_note.instrument);
  213. process_volume_column(p_track,p_note.volume);
  214. control.channel[p_track].current_command=p_note.command;
  215. control.channel[p_track].current_parameter=p_note.parameter;
  216. } else {
  217. CPNote n = song->get_pattern( control.position.current_pattern )->get_transformed_script_note( p_track, control.position.current_row );
  218. process_note( p_track, n );
  219. song->get_pattern( control.position.current_pattern )->scripted_clone( p_track, control.position.current_row );
  220. }
  221. }