Release 2.0  Copyright ã1991 by NeXT Computer, Inc.  All Rights Reserved.

MIDI and the MIDI Driver

Introduction

About This Document

This document consists of an introduction to MIDI and a specification of the NeXT MIDI driver C functions.  The sections ªWhat is MIDI?º and ªConnecting MIDI Devicesº are intended for all users of MIDI.  Programmers should additionally read ªNeXT Software Support for MIDIº to determine whether they'll use the Music KitÔ or the driver C functions.  Those planning to use the driver C functions should read ªThe MIDI Data Format,º ªMIDI Driver Overview,º and ªMIDI Driver C Functions.º

What is MIDI?

MIDI, the Musical Instrument Digital Interface, defines a software format and a hardware standard for exchanging information among electronic musical instruments (such as synthesizers, samplers, digital pianos, and guitar or wind controllers) as well as other devices (such as computers, sequencers, mixers, signal processors, and even stage lighting).  Originally designed to capture the performance gestures of a keyboard player, MIDI normally transmits keyboard-oriented information, such as which key the performer depressed and with what velocity, or which button or slider was adjusted on a synthesizer's control panel.  This sort of data is much more compact and more easily edited than the data in a digital audio recording of the same performance.  Unlike audio data, MIDI data can easily be used to control other instruments or to create a printed score (using a music notation application).

By hooking a MIDI instrument to a NeXTÔ computer, you can ªplayº the built-in DSP as a sound synthesizer.  You can also create an automated performance by editing music data files and sending the data to external MIDI synthesizers.  The demonstration program /NextDeveloper/Demos/Ensemble lets you explore these features, among others.

Non-musical applications could also be written to exploit MIDI's real-time control.  For example, with the mouse, a user can move only one graphical slider at a time; but with an external MIDI device, each finger could control a different slider independently.  Similarly, a group of users could operate different controls simultaneously, perhaps to play a game.

Connecting MIDI Devices

You can connect MIDI instruments to either of a NeXT computer's serial ports, using an external device known as a MIDI interface.  The instruments connect to the MIDI interface (or to each other) with standard MIDI cables, available at most music stores.  The MIDI interface adapts these cables' unidirectional 5-pin DIN connectors to the serial port's bidirectional mini-DIN connector.  Any number of instruments can be connected to a serial port through the interface, and the two ports can be used simultaneously (whether by the same application or by two different ones).  A single serial port can receive and transmit MIDI data at the same time.

Which MIDI interface to use depends on which computer you have.  While the older 68030-based NeXT computer has RS-422 serial ports, the 68040-based computersÐthe NeXTstation, NeXTstation Color, and NeXTcubeÐhave RS-423 ports (which are compatible with RS-232, and which support hardware flow control for modems).  MIDI interfaces designed for Macintosh computers work with the 68030-based NeXT computer, using a standard Macintosh serial cable, since the Macintosh also has RS-422 ports.  These interfaces can be obtained at most music stores.  Some interfaces include the cable that connects the interface to the computer.

Most Macintosh-style interfaces don't work with NeXT's 68040-based computers, however.  Typically, these interfaces exhibit the symptom of not transferring any MIDI data, or transferring it in only one direction (in but not out, or vice versa).  One Macintosh-style interface that is known to work is the Opcode Studio 3.  The appropriate cable for connecting it to 68040-based NeXT computers is described in the zs UNIX manual page (under ªNeXT 68040 to Some RS-422 Devicesº), and is available from Computer Cables and Devices at (203) 287-0197.  Opcode Systems may provide other solutions, including the modification of existing Pro Plus interfaces to work with 68040-based NeXT computers using standard serial cables.  (Contact Opcode Systems at (415) 369-8131.)  Rupert Nieberle in Berlin (phone:  49-30-618-7765) manufactures two MIDI interfaces that are compatible with the 68040-based machines, using standard cables.

The musical instrument must be set up correctly for MIDI communication to work as expected.  Because MIDI is a unidirectional protocol, there's no means for an application to verify that the external device is receiving the MIDI data that the application sends.  Thus the user is responsible for ensuring that the configuration is correct.  For instructions on setting up the MIDI device, see the owner's manual for that device.

In particular, note that most MIDI commands are sent on specific ªchannels.º  Unlike the left and right channels of analog audio signals, MIDI channels don't use separate cables, but instead are encoded in the MIDI data itself.  The sixteen MIDI channels are used for sending separate streams of commands to different synthesizers on a single MIDI network, or to the distinct sound-generating units within a single multi-timbral synthesizer (such as the NeXT computer's DSP, as used by the Music Kit).  There's no MIDI command that asks a device to start using a certain MIDI channel.  Instead, the user must manually set the MIDI device to transmit and receive on the channels expected by the software.  A typical default is to transmit and receive on channel 1.

NeXT Software Support for MIDI

There are two levels of programming support for MIDI in the NeXT system software:

·	The MIDI driver is a loadable Mach device driver that controls the flow of MIDI data to and from the serial ports.  The MIDI library, /usr/lib/libmidi.a, contains C functions for direct control of the MIDI driver.  Programming at this levelÐthe MIDI driver C functionsÐgives you the greatest amount of control over the buffering and timing of MIDI data.  However, this control comes at the expense of programming simplicity.

·	At a higher level, the Music Kit includes an Objective-C language class called Midi that provides an object-oriented interface to the MIDI driver.  Programming at this level is much easier, because the Midi object insulates you from details of the driver, and because the MIDI data is parsed and interpreted for you.  This level also lets you take advantage of Music Kit features such as the automatic translation of MIDI data into Scorefile commands or into DSP sound synthesis instructions.

Generally, the Music Kit is the recommended approach.  The Music Kit's Midi class will almost always give you all the functionality you need, without adding any noticeable delay to the MIDI processing provided by the driver.  However, if you're not using the Music Kit for anything else, and you're more concerned with maximum efficiency than programming ease, you may want to use the driver's C functions instead of the Midi class's Objective-C methods.  The functions also provide some features that the Midi class doesn'tÐfor example, you can manage the size of the MIDI data queue, manipulate the driver's timer, and filter out a few more kinds of MIDI commandsÐbut you'll rarely need these features.

For information on MIDI support in the Music Kit, see ªSound and Music Overviewº and ªMusic Performanceº in Sound, Music, and Signal Processing:  Concepts.  In Sound, Music, and Signal Processing:  Reference, see the Midi Class Specification, as well as the Music Kit C Functions and the ScoreFile reference.  The programming examples in /NextDeveloper/Examples/MusicKit are helpful, particularly those whose names begin with ªMidi.º  For a more advanced example, see Ensemble in the same directory.

The rest of this document contains information that's useful for programming with the MIDI driver C functions.  The sample C programs in /NextDeveloper/Examples/MidiDriver illustrate some of the functions documented here.  Information can also be gleaned from the header files in /usr/include/midi.

The MIDI Data Format

If you use the MIDI driver functions rather than the Music Kit, you'll be examining MIDI data as hexadecimal values, so you'll need to understand the MIDI data format.  Read this section for a synopsis of the data format, if you're not already familiar with the MIDI specification.

MIDI data consists of commands sent in an asynchronous serial stream at 31.25 kBaud.  The data is transmitted in ten-bit bytes, but the first and last bits of each byte are start and stop bits, added by the transmitting device and stripped off by the receiving device.  Thus, MIDI commands are considered to consist of eight-bit bytes.  A typical MIDI command contains:

·	One Status byte (whose most significant bit is set to 1).  The Status byte defines a type of command, such as Note On or Pitch Bend.

·	Zero, one, or two Data bytes (each having its most significant bit set to 0).  Data bytes contain values applied by the command, such as ªkey numberº and ªvelocity,º or ªamount of pitch bend.º  The type of command, specified by the preceding Status byte, determines how many Data bytes are expected.

There are two exceptions to the above pattern:

·	The Status byte may be omitted, in which case the type of command is given by the most recent Status byte.  This condition is called Running Status.

·	The Status byte F0 (hexadecimal) is the special System Exclusive command, which is followed by a Data byte identifying a particular manufacturer, and any number of subsequent Data bytes whose meaning the manufacturer is free to determine.  Only that manufacturer's instruments are expected to respond to the System Exclusive command.

Status bytes with hexadecimal values from 80 to EF are ªchannel commands.º  These MIDI commands are sent on specific MIDI channels, as determined by the rightmost four bits of the Status byte.  Most MIDI devices can be configured to respond only to certain channels, making it possible for a single MIDI data stream to deliver different musical information to different devices simultaneously.

Note that although MIDI bytes are classified as Status bytes or Data bytes, the term ªMIDI dataº refers generically to everything in a stream of MIDI commands, both Status bytes and Data bytes.

The file <midi/midi_types.h> includes a list of Status bytes and other standard MIDI definitions.  You can obtain the complete MIDI specification from the International MIDI Association at 11857 Hartsook St., North Hollywood, CA  91607, U.S.A.  For an introduction to the MIDI specification, including a summary of commands, see Gareth Loy's article ªMusicians Make a Standard:  The MIDI Phenomenonº in Computer Music Journal Vol. 9, No. 4 (Winter 1985).