forked from bastl-instruments/DRUM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UI.ino
260 lines (178 loc) · 6.14 KB
/
UI.ino
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
224
225
226
unsigned char page;
unsigned char sound,lastSound;
#define NUMBER_OF_PAGES 2
#define KNOB_MODE SMALL_BUTTON_2
#define DEFAULT_VELOCITY 127
boolean combo;
int var[NUMBER_OF_VOICES][6]; //??
boolean shift;
unsigned char volume[NUMBER_OF_VOICES];
unsigned char crush[NUMBER_OF_VOICES];
unsigned char currentSound[NUMBER_OF_VOICES];
void UI(){
if(test) testMode();
else{
hw.setColor(page+2);
renderSmallButtons();
renderCombo();
renderBigButtons();
renderKnobs();
renderTweaking(page);
renderTimeStretch();
}
}
#define VOLUME_COMPENSATION 100
void playSound(unsigned char _SOUND, unsigned char _VELOCITY){
unsigned char voice=_SOUND%3;
currentSound[voice]=_SOUND;
unsigned char _sample=map(getVar(_SOUND,SAMPLE),0,8,0,NUMBER_OF_WAVETABLES);
aSample[voice].setTable(WAVE_TABLES[_sample],WAVE_TABLES_NUM_CELLS[_sample]);
aSample[voice].setFreq((float)(getVar(_SOUND,RATE)+1)/16);
aSample[voice].setTimeStretch((getVar(_SOUND,STRETCH)<<2)+1);
aSample[voice].setEnd(map(getVar(_SOUND,CUT),0,255,16,aSample[voice].getLength()));
volume[voice]=(getVar(_SOUND,VOLUME)*(_VELOCITY))>>7;
crush[voice]=getVar(_SOUND,CRUSH);
aSample[voice].start();
}
int shiftSpeed;
void renderTimeStretch(){
for(int voice=0;voice<NUMBER_OF_VOICES;voice++){
if(aSample[voice].isPlaying()) aSample[voice].timeStretchUpdate();
}
}
void renderTweaking(unsigned char _page){
for(int voice=0;voice<NUMBER_OF_VOICES;voice++){
switch(_page){
case PAGE_G:
aSample[voice].setFreq((float)(getVar(currentSound[voice],RATE)+1)/16);
aSample[voice].setTimeStretch((getVar(currentSound[voice],STRETCH)<<2)+1);
aSample[voice].setEnd(map(getVar(currentSound[voice],CUT),0,255,16,aSample[voice].getLength()));
break;
case PAGE_B:
volume[voice]=getVar(currentSound[voice],VOLUME);
crush[voice]=getVar(currentSound[voice],CRUSH);
break;
}
}
//tweaking end
}
void renderSmallButtons(){
// knobMode
if(hw.justReleased(KNOB_MODE) && !combo){
if(page<NUMBER_OF_PAGES-1) page++;
else page=0;
hw.freezeAllKnobs();
}
if(hw.justPressed(SMALL_BUTTON_1)) shift=!shift;
if(hw.justReleased(SMALL_BUTTON_1) && !bootShift) shift=!shift;
if(hw.justPressed(EXTRA_BUTTON_1)) shift=!shift;
}
#define LONG_THRESHOLD 40
boolean countLong, longPress;
int longCount;
void renderCombo(){
if(countLong) {
longCount++;
if(longCount>LONG_THRESHOLD) longPress=true;
}
//loading saving presets
// if(hw.buttonState(SMALL_BUTTON_2) && hw.justPressed(SMALL_BUTTON_1)) loadPreset(0), hw.freezeAllKnobs(), combo=true;
for(int i=0;i<3;i++){
if(hw.buttonState(EXTRA_BUTTON_2) && hw.justPressed(i)) randomize(i+3*shift); // randomize
if(hw.buttonState(SMALL_BUTTON_1) && hw.buttonState(SMALL_BUTTON_2) && hw.justPressed(i)) countLong=true,longCount=0, combo=true; // save&load
if(hw.buttonState(SMALL_BUTTON_1) && hw.buttonState(SMALL_BUTTON_2) && hw.justReleased(i)){
if(longPress) storePreset(currentPreset),loadPreset(i+3), hw.freezeAllKnobs(), combo=true; // save&load
else storePreset(currentPreset), loadPreset(i+3*hw.buttonState(EXTRA_BUTTON_2)), hw.freezeAllKnobs(), combo=true;
// countLong=false, longCount=0,longPress=false;
}
if(hw.buttonState(SMALL_BUTTON_2) && hw.justPressed(i)) countLong=true,longCount=0, combo=true; // load
if(hw.buttonState(SMALL_BUTTON_2) && hw.justReleased(i)){
if(longPress) loadPreset(i+3), hw.freezeAllKnobs();
else loadPreset(i+3*hw.buttonState(EXTRA_BUTTON_2)), hw.freezeAllKnobs();
// countLong=false, longCount=0, longPress=false;
}
}
if(combo){
//turn off combo when all buttons are released
unsigned char _count=0;
for(int i=0;i<NUMBER_OF_BUTTONS;i++) _count+=hw.buttonState(i); // if(!hw.buttonState(i)) combo=false;
if(_count==0) combo=false,countLong=false, longCount=0,longPress=false;
//else combo=true;
}
}
void randomize(unsigned char _sound){
for(int i=0;i<NUMBER_OF_VARIABLES;i++) setVar(_sound,i,rand(maxValue[i])), hw.freezeAllKnobs();
}
void renderBigButtons(){
if(!combo){
for(int i=BIG_BUTTON_1;i<=BIG_BUTTON_3;i++) {
hw.setLed(i,hw.buttonState(i));
if(longPress) hw.setLed(i,true);
if(hw.justPressed(i)) {
if(!shift) sound=i, playSound(sound,DEFAULT_VELOCITY), shiftSpeed=0;
else sound=i+3, playSound(sound,DEFAULT_VELOCITY);
}
if(hw.justReleased(i)) ;
};
if(sound!=lastSound) hw.freezeAllKnobs();
lastSound=sound;
}
for(int i=BIG_BUTTON_1;i<=BIG_BUTTON_3;i++){
if(longPress) hw.setLed(i,true);
}
}
void renderKnobs(){
for(int i=0;i<NUMBER_OF_KNOBS;i++){
unsigned char _variable=i+VARIABLES_PER_PAGE*page;
if(hw.knobFreezed(i)) {
if(lastSound==sound){ // knobs are unfreezed when they come across their original value
if(inBetween( scale(hw.knobValue(i),KNOB_BITS,variableDepth[_variable]), scale(hw.lastKnobValue(i),KNOB_BITS,variableDepth[_variable]),getVar(sound,_variable) ) ) hw.unfreezeKnob(i),hw.setColor(WHITE); //external unfreez
}
}
else{
setVar(sound,_variable,scale(hw.knobValue(i),KNOB_BITS,variableDepth[_variable]));
}
}
}
int increaseValue(int _VALUE, int _OVERFLOW){
if(_VALUE < _OVERFLOW) _VALUE++;
else _VALUE=0;
return _VALUE;
}
int increaseValue(int _VALUE, int _OVERFLOW, int _INCREMENT){
if(_VALUE < _OVERFLOW) _VALUE+=_INCREMENT;
return _VALUE;
}
int decreaseValue(int _VALUE, int _OVERFLOW, int _INCREMENT){
if(_VALUE > _OVERFLOW) _VALUE-=_INCREMENT;
return _VALUE;
}
unsigned char getBits(unsigned char _val, unsigned char _offset, unsigned char _bits){
unsigned char returnVal=0;
for(int i=0;i<_bits;i++){
bitWrite(returnVal,i,bitRead(_val,_offset+i));
}
return returnVal;
}
void animation(){
hw.setLed(LED_1,true);
hw.update();
delay(150);
hw.setLed(LED_3,true);
hw.update();
delay(150);
hw.setLed(LED_2,true);
hw.update();
delay(150);
hw.setColor(BLUE);
hw.update();
delay(150);
hw.setColor(WHITE);
hw.update();
delay(150);
hw.setColor(BLACK);
hw.setLed(LED_1,false);
hw.setLed(LED_2,false);
hw.setLed(LED_3,false);
hw.update();
}