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