ttymidi
15 |
19 |
20 | In a nutshell
21 |24 | ttymidi is a GPL-licensed program that allows external 25 | serial devices to interface with ALSA MIDI applications. 26 | The main motivation behind ttymidi was to make 27 | Arduino boards talk to MIDI applications 28 | without the need to use (or build) 29 | any extra hardware. 30 |
31 | 32 |Usage
35 |37 | If you are using an Arduino board, simplest will be to use 38 | 39 | Arduino MIDI library, as described in the following sections. 40 | You may also use less sophisticated ardumidi library that is provided 41 | with ttymidi download package. If you are 42 | using another device, you should read the rest of this page to see how 43 | your device should communicate. Once your device is programmed and 44 | connected to your PC, ttymidi is executed in the 45 | following manner:
46 | 47 | 53 | 54 | 55 |56 |
ttymidi -s /dev/ttyS0 -b 960057 | 58 | 59 |
60 | Where /dev/ttyS0 is the serial port you want to read from,
61 | and 9600 is the bitrate. For Arduino with serial connection
62 | runnning at 115200bps (the default rate for ttymidi)
63 | the command would be:
64 |
67 | Arduino Diecimila: 68 |
ttymidi -s /dev/ttyUSB0 -v69 | 70 |
Arduino UNO: 71 |
ttymidi -s /dev/ttyACM0 -v72 | 73 | 74 |
75 | Where the -v gives me verbose output, which helps
76 | me when I'm debugging.
77 |
80 | ttymidi creates an ALSA MIDI input and output ports that can be 81 | interfaced to any compatible program. This is done in the following manner: 82 |
83 | 84 |85 |
86 | # start ttymidi 87 | ttymidi -s /dev/ttyUSB0 & 88 | 89 | # start some ALSA compatible MIDI 90 | # program (timidity, in this case) 91 | timidity -iA & 92 | 93 | # list available MIDI input clients 94 | aconnect -i 95 | 96 | # list available MIDI output clients 97 | aconnect -o 98 | 99 | # connect 100 | aconnect 128:0 129:0 101 | 102 | # ...where 128 and 129 are the client 103 | # numbers for ttymidi and timidity, 104 | # found with the commands above105 | 106 | 107 |
108 | If you would like to use a graphical interface to connect your MIDI 109 | clients, you can use something like 110 | qjackctl. As for 111 | the program that will consume the MIDI data, there are many out there 112 | (other than timidity, which 113 | was used in the previous example). Some crowd pleasers are 114 | fluidsynth, 115 | puredata, 116 | sooperlooper 117 | and ardour, 118 | to cite a few. 119 |
120 | 121 |If you are using automated patchbay connecting application, such as
122 | the patchbay feature of qjackctl or
123 | aj-snapshot you might want to rename the name of ttymidi client created by using
124 | -n flag.
ttymidi -s /dev/ttyUSB0 -v -n my_weird_controller126 | 127 |
Programming your serial device
131 |Arduino MIDI library
132 |ttymidi is nowadays compatible with
134 | Arduino MIDI library
135 | and we strongly recommend using it when communicating from Arduino to ttymidi.
136 | You only need to remember to call Serial.begin(int baud_rate)
137 | immediately after MIDI.begin(int channel) on your patch as
138 | the serial port of your computer is not able to operate at MIDI baud rate
139 | that MIDI library is using. For example:
141 | void setup() {
142 | MIDI.begin(4); // will listen incoming MIDI at channel 4
143 | Serial.begin(115200); // will change baud rate of MIDI traffic from 31250 to 115200
144 | }
145 |
146 | You'll find MIDI library examples with these changes made in ttymidi 147 | download package.
148 |Arduino interface
150 |This chapter explains how to use deprecated (but still functional) 152 | ardumidi library instead of Arduino MIDI library. To interface to 153 | ttymidi, the serial-attached device must 154 | send data in MIDI-format. 155 | In the case of Arduino boards, a library is provided that abstracts all 156 | the nitty-gritty details away. Below is a list of the available 157 | functions: 158 |
159 | 160 |161 |
162 | // Start/stop playing a certain note: 163 | midi_note_on(byte channel, byte key, byte velocity); 164 | midi_note_off(byte channel, byte key, byte velocity); 165 | 166 | // Change pressure of specific keys: 167 | midi_key_pressure(byte channel, byte key, byte value); 168 | 169 | // Change controller value (used for knobs, etc): 170 | midi_controller_change(byte channel, byte controller, byte value); 171 | 172 | // Change "program" (change the instrument): 173 | midi_program_change(byte channel, byte program); 174 | 175 | // Change key pressure of entire channels: 176 | midi_channel_pressure(byte channel, byte value); 177 | 178 | // Change pitch-bend wheel: 179 | midi_pitch_bend(byte channel, int value); 180 | 181 | // Check if a MIDI message has arrived from the PC 182 | int midi_message_available(); 183 | 184 | // Get a MIDI message from the PC 185 | MidiMessage read_midi_message(); 186 | 187 | // Get the pitch bend value from a MIDI message 188 | int get_pitch_bend(MidiMessage msg); 189 | 190 | // Send a comment: 191 | midi_comment(char* str); 192 | 193 | // Send a series of bytes (to be interpreted by another program): 194 | midi_printbytes(char* bytes, int len);195 | 196 | 197 |
198 | Where the parameters are in the range: channel (0–15), 199 | pitch-bend value (0–16383), all other values (0–127). 200 | The center position of the pitch-bend wheel is 8192. 201 |
202 | 203 |204 | Since MIDI uses numbers to represent notes, a few useful constants 205 | are defined in this library. With them, it is much easier to deal with 206 | MIDI nodes. For example: 207 |
208 | 209 |210 |
midi_note_on(0, MIDI_C + MIDI_SHARP - 2*MIDI_OCTAVE, 127)211 | 212 | 213 |
214 | All notes refer to the 4th octave by default. So the line above
215 | plays a C#2. As a shortcut to the 1st octave, constants such as
216 | MIDI_C0 can be used.
217 |
220 | Similarly, when receiving MIDI messages from the PC, you can use 221 | constants to figure out type of the incoming message: 222 |
223 | 224 |225 |
if (midi_message_available() > 0) {
226 | MidiMessage msg = read_midi_message();
227 | if (msg.command == MIDI_NOTE_ON && msg.channel == 0) {
228 | /*
229 | Since this is a "Note On" command, we know that
230 | - msg.param1 is the MIDI note
231 | - msg.param2 is the "velocity" of the note
232 |
233 | How do we know this? See the table below.
234 | */
235 | }
236 | }
237 |
238 |
239 |
240 | There are a couple of things to keep in mind when using MIDI in features of
241 | ardumidi. Firstly midi_message_available() will always
242 | flush the incoming Serial buffer of all non-MIDI data. In practice this
243 | means that you can't mix using the Serial library and
244 | ardumidi
245 | (unless you know what you are doing). Secondly the incoming Serial buffer
246 | of arduino can store at most 42 MIDI commands so you have to take care
247 | that buffer does not overflow. However this does not cause your patch to crash,
248 | only couple MIDI messages are lost.
249 |
252 | To install the ardumidi library, just copy its folder
253 | into Arduino's sketchbook/libraries directory.
254 | Also note that an example Arduino sketches are included in the
255 | ttymidi package. They should appear under Examples
256 | in Arduino IDE after you have succesfully installed the library.
257 | For instance, ardumidi_test, will play one C-minor
258 | chord every second.
259 |
General interface
267 |268 | If you are using another serial device in place of the Arduino, you'll 269 | need to program it to follow the MIDI specification. To sum this up, most 270 | of the commands sent to ttymidi are comprised of 3 bytes: 271 |
272 | 273 |274 |
275 | byte 1 byte 2 byte 3 276 | status parameter #1 parameter #2277 | 278 | 279 |
280 | Where status is a combination of a command and a
281 | channel. Why is it called status? I haven't a clue, but that's
282 | the name in the MIDI spec. The table below describes the codes for each
283 | command as well as their associated parameters.
284 |
| 291 | | code | 292 |parameter #1 | 293 |parameter #2 | 294 |
|---|---|---|---|
| note off | 297 |0x80-0x8F | 298 |key | 299 |velocity | 300 |
| note on | 303 |0x90-0x9F | 304 |key | 305 |velocity | 306 |
| pressure | 309 |0xA0-0xAF | 310 |key | 311 |pressure | 312 |
| controller change | 315 |0xB0-0xBF | 316 |controller | 317 |value | 318 |
| program change | 321 |0xC0-0xCF | 322 |program | 323 |- | 324 |
| channel pressure | 327 |0xD0-0xDF | 328 |value | 329 |- | 330 |
| pitch bend | 333 |0xE0-0xEF | 334 |range LSB | 335 |range MSB | 336 |
| bytes | 339 |0xFF | 340 |0 | 341 |0 | 342 |
347 | Most of these commands and parameters are self-explanatory. All parameters
348 | are given as a number in the range 0–127. That is, they're all 7-bit
349 | numbers with a 0 padded to their left (i.e. 0xxxxxxx) in order
350 | to make up 1 full byte (8 bits). For channel pressure and program change you
351 | should send only one data byte.
352 |
355 | One odd MIDI command is the pitch bend, whose parameter is a
356 | 14-bit number. For this reason, it is split into two parts: a least
357 | significant byte (LSB) and a most significant byte (MSB). These can be
358 | easily computed by quick logical and and shift operations:
359 |
362 |
363 | int range = 12345; 364 | byte range_lsb = range & 0x7F; 365 | byte range_msb = range >> 7;366 | 367 | 368 |
369 | The bytes command provides a way to send non-MIDI data
370 | through the serial port without messing up with ttymidi.
371 | All you need to do is send 0xFF 0x00 0x00 len data[0] data[1] ...,
372 | where len is the number of data[•] in
373 | the message. When seeing such messages, ttymidi will
374 | just print them to the screen (but not parse them), so you can use this
375 | to print comments from your serial device. You can also use it with
376 | sprintf for debugging.
377 |
380 | You may have noticed that, for all commands, the 1st byte always has a 1 381 | as the most-significant bit, while the other 2 (or 1) bytes always have a 0. 382 | This comes from the MIDI spec, and is used by ttymidi 383 | for data alignment purposes. Do not try to change that in your 384 | serial device's program, or it will royally mess things up. 385 |
386 | 387 |Downloads
390 |393 | Get ttymidi here 394 |
395 | 396 |
397 | The archive includes the ttymidi program, examples
398 | for Arduino MIDI library as
399 | well as the ardumidi libraries for Arduino. Also, don't
400 | forget to read the README file in that is included in the
401 | archive. It has important information about compiling ttymidi.
402 |
405 | It is possible that a more recent version is available at 406 | the project's trunk series. 407 |
408 | 409 |Found a Bug?
412 |415 | If you find a bug, please report it! 416 | I especially like when the report comes with a patch :) but that's not a requirement. 417 |
418 | 419 |Links
422 |424 |
-
425 |
- Arduino 426 |
- 427 | Arduino MIDI library 428 |
- How does MIDI work? — by Indiana University's CECM 429 |
- Arduino MIDI output board — a physical alternative to ttymidi 430 |
- Matthias Nagorni's website — examples for ALSA programming 431 |
- QJackCtl 432 |
- Timidity++ 433 |
- Fluidsynth 434 |
- Puredata 435 |
- Sooperlooper 436 |
- Ardour 437 |
Authors
442 |444 |
-
445 |
- Thiago Teixeira 446 |
Changelog
455 |457 |
-
458 |
- 0.60 459 |