-
Notifications
You must be signed in to change notification settings - Fork 0
/
zero_9833_triangle.ino
292 lines (236 loc) · 8.47 KB
/
zero_9833_triangle.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*
AD9833 Waveform Module
*/
#include <SPI.h>
#include "pitches.h"
#include "Parameters.h"
#include <MIDI.h>
#include "AD9833.h"
#define MIDI_CHANNEL 1
AD9833 AD(5, 3, 2); // SW SPI over the HW SPI pins (UNO);
AD9833 AD1(9, 3, 2); // SW SPI over the HW SPI pins (UNO);
MIDI_CREATE_INSTANCE(HardwareSerial, Serial1, MIDI);
void setup() {
analogReadResolution(12);
pinMode(GATE_PIN, OUTPUT); // gate output
pinMode(TRIG_PIN, OUTPUT); // trig output
pinMode(LED_PIN, OUTPUT); // led output
// Initialize pins to LOW
digitalWrite(GATE_PIN, LOW);
digitalWrite(TRIG_PIN, LOW);
digitalWrite(LED_PIN, LOW);
pinMode(SUB_OUT1, OUTPUT); // Square wave output 1
pinMode(SUB_OUT2, OUTPUT); // Square wave output 2
// Initialize pins to LOW
digitalWrite(SUB_OUT1, LOW);
digitalWrite(SUB_OUT2, LOW);
// Configure PWM pins as outputs
pinMode(VELOCITY_PWM_PIN, OUTPUT);
pinMode(NOTE_PWM_PIN, OUTPUT);
// Set initial PWM duty cycle to 0
analogWrite(VELOCITY_PWM_PIN, 0);
analogWrite(NOTE_PWM_PIN, 0);
pinMode(SYNC_PIN, OUTPUT);
digitalWrite(SYNC_PIN, LOW);
MIDI.begin(0);
MIDI.setHandleControlChange(myControlChange);
MIDI.setHandleNoteOn(DinHandleNoteOn);
MIDI.setHandleNoteOff(DinHandleNoteOff);
MIDI.setHandlePitchBend(myPitchBend);
SPI.begin();
AD.begin();
AD1.begin();
AD.setWave(AD9833_TRIANGLE);
AD.setFrequency(880.0, 0);
AD.setFrequencyChannel(0);
AD1.setWave(AD9833_TRIANGLE);
AD1.setFrequency(880.0, 0);
AD1.setFrequencyChannel(0);
numnotes = sizeof(notes) / sizeof(notes[0]);
bend_factor = 1;
delay(2000);
}
void myControlChange(byte channel, byte control, byte value) {
switch (control) {
case 5:
{ // Glide time
// Use braces to scope variables properly
float linearValue = map(value, 0, 127, 0, 4000); // Convert CC to 0-4000 range
glidetime = mapToExponential(linearValue, 1.5); // Apply exponential mapping
break;
}
case 65: // Glide on/off
glideEnabled = (value >= 64); // Turn on if value >= 64
break;
case 17:
oct_sel1 = map(value, 0, 127, 0, 2);
switch (oct_sel1) {
case 0:
oct_sw1 = 0.5;
break;
case 1:
oct_sw1 = 1;
break;
case 2:
oct_sw1 = 2;
break;
}
break;
case 18:
detune = map(value, 0, 127, 0, 15);
break;
case 19:
oct_sel2 = map(value, 0, 127, 0, 2);
switch (oct_sel2) {
case 0:
oct_sw2 = 0.5;
break;
case 1:
oct_sw2 = 1;
break;
case 2:
oct_sw2 = 2;
break;
}
break;
case 20:
interval = value;
if (interval > 12) {
interval = 12;
}
break;
case 21:
// Keytrack 0-127
keytrack = value;
update_keytrack(keytrack_pitch);
break;
case 22:
// Keytrack on/off
keytrack_on = (value >= 64);
update_keytrack(keytrack_pitch);
break;
case 23: // Sync on/off
syncEnabled = (value >= 64); // Turn on if value >= 64
if (syncEnabled) {
digitalWrite(SYNC_PIN, HIGH);
} else {
digitalWrite(SYNC_PIN, LOW);
}
break;
}
}
void update_keytrack(byte pitch) {
int noteDutyCycle;
if (keytrack_on) {
// Scale note value based on keytrack
int scaledValue = map(keytrack, 0, 127, 0, 100); // Scale keytrack to a percentage (0-100%)
noteDutyCycle = map(pitch * scaledValue / 100, 0, 127, 0, 255); // Apply scaling to pitch and map to PWM range
} else {
// Turn off the keytrack
noteDutyCycle = 0;
}
analogWrite(NOTE_PWM_PIN, noteDutyCycle); // Set the PWM output
}
void DinHandleNoteOn(byte channel, byte pitch, byte velocity) {
if (channel == MIDI_CHANNEL) {
if (velocity == 0) {
// Treat velocity 0 as Note Off
DinHandleNoteOff(channel, pitch, velocity);
return;
}
keytrack_pitch = pitch;
update_keytrack(keytrack_pitch);
// Set PWM duty cycle for velocity
int velocityDutyCycle = map(velocity, 0, 127, 0, 255); // Map velocity to 8-bit range
analogWrite(VELOCITY_PWM_PIN, velocityDutyCycle);
// Setup glide parameters
if (glideEnabled) {
glideStartFrequency1 = currentGlideFrequency1;
glideStartFrequency2 = currentGlideFrequency2;
glideTargetFrequency1 = notes[(pitch - MIDI_NOTE_START)];
glideTargetFrequency2 = notes[(pitch - MIDI_NOTE_START)]; // Base note without interval
isGliding = true;
} else {
// No glide, set directly
glideTargetFrequency1 = notes[(pitch - MIDI_NOTE_START)];
glideTargetFrequency2 = notes[(pitch - MIDI_NOTE_START)]; // Base note without interval
currentGlideFrequency1 = glideTargetFrequency1;
currentGlideFrequency2 = glideTargetFrequency2;
isGliding = false;
}
noteon = pitch; // Store the active note
digitalWrite(TRIG_PIN, HIGH);
TRIG_START = millis(); // Record the timestamp
digitalWrite(GATE_PIN, HIGH);
digitalWrite(LED_PIN, HIGH);
}
}
void DinHandleNoteOff(byte channel, byte note, byte velocity) {
if (channel == MIDI_CHANNEL) {
digitalWrite(GATE_PIN, LOW);
digitalWrite(LED_PIN, LOW);
}
}
void myPitchBend(byte channel, int bend) {
if (bend >= 0) {
// Increase frequency until it's twice the original at maximum positive bend
bend_factor = 1.0 + bend / 8192.0;
} else {
// Decrease frequency until it's half the original at maximum negative bend
bend_factor = 1.0 / (1.0 - bend / 8192.0);
}
}
// Function to map linear input to exponential curve
float mapToExponential(float x, float exponent) {
// Ensure x is within the range [0, 4000]
x = constrain(x, 0, 4000);
// Map the linear input to exponential curve
float y = pow(x / 4000.0, exponent) * 4000.0;
return y;
}
void loop() {
MIDI.read(); // Handle MIDI events
if (millis() - TRIG_START >= TRIG_LENGTH) {
digitalWrite(TRIG_PIN, LOW);
}
// Read FM input
adcValue = analogRead(ADC0_PIN);
modulation = (adcValue - ADC_CENTER) / float(ADC_CENTER);
fmModulation = pow(2.0, modulation * FM_RANGE);
// Handle glide if enabled
unsigned long currentMillis = millis();
if (isGliding && currentMillis - lastGlideUpdate >= 10) { // Update every 10ms
float step = (glidetime > 0) ? (10.0 / glidetime) : 1.0; // Step size based on glidetime
currentGlideFrequency1 += (glideTargetFrequency1 - currentGlideFrequency1) * step;
currentGlideFrequency2 += (glideTargetFrequency2 - currentGlideFrequency2) * step;
if (abs(currentGlideFrequency1 - glideTargetFrequency1) < 0.1 && abs(currentGlideFrequency2 - glideTargetFrequency2) < 0.1) {
// Glide finished
currentGlideFrequency1 = glideTargetFrequency1;
currentGlideFrequency2 = glideTargetFrequency2;
isGliding = false;
}
lastGlideUpdate = currentMillis;
}
// Dynamically calculate the interval-adjusted frequency for oscillator 2
float intervalFrequency2 = currentGlideFrequency2 * pow(2.0, interval / 12.0); // Apply interval as a semitone offset
// Apply modulation and settings
frequency1 = currentGlideFrequency1 * oct_sw1 * bend_factor * fmModulation;
frequency2 = intervalFrequency2 * oct_sw2 * bend_factor * fmModulation - detune;
squarewaveFrequency1 = frequency1 / 2.0; // One octave below
squarewaveFrequency2 = frequency2 / 2.0; // One octave below
// Toggle GPIO 10 for square wave 1
interval1 = 1000000.0 / squarewaveFrequency1; // Microseconds per half-period
if (micros() - lastToggle1 >= interval1) {
digitalWrite(SUB_OUT1, !digitalRead(SUB_OUT1)); // Toggle pin
lastToggle1 = micros();
}
// Toggle GPIO 11 for square wave 2
unsigned long interval2 = 1000000.0 / squarewaveFrequency2; // Microseconds per half-period
if (micros() - lastToggle2 >= interval2) {
digitalWrite(SUB_OUT2, !digitalRead(SUB_OUT2)); // Toggle pin
lastToggle2 = micros();
}
// Update the AD9833 frequencies
AD.setFrequency(frequency1, 0);
AD1.setFrequency(frequency2, 0);
}