-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThereMIDIn.cpp
More file actions
246 lines (222 loc) · 5.87 KB
/
Copy pathThereMIDIn.cpp
File metadata and controls
246 lines (222 loc) · 5.87 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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
// Do not remove the include below
#include "ThereMIDIn.h"
#include "LCDST7565.h"
#include "Bounce.h"
#include "Synthesizer.h"
#include "globals.h"
#include "MidiInterface.h"
Bounce up = Bounce(14, 12);
Bounce down = Bounce(3, 12);
Bounce back = Bounce(15, 12);
Bounce enter = Bounce(16, 12);
LCDST7565 lcd;
Synthesizer synth;
MidiInterface midi;
synth_preset_t synth_preset;
midi_configuration_t midiConf;
thereMIDIn_configuration_t config;
#define ADC_MAX 4095.f
#define ADC_HALF 2048.f
#define ADC_PIN_PITCH 2
#define ADC_PIN_VOLUME 3
#define MIDI_UPDATE_DEL 30
int pitchVal = 0;
int lastPitchVal = 0;
int volVal = 0;
int lastVolVal = 0;
int lastMidiUpdate = 0;
int lastTime = 0;
void setup()
{
// Configure pins
pinMode(3, INPUT_PULLUP);
pinMode(14, INPUT_PULLUP);
pinMode(15, INPUT_PULLUP);
pinMode(16, INPUT_PULLUP);
pinMode(13, OUTPUT);
// === Initialize default configuration ===
config.synthConf.masterGain = 0.45f;
config.synthConf.baseFreq = 500;
config.synthConf.enabled = true;
config.synthConf.semiRange = 12;
// OSC 1
config.synthConf.preset.osc[OSC1].enabled = true;
config.synthConf.preset.osc[OSC1].amplitude = 0.707f;
config.synthConf.preset.osc[OSC1].waveform = SINE;
config.synthConf.preset.osc[OSC1].wavetable_position = 0;
config.synthConf.preset.osc[OSC1].duty = 0.5f;
config.synthConf.preset.osc[OSC1].semitones = 0;
config.synthConf.preset.osc[OSC1].cents = 0;
// OSC 2
config.synthConf.preset.osc[OSC2].enabled = false;
config.synthConf.preset.osc[OSC2].amplitude = 0.3;
config.synthConf.preset.osc[OSC2].waveform = SQUARE;
config.synthConf.preset.osc[OSC2].wavetable_position = 0;
config.synthConf.preset.osc[OSC2].duty = 0.5f;
config.synthConf.preset.osc[OSC2].semitones = 0;
config.synthConf.preset.osc[OSC2].cents = 0;
// OSC 3
config.synthConf.preset.osc[OSC3].enabled = false;
config.synthConf.preset.osc[OSC3].amplitude = 0.6;
config.synthConf.preset.osc[OSC3].waveform = SAWTOOTH;
config.synthConf.preset.osc[OSC3].wavetable_position = 0;
config.synthConf.preset.osc[OSC3].duty = 0.5f;
config.synthConf.preset.osc[OSC3].semitones = 0;
config.synthConf.preset.osc[OSC3].cents = 0;
// Filter
config.synthConf.preset.filter.dryWet = 1.f;
config.synthConf.preset.filter.serParCF = 0.5f;
config.synthConf.preset.filter.flt1.enabled = true;
config.synthConf.preset.filter.flt1.frequency = 800.f;
config.synthConf.preset.filter.flt1.resonance = 1.0f;
config.synthConf.preset.filter.flt2.frequency = 1200.f;
// Set the synth configuration
synth.setConfiguration( &config.synthConf );
// MIDI
config.midiConf.antenna[CT_PITCH].cc = 0;
config.midiConf.antenna[CT_PITCH].use14Bit = true;
config.midiConf.antenna[CT_PITCH].muted = true;
config.midiConf.antenna[CT_VOLUME].cc = 1;
config.midiConf.antenna[CT_VOLUME].use14Bit = false;
config.midiConf.antenna[CT_VOLUME].muted = true;
config.midiConf.patch = 0;
config.midiConf.baseNote = 60;
config.midiConf.velocity = 100;
config.midiConf.enabled = true;
// Set the MIDI configuration
midi.setConfiguration( &config.midiConf );
lcd.setSynthesizerInstance( &synth );
lcd.setMidiInstance( &midi );
lcd.enterMenu( MENU_HOME );
lcd.update();
}
uint8_t led = 0xFF;
int lastPressUp = 0;
int lastPressDown = 0;
bool repeatUp = false;
bool repeatDown = false;
int lastPressBack = 0;
int lastPressOk = 0;
bool repeatBack = false;
bool repeatOk = false;
#define REPEAT_SPEED 80
#define REPEAT_DELAY 500
void loop()
{
if(up.fallingEdge())
{
lcd.buttonEvent(BUT_UP, PRESS_EVENT);
lastPressUp = millis();
}
else if(!up.read())
{
if(repeatUp && millis() - lastPressUp >= REPEAT_SPEED)
{
lcd.buttonEvent(BUT_UP, REPEATED_PRESS_EVENT);
lastPressUp = millis();
}
else if(millis() - lastPressUp >= REPEAT_DELAY)
{
lastPressUp = millis();
repeatUp = true;
}
}
else if(up.risingEdge())
{
repeatUp = false;
lcd.buttonEvent(BUT_UP, RELEASE_EVENT);
}
if(down.fallingEdge())
{
lcd.buttonEvent(BUT_DOWN, PRESS_EVENT);
lastPressDown = millis();
}
else if(!down.read())
{
if(repeatDown && millis() - lastPressDown >= REPEAT_SPEED)
{
lcd.buttonEvent(BUT_DOWN, REPEATED_PRESS_EVENT);
lastPressDown = millis();
}
else if(millis() - lastPressDown >= REPEAT_DELAY)
{
lastPressDown = millis();
repeatDown = true;
}
}
else if(down.risingEdge())
{
repeatDown = false;
lcd.buttonEvent(BUT_DOWN, RELEASE_EVENT);
}
if(back.fallingEdge())
{
lcd.buttonEvent(BUT_BACK, PRESS_EVENT);
lastPressBack = millis();
}
else if(!back.read())
{
if(repeatBack && millis() - lastPressBack >= REPEAT_SPEED)
{
lcd.buttonEvent(BUT_BACK, REPEATED_PRESS_EVENT);
lastPressBack = millis();
}
else if(millis() - lastPressBack >= REPEAT_DELAY)
{
lastPressBack = millis();
repeatBack = true;
}
}
else if(back.risingEdge())
{
repeatBack = false;
lcd.buttonEvent(BUT_BACK, RELEASE_EVENT);
}
if(enter.fallingEdge())
{
lcd.buttonEvent(BUT_OK, PRESS_EVENT);
lastPressOk = millis();
}
else if(!enter.read())
{
if(repeatOk && millis() - lastPressOk >= REPEAT_SPEED)
{
lcd.buttonEvent(BUT_OK, REPEATED_PRESS_EVENT);
lastPressOk = millis();
}
else if(millis() - lastPressOk >= REPEAT_DELAY)
{
lastPressOk = millis();
repeatOk = true;
}
}
else if(enter.risingEdge())
{
repeatOk = false;
lcd.buttonEvent(BUT_OK, RELEASE_EVENT);
}
up.update();
back.update();
down.update();
enter.update();
if(millis() - lastMidiUpdate > MIDI_UPDATE_DEL)
{
lastMidiUpdate = millis();
pitchVal = analogRead(ADC_PIN_PITCH);
if( pitchVal != lastPitchVal )
{
float offset = (float) (pitchVal - ADC_HALF) / ADC_MAX;
lastPitchVal = pitchVal;
midi.setOffset( CT_PITCH, offset);
synth.setOffset( CT_PITCH, offset);
}
volVal = analogRead(ADC_PIN_VOLUME);
if( volVal != lastVolVal )
{
float offset = (float) (volVal - ADC_HALF) / ADC_MAX;
lastVolVal = volVal;
midi.setOffset( CT_VOLUME, offset);
synth.setOffset( CT_VOLUME, offset);
}
}
}