cp_player_data.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  1. /*************************************************************************/
  2. /* cp_player_data.h */
  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. #ifndef CP_PLAYER_DATA_H
  31. #define CP_PLAYER_DATA_H
  32. #include "cp_config.h"
  33. #include "cp_mixer.h"
  34. #include "cp_song.h"
  35. #include "cp_tables.h"
  36. /**CPPlayer Data
  37. *@author Juan Linietsky
  38. */
  39. /******************************
  40. player_data.h
  41. ------------------------
  42. The player and its data.
  43. I hope you dont get sick reading this
  44. ********************************/
  45. //Default pan values
  46. class CPPlayer {
  47. enum {
  48. PAN_SURROUND = 512,
  49. PAN_RIGHT = 255,
  50. PAN_LEFT = 0,
  51. PAN_CENTER = 128
  52. };
  53. CPSong *song;
  54. CPMixer *mixer;
  55. struct Filter_Control {
  56. int32_t it_reso;
  57. int32_t it_cutoff;
  58. int32_t envelope_cutoff;
  59. int32_t final_cutoff;
  60. void process();
  61. void set_filter_parameters(int *p_cutoff, uint8_t *p_reso);
  62. };
  63. //tells you if a channel is doing
  64. //noteoff/notekill/notefade/etc
  65. enum {
  66. END_NOTE_NOTHING = 0,
  67. END_NOTE_OFF = 1,
  68. END_NOTE_FADE = 2,
  69. END_NOTE_KILL = 4
  70. };
  71. //Tells you what should a channel restart
  72. enum {
  73. KICK_NOTHING,
  74. KICK_NOTE,
  75. KICK_NOTEOFF,
  76. KICK_ENVELOPE
  77. };
  78. enum {
  79. MAX_VOICES = 256
  80. };
  81. struct Channel_Control;
  82. struct Voice_Control {
  83. struct Envelope_Control {
  84. int pos_index;
  85. int status;
  86. int value;
  87. bool sustain_looping;
  88. bool looping;
  89. bool terminated;
  90. bool active;
  91. bool kill;
  92. };
  93. Filter_Control filter;
  94. uint16_t reverb_send;
  95. uint16_t chorus_send;
  96. CPInstrument *instrument_ptr;
  97. CPSample *sample_ptr;
  98. // Sample_Data *sample_data;
  99. int32_t period;
  100. int32_t sample_start_index; /* The starting byte index in the sample */
  101. bool has_master_channel;
  102. int master_channel_index;
  103. int instruement_index;
  104. int instrument_index;
  105. int sample_index;
  106. int8_t NNA_type;
  107. int note_end_flags;
  108. uint8_t sample; /* which instrument number */
  109. int16_t output_volume; /* output volume (vol + sampcol + instvol) */
  110. int8_t channel_volume; /* channel's "global" volume */
  111. uint16_t fadeout_volume; /* fading volume rate */
  112. int32_t total_volume; /* total volume of channel (before global mixings) */
  113. uint8_t kick; /* if true = sample has to be restarted */
  114. uint8_t note; /* the audible note (as heard, direct rep of period) */
  115. int16_t panning; /* panning position */
  116. uint8_t nna; /* New note action type + master/slave flags */
  117. uint8_t volflg; /* volume envelope settings */
  118. uint8_t panflg; /* panning envelope settings */
  119. uint8_t pitflg; /* pitch envelope settings */
  120. uint8_t keyoff; /* if true = fade out and stuff */
  121. int16_t handle; /* which sample-handle */
  122. int32_t start; /* The start byte index in the sample */
  123. /* Below here is info NOT in MP_CONTROL!! */
  124. //ENVPR venv;
  125. //ENVPR penv;
  126. //ENVPR cenv;
  127. Envelope_Control volume_envelope_ctrl;
  128. Envelope_Control panning_envelope_ctrl;
  129. Envelope_Control pitch_envelope_ctrl;
  130. uint16_t auto_vibrato_pos; /* autovibrato pos */
  131. uint16_t auto_vibrato_sweep_pos; /* autovibrato sweep pos */
  132. int16_t masterchn;
  133. uint16_t masterperiod;
  134. Channel_Control *master_channel; /* index of "master" effects channel */
  135. void start_envelope(CPEnvelope *p_envelope, Envelope_Control *p_envelope_ctrl, Envelope_Control *p_from_env);
  136. bool process_envelope(CPEnvelope *p_envelope, Envelope_Control *p_envelope_ctrl);
  137. uint16_t display_volume;
  138. Voice_Control() {
  139. reset();
  140. }
  141. void reset();
  142. void update_info_from_master_channel();
  143. };
  144. struct Channel_Control {
  145. /* NOTE info */
  146. uint8_t note; /* the audible note as heard, direct rep of period */
  147. uint8_t real_note; /* the note that indexes the audible */
  148. int32_t sample_start_index; /* The starting byte index in the sample */
  149. uint8_t old_note;
  150. uint8_t kick;
  151. Filter_Control filter;
  152. uint16_t reverb_send;
  153. uint16_t chorus_send;
  154. int note_end_flags;
  155. /* INSTRUMENT INFO */
  156. CPInstrument *instrument_ptr;
  157. CPSample *sample_ptr;
  158. uint8_t instrument_index;
  159. uint8_t sample_index;
  160. bool new_instrument;
  161. /* SAMPLE SPECIFIC INFO */
  162. int32_t base_speed; /* what finetune to use */
  163. /* INSTRUMENT SPECIFIC INFO */
  164. int8_t NNA_type;
  165. int8_t duplicate_check_type;
  166. int8_t duplicate_check_action;
  167. bool volume_envelope_on;
  168. bool panning_envelope_on;
  169. bool pitch_envelope_on;
  170. bool has_own_period;
  171. bool row_has_note;
  172. /* VOLUME COLUMN */
  173. int16_t volume; /* amiga volume (0 t/m 64) to play the sample at */
  174. int16_t aux_volume;
  175. bool has_own_volume;
  176. bool mute;
  177. int16_t random_volume_variation; /* 0-100 - 100 has no effect */
  178. /* VOLUME/PAN/PITCH MODIFIERS */
  179. int8_t default_volume; // CHANNEL default volume (0-64)
  180. int16_t channel_volume; // CHANNEL current volume //chanvol - current!
  181. int16_t output_volume; /* output volume (vol + sampcol + instvol) //volume */
  182. int16_t channel_global_volume;
  183. uint16_t fadeout_volume; /* fading volume rate */
  184. int32_t period; /* period to play the sample at */
  185. /* PAN */
  186. int16_t panning; /* panning position */
  187. int16_t channel_panning;
  188. int8_t sliding;
  189. uint16_t aux_period; /* temporary period */
  190. /* TIMING */
  191. uint8_t note_delay; /* (used for note delay) */
  192. /* Slave Voice Control */
  193. Voice_Control *slave_voice; /* Audio Slave of current effects control channel */
  194. struct Carry {
  195. Voice_Control::Envelope_Control vol;
  196. Voice_Control::Envelope_Control pan;
  197. Voice_Control::Envelope_Control pitch;
  198. bool maybe;
  199. } carry;
  200. uint8_t slave_voice_index; /* Audio Slave of current effects control channel */
  201. uint8_t *row; /* row currently playing on this channel */
  202. /* effect memory variables */
  203. uint8_t current_command;
  204. uint8_t current_parameter;
  205. uint8_t current_volume_command;
  206. uint8_t current_volume_parameter;
  207. uint8_t volcol_volume_slide;
  208. /* CPSample Offset */
  209. int32_t lo_offset;
  210. int32_t hi_offset;
  211. /* Panbrello waveform */
  212. uint8_t panbrello_type; /* current panbrello waveform */
  213. uint8_t panbrello_position; /* current panbrello position */
  214. int8_t panbrello_speed; /* "" speed */
  215. uint8_t panbrello_depth; /* "" depth */
  216. uint8_t panbrello_info;
  217. /* Arpegio */
  218. uint8_t arpegio_info;
  219. /* CPPattern Loop */
  220. int pattern_loop_position;
  221. int8_t pattern_loop_count;
  222. /* Vibrato */
  223. bool doing_vibrato;
  224. int8_t vibrato_position; /* current vibrato position */
  225. uint8_t vibrato_speed; /* "" speed */
  226. uint8_t vibrato_depth; /* "" depth */
  227. uint8_t vibrato_type;
  228. /* Tremor */
  229. int8_t tremor_position;
  230. uint8_t tremor_speed; /* s3m tremor ontime/offtime */
  231. uint8_t tremor_depth;
  232. uint8_t tremor_info;
  233. /* Tremolo */
  234. int8_t tremolo_position;
  235. uint8_t tremolo_speed; /* s3m tremor ontime/offtime */
  236. uint8_t tremolo_depth;
  237. uint8_t tremolo_info;
  238. uint8_t tremolo_type;
  239. /* Retrig */
  240. int8_t retrig_counter; /* retrig value (0 means don't retrig) */
  241. uint8_t retrig_speed; /* last used retrig speed */
  242. uint8_t retrig_volslide; /* last used retrig slide */
  243. /* CPSample Offset */
  244. int32_t sample_offset_hi; /* last used high order of sample offset */
  245. uint16_t sample_offset; /* last used low order of sample-offset (effect 9) */
  246. uint16_t sample_offset_fine; /* fine sample offset memory */
  247. /* Portamento */
  248. uint16_t slide_to_period; /* period to slide to (with effect 3 or 5) */
  249. uint8_t portamento_speed;
  250. /* Volume Slide */
  251. uint8_t volume_slide_info;
  252. /* Channel Volume Slide */
  253. uint8_t channel_volume_slide_info;
  254. /* Global Volume Slide */
  255. uint8_t global_volume_slide_info;
  256. /* Channel Pan Slide */
  257. uint8_t channel_pan_slide_info;
  258. /* Pitch Slide */
  259. uint8_t pitch_slide_info;
  260. /* Tempo Slide */
  261. uint8_t tempo_slide_info;
  262. /* S effects memory */
  263. uint8_t current_S_effect;
  264. uint8_t current_S_data;
  265. /* Volume column memory */
  266. uint8_t volume_column_effect_mem;
  267. uint8_t volume_column_data_mem;
  268. int64_t last_event_usecs;
  269. bool reserved;
  270. void reset();
  271. Channel_Control() {
  272. channel_global_volume = 255;
  273. last_event_usecs = -1;
  274. }
  275. };
  276. struct Control_Variables { // control variables (dynamic version) of initial variables
  277. bool reached_end;
  278. char play_mode;
  279. bool filters;
  280. int global_volume;
  281. int speed;
  282. int tempo;
  283. int ticks_counter;
  284. int pattern_delay_1;
  285. int pattern_delay_2;
  286. Channel_Control channel[CPPattern::WIDTH];
  287. int max_voices;
  288. int voices_used; /* reference value */
  289. bool force_no_nna;
  290. bool external_vibrato;
  291. struct Position {
  292. int current_order;
  293. int current_pattern;
  294. int current_row;
  295. int force_next_order;
  296. bool forbid_jump;
  297. };
  298. int32_t random_seed;
  299. Position position;
  300. Position previous_position;
  301. };
  302. Voice_Control voice[MAX_VOICES];
  303. Control_Variables control;
  304. /* VOICE SETUP */
  305. void setup_voices();
  306. /* MIXER SETUP */
  307. void handle_tick();
  308. void update_mixer();
  309. /* NOTE / INSTRUMENT PROCESSING */
  310. void process_new_note(int p_track, uint8_t p_note);
  311. bool process_new_instrument(int p_track, uint8_t p_instrument);
  312. bool process_note_and_instrument(int p_track, int p_note, int p_instrument);
  313. /* EFFECT PROCESSING */
  314. void do_effect_S(int p_track);
  315. void do_panbrello(int p_track);
  316. void do_global_volume_slide(int p_track);
  317. void do_tremolo(int p_track);
  318. void do_retrig(int p_track);
  319. void do_pan_slide(int p_track);
  320. void do_channel_volume_slide(int p_track);
  321. void do_volume_slide(int p_track, int inf);
  322. void do_pitch_slide_down(int p_track, uint8_t inf);
  323. void do_pitch_slide_up(int p_track, uint8_t inf);
  324. void do_tremor(int p_track);
  325. void do_vibrato(int p_track, bool fine);
  326. void do_pitch_slide_to_note(int p_track);
  327. void run_effects(int p_track);
  328. void run_volume_column_effects(int p_track);
  329. void pre_process_effects();
  330. void do_arpegio(int p_track);
  331. uint64_t song_usecs;
  332. /* NNA */
  333. void process_NNAs();
  334. /* MISC UTILS */
  335. int find_empty_voice();
  336. void process_volume_column(int p_track, uint8_t p_volume);
  337. void process_note(int p_track, CPNote p_note);
  338. /* CPTables */
  339. static uint8_t auto_vibrato_table[128];
  340. static uint8_t vibrato_table[32];
  341. static int8_t panbrello_table[256];
  342. static void callback_function(void *p_userdata);
  343. public:
  344. //Play modes
  345. enum {
  346. PLAY_NOTHING = 0,
  347. PLAY_PATTERN = 1,
  348. PLAY_SONG = 2
  349. };
  350. int32_t get_frequency(int32_t period);
  351. int32_t get_period(uint16_t note, int32_t p_c5freq);
  352. int get_current_tempo() { return control.tempo; };
  353. int get_current_speed() { return control.speed; };
  354. int get_voices_used() { return control.voices_used; };
  355. int get_voice_envelope_pos(int p_voice, CPEnvelope *p_envelope);
  356. int get_voice_amount_limit() { return control.max_voices; };
  357. void set_voice_amount_limit(int p_limit);
  358. void set_reserved_voices(int p_amount);
  359. int get_reserved_voices_amount();
  360. bool is_voice_active(int p_voice);
  361. int get_channel_voice(int p_channel);
  362. const char *get_voice_sample_name(int p_voice);
  363. const char *get_voice_instrument_name(int p_voice);
  364. CPEnvelope *get_voice_envelope(int p_voice, CPInstrument::EnvelopeType p_env_type);
  365. int get_voice_envelope_pos(int p_voice, CPInstrument::EnvelopeType p_env_type);
  366. int get_voice_volume(int p_voice);
  367. int get_voice_sample_index(int p_voice);
  368. void set_virtual_channels(int p_amount);
  369. int get_virtual_channels() { return control.max_voices; };
  370. /* Play Info/Position */
  371. bool is_playing() { return (control.play_mode > 0); };
  372. int get_play_mode() { return (control.play_mode); };
  373. int get_current_order() { return control.position.current_order; };
  374. int get_current_row() { return control.position.current_row; };
  375. int get_current_pattern() { return control.position.current_pattern; };
  376. void goto_next_order();
  377. void goto_previous_order();
  378. void process_tick();
  379. CPMixer *get_mixer_ptr() {
  380. return mixer;
  381. }
  382. void reset();
  383. /* External player control - editor - */
  384. void play_start_pattern(int p_pattern);
  385. void play_start_song();
  386. void play_start_song_from_order(int p_order);
  387. void play_start_song_from_order_and_row(int p_order, int p_row);
  388. void play_start(int p_pattern, int p_order, int p_row, bool p_lock = true);
  389. void play_stop();
  390. void play_note(int p_channel, CPNote note, bool p_reserve = false);
  391. bool reached_end_of_song();
  392. void set_force_no_nna(bool p_force);
  393. void set_force_external_vibratos(bool p_force);
  394. void set_filters_enabled(bool p_enable);
  395. bool are_filters_enabled() { return control.filters; }
  396. void set_channel_global_volume(int p_channel, int p_volume); //0-255
  397. int get_channel_global_volume(int p_channel) const;
  398. int64_t get_channel_last_note_time_usec(int p_channel) const;
  399. CPSong *get_song() { return song; };
  400. CPPlayer(CPMixer *p_mixer, CPSong *p_song);
  401. ~CPPlayer();
  402. };
  403. #endif