Convert MIDI ShortMessage into a hex-dump string. - Java javax.sound.midi

Java examples for javax.sound.midi:MidiMessage

Description

Convert MIDI ShortMessage into a hex-dump string.

Demo Code

/*//from w ww . j  av  a2s .  c o  m
 * Copyright 2004 Hiroo Hayashi
 *
 * This file is part of JSynthLib.
 *
 * JSynthLib is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published
 * by the Free Software Foundation; either version 2 of the License,
 * or(at your option) any later version.
 *
 * JSynthLib is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with JSynthLib; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA
 */
//package com.java2s;

import javax.sound.midi.*;

public class Main {
    /**
     * Convert <code>ShortMessage</code> into a hexa-dump string.
     *
     * @param m a <code>ShortMessage</code> value
     * @return a <code>String</code> value
     * @exception InvalidMidiDataException if an error occurs
     */
    public static String shortMessageToString(ShortMessage m)
            throws InvalidMidiDataException {
        int c = m.getStatus();
        switch (c < 0xf0 ? c & 0xf0 : c) {
        case 0x80:
        case 0x90:
        case 0xa0:
        case 0xb0:
        case 0xe0:
        case 0xf2:
            return (hex(c) + " " + hex(m.getData1()) + " " + hex(m
                    .getData2()));
        case 0xc0:
        case 0xd0:
        case 0xf1:
        case 0xf3:
            return (hex(c) + " " + hex(m.getData1()));
        case 0xf4:
        case 0xf5:
        case 0xf6:
        case 0xf7:
        case 0xf8:
        case 0xf9:
        case 0xfa:
        case 0xfb:
        case 0xfc:
        case 0xfd:
        case 0xfe:
        case 0xff:
            return (hex(c));
        default:
            throw new InvalidMidiDataException();
        }
    }

    private static String hex(int c) {
        String s = Integer.toHexString(c);
        return s.length() == 1 ? "0" + s : s;
    }
}

Related Tutorials