-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSimpleSynth.ino
More file actions
146 lines (114 loc) · 3.18 KB
/
SimpleSynth.ino
File metadata and controls
146 lines (114 loc) · 3.18 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
/*
__ _ _____ ___ ___ _ _ __ _ ______ _
/__|_|/ \(_ | | |\| | |_)|_||\|(_ | |_| | | / \|\|
\_|| |\_/__) | _|_| | | | \| || |__)|__| | | _|_\_/| |
https://ghostintranslation.bandcamp.com/
https://www.instagram.com/ghostintranslation/
https://github.com/ghostintranslation
*/
#include <Audio.h>
#include <MIDI.h>
MIDI_CREATE_DEFAULT_INSTANCE(); // MIDI library init
#include "Synth.h"
int ledPin = 13;
bool controllerIsLaunchpad = true;
const int interval_time = 50;
elapsedMillis clock_count;
Synth synth(A0, A1, A2, A3, A4, A5, A6, A7, A8);
//
AudioOutputI2S i2s2;
AudioConnection patchCord1(*synth.getOutput(), 0, i2s2, 0);
AudioConnection patchCord2(*synth.getOutput(), 0, i2s2, 1);
AudioOutputUSB usb1;
AudioConnection patchCord3(*synth.getOutput(), 0, usb1, 0);
AudioConnection patchCord4(*synth.getOutput(), 0, usb1, 1);
AudioControlSGTL5000 sgtl5000_1;
/**
* Setup
*/
void setup() {
Serial.begin(115200);
pinMode(ledPin, OUTPUT);
MIDI.setHandleNoteOn(onNoteOn);
MIDI.setHandleNoteOff(onNoteOff);
MIDI.begin(MIDI_CHANNEL_OMNI);
usbMIDI.setHandleNoteOn(onNoteOn);
usbMIDI.setHandleNoteOff(onNoteOff);
usbMIDI.setHandleStop(onStop);
usbMIDI.setHandleSystemReset(onStop);
// Audio connections require memory to work.
AudioMemory(40);
sgtl5000_1.enable();
sgtl5000_1.volume(0.5);
while (!Serial && millis() < 2500); // wait for serial monitor
// Starting sequence
Serial.println("Ready!");
digitalWrite(ledPin, HIGH);
delay(1000);
digitalWrite(ledPin, LOW);
if(controllerIsLaunchpad){
MIDI.sendControlChange(0,2,1);
MIDI.sendControlChange(0,2,1);
MIDI.sendControlChange(0,2,1);
}
}
/**
* Loop
*/
void loop() {
MIDI.read();
usbMIDI.read();
// Leveraging the overload
if (clock_count >= interval_time) {
// Do things every 50ms
// Synth update
synth.update();
// Launchpad lights
if(controllerIsLaunchpad){
for (int i = 0; i < voiceCount ; i++) {
if(synth.getVoices()[i]->isNotePlayed() && !synth.getVoices()[i]->isActive()){
MIDI.sendNoteOn(synth.getVoices()[i]->currentNote, 0, 1);
synth.getVoices()[i]->setNotePlayed(false);
}else if(synth.getVoices()[i]->isNotePlayed() && synth.getVoices()[i]->isActive()){
MIDI.sendNoteOn(synth.getVoices()[i]->currentNote, 51, 1);
}
}
}
clock_count = 0;
}
}
/**
* Midi note on callback
*/
void onNoteOn(byte channel, byte note, byte velocity) {
synth.noteOn(note);
digitalWrite(ledPin, HIGH);
if(controllerIsLaunchpad){
MIDI.sendNoteOn(note, 51, 1);
}
}
/**
* Midi note off callback
*/
void onNoteOff(byte channel, byte note, byte velocity) {
synth.noteOff(note);
digitalWrite(ledPin, LOW);
}
/**
* Midi stop callback
*/
void onStop() {
synth.stop();
}
void myControlChange(byte channel, byte control, byte value){
Serial.print("myControlChange ");
Serial.print(channel);
Serial.print(" ");
Serial.print(control);
Serial.print(" ");
Serial.print(value);
}
byte noteToLaunchpadNote(byte note){
//byte launchpadNote = this->currentNote%8 + (this->currentNote/8 * 16);
return note/16*8 + note%8;
}