001package net.kreatious.pianoleopard.midi.track;
002
003import static java.util.stream.Collectors.joining;
004
005import java.util.stream.IntStream;
006
007import javax.sound.midi.MidiMessage;
008import javax.sound.midi.ShortMessage;
009
010/**
011 * Verb/Provides
012 *
013 * @author Jay-R Studer
014 */
015public class MidiMessageToString {
016    /**
017     * @param message
018     *            t
019     * @return t
020     */
021    public static String convert(MidiMessage message) {
022        if (message instanceof ShortMessage == false) {
023            return IntStream.range(0, message.getLength()).map(i -> message.getMessage()[i] & 0xFF)
024                    .mapToObj(Integer::toHexString).map(s -> s.length() == 1 ? "0" + s : s).collect(joining());
025        }
026
027        final ShortMessage msg = (ShortMessage) message;
028        switch (msg.getCommand()) {
029        case ShortMessage.NOTE_OFF:
030            return "NOTE_OFF[channel: " + msg.getChannel() + ", key:" + msg.getData1() + ", velocity: "
031                    + msg.getData2() + "]";
032        case ShortMessage.NOTE_ON:
033            return "NOTE_ON[channel: " + msg.getChannel() + ", key:" + msg.getData1() + ", velocity: " + msg.getData2()
034                    + "]";
035        case ShortMessage.CONTROL_CHANGE:
036            return "CONTROL_CHANGE[channel: " + msg.getChannel() + ", control:" + msg.getData1() + ", value: "
037                    + msg.getData2() + "]";
038        case ShortMessage.PITCH_BEND:
039            return "PITCH_BEND[channel: " + msg.getChannel() + ", key:" + msg.getData1() + ", value: " + msg.getData2()
040                    + "]";
041        case ShortMessage.POLY_PRESSURE:
042            return "POLY_PRESSURE[channel: " + msg.getChannel() + ", key:" + msg.getData1() + ", value: "
043                    + msg.getData2() + "]";
044        case ShortMessage.PROGRAM_CHANGE:
045            return "PROGRAM_CHANGE[channel: " + msg.getChannel() + ", progryam:" + msg.getData1() + "]";
046        case ShortMessage.CHANNEL_PRESSURE:
047            return "CHANNEL_PRESSURE[channel: " + msg.getChannel() + ", key:" + msg.getData1() + ", value: "
048                    + msg.getData2() + "]";
049        case ShortMessage.SYSTEM_RESET:
050            return "SYSTEM_RESET";
051        case ShortMessage.START:
052            return "START";
053        case ShortMessage.STOP:
054            return "STOP";
055        case ShortMessage.CONTINUE:
056            return "CONTINUE";
057        default:
058            return "Unknown["
059                    + IntStream.range(0, message.getLength()).map(i -> message.getMessage()[i] & 0xFF)
060                            .mapToObj(Integer::toHexString).map(s -> s.length() == 1 ? "0" + s : s).collect(joining())
061                    + "]";
062        }
063    }
064}