Class MidiProcessor
- java.lang.Object
-
- com.github.noahr_atc.midisplitter.MidiProcessor
-
- All Implemented Interfaces:
AutoCloseable
,Receiver
public class MidiProcessor extends Object implements Receiver
AReceiver
that splits MIDI chords into notes distributed across all MIDI channels before forwarding the newMidiMessage
s to another Receiver.- Since:
- 2020-03-08
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static class
MidiProcessor.ChannelStatus
Represents whether or not a MIDI channel is available, as well as how many notes need to be turned off before the will be available.static class
MidiProcessor.ExceededMidiChannelsException
Indicates that all 16 MIDI channels are in use and the current note cannot be distributed to a unique channel.protected static class
MidiProcessor.NoteMapping
Represents an entry in aMidiProcessor
's note translation map.
-
Constructor Summary
Constructors Constructor Description MidiProcessor(MidiDevice midiReceiver)
Constructs aMidiProcessor
using aMidiDevice
without debugging output.MidiProcessor(MidiDevice midiReceiver, boolean debugMode)
Constructs aMidiProcessor
using aMidiDevice
with the option to run in debugging mode.MidiProcessor(Receiver receiver)
Constructs aMidiProcessor
using aReceiver
without debugging output.MidiProcessor(Receiver receiver, boolean debugMode)
Constructs aMidiProcessor
using aReceiver
with the option to run in debugging mode.
-
Method Summary
Modifier and Type Method Description boolean
channelAvailable(int channel)
Reports whether or not a MIDI channel is currently in use.void
close()
Closes thisMidiProcessor
and releases its resources, specifically the MIDI device assigned with setReceiver or the constructor.int
firstAvailableChannel()
Finds the first MIDI channel from 0 that a message hasn't currently been translated to.boolean
isRunning()
Checks whether or notclose
has been called on thisMidiProcessor
.int
leastUsedChannel()
Finds the MIDI channel with the least amount of uses, returning the lower channel if multiple have an equal number of uses.void
send(MidiMessage message, long timeStamp)
Sends a MIDI message to this receiver, along with an optional timestamp.void
setReceiver(MidiDevice midiReceiver)
Opens a MIDI device and sets it as theReceiver
that processed messages are sent to.void
setReceiver(Receiver receiver)
Sets theReceiver
that processed messages are sent to.ShortMessage
translateMessage(ShortMessage message)
Translates the provided message onto the correct MIDI channel.
-
-
-
Constructor Detail
-
MidiProcessor
public MidiProcessor(MidiDevice midiReceiver, boolean debugMode) throws MidiUnavailableException
Constructs aMidiProcessor
using aMidiDevice
with the option to run in debugging mode. All MIDI channels are set to available.- Parameters:
midiReceiver
- the MIDI device to send processed messages todebugMode
- specifies whether to enable debugging messages- Throws:
MidiUnavailableException
- if the providedMidiDevice
won't supply a receiver
-
MidiProcessor
public MidiProcessor(MidiDevice midiReceiver) throws MidiUnavailableException
Constructs aMidiProcessor
using aMidiDevice
without debugging output. All MIDI channels are set to available.- Parameters:
midiReceiver
- the MIDI device to send processed messages to- Throws:
MidiUnavailableException
- if the providedMidiDevice
won't supply a receiver
-
MidiProcessor
public MidiProcessor(Receiver receiver, boolean debugMode)
Constructs aMidiProcessor
using aReceiver
with the option to run in debugging mode. All MIDI channels are set to available.- Parameters:
receiver
- the MIDI receiver to send processed messages todebugMode
- specifies whether to enable debugging messages
-
-
Method Detail
-
send
public void send(MidiMessage message, long timeStamp)
Sends a MIDI message to this receiver, along with an optional timestamp. Set timestamp to -1 if not used.
-
close
public void close()
Closes thisMidiProcessor
and releases its resources, specifically the MIDI device assigned with setReceiver or the constructor.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceReceiver
-
isRunning
public boolean isRunning()
Checks whether or notclose
has been called on thisMidiProcessor
.- Returns:
false
ifclose
has been called;true
otherwise
-
setReceiver
public void setReceiver(MidiDevice midiReceiver) throws MidiUnavailableException, NullPointerException
Opens a MIDI device and sets it as theReceiver
that processed messages are sent to.- Parameters:
midiReceiver
- the desired MIDI device to receive messages- Throws:
MidiUnavailableException
- if the providedMidiDevice
won't supply a receiverNullPointerException
- if the providedMidiDevice
is null- See Also:
setReceiver(Receiver)
-
setReceiver
public void setReceiver(Receiver receiver)
Sets theReceiver
that processed messages are sent to.- Parameters:
receiver
- the desired MIDI receiver- See Also:
setReceiver(MidiDevice)
-
channelAvailable
public boolean channelAvailable(int channel)
Reports whether or not a MIDI channel is currently in use.- Parameters:
channel
- the channel to check for availability- Returns:
true
ifchannel
is available;false
ifchannel
is out of range or in use
-
firstAvailableChannel
public int firstAvailableChannel() throws MidiProcessor.ExceededMidiChannelsException
Finds the first MIDI channel from 0 that a message hasn't currently been translated to.- Returns:
- the MIDI channel number of the available channel
- Throws:
MidiProcessor.ExceededMidiChannelsException
- if all MIDI channels are in use
-
leastUsedChannel
public int leastUsedChannel()
Finds the MIDI channel with the least amount of uses, returning the lower channel if multiple have an equal number of uses.- Returns:
- the MIDI channel number of the least used channel
-
translateMessage
public ShortMessage translateMessage(ShortMessage message)
Translates the provided message onto the correct MIDI channel. If it is available, the original channel ofmessage
is chosen, otherwise the next available channel from 0 is chosen.- Parameters:
message
- the message to be translated- Returns:
- the translated version of
message
-
-