001package net.kreatious.pianoleopard.midi.event;
002
003import javax.sound.midi.ShortMessage;
004
005/**
006 * Represents an event that is associated with a pedal.
007 *
008 * @author Jay-R Studer
009 */
010public class PedalEvent extends Event {
011    private final Pedal pedal;
012    private final boolean on;
013    private final Slot slot;
014
015    PedalEvent(ShortMessage message, long time) {
016        super(message, time);
017
018        pedal = Pedal.lookup(message.getData1()).orElseThrow(
019                () -> new IllegalArgumentException(message.getData1() + " is not a pedal message"));
020
021        on = message.getData2() >= 64;
022        slot = new Slot(message.getChannel(), pedal);
023    }
024
025    private PedalEvent(int channel, long time, Pedal pedal, boolean on, Slot slot) {
026        super(channel, time);
027        this.pedal = pedal;
028        this.on = on;
029        this.slot = slot;
030    }
031
032    static boolean canCreate(ShortMessage message) {
033        return message.getCommand() == ShortMessage.CONTROL_CHANGE && message.getData1() >= 64
034                && message.getData1() <= 67;
035    }
036
037    /**
038     * @return the pedal associated with this event
039     */
040    public Pedal getPedal() {
041        return pedal;
042    }
043
044    @Override
045    public boolean isOn() {
046        return on;
047    }
048
049    @Override
050    public Slot getSlot() {
051        return slot;
052    }
053
054    @Override
055    @SuppressWarnings("unchecked")
056    public PedalEvent createOff(long offTime) {
057        return new PedalEvent(getChannel(), offTime, pedal, false, slot);
058    }
059}