blob: 816449d65b63479d6d7c849d03cdf09096c5e70d [file] [log] [blame]
Glenn Kasten45508f52015-11-09 15:47:54 -08001page.title=MIDI Architecture
2@jd:body
3
4<!--
5 Copyright 2015 The Android Open Source Project
6
7 Licensed under the Apache License, Version 2.0 (the "License");
8 you may not use this file except in compliance with the License.
9 You may obtain a copy of the License at
10
11 http://www.apache.org/licenses/LICENSE-2.0
12
13 Unless required by applicable law or agreed to in writing, software
14 distributed under the License is distributed on an "AS IS" BASIS,
15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 See the License for the specific language governing permissions and
17 limitations under the License.
18-->
19
20<div id="qv-wrapper">
21 <div id="qv">
22 <h2>In this document</h2>
23 <ol id="auto-toc">
24 </ol>
25 </div>
26</div>
27
28<p>
29This article describes the generic MIDI architecture, independent of
30any platform implementation, API, or platform-specific features.
31</p>
32
33<h2 id="keyConcepts">Key concepts</h2>
34
35<h3 id="events">Events</h3>
36
37<p>
38The MIDI protocol is designed for event-based communication.
39An <a href="https://en.wikipedia.org/wiki/Event_(computing)">event</a>
40is an indication that something happened or will happen at a specified
41time. MIDI events are represented by <em>messages</em>, atomic
42bundles of information.
43</p>
44
45<h3 id="transport">Transport</h3>
46
47<p>
48MIDI messages are encoded and delivered via a
49<a href="https://en.wikipedia.org/wiki/Transport_layer">transport layer</a>,
50abbreviated <em>transport</em>, which sends the raw MIDI data
51to the recipient who then decodes the data into messages.
52</p>
53
54<p>
55Hardware-based MIDI transports include:
56</p>
57<ul>
58<li>MIDI 1.0 current loop with
59<a href="https://en.wikipedia.org/wiki/DIN_connector">5-pin DIN</a> connector</li>
60<li>USB</li>
61<li>Bluetooth Low Energy (BLE)</li>
62</ul>
63
64<h3 id="messageRepresentation">Message representation</h3>
65
66<p>
67A MIDI transport specification describes how to convey messages.
68Although the packaging of messages is transport-specific at the
69lowest level, at a higher level applications can consider a
70time-ordered sequence of messages to be a demarcated
71<a href="https://en.wikipedia.org/wiki/Bytestream">byte stream</a>.
72This is possible because each message contains
73enough information to determine the total length of the message,
74provided the start of the message boundary is known.
75</p>
76
77<p>
78Most MIDI messages are short (one to three bytes), yet there is the
79capability for longer messages via <em>SysEx</em>.
80</p>
81
82<h3 id="timestamps">Timestamps</h3>
83
84<p>
85A <a href="https://en.wikipedia.org/wiki/Timestamp">timestamp</a>
86is an optional label attached to a message at origination or upon receipt,
87depending on the transport. The timestamp is expressed in time units
88such as seconds or
89<a href="https://en.wikipedia.org/wiki/Jiffy_(time)">ticks</a>.
90</p>
91
92<p>
93In the absence of an explicit timestamp, the system must substitute
94the timestamp of the immediately preceding message or the current
95time. The accuracy of these timestamps, whether explicit or implicit,
96is an important aspect of the reliability of a MIDI-based system.
97</p>
98
99<p>
100Timestamps are not part of the MIDI 1.0 protocol. They are often added
101as part of a platform-specific API. The BLE transport has timestamps
102to indicate the timing of the multiple individual messages sent within
103one BLE packet.
104</p>
105
106<h3 id="devices">Devices</h3>
107
108<p>
109A <a href="https://en.wikipedia.org/wiki/Peripheral">peripheral</a>
110provides input/output (I/O) capability for a computer. The terms
111<em>MIDI peripheral</em> and <em>MIDI device</em> commonly
112refer to any hardware or software module that supports the MIDI protocol.
113Within this document, <em>MIDI peripheral</em> refers to the
114physical entity and <em>MIDI device</em> describes the module that
115actually implements MIDI.
116</p>
117
118<h3 id="ports">Ports</h3>
119
120<p>
121A <a href="https://en.wikipedia.org/wiki/Computer_port_(hardware)">port</a>
122is an interface point between computers and peripherals.
123</p>
124
125<p>
126MIDI 1.0 uses a female 5-pin DIN socket as the port.
127Each port is either <em>OUT</em> (source of MIDI data), <em>IN</em> (sink for MIDI data),
128or <em>THRU</em> (meaning an <em>IN</em> which is directly routed to an <em>OUT</em>).
129</p>
130
131<p>
132Other transports such as USB and BLE extend the
133<a href="https://en.wikipedia.org/wiki/Computer_port_(software)">port concept</a>.
134</p>
135
136<p>
137A MIDI device has at least one <em>OUT</em> port, <em>IN</em> port, or both.
138</p>
139
140<p>
141The MIDI device supplies stream(s) of messages originating at each <em>OUT</em> port,
142and receives stream(s) of messages arriving at each <em>IN</em> port.
143The terms <em>IN</em> and <em>OUT</em> are of course relative to one port;
144from the perspective of the other port the reverse term applies.
145</p>
146
147<h3 id="connection">Connection</h3>
148
149<p>
150In the MIDI 1.0 transport, an <em>OUT</em> port connects to at most
151one <em>IN</em> or <em>THRU</em> port due to the nature of the current loop.
152In USB and BLE transports, the same is true at the lowest layer, though
153an implementation may re-condition the message stream so that it can
154be broadcast to multiple <em>IN</em> ports.
155</p>
156
157<h3 id="cable">Cables</h3>
158
159<p>
160A MIDI 1.0 <a href="https://en.wikipedia.org/wiki/Cable">cable</a> is the
161physical bundle of wires that connects an <em>OUT</em> port to an <em>IN</em> or <em>THRU</em> port.
162The cable carries data only.
163</p>
164
165<p class="note">
166<strong>Note:</strong>
167There are non-standard modifications to MIDI that supply power over the
168two unused pins. This is called <em>phantom power</em>.
169</p>
170
171<p>
172A <a href="https://en.wikipedia.org/wiki/USB#Cabling">USB cable</a>
173is similar, except there is a wide variety of connector types,
174and the <em>IN</em>/<em>OUT</em>/<em>THRU</em> concept is replaced by the host/peripheral role.
175</p>
176
177<p>
178When operating in USB host mode, the host device supplies power to the
179MIDI peripheral. Most small MIDI peripherals take one USB unit load (100
180mA) or less. However some larger peripherals, or peripherals with audio
181output or lights, require more power than the host device can supply.
182If you experience problems, try another MIDI peripheral or a powered
183USB hub.
184</p>
185
186<h3 id="channel">Channel</h3>
187
188<p>
189Each MIDI message stream in multiplexed among 16 <em>channels</em>.
190Most messages are directed at a specific channel,
191but there are message types that aren't channel-specific.
192Conventionally the channels are numbered one to 16, though
193represented by channel values of zero to 15.
194</p>
195
196<p>
197If the application needs more than 16 channels or a higher throughput
198than one message stream can support, then multiple ports
199must be used.
200</p>
201
202<p>
203In MIDI 1.0, this is accomplished by multiple cables connecting pairs of ports.
204</p>
205
206<p>
207In the MIDI over USB transport, a single USB endpoint can support multiple
208ports, each identified by a <em>cable number</em> [sic].
209According to the USB MIDI specification,
210the <em>cable number</em> identifies the virtual port within the endpoint.
211</p>
212
213<p class="note">
214<strong>Note:</strong>
215<em>port number</em> would have been a more accurate term,
216given that it identifies a port.
217</p>
218
219<p>
220Thus a single USB physical cable can carry more than one set of 16 channels.
221</p>
222
223<h2 id="platformImplementation">Platform implementation</h2>
224
225<p>
226As noted in the introduction, these generic MIDI concepts apply to all
227implementations. For the interpretation of the concepts on the Android
228platform, see the
229<a href="http://developer.android.com/reference/android/media/midi/package-summary.html">
230Android MIDI User Guide for <code>android.media.midi</code></a>.
231</p>