Subversion Repositories svn.mios

Rev

Rev 213 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
213 tk 1
/*
2
 * @(#)MidiUtils.java   beta7   2006/04/23
3
 *
4
 * Copyright (C) 2006    Adam King (adamjking@optusnet.com.au)
5
 *
6
 * This application is free software; you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation; either version 2 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This application is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this application; if not, write to the Free Software
18
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
 */
20
 
21
package org.midibox.midi;
22
 
23
import javax.sound.midi.ShortMessage;
24
 
25
public class MidiUtils {
26
 
27
    public static final char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6',
28
            '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
29
 
30
    public static int get14bitValue(int nLowerPart, int nHigherPart) {
31
        return (nLowerPart & 0x7F) | ((nHigherPart & 0x7F) << 7);
32
    }
33
 
34
    public static int signedByteToUnsigned(byte b) {
35
        return b & 0xFF;
36
    }
37
 
38
    public static String getHexString(byte[] aByte) {
39
        StringBuffer sbuf = new StringBuffer(aByte.length * 3 + 2);
40
        for (int i = 0; i < aByte.length; i++) {
41
            if (i > 0) {
42
                sbuf.append(' ');
43
            }
44
            sbuf.append(getHexString((int) aByte[i]));
45
        }
46
        return new String(sbuf);
47
    }
48
 
49
    public static String getHexString(int num) {
50
        StringBuffer sbuf = new StringBuffer();
51
        sbuf.append(hexDigits[(num & 0xF0) >> 4]);
52
        sbuf.append(hexDigits[num & 0x0F]);
53
 
54
        return new String(sbuf);
55
    }
56
 
57
    public static String getDecimalString(byte[] aByte) {
58
        StringBuffer sbuf = new StringBuffer(aByte.length * 4 + 2);
59
        for (int i = 0; i < aByte.length; i++) {
60
            sbuf.append(' ');
61
            sbuf.append(getDecimalString((int) aByte[i]));
62
        }
63
        return new String(sbuf);
64
    }
65
 
66
    public static String getDecimalString(int num) {
67
        StringBuffer sbuf = new StringBuffer();
68
 
69
        sbuf.append("000");
70
        String value = "" + (num & 0xFF);
71
        sbuf.replace(sbuf.length() - value.length(), sbuf.length(), value);
72
 
73
        return new String(sbuf);
74
    }
75
 
76
    public static String intToHex(int i) {
77
        return "" + hexDigits[(i & 0xF0) >> 4] + hexDigits[i & 0x0F];
78
    }
79
 
80
    public static String getHexString(ShortMessage sm) {
81
        // bug in J2SDK 1.4.1
82
        // return getHexString(sm.getMessage());
83
        int status = sm.getStatus();
84
        String res = intToHex(sm.getStatus());
85
        // if one-byte message, return
86
        switch (status) {
87
        case 0xF6: // Tune Request
88
        case 0xF7: // EOX
89
        // System real-time messages
90
        case 0xF8: // Timing Clock
91
        case 0xF9: // Undefined
92
        case 0xFA: // Start
93
        case 0xFB: // Continue
94
        case 0xFC: // Stop
95
        case 0xFD: // Undefined
96
        case 0xFE: // Active Sensing
97
        case 0xFF:
98
            return res;
99
        }
100
        res += ' ' + intToHex(sm.getData1());
101
        // if 2-byte message, return
102
        switch (status) {
103
        case 0xF1: // MTC Quarter Frame
104
        case 0xF3: // Song Select
105
            return res;
106
        }
107
        // switch (sm.getCommand()) {
108
        // case 0xC0:
109
        // case 0xD0:
110
        // return res;
111
        // }
112
        // 3-byte messages left
113
        res += ' ' + intToHex(sm.getData2());
114
        return res;
115
    }
116
}