overtone/midi-clj — pull request #4 by luntar (2012-10-16 06:55:51+00:00)
Title: midi-sysex update: Status byte conversion to 2s-comp and support for "hex-strings"

Hi Sam,

Note - this is my first pull request and I’m new to functional programming and Clojure so I hope you find some of this useful.

Summary of my pull-request:
1)	Function midi-sysex shall support “byte” values larger than 127 (e.g. any MIDI status byte)

2)	Function midi-sysex shall support messages encoded as strings and ignore commas and white space. This change must not break the existing byte-seq and byte-array support.

Some Details:
In the method byte-seq-to-array, I added a call to unchecked-byte; this allows Clojure to perform the type coercion without raising an uncheckedByteCast exception (like Java’s default behavior). This relies requirement #1 described above.

For feature #2 (and perhaps this is where the Clojure expert would sigh), I convert the string using a new function byte-str-to-seq. I do the “hex2int” conversion using a hash (hex-char-values) and the map function.

Possibly Important:
Since I use the unchecked-byte function, the code is now dependent on Clojure 1.3.0 or better.

Rational:
The rationale behind adding string support to the midi-sysex: 1) It makes it much easier to use at the REPL, 2) while in “how do I make my favorite MIDI controller do ____” mode, it’s potentially easier to cut-n-paste MIDI trace data and documentation snippets, and finally, there’s some existing “DAW/controller mapping” architectures out there that uses message string pattern matching. Perhaps we could leverage some of this work, and at the very least, make it easier to integrate/add more hardware controllers to the overtone project.

Examples:
Here are some examples of the proposed midi-sysex capability:

(def mo (midi-out))

; this is the old and still supported message format
(midi-sysex mo [-16 126 127 6 1 -9])
; now you can do this too:
(midi-sysex mo [0xF0 0x7E 0x7F 0x06 0x01 0xF7”)
; and this:
(midi-sysex mo "F0 7E 7F 06 01 F7”)
(midi-sysex mo "F07E7F0601F7”)
(midi-sysex mo "f0 7e 7f 06 01 f7”)
(midi-sysex mo "f0,7e,7f,06,01,f7”)

(def cmd 0x01)
(midi-sysex mo (concat "f07e7f06” cmd “01f7”))

Relevant Overtone Forum Post: https://groups.google.com/forum/?hl=en&fromgroups=#!topic/overtone/8oyHC_fB4Tw