-
Notifications
You must be signed in to change notification settings - Fork 0
/
EX.cpp
228 lines (201 loc) · 5.18 KB
/
EX.cpp
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
227
228
#include "Arduino.h"
#include <Wire.h>
#include "EX.h"
EX::EX(bool i2cEnabled, byte i2cAddr, bool midiEnabled, byte midiChannel)
{
_i2cEnabled = i2cEnabled;
_i2cAddr = i2cAddr;
_midiEnabled = midiEnabled;
_midiChannel = midiChannel;
_target = 1;
for (int i=0; i<4; i++) {
recStatus[i] = false;
recEnd[i] = false;
}
}
void EX::send1(byte b1)
{
byte msg[] = {b1};
sendI2CCommand(msg, 1);
}
void EX::send2(byte b1, byte b2)
{
byte msg[] = {b1, b2};
sendI2CCommand(msg, 2);
}
void EX::send4(byte b1, byte b2, byte b3, byte b4)
{
byte msg[] = {b1, b2, b3, b4};
sendI2CCommand(msg, 4);
}
void EX::sendI2CCommand(byte msg[], int msgLength)
{
Wire.beginTransmission(_i2cAddr);
for (short b = 0; b < msgLength; b++) {
Wire.write(msg[b]);
}
Wire.endTransmission();
delay(100);
}
byte EX::getState(byte loop) {
send2(0x59, loop);
Wire.requestFrom(_i2cAddr, 1);
byte s;
while (Wire.available()) { // peripheral may send less than requested
s = Wire.read();
}
return s;
}
void EX::toggleCommand(byte cmd)
{
send4(0x46, cmd, 0x00, 0x00); // initialize at 0 to ensure toggle
send4(0x46, cmd, 0x00, 0x01);
send4(0x46, cmd, 0x00, 0x00);
}
void EX::toggleCC(byte cc)
{
usbMIDI.sendControlChange(cc, 0, _midiChannel);
delay(30);
usbMIDI.sendControlChange(cc, 127, _midiChannel);
delay(30);
usbMIDI.sendControlChange(cc, 0, _midiChannel);
}
void EX::setTarget(byte t)
{
Serial.print("\nset target ");
Serial.println(t);
if (_i2cEnabled) send4(0x46, 0x07, 0x00, t);
if (_midiEnabled) {
byte trigNote[] = {48, 50, 52, 53};
triggerNote(trigNote[t-1]);
}
_target = t;
}
void EX::setBitDepth()
{
// flip bit depth from 32 to 16 to force clearing all loops
Serial.println("\nset bitrate / clear loops");
if (_i2cEnabled) {
send4(0x46, 0x59, 0x00, 2);
send4(0x46, 0x59, 0x00, 1);
}
if (_midiEnabled) {
usbMIDI.sendControlChange(59, 127, _midiChannel);
delay(30);
usbMIDI.sendControlChange(59, 64, _midiChannel);
}
}
void EX::clearTarget()
{
// clears target loop then sets new target
Serial.print("\nclear target ");
Serial.println(_target);
if (_i2cEnabled) send1(0x58);
if (_midiEnabled) {
byte sysex[9] = {0xF0, 0x00, 0x21, 0x27, 0x5D, 0x60, 0x06, 0x58, 0xF7};
usbMIDI.sendSysEx(9, sysex, true);
}
}
void EX::clearAll()
{
// clears all loops
for (byte i=1; i<5; i++) {
setTarget(i);
delay(30);
clearTarget();
}
}
void EX::setRecordGain(short g)
{
if (_i2cEnabled) send4(0x46, 8, highByte(g), lowByte(g));
if (_midiEnabled) usbMIDI.sendControlChange(8, 111, _midiChannel);
}
void EX::setLoopPan(byte loop, short p)
{
if (_i2cEnabled) send4(0x46, 16 + loop, highByte(p), lowByte(p));
if (_midiEnabled) usbMIDI.sendControlChange(16 + loop, getCCVal(-100, 100, p), _midiChannel);
}
void EX::setLoopGain(byte loop, short g)
{
if (_i2cEnabled) send4(0x46, 20 + loop, highByte(g), lowByte(g));
if (_midiEnabled) usbMIDI.sendControlChange(20 + loop, getCCVal(-40, 6, g), _midiChannel);
}
void EX::setOverdubFade(short f)
{
if (_i2cEnabled) send4(0x46, 49, highByte(f), lowByte(f));
if (_midiEnabled) usbMIDI.sendControlChange(49, 125, _midiChannel);
}
void EX::randomTarget()
{
// sets random loop target
setTarget(random(1,5));
}
void EX::toggleRecord()
{
// toggles between recording and not then sets new random target
Serial.print("\ntoggle record ");
Serial.println(_target);
if (_i2cEnabled) toggleCommand(56);
if (_midiEnabled) triggerNote(55);
byte i = _target - 1;
recStatus[i] = !recStatus[i];
Serial.println(recStatus[i]);
if (recStatus[i] == true) recEnd[i] = millis() + random(2000, 19000);
}
void EX::togglePlay()
{
// toggles between play/pause then sets new random target
Serial.print("\nplay/pause/mute ");
Serial.println(_target);
if (_i2cEnabled) toggleCommand(57);
if (_midiEnabled) triggerNote(57);
}
void EX::toggleReverse()
{
//toggles between reverse and forward playback then sets new random target
Serial.print("\nreverse ");
Serial.println(_target);
if (_i2cEnabled) toggleCommand(58);
if (_midiEnabled) triggerNote(59);
}
void EX::toggleOctave()
{
// toggles between octave down and normal octave then sets new random target
Serial.print("\noctave down ");
Serial.println(_target);
if (_i2cEnabled) toggleCommand(62);
if (_midiEnabled) triggerNote(60);
}
void EX::triggerNote(byte note)
{
usbMIDI.sendNoteOn(note, 127, _midiChannel);
delay(100);
usbMIDI.sendNoteOff(note, 127, _midiChannel);
}
void EX::printStatus()
{
Serial.println("EX Status:");
Serial.print("MIDI Enabled: ");
Serial.println(_midiEnabled);
Serial.print("MIDI Channel: ");
Serial.println(_midiChannel);
Serial.print("I2C Enabled: ");
Serial.println(_i2cEnabled);
Serial.print("I2C Address: ");
Serial.println(_i2cAddr);
}
int EX::getCCVal(int low, int high, int cval)
{
int span = abs(low - high) + 1;
Serial.print("\nspan: ");
Serial.println(span);
float stepSize = (float)128/(float)span;
Serial.print("step size: ");
Serial.println(stepSize);
int adjVal = cval - low;
Serial.print("adjval: ");
Serial.println(adjVal);
Serial.println(int(adjVal * stepSize));
Serial.println();
return int(adjVal * stepSize);
}