001package net.kreatious.pianoleopard.midi.event;
002
003import java.util.Optional;
004
005/**
006 * Represents the various foot pedal
007 *
008 * @author Jay-R Studer
009 */
010public enum Pedal {
011    /**
012     * Sustain pedal.
013     * <p>
014     * While this pedal is pressed, played notes decay slowly. This sounds as if
015     * note off messages are ignored.
016     */
017    SUSTAIN(64),
018
019    /**
020     * Sostenuto pedal.
021     * <p>
022     * When this pedal is pressed, only the notes currently being played are
023     * sustained. Commonly abbreviated as S.P.
024     */
025    SOSTENUTO(65),
026
027    /**
028     * Portamento pedal.
029     * <p>
030     * While this pedal is pressed, notes glide continuously in pitch from one
031     * to the next.
032     */
033    PORTAMENTO(66),
034
035    /**
036     * Soft pedal.
037     * <p>
038     * While this pedal is pressed, notes are much quieter.
039     */
040    SOFT(67);
041
042    private final int data;
043
044    private Pedal(int data) {
045        this.data = data;
046    }
047
048    /**
049     * Returns the raw MIDI data value associated with a control change message.
050     * 
051     * @return the data1 value for a control change message using this pedal
052     */
053    public int getData() {
054        return data;
055    }
056
057    /**
058     * Determines the pedal enum associated with a MIDI data value.
059     *
060     * @param data
061     *            the MIDI data1 value associated with a control change message
062     * @return an optional containing the pedal associated with the data, or
063     *         empty
064     */
065    public static Optional<Pedal> lookup(int data) {
066        switch (data) {
067        case 64:
068            return Optional.of(SUSTAIN);
069        case 65:
070            return Optional.of(PORTAMENTO);
071        case 66:
072            return Optional.of(SOSTENUTO);
073        case 67:
074            return Optional.of(SOFT);
075        default:
076            return Optional.empty();
077        }
078    }
079}