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
293
294
295
296
297
298
299
300
301
302
303
304
305
|
/* Digital MIDI monophonic synth
*
* Jan Tuomi 2016
* This project is in the public domain.
* Based on a public domain project by Tim Barrass, 2013.
*
* Connections:
* PIN 9 - AUDIO OUT
* RX - MIDI SIGNAL IN
* PIN 10 - WAVEFORM TOGGLE BUTTON
* ANALOG 0 - DETUNE
* ANALOG 1 - ATTACK
* ANALOG 2 - DECAY
* ANALOG 3 - SUSTAIN
* ANALOG 4 - RELEASE
* ANALOG 5 - VIBRATO AMP
*/
#include <MIDI.h>
#include <MozziGuts.h>
#include <Oscil.h> // oscillator template
#include <Line.h> // for envelope
#include <mozzi_midi.h>
#include <ADSR.h>
#include <mozzi_fixmath.h>
#include <Portamento.h>
/* Include waveform tables for oscillators */
#include <tables/cos512_int8.h>
#include <tables/square_analogue512_int8.h>
#include <tables/triangle_analogue512_int8.h>
#include <tables/saw_analogue512_int8.h>
// use #define for CONTROL_RATE, not a constant
#define CONTROL_RATE 256 // powers of 2 please
// Mozzi example uses COS waves for carrier and modulator
// Shit gets brutal very fast if you use a saw/square carrier
// Shit gets subtly more brutal if you use smaller wavetables (fewer samples per cycle)
/* Use an array of three oscillators
* The first will get double the amplitude */
Oscil<SAW_ANALOGUE512_NUM_CELLS, AUDIO_RATE> oscils[3] =
{(SAW_ANALOGUE512_DATA), (SAW_ANALOGUE512_DATA), (SAW_ANALOGUE512_DATA)};
/* Add a modulation and LFO oscillator */
Oscil<COS512_NUM_CELLS, AUDIO_RATE> oscModulator(COS512_DATA);
Oscil<COS512_NUM_CELLS, AUDIO_RATE> oscLFO(COS512_DATA);
/* Keep track of the currently selected waveform with an enum */
enum TableType {
Cosine,
Square,
Saw,
Triangle
} currentTable;
/* Store table data in a table for nice access */
const int8_t *tables[4] =
{ COS512_DATA, SQUARE_ANALOGUE512_DATA, SAW_ANALOGUE512_DATA, TRIANGLE_ANALOGUE512_DATA };
/* ADSR envelope for tuning the tone */
ADSR <CONTROL_RATE, AUDIO_RATE> envelope;
Portamento <CONTROL_RATE>aPortamento;
MIDI_CREATE_DEFAULT_INSTANCE();
/* Pin constants */
/* LED is used for waveform toggle visualisation */
#define LED 13
/* Table toggle button for changing waveform */
#define TABLE_TOGGLE 10
const uint8_t ANALOG_INPUTS[]
= {0, 1, 2, 3, 4, 5};
// workaround for a idle buzzing bug
int sustainKillTimer = 0;
float carrierFreq = 10.f;
float modFreq = 10.f;
float modDepth = 0.5;
float amplitude = 0.f;
float modOffset = 1;
byte lastnote = 0;
//int portSpeed = 100;
byte detuneCents = 0;
int analog_values[6] = { 0 };
int tableToggleTimer = 0;
int TABLE_TOGGLE_TIMER_MAX = 100;
float detuneCoefficients[] = {1, 1};
float modOffsets[] = {
4, 3.5, 3, 2.5,
2, 1.5, 1, 0.6666667,
0.5, 0.4, 0.3333333, 0.2857,
0.25, 0, 0, 0,
0, 0, 0
}; // freq ratios corresponding to DP's preferred intervals of 7, 12, 7, 19, 24, 0, 12, -12, etc
int attackTime, decayTime, releaseTime;
int sustainLevel;
float vibratoAmp = 0.f;
/* Maximum value for int */
const int SUSTAIN_TIME = (~0 >> 1);
void setup()
{
pinMode(LED, OUTPUT);
pinMode(TABLE_TOGGLE, INPUT);
MIDI.begin(MIDI_CHANNEL_OMNI);
MIDI.setHandleNoteOn(HandleNoteOn);
MIDI.setHandleNoteOff(HandleNoteOff);
MIDI.setHandleControlChange(HandleControlChange);
MIDI.setHandlePitchBend(HandlePitchBend);
MIDI.setHandleProgramChange(HandleProgramChange);
envelope.setADLevels(255, 174);
attackTime = 188;
decayTime = 345;
sustainLevel = 255;
releaseTime = 345;
envelope.setTimes(attackTime, decayTime, SUSTAIN_TIME, releaseTime); // 20000 is so the note will sustain 20 seconds unless a noteOff comes
envelope.setSustainLevel(255);
oscLFO.setFreq(10); // default frequency
startMozzi(CONTROL_RATE);
}
void HandleProgramChange (byte channel, byte number)
{
// Use MIDI Program Change to select an interval between carrier and modulator oscillator
modOffset = modOffsets[number % 15];
}
void HandleNoteOn(byte channel, byte note, byte velocity)
{
//aPortamento.start((byte)(((int) note) - 5));
lastnote = note;
envelope.noteOn();
oscLFO.setPhase(0);
oscModulator.setPhase(0);
sustainKillTimer = -1;
digitalWrite(LED, HIGH);
}
void HandleNoteOff(byte channel, byte note, byte velocity)
{
if (note == lastnote)
{
envelope.noteOff();
sustainKillTimer = releaseTime;
digitalWrite(LED, LOW);
}
}
void HandlePitchBend (byte channel, int bend)
{
float shifted = float ((bend + 8500) / 2048.f) + 0.1f;
oscLFO.setFreq(shifted);
}
void HandleControlChange (byte channel, byte number, byte value)
{
if(number == 1)
{
float divided = float(value / 46.f);
modDepth = (divided * divided);
if (modDepth > 5)
{
modDepth = 5;
}
if (modDepth < 0.2)
{
modDepth = 0;
}
}
}
void setTables(const int8_t *TABLE_NAME) {
for (int i = 0; i < sizeof(oscils)/sizeof(oscils[0]); i++) {
oscils[i].setTable(TABLE_NAME);
}
}
void readPotsAndUpdate() {
/* Read digital inputs
* i.e. waveform switch button
*/
if (tableToggleTimer > TABLE_TOGGLE_TIMER_MAX) {
byte toggled = digitalRead(TABLE_TOGGLE);
digitalWrite(LED, toggled);
if (toggled) {
currentTable = (TableType)(((byte)currentTable + 1) % 4);
setTables(tables[(unsigned) currentTable]);
tableToggleTimer = 0;
}
} else {
tableToggleTimer++;
}
/* Read analog inputs */
unsigned i;
for (i = 0; i < sizeof(ANALOG_INPUTS) / sizeof(ANALOG_INPUTS[0]); i++) {
analog_values[i] = mozziAnalogRead(i);
}
/* Map read values to corresponding variables */
updateDetune(analog_values[0]);
updateAttack(analog_values[1]);
updateDecay(analog_values[2]);
updateSustain(analog_values[3]);
updateRelease(analog_values[4]);
updateVibrato(analog_values[5]);
}
void updateDetune(int analog) {
detuneCents = map(analog, 0, 1024, 0, 100);
detuneCoefficients[0] = 1 + 0.0005946 * detuneCents;
detuneCoefficients[1] = 1 - 0.0005946 * detuneCents;
}
void updateAttack(int analog) {
attackTime = map(analog, 0, 1024, 0, 1024);
envelope.setAttackTime(attackTime);
}
void updateDecay(int analog) {
decayTime = map(analog, 0, 1024, 0, 1024);
envelope.setDecayTime(decayTime);
}
void updateSustain(int analog) {
sustainLevel = map(analog, 0, 1024, 0, 255);
envelope.setSustainLevel(sustainLevel);
}
void updateRelease(int analog) {
sustainTime = map(analog, 0, 1024, 0, 1024);
envelope.setReleaseTime(releaseTime);
}
void updateVibrato(int analog) {
vibratoAmp = ((float) map(analog, 0, 1024, 0, 100) ) / 100.f;
}
void updateControl()
{
MIDI.read();
envelope.update();
carrierFreq = mtof(lastnote);
modFreq = carrierFreq * modOffset;
// update oscil types
readPotsAndUpdate();
// set carrier frequencies
oscils[0].setFreq(carrierFreq);
oscils[1].setFreq(carrierFreq * detuneCoefficients[0]);
oscils[2].setFreq(carrierFreq * detuneCoefficients[1]);
oscModulator.setFreq(modFreq);
}
int updateAudio()
{
/* Sum together the signals from the 3 oscillators */
int carrierSum = 0;
for (int i = 0; i < sizeof(oscils)/sizeof(oscils[0]); i++) {
/* The carrier signal gets double the amplitude */
if (i == 0)
carrierSum += 1.5f * (oscils[i].next() >> 3);
else
carrierSum += 0.75f * (oscils[i].next() >> 3);
}
int total = (carrierSum * (envelope.next() >> 1)) >> 8;
if (abs(total) < 5 || sustainKillTimer == 0)
total = 0;
if (sustainKillTimer > 0) {
sustainKillTimer--;
}
return total;
}
void loop()
{
audioHook();
}
|