Page 1 of 1

low-level MIDI handling

Posted: Wed Jan 20, 2016 1:41 pm
by joeboy
I have a Raspbery Pi running FreeBSD, but I think this is more of a general question about MIDI message encoding, so please keep reading even if you run a different OS.

I have my SY-35 hooked up to the Pi with one of those generic Chinese USB->MIDI devices
I can record some sort of MIDI data to a file by running

Code: Select all

dd if=/dev/umidi0.0 of=/tmp/file bs=1
and playing the keys
(if I engage aftertouch it crashes the connection, but that's another matter entirely)

But when I open that file up, it's a bunch of gibberish. The kind of thing you get when you open a binary package in a text editor: (~P?-~P?^@♦~P3^@=0...)

Question 1: what exactly is that gibberish, and how do I translate it into something more meaningful?

The captured input can be routed back into the synth with:

Code: Select all

dd if=/tmp/file of=/dev/umidi0.0 bs=1
but all the notes play at once without any regard for time.

Question 2: if I wanted to play a midi note on the SY-35 directly from the Pi, in what format would the information need to be passed? What basic system command would I issue?

I'm sorry if this is too specific, but there seem to be a lot of dudes here who know their MIDI, so hopefully somebody might be able to point me in the right direction.

Re: low-level MIDI handling

Posted: Wed Jan 20, 2016 2:01 pm
by joeboy
So continuing to search and twiddle, I found out about the hexdump command.
This is what playing middle C gives me, after running hexdump:

Code: Select all

0000000 fefe fefe fefe fefe fefe 3c90 903a 003c
*
0000010
It looks like something now, but I'm not sure what to try next. I know very little about how data is represented

Re: low-level MIDI handling

Posted: Wed Jan 20, 2016 8:22 pm
by desmond
Just look up the MIDI specification online - it will tell you all you need to know about what data types there are, and how they are represented - eg a Note On has a particular value, and is three bytes - the first byte is the Note On message and also the MIDI channel, the second byte is the note number, and the third byte is the velocity.

There's nothing very hard here to understand, it's very straightforward.

Re: low-level MIDI handling

Posted: Wed Jan 20, 2016 9:48 pm
by synthroom
Just asking - is baud rate important here? Roland D-50s are particular about the data transmittion rate and modern computer/midi interfaces are too fast for the D-50. Perhaps something like that is going on here?

Re: low-level MIDI handling

Posted: Thu Jan 21, 2016 12:49 pm
by joeboy
desmond wrote:Just look up the MIDI specification online - it will tell you all you need to know about what data types there are, and how they are represented - eg a Note On has a particular value, and is three bytes - the first byte is the Note On message and also the MIDI channel, the second byte is the note number, and the third byte is the velocity.

There's nothing very hard here to understand, it's very straightforward.
I had read up on the basics of MIDI before I started messing around. I was expecting a bunch of ones and zeroes instead of the output I got.
I've since found out about xxd, which does what I'm looking for.
synthroom wrote:Just asking - is baud rate important here? Roland D-50s are particular about the data transmittion rate and modern computer/midi interfaces are too fast for the D-50. Perhaps something like that is going on here?
I don't think so. I was able to play notes on the sy35 after I passed correctly-formatted data

Sorry if this is a dumb thread. I'm learning about binary, raw, and hex data as I go along

Re: low-level MIDI handling

Posted: Thu Jan 21, 2016 12:55 pm
by meatballfulton
A lot of the MIDI docs show binary rather than hex for some reason. It's a PITA to read binary, esp.since most sequencers and MIDI monitors display in hex and you constantly have to do the conversions.

Have fun.

Re: low-level MIDI handling

Posted: Thu Jan 21, 2016 2:20 pm
by joeboy
I found the 1s and 0s useful for explaining what exactly is in each byte.
In any case, I just would up using the write() function in C, which passes the note-on as hex in this tutorial.

I guess that's really all I need to do for the time being. It's nice how simple UNIX can be.

Re: low-level MIDI handling

Posted: Sat Jan 23, 2016 8:15 pm
by Tidda
joeboy wrote:So continuing to search and twiddle, I found out about the hexdump command.
This is what playing middle C gives me, after running hexdump:

Code: Select all

0000000 fefe fefe fefe fefe fefe 3c90 903a 003c
*
0000010
It looks like something now, but I'm not sure what to try next. I know very little about how data is represented
Your bytes are in the wrong order for translation purposes because of the way your computer stores them in a file. (difference between big and little endian)
So it actually is: fe fe fe fe fe fe fe fe fe fe 90 3c 3a 90 3c 00

fe = active sense message (10x)
90 = note on, on channel 1 for note number ..
3c = middle C, with a velocity of ..
3a = 58 (decimal) followed by another ..
90 = note on, on channel 1 for note number ..
3c = middle C, with a velocity of ..
00 = zero (in this case note off message is implemented as a note on message with value 0)

Re: low-level MIDI handling

Posted: Sun Jan 24, 2016 7:20 am
by joeboy
Thank you for that explanation. It will come in handy when I start thinking about things beyond note-on and note-off.

Right now I'm using srfi-4 in Scheme to send messages to the hardware, and I can just specify integer values for each byte, which is nice.

Re: low-level MIDI handling

Posted: Mon Feb 01, 2016 8:43 pm
by raito
A couple of points:

Your notes play back all at once because you're just dumping the file back over MIDI. When you recorded it, the command just kept adding to the file. When you play it back, there's no timing, so it just goes out as fast as it can. MIDI recorders also timestamp the incoming data. I'd recommend looking up the MIDI file specification, and adjusting your programming to add the correct information to the files you save.

Many of the MIDI docs use binary because the MIDI spec starts out with an electrical specification, and works up from there. If you were looking at your MIDI stream with an oscilloscope you'd be doing the conversion the other way.

In theory, MIDI bit rate is 31250/sec. Note that while in this case bit rate is equal to baud rate, that's not the case for all communications. Exceeding that is technically not allowed by the spec, though any unit that insists on it probably isn't being smart.

Scheme huh? I do all my stuff outside work with ANSI Common Lisp.

Re: low-level MIDI handling

Posted: Sat Feb 13, 2016 9:34 pm
by cornutt
raito wrote: In theory, MIDI bit rate is 31250/sec. Note that while in this case bit rate is equal to baud rate
Not the data bit rate. MIDI uses 8 data bits, 1 start bit and 1 stop bit. So it's 10 baud for every 8 data bits, and you get 3125 data bytes/sec.