Created
February 23, 2020 09:09
-
-
Save nullstalgia/bb17c970a6f069c9e564d6f21f820756 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <AppleMidi.h> | |
#include <ESP8266WiFi.h> | |
#include <ESP8266HTTPClient.h> | |
#include <WiFiUdp.h> | |
#include <SoftwareSerial.h> | |
#include <LEAmDNS.h> | |
#include <LEAmDNS_Priv.h> | |
#include <ESP8266mDNS.h> | |
#include <LEAmDNS_lwIPdefs.h> | |
#include <LEATimeFlag.h> | |
bool isConnected = false; | |
APPLEMIDI_CREATE_INSTANCE(WiFiUDP, AppleMIDI); | |
const char* ssid = "YOU_SSID"; | |
const char* password = "YOUR_PASSWORD"; | |
MDNSResponder mdns; | |
void OnAppleMidiConnected(uint32_t ssrc, char* name); | |
void OnAppleMidiDisconnected(uint32_t ssrc); | |
byte midiByte; | |
SoftwareSerial midiSerial(D5, D6, false, 256); // RX, TX | |
void setup() { | |
midiSerial.begin(31250); | |
Serial.begin(115200); | |
WiFi.hostname("Pianino"); | |
WiFi.begin(ssid, password); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(1000); | |
Serial.println("Connecting..."); | |
} | |
Serial.println("Connected."); | |
Serial.println(); | |
Serial.print(F("MIDI @ ")); | |
Serial.print(WiFi.localIP()); | |
Serial.println(F(":5004")); | |
mdns.begin("Pianino"); | |
delay(2000); | |
mdns.addService("apple-midi", "udp", 5004); | |
AppleMIDI.begin("Pianino"); | |
AppleMIDI.OnConnected(OnAppleMidiConnected); | |
AppleMIDI.OnDisconnected(OnAppleMidiDisconnected); | |
} | |
void OnAppleMidiConnected(uint32_t ssrc, char* name) { | |
isConnected = true; | |
Serial.print(F("Connected to session ")); | |
Serial.println(name); | |
for (int i = 0; i < 127; i++) { | |
AppleMIDI.sendNoteOn(i, 0, 1); | |
} | |
} | |
void OnAppleMidiDisconnected(uint32_t ssrc) { | |
isConnected = false; | |
Serial.println(F("Disconnected")); | |
} | |
int controlNumber = 0; | |
int noteNumber = 0; | |
int noteVelocity = 0; | |
int waitingOn = 2; | |
byte noteON = 144; | |
byte pedalON = 176; | |
void parseNote(byte midiByte) { | |
if (midiByte == noteON) { | |
controlNumber = noteON; | |
waitingOn = 2; | |
} else if (midiByte == pedalON) { | |
controlNumber = pedalON; | |
waitingOn = 2; | |
} else { | |
if (waitingOn == 2) { | |
noteNumber = midiByte; | |
waitingOn -= 1; | |
} else if (waitingOn == 1) { | |
noteVelocity = midiByte; | |
waitingOn -= 1; | |
} | |
if (waitingOn == 0) { | |
if (controlNumber == pedalON) { | |
AppleMIDI.sendControlChange(64, noteVelocity, 1); | |
} else { | |
AppleMIDI.sendNoteOn(noteNumber, noteVelocity, 1); | |
} | |
waitingOn = 2; | |
} | |
} | |
} | |
int counter = 0; | |
void loop() { | |
// AppleMIDI.run(); | |
// mdns.update(); | |
if (counter == 0) { | |
AppleMIDI.run(); | |
mdns.update(); | |
} | |
if (isConnected && midiSerial.available() > 0) { | |
// AppleMIDI.run(); | |
// mdns.update(); | |
midiByte = midiSerial.read(); | |
if (midiByte != 248 && midiByte != 254) { | |
parseNote(midiByte); | |
} | |
} | |
// else { | |
// } | |
counter++; | |
counter = counter % 1000; | |
// delay(1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment