-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayback.cpp
More file actions
223 lines (209 loc) · 6.44 KB
/
playback.cpp
File metadata and controls
223 lines (209 loc) · 6.44 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/*
plexus - a launchpad cv/gate sequencer/keyboard/arpeggiator
Copyright (C) 2017 Dougall IRVING
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "led.h"
#include "playback.h"
#include "hardware.h"
#include "notes.h"
unsigned int tick = 0;
Sequence sequences[SEQUENCE_COUNT];
Playback playback[VOICE_COUNT];
byte select_voice = 0;
// current output state of playback sequencer
byte playback_led[VOICE_COUNT] = {
0
};
boolean playback_gate[VOICE_COUNT] = {
false
};
boolean playback_led_on[VOICE_COUNT] = {
false
};
void playback_setup()
{
for (int idx = 0; idx < VOICE_COUNT; idx++)
{
playback[idx].playing = false;
playback[idx].playing = false;
playback[idx].button_down = false;
playback[idx].hold_on = false;
playback[idx].initial_button = 0;
playback[idx].current_step = 0;
playback[idx].current_button = 0;
playback[idx].gate = false;
playback[idx].remaining_gate_time = 0;
playback[idx].sequence = &sequences[idx];
}
//TBD Initialize these with something interesting or load from persistent memory
for (int idx = 0; idx < SEQUENCE_COUNT ; idx++ )
{
sequences[idx].length = 4;
for (int step = 0; step < SEQUENCE_LENGTH; step++ )
{
sequences[idx].steps[step].offset_x = step;
sequences[idx].steps[step].offset_y = step;
sequences[idx].steps[step].gate = true;
sequences[idx].steps[step].gate_length = 1;
}
}
}
void playback_start()
{
grid_led_update = true;
}
void playback_button_down ( const byte button, const byte voice)
{
if (!playback[voice].button_down)
{
playback[voice].button_down = true;
if (((playback[voice].playing) && (playback[voice].initial_button != button)) || (!playback[voice].playing))
{
playback[voice].playing = true;
playback[voice].initial_button = button;
playback[voice].current_step = 0;
playback[voice].current_button = button;
playback[voice].gate = playback[voice].sequence->steps[0].gate;
playback[voice].remaining_gate_time = playback[voice].sequence->steps[0].gate_length;
if (tick ==0)
playback[voice].remaining_gate_time += 1;
set_cv_output_hw(voice,get_note_number(playback[voice].current_button));
playback_led_on[voice] = true;
playback_led[voice] = button;
grid_led_update = true;
set_gate_output_hw(voice,playback[voice].gate);
}
}
}
void playback_button_up( const byte button, const byte voice)
{
if (playback[voice].button_down)
{
if (button != playback[voice].initial_button)
return;
playback[voice].button_down = false;
if (!playback[voice].hold_on)
{
playback[voice].playing = false;
playback_gate[voice] = false;
playback_led_on[voice] = false;
grid_led_update = true;
set_gate_output_hw(voice,false);
}
}
}
void playback_set_hold ( const byte voice, const boolean hold_state )
{
playback[voice].hold_on = hold_state;
//if we are currently holding a sequence then end it
if ((!hold_state) && (playback[voice].playing) && (!playback[voice].button_down))
{
playback[voice].playing = false;
playback_gate[voice] = false;
playback_led_on[voice] = false;
grid_led_update = true;
set_gate_output_hw(voice,false);
}
}
void playback_clock_rising ()
{
set_clock_output_hw(true);
for (int voice = 0; voice < VOICE_COUNT; voice++)
{
if (playback[voice].playing)
{
playback[voice].remaining_gate_time--;
if (playback[voice].remaining_gate_time == 0)
{
playback[voice].current_step++;
playback[voice].current_step %= playback[voice].sequence->length;
byte this_step = playback[voice].current_step;
unsigned char x = get_x_from_button(playback[voice].initial_button);
x += playback[voice].sequence->steps[this_step].offset_x;
while ( x < 0 )
x += 8;
x = x % GRID_WIDTH;
unsigned char y = get_y_from_button(playback[voice].initial_button);
y += playback[voice].sequence->steps[this_step].offset_y;
while ( y < 0 )
y += 8;
y = y % GRID_HEIGHT;
playback[voice].current_button = get_button_from_x_y(x, y);
playback_led[voice] = playback[voice].current_button ;
set_cv_output_hw(voice,get_note_number(playback[voice].current_button));
playback[voice].gate = playback[voice].sequence->steps[this_step].gate;
playback[voice].remaining_gate_time = playback[voice].sequence->steps[this_step].gate_length;
playback_gate[voice] = playback[voice].gate;
grid_led_update = true;
set_gate_output_hw(voice,playback[voice].gate);
}
}
}
}
void playback_clock_falling ()
{
set_clock_output_hw(false);
for (int voice = 0; voice < VOICE_COUNT; voice++)
{
if ((playback[voice].playing) && (playback[voice].remaining_gate_time <= 1))
{
playback_gate[voice] = false;
set_gate_output_hw(voice,false);
}
}
}
void playback_clock()
{
tick = (tick + 1) % 2;
if (tick)
playback_clock_rising();
else
playback_clock_falling();
}
boolean playback_led_update()
{
for (int idx = 0; idx < GRID_SIZE; idx++ )
{
if (( idx == playback_led[select_voice] ) && (playback_led_on[select_voice]))
{
if (set_grid_led(idx, LED_GREEN))
return true;
}
else if (( idx == playback_led[0] ) && (playback_led_on[0]))
{
if ( set_grid_led(idx, LED_YELLOW))
return true;
}
else if (( idx == playback_led[1] ) && (playback_led_on[1]))
{
if ( set_grid_led(idx, LED_YELLOW))
return true;
}
else if (( idx == playback_led[2] ) && (playback_led_on[2]))
{
if ( set_grid_led(idx, LED_YELLOW))
return true;
}
else if (( idx == playback_led[3] ) && (playback_led_on[3]))
{
if ( set_grid_led(idx, LED_YELLOW))
return true;
}
else
{
if ( set_grid_led(idx, LED_OFF))
return true;
}
}
return false;
}