Skip to content

Commit

Permalink
rtipMIDI fix attempt
Browse files Browse the repository at this point in the history
  • Loading branch information
serifpersia committed Oct 20, 2024
1 parent 550f316 commit e1f53cd
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 48 deletions.
30 changes: 12 additions & 18 deletions ESP32_PianoLux/ESP32_PianoLux.ino
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
// Once this AP is no longer visible your ESP32 should show you your IP, you might need to recconect the board or press reset button to see it.
// Connect to PianoLux web interface via ip the link should look like http://192.168.1.32/ or use pianolux.local http://pianolux.local/.

String firmwareVersion = "v1.10";
String firmwareVersion = "v1.11";

// Define board types with unique values
#define ESP32 1
Expand Down Expand Up @@ -205,7 +205,7 @@ uint8_t MODE = COMMAND_SET_COLOR;
uint8_t serverMode;

uint8_t animationIndex;

uint8_t numConnectedClients = 0;
uint8_t splashMaxLength = 8;
uint8_t SPLASH_HEAD_FADE_RATE = 5;

Expand Down Expand Up @@ -311,15 +311,6 @@ uint8_t isConnected = 0;

APPLEMIDI_CREATE_DEFAULTSESSION_INSTANCE();

// Task handles
TaskHandle_t midiTaskHandle = NULL;

void midiTask(void* pvParameters) {
while (1) {
MIDI.read(); // Handle MIDI messages
vTaskDelay(pdMS_TO_TICKS(1)); // Adjust the delay as needed
}
}
void StartupAnimation() {
for (uint8_t i = 0; i < NUM_LEDS; i++) {
leds[i] = CHSV(getHueForPos(i), 255, 255);
Expand Down Expand Up @@ -511,11 +502,6 @@ void setup() {
setIPLeds();
}

// Determine core assignment based on board type
// Create the MIDI task
BaseType_t coreToUse = (BOARD_TYPE == ESP32 || BOARD_TYPE == ESP32S3) ? 1 : 0;
xTaskCreatePinnedToCore(midiTask, "MIDITask", 2048, NULL, 1, &midiTaskHandle, coreToUse);

MIDI.begin();

AppleMIDI.setHandleConnected([](const APPLEMIDI_NAMESPACE::ssrc_t& ssrc, const char* name) {
Expand All @@ -534,11 +520,17 @@ void setup() {

MIDI.setHandleNoteOn([](byte channel, byte note, byte velocity) {
noteOn(note, velocity);
sendESP32Log("RTP MIDI IN: NOTE ON: Channel: " + String(channel) + " Pitch: " + String(note) + " Velocity: " + String(velocity));
if (numConnectedClients != 0)
{
sendESP32Log("RTP MIDI IN: NOTE ON: Channel: " + String(channel) + " Pitch: " + String(note) + " Velocity: " + String(velocity));
}
});
MIDI.setHandleNoteOff([](byte channel, byte note, byte velocity) {
noteOff(note);
sendESP32Log("RTP MIDI IN: NOTE OFF: Channel: " + String(channel) + " Pitch: " + String(note) + " Velocity: " + String(velocity));
if (numConnectedClients != 0)
{
sendESP32Log("RTP MIDI IN: NOTE OFF: Channel: " + String(channel) + " Pitch: " + String(note) + " Velocity: " + String(velocity));
}
});

#if BOARD_TYPE == ESP3S3 || BOARD_TYPE == ESP32
Expand All @@ -556,6 +548,8 @@ void setup() {

void loop() {

MIDI.read();

// Handle USB
#if BOARD_TYPE == ESP32S3 || BOARD_TYPE == ESP32S2
usbh_task();
Expand Down
43 changes: 14 additions & 29 deletions ESP32_PianoLux/MidiParser.ino
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ static void midi_transfer_cb(usb_transfer_t *transfer) {
switch (statusByte & 0xF0) {
case 0x80: // Note Off
noteOff(channel);
sendESP32Log("USB MIDI IN: NOTE OFF Pitch: " + String(channel) + " Velocity: " + String(value));
if (numConnectedClients != 0)
{
sendESP32Log("USB MIDI IN: NOTE OFF Pitch: " + String(channel) + " Velocity: " + String(value));
}
if (isConnected) {
MIDI.sendNoteOff(channel, value, 1);
sendESP32Log("RTP MIDI Out: Note OFF " + String(channel) + " Velocity: " + String(value));
Expand All @@ -38,40 +41,22 @@ static void midi_transfer_cb(usb_transfer_t *transfer) {
case 0x90: // Note On
if (value == 0) {
noteOff(channel); // Treat "Note On" with 0 velocity as "Note Off"
sendESP32Log("USB MIDI IN: NOTE OFF Pitch: " + String(channel) + " Velocity: " + String(value));
if (isConnected) {
MIDI.sendNoteOff(channel, value, 1);
sendESP32Log("RTP MIDI Out: Note OFF " + String(channel) + " Velocity: " + String(value));
}
} else {
noteOn(channel, value);
sendESP32Log("USB MIDI IN: NOTE ON Pitch: " + String(channel) + " Velocity: " + String(value));
if (isConnected) {
MIDI.sendNoteOn(channel, value, 1);
sendESP32Log("RTP MIDI Out: Note ON " + String(channel) + " Velocity: " + String(value));
}
}
break;

case 0xB0: // Control Change
// Process Control Change messages
switch (channel) {
case 64: // Sustain Pedal
MIDI.sendControlChange(channel, value, 1);
sendESP32Log("RTP MIDI Out: Sustain Pedal CC " + String(channel) + " Value: " + String(value));
break;

case 67: // Soft Pedal
MIDI.sendControlChange(channel, value, 1);
sendESP32Log("RTP MIDI Out: Soft Pedal CC " + String(channel) + " Value: " + String(value));
break;

case 66: // Sostenuto Pedal
MIDI.sendControlChange(channel, value, 1);
sendESP32Log("RTP MIDI Out: Sostenuto Pedal CC " + String(channel) + " Value: " + String(value));
break;
if (numConnectedClients != 0) {
String logMessage = (value == 0)
? "USB MIDI IN: NOTE OFF Pitch: " + String(channel) + " Velocity: " + String(value)
: "USB MIDI IN: NOTE ON Pitch: " + String(channel) + " Velocity: " + String(value);
sendESP32Log(logMessage);
}
if (isConnected) {
MIDI.sendNoteOn(channel, value, 1);
sendESP32Log("RTP MIDI Out: Note ON " + String(channel) + " Velocity: " + String(value));
}
break;

}
}
esp_err_t err = usb_host_transfer_submit(transfer);
Expand Down
1 change: 0 additions & 1 deletion ESP32_PianoLux/WebServer.ino
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
uint8_t numConnectedClients = 0;
bool inUse = false;

void webSocketEvent(uint8_t num, WStype_t type, uint8_t *payload, size_t length) {
Expand Down

0 comments on commit e1f53cd

Please sign in to comment.