Basically, I'm creating a file of fixed length (260 bytes, in /tmp), mmap() it and update the contents based on the MIDI stream I'm receiving. The first 128 bytes are for keys, where each byte is the last seen velocity (0 means "off"). The following 128 bytes are for the cc messages. Bytes 257 and 258 store the pitch bend, a 14-bit value with MSB in byte 257. Byte 259 is the last received program change value and 260 is the channel aftertouch.
MATLAB Access
Accessing the values from MATLAB does not require any special functions, it can simply be done using
mm = memmapfile('/tmp/midibroadcast');
then the data can be obtained in real-time from mm.Data. As described above, mm.Data(1:128) are the last seen key down velocities (0 meaning key is not pressed) and mm.Data(129:256) are controllers 0..127. The pitch bend value can be obtained as mm.Data(257)*256+mm.Data(258) (note this may be buggy; my cheap controller (KeyRig 25) does not let me set and hold precise pitch bends, so I can't test it properly). mm.Data(259) is the program change and mm.Data(260) shows the current aftertouch amount.
Python Access
From Python the values can be accessed as easily:
import os import mmap mfd = os.open('/tmp/midibroadcast', os.O_RDONLY) mfile = mmap.mmap(mfd, 0, prot=mmap.PROT_READ)
Remembering that Python counts from 0, the controllers can be read in real-time as mfile[128] to mfile[255]: just subtract 1 from the descriptions above.
Any language which can do memory-mapping should be able to do the same, but it should even be possible to read the current state just by re-reading the /tmp/midibroadcast file.
Enjoy!
No comments:
Post a Comment