com.groovemanager.midi
Class MIDIManager

java.lang.Object
  extended by com.groovemanager.midi.MIDIManager
All Implemented Interfaces:
java.util.EventListener, org.eclipse.jface.util.IPropertyChangeListener

public class MIDIManager
extends java.lang.Object
implements org.eclipse.jface.util.IPropertyChangeListener

A MIDIManager can be used as a central instance for controlling MIDI data flow. A MIDIManager is connected to a ConfigManager instance from which it retreives the in- and output device to use.

Author:
Manu

Field Summary
private  ConfigManager configManager
          The ConfigManager used to retreive selected in- and output devices
static MIDIManager DEFAULT_MIDI_MANAGER
          The default MIDIManager implementaiton
protected  long eventid
          increasing number used for creating event ids that can be assigned to note on events so that a note off event can be assigned to a previously sent note on event
private  java.util.HashMap events
          set of events that have been created with an event id and have not yet been finished.
protected  javax.sound.midi.MidiDevice inDevice
          The current output device
protected  MIDIInReceiver inReceiver
          The MIDI In Receiver used by this MIDIManager
protected  javax.sound.midi.MidiDevice outDevice
          The current output device
 
Constructor Summary
MIDIManager()
          Construct a new MIDIManager using the default ConfigManager
MIDIManager(ConfigManager configManager)
          Construct a new MIDIManager instance using the specified ConfigManager
 
Method Summary
 void addMIDIListener(MIDIListener listener, int[] acceptedStatus)
          Add a MIDIListener to this MIDIManager.
protected  MIDIInReceiver createInReceiver()
          Create and return the MIDIInReceiver.
(package private)  void disableInListener()
          This method is called from a MIDIInReceiver to indicate that he can be disconnected from the input device
 void dispose()
          Mark this MIDIManager as out of use
(package private)  void enableInListener()
          This method is called from a MIDIInReceiver to indicate that he needs to be connected to the input device
protected  void finalize()
           
static MIDIManager getDefault()
          Get the default MIDIManager implemetation
protected  javax.sound.midi.MidiDevice getInDevice()
          Get the current input device
protected  long getNewEventId()
          Get a new unique even id
 javax.sound.midi.MidiDevice getOutDevice()
          Get the current output device
 void initMidi()
          Initialize input and output device
private  void initMidiIn()
          Initialize the input device
private  void initMidiOut()
          Initialize the output device
 void propertyChange(org.eclipse.jface.util.PropertyChangeEvent event)
           
 void removeMIDIListener(MIDIListener listener)
          Remove a MIDIListener from this MIDIManager
 void sendController(int channel, byte controllerNr, byte value)
          Send a single controller event
protected  void sendMidiMessage(javax.sound.midi.MidiMessage message, long evid, boolean start)
          Send a MidiMessage to the output device
 void sendProgramChange(int channel, byte program)
          Send a single program change event
 void sendSingleNoteOff(long eventid, int channel, int key)
          Send a single note off event
 long sendSingleNoteOn(int channel, int velocity, int key)
          Send a single note on event
 void sendSysEx(int[] data)
          Send a single SysEx message
protected  void startEvent(long evid)
          Internal method for starting an event that expects another event in the future for completition (like note on and note off). the event id will be remembered until the expected event is sent.
protected  void stopEvent(long evid)
          Internal method indicating that the corresponding event to the one that created the given event id has been sent.
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

configManager

private final ConfigManager configManager
The ConfigManager used to retreive selected in- and output devices


DEFAULT_MIDI_MANAGER

public static final MIDIManager DEFAULT_MIDI_MANAGER
The default MIDIManager implementaiton


eventid

protected long eventid
increasing number used for creating event ids that can be assigned to note on events so that a note off event can be assigned to a previously sent note on event


events

private java.util.HashMap events
set of events that have been created with an event id and have not yet been finished.


inDevice

protected javax.sound.midi.MidiDevice inDevice
The current output device


inReceiver

protected final MIDIInReceiver inReceiver
The MIDI In Receiver used by this MIDIManager


outDevice

protected javax.sound.midi.MidiDevice outDevice
The current output device

Constructor Detail

MIDIManager

public MIDIManager()
Construct a new MIDIManager using the default ConfigManager


MIDIManager

public MIDIManager(ConfigManager configManager)
Construct a new MIDIManager instance using the specified ConfigManager

Parameters:
configManager - The ConfigManager to use
Method Detail

addMIDIListener

public void addMIDIListener(MIDIListener listener,
                            int[] acceptedStatus)
Add a MIDIListener to this MIDIManager. The listener will be notified of all incoming MIDIEvents on the MIDIManagerīs input device that match the geiven accepted message types

Parameters:
listener - The MIDIListener to add
acceptedStatus - Array of status bytes this listener accepts. Can be any of MetaMessage.META, SysexMessage.SYSTEM_EXCLUSIVE, SysexMessage.SPECIAL_SYSTEM_EXCLUSIVE or ShortMessage.*. The array may also be null to indicate that all types of messages are accepted.

createInReceiver

protected MIDIInReceiver createInReceiver()
Create and return the MIDIInReceiver. The default implementation creates a MIDIInReceiver that listens to all types of events. Subclasses may override this method.

Returns:
The new created MIDIInReceiver

disableInListener

void disableInListener()
This method is called from a MIDIInReceiver to indicate that he can be disconnected from the input device


dispose

public void dispose()
Mark this MIDIManager as out of use


enableInListener

void enableInListener()
This method is called from a MIDIInReceiver to indicate that he needs to be connected to the input device


finalize

protected void finalize()
                 throws java.lang.Throwable
Overrides:
finalize in class java.lang.Object
Throws:
java.lang.Throwable
See Also:
Object.finalize()

getDefault

public static MIDIManager getDefault()
Get the default MIDIManager implemetation

Returns:
The default MIDIManager implementation

getInDevice

protected javax.sound.midi.MidiDevice getInDevice()
                                           throws javax.sound.midi.MidiUnavailableException
Get the current input device

Returns:
The current input device
Throws:
javax.sound.midi.MidiUnavailableException - If the device could not be found or if no device is selected

getNewEventId

protected long getNewEventId()
Get a new unique even id

Returns:
A new unique event id

getOutDevice

public javax.sound.midi.MidiDevice getOutDevice()
                                         throws javax.sound.midi.MidiUnavailableException
Get the current output device

Returns:
The current output device
Throws:
javax.sound.midi.MidiUnavailableException - If the device could not be found or if no device is selected

initMidi

public void initMidi()
Initialize input and output device


initMidiIn

private void initMidiIn()
Initialize the input device


initMidiOut

private void initMidiOut()
Initialize the output device


propertyChange

public void propertyChange(org.eclipse.jface.util.PropertyChangeEvent event)
Specified by:
propertyChange in interface org.eclipse.jface.util.IPropertyChangeListener
See Also:
IPropertyChangeListener.propertyChange(org.eclipse.jface.util.PropertyChangeEvent)

removeMIDIListener

public void removeMIDIListener(MIDIListener listener)
Remove a MIDIListener from this MIDIManager

Parameters:
listener - The MIDIListener to remove

sendController

public void sendController(int channel,
                           byte controllerNr,
                           byte value)
                    throws javax.sound.midi.InvalidMidiDataException,
                           javax.sound.midi.MidiUnavailableException
Send a single controller event

Parameters:
channel - The channel on which the controller should be sent
controllerNr - The controller number from 0 to 127
value - The controller value from 0 to 127
Throws:
javax.sound.midi.InvalidMidiDataException - Should not happen
javax.sound.midi.MidiUnavailableException - If the message could not be sent

sendMidiMessage

protected void sendMidiMessage(javax.sound.midi.MidiMessage message,
                               long evid,
                               boolean start)
                        throws javax.sound.midi.MidiUnavailableException
Send a MidiMessage to the output device

Parameters:
message - The MidiMessage to send
evid - The event id of the message. If this message expects another message in the future for completing, this event id must be other than -1. Or if this message is the expected follow-up message to a previous one it the event id should contain the event id with which the previous message was sent. Otherwise -1 should be used as event id.
start - Inidcates whether this event is a start event that expects a follow-up message (true) or if it is the follow-up message to a previous message (false). This value is only recognized if eventid != -1.
Throws:
javax.sound.midi.MidiUnavailableException - If the message could not be sent

sendProgramChange

public void sendProgramChange(int channel,
                              byte program)
                       throws javax.sound.midi.InvalidMidiDataException,
                              javax.sound.midi.MidiUnavailableException
Send a single program change event

Parameters:
channel - The channel on which the event should be sent (from 0 to 15)
program - The program number to change to (from 0 to 127)
Throws:
javax.sound.midi.InvalidMidiDataException - Should not happen
javax.sound.midi.MidiUnavailableException - If the message could not be sent

sendSingleNoteOff

public void sendSingleNoteOff(long eventid,
                              int channel,
                              int key)
                       throws javax.sound.midi.InvalidMidiDataException,
                              javax.sound.midi.MidiUnavailableException
Send a single note off event

Parameters:
eventid - The event id of the note on event to which this note event belongs. If this event is independent of any note on event, -1 should be used.
channel - The channel on which the event should be sent (between 0 and 15)
key - The note value of the note on event (between 0 and 127)
Throws:
javax.sound.midi.InvalidMidiDataException - Should not happen
javax.sound.midi.MidiUnavailableException - If the message could not be sent

sendSingleNoteOn

public long sendSingleNoteOn(int channel,
                             int velocity,
                             int key)
                      throws javax.sound.midi.InvalidMidiDataException,
                             javax.sound.midi.MidiUnavailableException
Send a single note on event

Parameters:
channel - The channel on which the event should be sent (between 0 and 15)
velocity - The velocity of the note on event (between 0 and 127)
key - The note value of the note on event (between 0 and 127)
Returns:
A unique event id that should be used, when a corresponding note off event is sent
Throws:
javax.sound.midi.InvalidMidiDataException - Should not happen
javax.sound.midi.MidiUnavailableException - If the message could not be sent

sendSysEx

public void sendSysEx(int[] data)
               throws javax.sound.midi.InvalidMidiDataException,
                      javax.sound.midi.MidiUnavailableException
Send a single SysEx message

Parameters:
data - The whole message data of the sysex message
Throws:
javax.sound.midi.InvalidMidiDataException - If the provided data is not a valid MIDI data
javax.sound.midi.MidiUnavailableException - If the message could not be sent

startEvent

protected void startEvent(long evid)
Internal method for starting an event that expects another event in the future for completition (like note on and note off). the event id will be remembered until the expected event is sent.

Parameters:
evid - The event id

stopEvent

protected void stopEvent(long evid)
Internal method indicating that the corresponding event to the one that created the given event id has been sent. If after this call no more events are pending to be completed by another event, the output device will be closed.

Parameters:
evid -