public class OutputModel extends Object implements AutoCloseable
Modifier and Type | Class and Description |
---|---|
static class |
OutputModel.EventAction
Indicates which action to take in response to an event handler.
|
Constructor and Description |
---|
OutputModel(net.kreatious.pianoleopard.midi.SequencerFactory sequencerFactory)
Constructs a new
OutputModel with the specified initial state. |
Modifier and Type | Method and Description |
---|---|
void |
addCloseable(Closeable closeable)
Adds a closeable to close when this output model is closed.
|
void |
addCurrentTimeListener(LongConsumer listener)
Adds a listener to notify when the current playback time in microseconds
has changed.
|
void |
addEventHandler(BiFunction<MidiMessage,Optional<Event>,OutputModel.EventAction> handler)
Adds an event handler to handle MIDI events.
|
void |
addOpenListener(Consumer<? super ParsedSequence> listener)
Adds a listener to notify when a parsed MIDI file is opened.
|
void |
addOutputDeviceListener(Consumer<? super MidiDevice.Info> listener)
Adds a listener to notify when the output device has changed.
|
void |
addPlayListener(Runnable listener)
Adds a listener to notify when a parsed MIDI file is played from the
beginning.
|
void |
close() |
void |
openMidiFile(File midi)
Parses a MIDI file and prepares it for playback.
|
void |
sendMessage(MidiMessage message)
Sends a MIDI message to the output.
|
void |
setCurrentTime(long time)
Seeks the sequence to the specified time
|
void |
setOutputDevice(MidiDevice output)
Reconnects the sequencer to a different MIDI output device.
|
void |
setTempoFactor(float factor)
Adjusts the tempo of played back sequences.
|
void |
start()
Starts playback of the currently loaded MIDI file.
|
public OutputModel(net.kreatious.pianoleopard.midi.SequencerFactory sequencerFactory) throws MidiUnavailableException
OutputModel
with the specified initial state.
The output model is initially unconnected to any MIDI devices. After construction, it is expected that an output device will be set by the consumer.
sequencerFactory
- A factory for producing the Sequencer
, such as
SystemSequencerFactory
.MidiUnavailableException
- if the MIDI system is unavailable.public void setOutputDevice(MidiDevice output) throws MidiUnavailableException
output
- the new output MIDI device to reconnect toMidiUnavailableException
- if the MIDI system is unavailable.public void start()
Any registered start listeners will be called with the last opened MIDI sequence.
public void setTempoFactor(float factor)
Values higher than 1.0 are faster than normal, less than 1.0 are slower than normal. A value of 1.0 indicates that the regular tempo should be applied. Tempo factors do not affect the microsecond values of MIDI events.
factor
- the tempo factor to setpublic void setCurrentTime(long time)
time
- the time in microseconds to seek topublic void openMidiFile(File midi) throws IOException
Any registered start listeners will be called with the parsed sequence. The file of the parsed sequence will be the specified MIDI file.
midi
- the MIDI file to openIOException
- if an I/O error occurspublic void addOutputDeviceListener(Consumer<? super MidiDevice.Info> listener)
listener
- the listener to addpublic void addOpenListener(Consumer<? super ParsedSequence> listener)
listener
- the listener to addpublic void addPlayListener(Runnable listener)
listener
- the listener to addpublic void addEventHandler(BiFunction<MidiMessage,Optional<Event>,OutputModel.EventAction> handler)
The return value of the handler determines the action to take. A list of
actions is provided on OutputModel.EventAction
. The default action is to play
the event.
Event handlers are allowed to mutate the channel of the MidiMessage
object before returning. The Event
object contains the original
message before any mutations are applied. Handlers are encouraged to use
the Event object whenever possible.
handler
- the event handler to add.public void addCurrentTimeListener(LongConsumer listener)
This listener is called asynchronously several times per second for the lifetime of this object from a different thread than the one which invokes this method.
listener
- the listener to addpublic void addCloseable(Closeable closeable)
Used for releasing resources closely tied with the lifetime of this output model. Resources will be released in the same order they are registered.
closeable
- the closeable to addpublic void sendMessage(MidiMessage message)
message
- the MIDI message to send to the connected output devicepublic void close() throws InterruptedException, IOException
close
in interface AutoCloseable
InterruptedException
IOException
Copyright © 2015 Kreatious LLC. All rights reserved.