Google+
4elements web design & consultancy

4elements

blog

  • like 1 16

    An Introduction to Web MIDI

    4elements | web design The Hague blog • An Introduction to Web MIDI

    MIDI is everywhere, from game consoles to audio hardware and music production software, and now it is available in JavaScript. This tutorial will get you up and running with Web MIDI.

    This tutorial assumes you have experience with JavaScript programming and a basic understanding of MIDI, but it will cover some of the basics nonetheless.

    What is MIDI?

    MIDI, or Music Instrument Digital Interface, is a standard protocol that describes the way all MIDI compatible hardware and software communicates. In other words, it is the digital language that all MIDI compatible hardware and software uses to communicate.

    The language is very expressive, it allows a lot of information to be packed into a small amount of space. The information can include everything from musical notes, key velocities and sustain pedal activity, through to stereo positioning and ADSR envelope data.

    What is Web MIDI?

    Web MIDI, a standard JavaScript API specified by the W3C, allows JavaScript to communicate with MIDI compatible devices that are connected to a user's computer.

    When combined with Web Audio, these APIs provide a powerful set of tools that can be used to create a wide range of audio and music applications for the web.

    The Basics

    We are now going to take a look at the Web MIDI API and begin to construct the code that will allow us to send and receive some simple MIDI messages.

    Detecting Browser Support

    The first thing we need to do is make sure the web browser that is running our JavaScript code supports the standard Web MIDI API. This can be done easily enough by checking for the existence of the requestMIDIAccess function.

    If the requestMIDIAccess function exists then Web MIDI is available for us to use, and we can ask the browser for access to the user's MIDI devices.

    Requesting MIDI Access

    Due to the nature of Web MIDI, some users may choose to deny JavaScript access to any MIDI devices that are connected to their computers, for privacy reasons. Because of that we must first request access to the MIDI devices and then handle the response appropriately.

    The requestMIDIAccess function returns a Promise that will succeed if MIDI access is permitted, and fail if MIDI access is denied. If the request is successful we will be provided with a MIDIAccess object that gives us access to any MIDI compatible devices connected to the user's computer.

    MIDI Input and Output

    MIDI devices are made available to our code in two flavors, input devices and output devices. Input devices may send our code MIDI messages, and our code may send MIDI messages to output devices. If a device can send and receive MIDI messages, it will be presented as an input device and an output device.

    The MIDIAccess object exposes maps containing the available devices.

    Each MIDIInput and MIDIOutput object represents a physical MIDI device, and provide information about the devices including the device name and the device manufacturer. That information could be used within a web application to present a list of usable devices to the user.

    Receiving MIDI Messages

    A MIDIInput object will dispatch a MIDIMessageEvent object whenever a MIDI message is sent from the physical device represented by the MIDIInput object. To receive MIDI messages from a device we simply add an event listener to the relevant MIDIInput object.

    The following code will listen for MIDI messages sent from all input devices.

    A MIDIMessageEvent object has a data property that is a Uint8Array object containing three values. Those values are the three bytes that form the raw MIDI message, and we need to read the MIDI message information from those bytes.

    The channel is the MIDI channel identifier which the MIDI message is associated with.

    The type is the MIDI event type, which could be anything from a key being pressed through to the frequency of a filter being changed. For now we are only interested in two event types, note-on (0x90) which indicates a key has been pressed, and note-off (0x80) which indicates a key has been released.

    The note passed to the handleNoteOn and handleNoteOff functions is the MIDI note, and the velocity is the speed at which the key was pressed or released. The velocity can be any integer in the range 0 to 127, inclusive.

    So there you have it, our code is now receiving note information from physical MIDI devices. What you do with the incoming note information is entirely up to you, you could use it to play sounds with Web Audio or go crazy and use it to splat pixels into a canvas. You could also send the note information directly to one of the available MIDI output devices.

    Sending MIDI Messages

    A MIDIOutput object has a send function that can be used to send a MIDI message to a physical MIDI device. This process can actually get quite complex due to the expressiveness of the MIDI language, but we are going to keep things simple here and only deal with the sending of note-on and note-off information.

    We will use three functions here, one for sending note-on messages, one for sending note-off messages, and one for building and sending the MIDI messages.

    The sendNoteOn and sendNoteOff functions will contain the same code, the only difference will be the MIDI event type that they pass to the sendMessage function.

    The sendMessage function will construct the raw MIDI message and send it immediately to the MIDI device. For simplicity, all of the messages will be sent on MIDI channel 0.

    Now we can receive and send MIDI note information, and that alone presents us with a lot of possibilities for audio based web applications.

    MIDI Connections and Disconnections

    It is possible that a MIDI device could be connected to, or disconnected from, a user's computer while an application is running in the browser. If that happens we need a way to detect it so we can update the state of the application accordingly.

    Luckily, the MIDIAccess object allows us to listen for state changes, which basically means it allows us to detect when MIDI devices are connected and disconnected.

    Rolling back to the handleAccess function, we can add an event listener to the MIDIAccess object and respond to any state changes.

    The MIDIConnectionEvent object has a port property that will either be a MIDIInput or MIDIOutput object, the state of which has changed.

    Depending on the complexity of your web application, you may also want to update device lists or switch the active output device when a state change is detected.

    Live Demonstration

    The following demonstration is based on the code in this tutorial. It indicates if it has successfully gained access to a MIDIAccess object, and if it has, any note-on and note-off MIDI messages it receives from any MIDIInput object will be displayed.

    Additional Resources

    martijn broeders

    founder/ strategic creative at 4elements web design & consultancy
    e-mail: .(JavaScript must be enabled to view this email address)
    phone: 06 38 55 28 54

By - category

    By - date