Midi over USB to keyboard

Hi All, I want to build a foot switch unit to play my Yamaha keyboard, using midi note data files I create on SD card.
Kind of a manual step sequencer really, to add keyboards back into my band. I played keyboards before, but now bass and vocals, so my hands are a bit busy normally.
The Yamaha keyboard has a usb Midi system which works from my PC, with some experiments I did before.
For a stand-alone foot unit, I guess ESP32 might be ideal.
My background is PIC controllers and I've done midi control to things with old DIN midi sockets. Usb on PIC is a bit expensive and seems incredibly complicated.
I've been playing with ESP32 WiFi, LCD display, webserver, etc and got on ok so far.
Any ESP32 ideas / advice ?

I use 3 USB keyboards with my band, connected to a PC, and the keyboards are automatically installed ad MIDI inputs under Windows. I don't know what Yamaha keyboard you use (exact model?) but I doubt you can do the same with any Arduino or ESP32, because the USB to MIDI conversion is handled by the Windows plug-n-play drivers.
But you can use the standard MIDI port (it's a serial port after all, set to a specific speed 31250 baud....)., so you don't actually need any specific Arduino or ESP32.

MIDI connection is pretty straightforward, especially because you need just to connect a MIDI OUT (or TX pin) from Arduino to MIDI IN of the keyboard.

Here is a basic description of a MIDI OUT connection from Arduino:

To make things easier you could get one of the Arduino MIDI shields available.

Thanks docdoc,
My keyboard is a Yamaha PSR-E473, which has a USB Midi system, not traditional 5 pin DIN plug.
As I said, I have already done experiments with a PIC and a USB host PCB (with usb midi code installed). That worked ok as proof of playing chords, etc on the keyboard.
I think ESP32 would better for a fully integrated system with SD card storage, LCD or even colour TFT display, etc.
Also, I'd want my unit to be able to step record from the keyboard, to generate data files onto SD card (for playback later), which again I've trialed ok, but couldn't store the data with PIC at the time.

It's a shame it doesn't have a standard MIDI connector. I've never had to deal with keyboards without a MIDI connector, so I can't tell you exactly if and how it can be directly interfaced with Arduino.

I can imagine you could do it using the PC as a "bridge", that is, with the Arduino connected to the PC via MIDI (either with a MIDI-USB adapter for PCs, or with the Arduino via USB-serial and using the Hairless MIDI Bridge software) and the keyboard connected via USB, using some software to send the messages received from the Arduino directly to the keyboard's MIDI port. But I can't tell you how you could do it; I've never done anything like it.

Another scenario would be to use the PC directly: still connecting the Arduino via MIDI as before, you could play the PC directly. You could install a VST host application (there are many, like Cantabile, VSTHost, etc) and load the VSTs (Virtual Instruments) of the instruments you want to emulate/play into it. VSTs have very good quality compared to native PC instruments; I use several (all free, by the way) to emulate Hammonds, synthesizers, electric pianos, etc., as well as various VST effects, such as reverb, delay, etc. (yes, I play keyboards in a Genesis Tribute band...).

Extreme solution: switch to another keyboard using standard MIDI plugs (I still have an old Roland to be used with MIDI) or, even better, a small MIDI expander.

Thanks docdoc,
I think you might misunderstand my situation.
I have controlled my Yamaha keyboard through the USB Midi, using a PIC controller ..... No computer involved (after the PIC was programmed).
However, now I've played with ESP32 and then look again at chunking lots of data from SD card, SPI TFT screen options and maybe the possibilities to make the SD card files direct from the Yamaha keyboard (as a record mode) ... I think PIC controller loses out to ESP32.
Just wanted to know if ESP32 can do USB host and get some advice.

I don't know much about PIC controllers because I only used some many years ago (in the 1990s), but I know they're very cheap and small. I'm curious how you connected your keyboard to a PIC. I assume you used some sort of USB host board, but I don't know how MIDI communication over USB works. But if you managed to do it with a PIC, there's definitely a way to do it with Arduino (not necessarily an ESP32 in my opinion), by installing a USB Host board on it too (yes, they exist for Arduino too).
But unfortunately I can't help you directly with this, I'm sorry.

Thanks docdoc, I'd be confident with PIC for SD card and the SPI display, but having looked into USB hosting, WiFi, Bluetooth etc starts to get expensive.
The other problem with PIC is choosing one device that can do everything you need. Might need 2 UARTs, SPI and I2C, plus USB host, maybe Bluetooth ... that quickly becomes top-end of the price range.
You can't just change the peripherals to whatever pins you like on a PIC.
I run my own company building electronic control systems, mainly CAN bus and heavy motor / hydraulic control systems, with maybe several PICs on different boards and 50 metres of wiring looms in one kit. That commercial market is ok for PIC.
For hobby stuff, ESP32 is amazing for only a few quid, all in, with WiFi and all.
I now know that the newer ESP-S2 or S3 have USB host, but as I've already proven the Hobbytronics USB host module, I can just run that from UART on the ESP32s I've already got here.
I'll let you know how it goes.

BTW, what's your band ? You on Facebook ?
I have 2 bands, both in Peterborough UK.
Smoke n Mirrors - rock covers band.
Posh Town blue - modern blues band (Gary Moore, Kenny Wayne Shepherd, etc)

I understand, even if I had a very little experience with PICs.
I hope someone else could help you better than me, I'm curious about how a USB interface can be seen as a MIDI device (without the intervention of a device driver), so I'll keep watching this topic. Ok, USB is "serial" anyway, but I don't know how it can be practically used as a MIDI line.

Yes, it's called "Willow Farm," after a section of Genesis' "Supper's Ready." We play almost all the "old" Genesis (i.e., the "Gabriel era"), up to Duke. But after a couple of member changes, we haven't played live in two years...

I'm happy for you (I understood you were English when you said "quid"... :wink: )!
I don't have much free time to play in more than one band anymore... And I never studied piano, I'm self-taught, so Genesis songs are often a challenge for me because of their structure and the rapidly changing chords and/or for odd/unusual bars (like 9/8 in Supper's ready or 6/4 in Watcher of the skies or Squonk)...

Thanks for this "digression" but unfortunately I must stop here, for any further "cultural exchanges," I think we'll have to go PM before the mods scold us. :smiling_face_with_sunglasses:

Thanks docdoc, yes we'll get into trouble chatting bands.

I was surprised that the USB midi thing was actually quite simple in the end. Hobbytronics don't sell the tiny USB host interface board now (mainly because they're on eBay / AliExpress now, for pennies).
However, they supplied various software uploads for it, one of which is MIDI code (like a windows driver).

I programmed it with their midi code and then my keyboard thinks I've plugged a computer into its USB socket.

Then I sent midi data (3 bytes) from a UART to the interface board and my keyboard played a note ... That's it !
Second UART message could be ... to PLAY another note (now playing a chord) ... or to STOP the first note ... simple.
Third message ... you get the idea.

Two key points here, a UART can transfer data to the interface board at very high speed. The USB output is very high speed.
This is all many times faster than old fashioned midi at 31,250 bits per second.

And ... the duplex USB interface can give me data FROM the keyboard too.
I had conversations with Yamaha about why my keyboard didn't have "proper" midi sockets and these "Speed upgrades" and "Two way Comms" were the main reasons. Now I understand it, it's an obvious step into the future of midi, even on a fairly cheap keyboard.

Oh, another point ...
The Yamaha engineer told me I just need a USB hub, to connect to several keyboards at the same time. I only have one, so didn't research that option.

I know MIDI messages and formats well, so I can fully understand that, thanks.
What I was wondering is if, once an USB-MIDI device (i.e. your keyboard) has been connected to the USB Host adapter, you can just send the MIDI data message and it is acquired by the device and the corresponding note played (if a NoteOn message is sent). If this is the behaviour, it looks like using Arduino (and ad USB Host board or shield) is a feasable solution.

Oh yes, effectively the USB host interface "tells" the keyboard that you've plugged a computer into it.
USB is serial data, in some ways "the same" as old fashioned midi, just way, way faster.

I now need to work out the SD card thing and how to structure files, then I can start trials playing my keyboard.
My plan is too have a little box on my mic stand, plugged into my keyboard USB. This gives 5v power 500mA and data.
Display, SD card and ESP32 in this box, with buttons to select "SONG" (which file ?) and to step between song parts, verse, chorus, etc, if necessary.
Then a foot switch on floor, ON to play a chord, OFF to stop, then automatically goto next chord.
I'll be singing and playing bass, so will take some getting used to.

This picture is the USB host (top), SD card interface and my TFT 3.5" display. The display has an SDcard reader on it, but would be awkward to access maybe.

The picture shows very large text on the TFT, as I was thinking the box would be on the floor, with foot switches.
However, if the box is on my mic stand, I can go smaller on text and get more info on screen. My eye sight's not that bad :joy:

The display shows "Chorus - Chord 99 ON" (playing, me pressing foot switch).
Most of our songs only have a few slow chords in a verse and a few in a chorus, so 99 is shown just to get the text in the right places on the screen

Could even go touch screen maybe

Good to know, thanks, I have a "MIDI Mapper" unfinished project in my drawer, maybe I could somehow make use of this information.

Please update us if you proceed with this project.

What was the goal of the "midi mapper" ?
What processor, etc ?

Well, I have to make a (short?) description first.
[offtopic]
Rutherford (from original Genesis bassist) played a Moog Taurus together with a doubleneck guitar (4 strings bass and 12-strings). When in some songs or sections he used the Taurus he often play the 12-strings electric guitar. Our bassist has a MIDI "bass pedalboard" (Studiologic MP113) connected to a Moog Taurus emulator expander (Behringer Toro), and he has also a custom-made double neck so he's able to (almost) reproduce those songs. The problem is the pedal: it can be set to play an octave between the three available (-1, 0 and +1 from the default one), but some Genesis songs need to play from a lower octave and the pedal can't send note messages for such octave.
[/offtopic]

So here is the Arduino part: my first goal was to use a plain Arduino UNO or a Nano, in a small box to be connected between the pedal and the expander and automatically shift down 12 semitones each note On/Off coming from the pedal and send this "mapped" one to the expander.
I see the MIDI pedalboard has an USB connector so I suppose/hope it could be used to send the notes so thanks to your description, I think I'll investigate this option in the next days (I need to physically have the pedalboard here first, together with its reference manual). I just need to get an USB Host and give it a try.

Thanks, that sounds like a cool project.
"Dropping" midi notes by 1 octave sounds simple to do.
However, using old fashioned midi at 31,250 bits per second, I can imagine audible delays, which ruin everything.
Converting to USB midi from the beginning of your path might negate those delays, but I guess you have to convert back again somewhere in the system.
Processor speed might be key. One thing I didn't mention in my posts is that ESP is much faster than most PIC controllers, so maybe you could get it working.

Yep, I know, this is a snippet from my first test sketch (executed after reading a MIDI packet, I can even change the channel if needed)):

  if (MIDI.read())
  {
    byte Type = MIDI.getType();
    byte Data1 = MIDI.getData1(); // Note
    byte Data2 = MIDI.getData2(); // Velocity
    byte Channel = MIDI.getChannel();
    // Transpose?
    if(Type == midi::NoteOn || Type == midi::NoteOff) Data1 -= 12;
    // Send to expander
    MIDI.send(Type, Data1, Data2, Channel);
}

Not really. You can imagine a pedalboard neither have much notes to play in sequence (single notes, and remember it's played by a foot!) and nor very short notes played (bass pedal notes are generally long). Btw I made a few tests and the delay is practically imperceptible, the MIDI acquisition, mapping, and sending requires a bunch of milliseconds.