8/16/2019 CIS4390 - Presentation - Java Sound API MIDI - Jon Cobb
1/16
An Introduction to MIDI Programming with
The Java Sound API : Overview
Provides low level services for digital audio andmidi processing.
Is essentially an abstraction layer over the lowerlevel native services offered by the operatingsystem.Is hardware agnostic and relies on the operatingsystem to provide access to hardwareresources.The Java Sound packages were designed toprovide application programmers with aconsistent interface to resources and servicesregardless of the audio hardware configuration.
The Java Sound API : Packages
Two main packages – targeted byapplication programmers
javax.sound.sampled – provides services forcapturing, mixing, and playback of digital audiostreams.
javax.sound.midi – provides services to supportMIDI synthesis, sequencing, and eventcapture/playback.
8/16/2019 CIS4390 - Presentation - Java Sound API MIDI - Jon Cobb
2/16
8/16/2019 CIS4390 - Presentation - Java Sound API MIDI - Jon Cobb
3/16
Devices, Transmitters, andReceivers
Transmitters generate MIDI messages andstream them to connected receivers.
MIDI Streaming vs. Sequencing Streaming – Real Time
Processing
MessagesMessage TypeChannel Number
Associated Data
Sequencing – Relative
Time Processing
EventsMessage TypeChannel Number
Associated DataRelative Time
MIDI Streaming
Can be thought of as “Live” MIDIMIDI Messages contain no timing informationand are processed immediately as they arrive.Examples:
A MIDI controller generates MIDI messages which arestreamed to capture devices (like sequencers) duringrecording or sound generators (like synthesizers)during performance.
A MIDI sequencer generates MIDI messages whichare streamed to devices which generate sound (likesynthesizers) during playback.
MIDI Sequencing
Can be thought of as “Scheduled” MIDI.MIDI Events contain MIDI Messages andassociated timing values.Examples:
Playback - A MIDI file stores a series of MIDI Eventswhich a sequencer can use to generate MIDImessages to send to sound generating devices at theappropriate time.Capture - A MIDI sequencer captures MIDI messagesfrom a MIDI controller and adds a time stamp to themessage data for storage and subsequent playback.
8/16/2019 CIS4390 - Presentation - Java Sound API MIDI - Jon Cobb
4/16
MIDI Data Flow Sequences and Tracks
Sequence – stored in a MIDI File ormanaged by a MIDI Sequencer.
A Sequence can be thought of as a MIDIschedule.
Track – represents a MIDI Event schedulefor a single instrument/performer/voice.
Sequences and Tracks
A sequence can be thought of as an entire“song,” whereas a track can be thought of asa single part of the song like “the guitar part”or the “drum part.”
A sequence can, but does not necessarilyhave to, contain multiple tracks.
Sequences and Tracks
A Sequence is a collection of Tracks
A Track is a collection of MIDI Events
A MIDI Event is a MIDI Message combinedwith timing information.
8/16/2019 CIS4390 - Presentation - Java Sound API MIDI - Jon Cobb
5/16
MidiDevice Interface
Interface implemented by softwarerepresentations of MIDI devices.
Can represent both software devices andhardware devices.
Implementations provide all functionalityrequired by a MIDI port (input or output).
MidiDevice Interface
open(), close(), isOpen()
getDeviceInfo()
getReceiver(), getReceivers(),getMaxReceivers()
getTransmitter(), getTransmitters(),getMaxTransmitters()
getMicrosecondPosition()
MidiDevice.Info Class
A data access class whose soleresponsibility is to provide informationdescribing a MidiDevice instance.
Clients of a MidiDevice may obtain aninstance of this class to query for informationabout the MidiDevice by calling theMidiDevice’s getDeviceInfo() method.
8/16/2019 CIS4390 - Presentation - Java Sound API MIDI - Jon Cobb
6/16
MidiDevice.Info Class
getName()
getVendor()
getVersion()
getDescription()
Transmitter Interface
Interface implemented by software objectsthat transmit MIDI messages on behalf of aMidiDevice implementation.
Examples: Sequencers and MIDI Input Portsprovide Transmitters to clients.
Transmitter Interface
Instances of a MidiDevice’s Transmitterimplementation can be obtained by callingthe MidiDevice’s getTransmitter() method.
Be sure to call the Transmitter instance’sclose() method when the Transmitter is nolonger needed.
Transmitter Interface
setReceiver(), getReceiver()
close()
8/16/2019 CIS4390 - Presentation - Java Sound API MIDI - Jon Cobb
7/16
Receiver Interface
Interface implemented by software objectsthat receive MIDI messages on behalf of aMidiDevice implementation.
Examples: Synthesizers and MIDI OutputPorts provide Receivers to clients.
Receiver Interface
Instances of a MidiDevice’s Receiverimplementation can be obtained by callingthe MidiDevice’s getReceiver() method.
Be sure to call the Receiver instance’sclose() method when the Receiver is nolonger needed.
Receiver Interface
send()
close()
MidiMessage Class
Base class for all MIDI Messageimplementations.
Provides access to the message’s statusbyte, data bytes, and total message length.
Subclasses include ShortMessage,
MetaMessage, and SysExMessage classes.
8/16/2019 CIS4390 - Presentation - Java Sound API MIDI - Jon Cobb
8/16
MidiMessage Class
getStatus()
getLength()
getMessage()
ShortMessage Class
Implements data access and transport forshort messages (2 data bytes) which areusually associated with MIDI channels.
Examples: Note On and Note Off messages
ShortMessage Class
getCommand()
getChannel()
getData1(), getData2()
getDataLength()
setMessage()
ShortMessage Class
Status code byte values are provided bystatic fields on the ShortMessage class.
Examples:ShortMessage.NOTE_ONShortMessage.NOTE_OFFShortMessage.PITCH_BEND
8/16/2019 CIS4390 - Presentation - Java Sound API MIDI - Jon Cobb
9/16
SysexMessage Class
Implements data access and transport forMIDI system exclusive messages.
MIDI system exclusive messages are largelyvendor specific and are variable in size.
SysexMessage Class
getData()
setMessage()
MetaMessage Class
Implements data access and transport forMIDI meta messages.
MetaMessages are used in MIDI files andSequences and are not used during livecommunication.
Contain data useful for sequencers liketempo changes, time signatures, etc.
MetaMessage Class
getType()
getData()
setMessage()
8/16/2019 CIS4390 - Presentation - Java Sound API MIDI - Jon Cobb
10/16
Sequence Interface
Extends the MidiDevice Interface
Implemented by classes which representMIDI Sequencers.
Sequencer Interface
getSequence(), setSequence()
getTickLength()
getTickPosition(), setTickPosition()
start(), stop()
Sequencer Interface
startRecording(), stopRecording()
isRunning(), isRecording()
getTrackSolo(), setTrackSolo()
getTrackMute(), setTrackMute()
Sequencer Interface
addControllerEventListener()
addMetaEventListener()
8/16/2019 CIS4390 - Presentation - Java Sound API MIDI - Jon Cobb
11/16
8/16/2019 CIS4390 - Presentation - Java Sound API MIDI - Jon Cobb
12/16
MidiEvent Class
Implements data access and transport for aMIDI Event.
Is, quite simply, a container for aMidiMessage object and an associated tickvalue.
MidiEvent Class
getTick(), setTick()
getMessage()
Event Listener Interfaces
ControllerEventListener InterfacecontrolChange()
MetaEventListener Interfacemeta()
Synthesizer Interface
Extends the MidiDevice Interface
Implemented by software objects thatrepresent MIDI Synthesizers.
Implementations of this interface are theonly objects in the Midi package that
produce audio.
8/16/2019 CIS4390 - Presentation - Java Sound API MIDI - Jon Cobb
13/16
Synthesizer Interface
getChannels()
getAvailableInstruments()
getMaxPolyphony()
getLatency()
getVoiceStatus()
Instrument Class
Implements the sound synthesis algorithmsfor a specific instrument provided by aSoundbank.
To use an instance of an Instrument objectwith a Synthesizer, that Instrument and itsSoundbank must be loaded into theSynthesizer.
Instrument Class
getName()
getPatch()
Patch Class
Implementation of an Instrument’s soundpatch.
Represents the “location” at which aninstrument’s data is loaded into asynthesizer.
Provides the bank and program numbers to
a MidiChannel so the channel can use theinstrument.
8/16/2019 CIS4390 - Presentation - Java Sound API MIDI - Jon Cobb
14/16
8/16/2019 CIS4390 - Presentation - Java Sound API MIDI - Jon Cobb
15/16
MidiSystem Class
Provides access to the system’s MIDI
resources (installed hardware and softwarebased MIDI devices, soundbanks, etc.)
Like most subsystems in the Java Libraries,the MIDI subsystem is implemented as aSingleton by exposing only static methods.
MidiSystem Class
getMidiDeviceInfo()
getMidiDevice()
getReceiver(), getTransmitter(),getSynthesizer(), getSequencer()
getSequence()
MidiSystem Class
getMidiFileTypes()
write()
8/16/2019 CIS4390 - Presentation - Java Sound API MIDI - Jon Cobb
16/16
References
JavaSound API Programmer’s Guide
http://download.oracle.com/javase/1.5.0/docs/guide/sound/programmer_guide/index.html
Java SE 6 APIhttp://download.oracle.com/javase/6/docs/api/javax/sound/midi/package-summary.html
Java Sound API – Soundbankshttp://www.oracle.com/technetwork/java/soundbanks-135798.html