1 /*************************************************************************/
2 /* cp_player_data.cpp */
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 #include "cp_player_data.h"
32 #include <stdio.h>
33
CPPlayer(CPMixer * p_mixer,CPSong * p_song)34 CPPlayer::CPPlayer(CPMixer *p_mixer, CPSong *p_song) {
35
36 song = p_song;
37 mixer = p_mixer;
38 control.max_voices = p_mixer->get_total_voice_count() - 1; //leave one for the sample
39 control.force_no_nna = false;
40 control.external_vibrato = false;
41 control.filters = true;
42 control.random_seed = 128364; //anything
43 control.play_mode = 0;
44 set_virtual_channels(p_mixer->get_total_voice_count());
45 mixer->set_callback(&CPPlayer::callback_function, this);
46
47 reset();
48 }
~CPPlayer()49 CPPlayer::~CPPlayer() {
50 }
51
set_virtual_channels(int p_amount)52 void CPPlayer::set_virtual_channels(int p_amount) {
53
54 if (p_amount < 1) return;
55 if (p_amount > mixer->get_total_voice_count())
56 return;
57
58 control.max_voices = p_amount;
59 }
60
callback_function(void * p_userdata)61 void CPPlayer::callback_function(void *p_userdata) {
62
63 CPPlayer *pd = (CPPlayer *)p_userdata;
64 pd->process_tick();
65 }
66
process_tick()67 void CPPlayer::process_tick() {
68
69 handle_tick();
70 mixer->set_callback_interval(2500000 / control.tempo);
71 song_usecs += 2500000 / control.tempo;
72 }
73
reset()74 void CPPlayer::reset() {
75
76 if (mixer == NULL) return;
77 if (song == NULL) return;
78
79 int i;
80
81 for (i = 0; i < control.max_voices; i++) {
82
83 voice[i].reset();
84 mixer->stop_voice(i);
85 }
86
87 for (i = 0; i < CPPattern::WIDTH; i++) {
88
89 control.channel[i].reset();
90 control.channel[i].channel_volume = song->get_channel_volume(i);
91 control.channel[i].channel_panning = ((int)song->get_channel_pan(i) * PAN_RIGHT / 64);
92 if (song->is_channel_surround(i))
93 control.channel[i].channel_panning = PAN_SURROUND;
94 control.channel[i].mute = song->is_channel_mute(i);
95 control.channel[i].chorus_send = song->get_channel_chorus(i) * 0xFF / 64;
96 control.channel[i].reverb_send = song->get_channel_reverb(i) * 0xFF / 64;
97 }
98
99 control.speed = song->get_speed();
100 control.tempo = song->get_tempo();
101 control.global_volume = song->get_global_volume();
102
103 control.position.current_pattern = 0;
104 control.position.current_row = 0;
105 control.position.current_order = 0;
106 control.position.force_next_order = -1;
107 control.ticks_counter = control.speed;
108 control.position.forbid_jump = false;
109
110 song_usecs = 0;
111 }
112
get_channel_last_note_time_usec(int p_channel) const113 int64_t CPPlayer::get_channel_last_note_time_usec(int p_channel) const {
114
115 CP_FAIL_INDEX_V(p_channel, 64, -1);
116 return control.channel[p_channel].last_event_usecs;
117 }
118
set_channel_global_volume(int p_channel,int p_volume)119 void CPPlayer::set_channel_global_volume(int p_channel, int p_volume) {
120
121 CP_FAIL_INDEX(p_channel, 64);
122 control.channel[p_channel].channel_global_volume = CLAMP(p_volume, 0, 255);
123 }
124
get_channel_global_volume(int p_channel) const125 int CPPlayer::get_channel_global_volume(int p_channel) const {
126
127 CP_FAIL_INDEX_V(p_channel, 64, -1);
128 return control.channel[p_channel].channel_global_volume;
129 }
130
reached_end_of_song()131 bool CPPlayer::reached_end_of_song() {
132
133 return control.reached_end;
134 }
set_force_external_vibratos(bool p_force)135 void CPPlayer::set_force_external_vibratos(bool p_force) {
136
137 control.external_vibrato = p_force;
138 }
set_force_no_nna(bool p_force)139 void CPPlayer::set_force_no_nna(bool p_force) {
140
141 control.force_no_nna = p_force;
142 }
143