MIDI Programming |
Introduction |
There is a separate device file for each installed MIDI interface.
The device name contains two decimal digits which specify the interface
number. The interface number is shown in the printout produced by cat
/dev/sndstat. For example the device file for the first installed
MIDI port is /dev/midi00. Name /dev/midi is a symbolic
link that points to the default MIDI device file which is usually /dev/midi00.
These device files have similar capabilities than the ordinary /dev/tty interface. Everything written to the device will be sent to the midi port as soon as possible (there could be some earlier written bytes in the queue which delay the transmit). There are no timing features which make it difficult to use these devices for sequencer like applications. The intended use for this interface is sending and receiving system exclusive messages. For example this is required when making a patch editors and librarians for various MIDI synthesizers. Reading from the device waits until there are at least one byte in the receive buffer. When the first byte is received, the driver will not wait for additional characters. This means that the read returns usually less bytes than requested. Since the MIDI transfer rate is fairly high (about 31 kbaud), several bytes will be received before the reading process finally gets activated and is able to continue execution of the read call. On my 486/50 system it can receive up to about 60 bytes at a time. On a slower or heavier loaded system the read could return even more data at once. There are couple of unnecessary delays in the current implementation, but they seems to be harmless. For example it is possible to route the incoming midi data from one port into another using cat /dev/midi00 > /dev/midi01. There is no annoying delays between a keypress on the keyboard and the sound on the synth connected to the /dev/midi01. The /dev/midi interface supports select but currently just with Linux. To use the raw MIDI devices, you will need some knowledge of the MIDI protocol. The official MIDI 1.0 specification is sold by MIDI Manufacturer's Association (MMA). There are some books containing the most important parts of the protocol. In addition unofficial hacks of various MIDI specifications are available using anonymous ftp from ftp://mitpress.mit.edu/pub/Computer-Music-Journal/Documents/MIDI or read an HTML version here. |
Changing parameters |
We don't give exact details at this time since the interface will change
before final release. Here are just some hints.
There is possibility to set a time-out which the process waits for the first byte. By default it waits infinitely. Intelligent MIDI adaptersThe Roland MPU-401 and some other pro-level MIDI cards have various advanced features not available in most soundcards.For accessing the MPU-401 in it is intelligent mode, there are two ioctl calls. The first turns on the intelligent mode (or rather it resets the card and turns off the UART mode). The second one sends a command to the card. Since some commands have parameters or return some data, there will be a way to handle these situations. Unlike in some other MPU-401 drivers, OSS will not contain separate ioctl for each of the commands. There will be just one and the command (with parameters) are passed in the argument. The MPU-401 specific features are available only with cards supporting the intelligent mode. Several sound cards have a MPU-401 compatible interface which supports the UART mode only. We have used the MPU-401 for recording midi bytes and it works fine. The data received from the card contains all the information returned by the card, including the MIDI data, timing bytes, MPU marks and messages. I have not tried output yet but it should be possible to make it work. The MPU interface is not there so that everyone can program the MPU-401 itself. The /dev/sequencer2 interface will support all the features of MPU-401 in way that is portable. Some other MIDI cards have advanced features like tape sync and the sequencer2 interface makes these features accessible in device independent way. The MPU interface is there just for making it easier to port MPU specific applications from other environments. (The main reason why it is there is that it makes it easier to study ins and outs of the MPU-401). Using the raw MPU interface requires deep knowledge about the MPU-401 internals. There are at least two sources for this information. The first is the MPU-401 Technical Reference Manual and the other is the developers library for the Music Quest MQX-32M. |
What is MIDI? |
MIDI is an acronym for Musical Instrument's Digital Interface (or something
like it). MIDI 1.0 Detailed Specification defines both the hardware level
interface and the communication protocol used for communication between
devices having MIDI interface. It is primarily a data communication specification
but also used in many other ways. I don't try to give complete description
here. Just the rough idea.
The hardware level MIDI interface is like a RS-323 port but it is not plug compatible with it. MIDI cable has 5 pin DIN connectors at the ends and the transfer rate is nonstandard (31250 baud???). One cable can carry data just to one direction. Bi-directional connection requires two cables. More than two devices can be connected together by chaining the devices. The MIDI devices communicate by sending messages through the MIDI cable. Every message starts with a status byte and may have one or more additional data bytes. The status byte has 1 in the most significant bit while the data bytes have 0. This means that the data bytes may have just 128 different values and to carry just 7 bits of information. The first four bits of a status byte (status 0xF0) specify the type of the status and the last 4 bits carry the MIDI channel number. Status bytes 0xF0 to 0xFF are reserved for system messages (the last 4 bits contain the message type). There are 16 possible channels in the MIDI cable. Each of them can be assigned to physically separate devices or some devices could interpret the messages sent to all channels. Some parameters such as instrument (program) number are assigned by channel so each device listening a particular MIDI channel will play using the same instrument number. The device has freedom to interpret the instrument number as it wish. For example when the player hits a key on the keyboard, a NOTE ON message is transmitted to the MIDI cable. It starts with a status byte 0x9X where the X is the channel number. There are two data bytes following the status. The first is the note number which tells which key the player has pressed. The second specifies the velocity of the keypress. The velocity is used to control the volume and some other parameters of the played sound. It is important to notice that no sound is transferred through the MIDI line. Just instructions how the receiving instrument should control itself. MIDI file is a file containing MIDI messages and some other data which can be used by the MIDI sequencers and other applications. It is a well defined interchange format which makes it possible to transfer songs between virtually any application supporting the format. In the PC workd these files have an extension .MID. Unlike some other file formats for storing musical information (.MOD) MIDI files don't contain any instrument data. The instruments are defined just by including some MIDI program change messages into the files. The playing system has complete freedom to assign actual instrument timbres for the program numbers. |