This repository has been archived by the owner on Jun 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMozziMonoSynth.ino
227 lines (188 loc) · 6.18 KB
/
MozziMonoSynth.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
/**
* Title: MozziMonoSynth
* Version: 0.1.0
* Author: D Cooper Dalrymple
* Created: 11/04/2020
* Updated: 11/17/2020
* Description: 14-bit monophonic synthesizer using the Mozzi audio synthesis library. Best used for bass voices. Uses HiFi mode on Arduino Uno with hex inverter buffer and hardware low pass filter.
* https://dcooperdalrymple.com/
*/
// Global Constants
#include "Constants.h"
//#define SERIAL_DEBUG // Enable Debugging
#define CONTROL_RATE 64
// Libraries
#ifndef SERIAL_DEBUG
#include <MIDI.h>
MIDI_CREATE_DEFAULT_INSTANCE();
#endif
#include <MozziGuts.h>
#include <mozzi_fixmath.h>
#include <mozzi_midi.h>
#include <Line.h>
// Local Classes
#include "NoteBank.h"
#include "Controls.h"
#include "Voice.h"
#include "Program.h"
// Objects
NoteBank note_bank;
Voice voice;
Program program;
// Local Function Declarations
void loadProgramVoice(uint8_t index = 0);
void updateVoiceControls();
// Begin Setup
void setup() {
pinMode(STATUS_LED, OUTPUT);
digitalWrite(STATUS_LED, LOW);
Controls::setup();
Controls::setUpdateHandler(updateVoiceControl);
Controls::setWriteProgramCallback(writeProgramVoice);
Controls::setReadProgramCallback(loadProgramVoice);
voice.init();
//loadProgramVoice(0);
startMozzi(CONTROL_RATE);
#ifndef SERIAL_DEBUG
MIDI.setHandleNoteOn(receiveNoteOn);
MIDI.setHandleNoteOff(receiveNoteOff);
MIDI.begin(MIDI_CHANNEL_OMNI);
#else
Serial.begin(9600);
Serial.println("MozziMonoSynth v0.1.0 - D Cooper Dalrymple 2020");
receiveNoteOn(0, 49, 255); // Play note if debugging
#endif
}
void loop() {
audioHook();
}
void updateControl() {
#ifndef SERIAL_DEBUG
// Flush Midi buffer
while (MIDI.read()) { };
#endif
// Check for control updates and update voice with handler
Controls::update();
// Update control rate parameters of voice
if (Controls::getState() != STATE_PROGRAM) voice.update();
// LED display
switch (Controls::getState()) {
case STATE_PRIMARY:
// Fade LED with LFO and Envelope
Controls::setLed(LED_1_KEY, voice.getCurrentLFO());
Controls::setLed(LED_2_KEY, voice.getCurrentGain());
break;
case STATE_SECONDARY:
Controls::setLed(LED_1_KEY, LED_ON);
Controls::setLed(LED_2_KEY, LED_OFF);
break;
case STATE_TERTIARY:
Controls::setLed(LED_1_KEY, LED_OFF);
Controls::setLed(LED_2_KEY, LED_ON);
break;
case STATE_PROGRAM:
// Indicate the selected program
uint8_t p = Controls::getSelectedProgram();
Controls::setLed(LED_1_KEY, p & B10 ? LED_ON : LED_DIM);
Controls::setLed(LED_2_KEY, p & B01 ? LED_ON : LED_DIM);
break;
}
}
int16_t updateAudio() {
Controls::updateLeds();
if (Controls::getState() == STATE_PROGRAM) {
return 0; // Mute when reading/writing program
} else {
return voice.next();
}
}
// Midi Callbacks
void receiveNoteOn(byte channel, byte note, byte velocity) {
digitalWrite(STATUS_LED, HIGH);
note_bank.removeNote(note); // Prevent duplicates
note_bank.addNote(note);
voice.noteOn(note_bank.getNote());
}
void receiveNoteOff(byte channel, byte note, byte velocity) {
if (note_bank.removeNote(note) == false) return;
if (note_bank.hasNote()) {
voice.noteOn(note_bank.getNote());
} else {
digitalWrite(STATUS_LED, LOW);
voice.noteOff();
}
}
// Controls transfer to voice
void updateVoiceControl(uint8_t key, uint16_t value) {
#ifdef SERIAL_DEBUG
Serial.print("Control Change: ");
Serial.print(key);
Serial.print(", ");
Serial.println(value);
#endif
switch (key) {
case OSC_1_WAVEFORM_KEY:
voice.setOscillatorWaveform(0, (uint8_t)value);
break;
case OSC_2_WAVEFORM_KEY:
voice.setOscillatorWaveform(1, (uint8_t)value);
break;
case OSC_DETUNE_KEY:
voice.setOscillatorDetune(value);
break;
case OSC_MIX_KEY:
voice.setOscillatorMix((uint8_t)value);
break;
case OSC_TUNE_KEY:
voice.setOscillatorTune(value);
break;
case LFO_AMOUNT_KEY:
voice.setLfoAmount((uint8_t)value);
break;
case LFO_FREQUENCY_KEY:
voice.setLfoFrequency(value);
break;
case LFO_DESTINATION_KEY:
voice.setLfoDestination((uint8_t)value);
break;
case LPF_FREQUENCY_KEY:
if (Controls::isUpdated(LPF_RESONANCE_KEY)) break;
case LPF_RESONANCE_KEY:
voice.setFilterFrequencyResonance((uint8_t)Controls::getPot(LPF_FREQUENCY_KEY), (uint8_t)Controls::getPot(LPF_RESONANCE_KEY));
break;
case ENV_1_ATTACK_KEY:
if (Controls::isUpdated(ENV_1_DECAY_KEY)) break;
case ENV_1_DECAY_KEY:
if (Controls::isUpdated(ENV_1_RELEASE_KEY)) break;
case ENV_1_RELEASE_KEY:
voice.setEnvelopeTimes(0, Controls::getPot(ENV_1_ATTACK_KEY), Controls::getPot(ENV_1_RELEASE_KEY), Controls::getPot(ENV_1_DECAY_KEY));
break;
case ENV_2_AMOUNT_KEY:
voice.setEnvelopeAmount(1, (uint8_t)value);
break;
case ENV_2_LEVEL_KEY:
voice.setEnvelopeLevel(1, (uint8_t)value);
break;
case ENV_2_ATTACK_KEY:
if (Controls::isUpdated(ENV_2_DECAY_KEY)) break;
case ENV_2_RELEASE_KEY:
if (Controls::isUpdated(ENV_2_RELEASE_KEY)) break;
case ENV_2_DECAY_KEY:
voice.setEnvelopeTimes(1, Controls::getPot(ENV_2_ATTACK_KEY), Controls::getPot(ENV_2_RELEASE_KEY), Controls::getPot(ENV_2_DECAY_KEY));
break;
}
}
// Program transfer to controls and voice
void writeProgramVoice(uint8_t index) {
for (uint8_t i = 0; i < CONTROLS_NUM_POTS; i++) {
program.set(i, Controls::getPot(i, false));
}
program.write(index);
}
void loadProgramVoice(uint8_t index) {
program.read(index);
for (uint8_t i = 0; i < CONTROLS_NUM_POTS; i++) {
Controls::setPot(i, program.get(i), true);
updateVoiceControl(i, program.get(i));
}
}