-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsequence.c
More file actions
209 lines (164 loc) · 6.16 KB
/
sequence.c
File metadata and controls
209 lines (164 loc) · 6.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include "sequence.h"
void delete_sequence_step(FlizzerTrackerApp* tracker) {
uint8_t sequence_position = tracker->tracker_engine.sequence_position;
uint8_t* pattern = &tracker->tracker_engine.song->sequence.sequence_step[sequence_position]
.pattern_indices[tracker->current_channel];
*pattern = 0;
}
void edit_sequence_step(FlizzerTrackerApp* tracker, int8_t delta) {
uint8_t digit = tracker->current_digit;
uint8_t sequence_position = tracker->tracker_engine.sequence_position;
uint8_t pattern_index = tracker->tracker_engine.song->sequence.sequence_step[sequence_position]
.pattern_indices[tracker->current_channel];
uint8_t* pattern = &tracker->tracker_engine.song->sequence.sequence_step[sequence_position]
.pattern_indices[tracker->current_channel];
uint8_t temp_pattern = *pattern;
switch(digit) {
case 0: // upper nibble
{
int8_t nibble = ((pattern_index & 0xf0) >> 4);
if(nibble + delta < 0) {
nibble = 0xf;
}
else if(nibble + delta > 0xf) {
nibble = 0;
}
else {
nibble += delta;
}
temp_pattern &= 0x0f;
temp_pattern |= (nibble << 4);
break;
}
case 1: // lower nibble
{
int8_t nibble = (pattern_index & 0x0f);
if(nibble + delta < 0) {
nibble = 0xf;
}
else if(nibble + delta > 0xf) {
nibble = 0;
}
else {
nibble += delta;
}
temp_pattern &= 0xf0;
temp_pattern |= nibble;
break;
}
}
if(check_and_allocate_pattern(&tracker->song, temp_pattern)) {
*pattern = temp_pattern;
}
}
void sequence_edit_event(FlizzerTrackerApp* tracker, FlizzerTrackerEvent* event) {
if(event->input.key == InputKeyOk && event->input.type == InputTypeShort &&
!tracker->tracker_engine.playing) {
tracker->editing = !tracker->editing;
}
if(event->input.key == InputKeyOk && event->input.type == InputTypeLong) {
if(!(tracker->editing)) {
if(tracker->tracker_engine.playing) {
stop_song(tracker);
}
else {
if(tracker->tracker_engine.pattern_position == tracker->song.pattern_length - 1 &&
tracker->tracker_engine.sequence_position ==
tracker->song.num_sequence_steps -
1) // if we are at the very end of the song
{
stop_song(tracker);
}
else {
play_song(tracker, true);
}
}
}
}
if(event->input.key == InputKeyRight && event->input.type == InputTypeShort) {
tracker->current_digit++;
if(tracker->current_digit > 1) {
tracker->current_channel++;
tracker->current_digit = 0;
if(tracker->current_channel > SONG_MAX_CHANNELS - 1) {
tracker->current_channel = 0;
}
}
}
if(event->input.key == InputKeyLeft && event->input.type == InputTypeShort) {
tracker->current_digit--;
if(tracker->current_digit > 1) // unsigned int overflow
{
tracker->current_channel--;
tracker->current_digit = 1;
if(tracker->current_channel > SONG_MAX_CHANNELS - 1) // unsigned int overflow
{
tracker->current_channel = SONG_MAX_CHANNELS - 1;
}
}
}
if(event->input.key == InputKeyDown && event->input.type == InputTypeShort) {
if(!(tracker->editing)) {
tracker->tracker_engine.sequence_position++;
if(tracker->tracker_engine.sequence_position >=
tracker->tracker_engine.song->num_sequence_steps) {
tracker->tracker_engine.sequence_position = 0;
}
}
if(tracker->editing) {
edit_sequence_step(tracker, -1);
}
}
if(event->input.key == InputKeyUp && event->input.type == InputTypeShort) {
if(!(tracker->editing)) {
int16_t temp_sequence_position = tracker->tracker_engine.sequence_position - 1;
if(temp_sequence_position < 0) {
tracker->tracker_engine.sequence_position =
tracker->tracker_engine.song->num_sequence_steps - 1;
}
else {
tracker->tracker_engine.sequence_position--;
}
}
if(tracker->editing) {
edit_sequence_step(tracker, 1);
}
}
if(event->input.key == InputKeyRight && event->input.type == InputTypeLong &&
!(tracker->editing)) // set loop begin or loop end for the song
{
TrackerSong* song = &tracker->song;
if(song->loop_start == song->loop_end && song->loop_end == 0) // if both are 0
{
song->loop_end = tracker->tracker_engine.sequence_position;
}
else {
if(tracker->tracker_engine.sequence_position < song->loop_end) {
song->loop_start = tracker->tracker_engine.sequence_position;
}
if(tracker->tracker_engine.sequence_position > song->loop_start) {
song->loop_end = tracker->tracker_engine.sequence_position;
}
}
}
if(event->input.key == InputKeyLeft && event->input.type == InputTypeLong &&
!(tracker->editing)) // erase loop begin and loop end points
{
TrackerSong* song = &tracker->song;
song->loop_start = song->loop_end = 0;
}
if(event->input.key == InputKeyUp && event->input.type == InputTypeLong &&
!(tracker->editing)) // jump to the beginning
{
tracker->tracker_engine.sequence_position = 0;
}
if(event->input.key == InputKeyDown && event->input.type == InputTypeLong &&
!(tracker->editing)) // jump to the end
{
tracker->tracker_engine.sequence_position = tracker->song.num_sequence_steps - 1;
}
if(event->input.key == InputKeyBack && event->input.type == InputTypeShort &&
tracker->editing) {
delete_sequence_step(tracker);
}
}