Subversion Repositories svn.mios

Rev

Rev 290 | Go to most recent revision | Details | 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 String[] ccNames = { "Bank Select", "Modulation Wheel",
28
            "Breath Controller", "Undefined", "Foot Controller",
29
            "Portamento Time", "Data Entry", "Volume", "Balance",
30
            "Undefined", "Pan", "Expression Controller", "Effect Control 1",
31
            "Effect Control 2", "Undefined", "Undefined", "GP Controller 1",
32
            "GP Controller 2", "GP Controller 3", "GP Controller 4",
33
            "Undefined", "Undefined", "Undefined", "Undefined", "Undefined",
34
            "Undefined", "Undefined", "Undefined", "Undefined", "Undefined",
35
            "Undefined", "Undefined", "LSB 0 (Bank Select)",
36
            "LSB 1 (Modulation Wheel)", "LSB 2 (Breath Controller)",
37
            "LSB 3 (Undefined)", "LSB 4 (Foot Controller)",
38
            "LSB 5 (Portamento Time)", "LSB 6 (Data Entry)",
39
            "LSB 7 (Channel Volume)", "LSB 8 (Balance)", "LSB 9 (Undefined)",
40
            "LSB 10 (Pan)", "LSB 11 (Expression Controller)",
41
            "LSB 12 (Effect  1)", "LSB 13 (Effect  2)", "LSB 14 (Undefined)",
42
            "LSB 15 (Undefined)", "LSB 16 (GP ler 1)", "LSB 17 (GP ler 2)",
43
            "LSB 18 (GP ler 3)", "LSB 19 (GP ler 4)", "LSB 20 (Undefined)",
44
            "LSB 21 (Undefined)", "LSB 22 (Undefined)", "LSB 23 (Undefined)",
45
            "LSB 24 (Undefined)", "LSB 25 (Undefined)", "LSB 26 (Undefined)",
46
            "LSB 27 (Undefined)", "LSB 28 (Undefined)", "LSB 29 (Undefined)",
47
            "LSB 30 (Undefined)", "LSB 31 (Undefined)",
48
            "Damper Pedal On/Off (Sustain)", "Portamento On/Off",
49
            "Sustenuto On/Off", "Soft Pedal On/Off", "Legato Footswitch",
50
            "Hold 2", "Sound Controller 1", "Sound Controller 2",
51
            "Sound Controller 3", "Sound Controller 4", "Sound Controller 5",
52
            "Sound Controller 6", "Sound Controller 7", "Sound Controller 8",
53
            "Sound Controller 9", "Sound Controller 10", "GP Controller 5",
54
            "GP Controller 6", "GP Controller 7", "GP Controller 8",
55
            "Portamento Control", "Undefined", "Undefined", "Undefined",
56
            "Undefined", "Undefined", "Undefined", "Effects 1 Depth",
57
            "Effects 2 Depth", "Effects 3 Depth", "Effects 4 Depth",
58
            "Effects 5 Depth", "Data Increment", "Data Decrement",
59
            "Non-Reg. Param. No. - LSB", "Non-Reg. Param. No. - MSB",
60
            "Reg. Param. No. - LSB", "Reg. Param. No. - MSB", "Undefined",
61
            "Undefined", "Undefined", "Undefined", "Undefined", "Undefined",
62
            "Undefined", "Undefined", "Undefined", "Undefined", "Undefined",
63
            "Undefined", "Undefined", "Undefined", "Undefined", "Undefined",
64
            "Undefined", "Undefined", "All Sound Off", "Reset All Controllers",
65
            "Local Control On/Off", "All Notes Off", "Omni Mode Off",
66
            "Omni Mode On", "Poly Mode On/Off", "Poly Mode On" };
67
 
68
    public static String[] cc14BitNames = { "Bank Select (14 bit)", "Modulation Wheel (14 bit)",
69
        "Breath Controller (14 bit)", "Undefined (14 bit)", "Foot Controller (14 bit)",
70
        "Portamento Time (14 bit)", "Data Entry MSB (14 bit)", "Volume (14 bit)", "Balance (14 bit)",
71
        "Undefined (14 bit)", "Pan (14 bit)", "Expression Controller (14 bit)", "Effect Control 1 (14 bit)",
72
        "Effect Control 2 (14 bit)", "Undefined (14 bit)", "Undefined (14 bit)", "GP Controller 1 (14 bit)",
73
        "GP Controller 2 (14 bit)", "GP Controller 3 (14 bit)", "GP Controller 4 (14 bit)",
74
        "Undefined (14 bit)", "Undefined (14 bit)", "Undefined (14 bit)", "Undefined (14 bit)", "Undefined (14 bit)",
75
        "Undefined (14 bit)", "Undefined (14 bit)", "Undefined (14 bit)", "Undefined (14 bit)", "Undefined (14 bit)",
76
        "Undefined (14 bit)", "Undefined (14 bit)", "Bank Select (14 bit)", "Modulation Wheel (14 bit)",
77
        "Breath Controller (14 bit)", "Undefined (14 bit)", "Foot Controller (14 bit)",
78
        "Portamento Time (14 bit)", "Data Entry MSB (14 bit)", "Volume (14 bit)", "Balance (14 bit)",
79
        "Undefined (14 bit)", "Pan (14 bit)", "Expression Controller (14 bit)", "Effect Control 1 (14 bit)",
80
        "Effect Control 2 (14 bit)", "Undefined (14 bit)", "Undefined (14 bit)", "GP Controller 1 (14 bit)",
81
        "GP Controller 2 (14 bit)", "GP Controller 3 (14 bit)", "GP Controller 4 (14 bit)",
82
        "Undefined (14 bit)", "Undefined (14 bit)", "Undefined (14 bit)", "Undefined (14 bit)", "Undefined (14 bit)",
83
        "Undefined (14 bit)", "Undefined (14 bit)", "Undefined (14 bit)", "Undefined (14 bit)", "Undefined (14 bit)",
84
        "Undefined (14 bit)", "Undefined (14 bit)",
85
        "Damper Pedal On/Off (Sustain)", "Portamento On/Off",
86
        "Sustenuto On/Off", "Soft Pedal On/Off", "Legato Footswitch",
87
        "Hold 2", "Sound Controller 1", "Sound Controller 2",
88
        "Sound Controller 3", "Sound Controller 4", "Sound Controller 5",
89
        "Sound Controller 6", "Sound Controller 7", "Sound Controller 8",
90
        "Sound Controller 9", "Sound Controller 10", "GP Controller 5",
91
        "GP Controller 6", "GP Controller 7", "GP Controller 8",
92
        "Portamento Control", "Undefined", "Undefined", "Undefined",
93
        "Undefined", "Undefined", "Undefined", "Effects 1 Depth",
94
        "Effects 2 Depth", "Effects 3 Depth", "Effects 4 Depth",
95
        "Effects 5 Depth", "Data Increment", "Data Decrement",
96
        "Non-Reg. Param. No. (14 bit)", "Non-Reg. Param. No. (14 bit)",
97
        "Reg. Param. No. (14 bit)", "Reg. Param. No. (14 bit)", "Undefined",
98
        "Undefined", "Undefined", "Undefined", "Undefined", "Undefined",
99
        "Undefined", "Undefined", "Undefined", "Undefined", "Undefined",
100
        "Undefined", "Undefined", "Undefined", "Undefined", "Undefined",
101
        "Undefined", "Undefined", "All Sound Off", "Reset All Controllers",
102
        "Local Control On/Off", "All Notes Off", "Omni Mode Off",
103
        "Omni Mode On", "Poly Mode On/Off", "Poly Mode On" };
104
 
105
    public static final String[] s_astrKeyNames = { "C", "C#", "D", "D#", "E",
106
            "F", "F#", "G", "G#", "A", "A#", "B" };
107
 
108
    public static final String[] s_astrKeySignatures = { "Cb", "Gb", "Db",
109
            "Ab", "Eb", "Bb", "F", "C", "G", "D", "A", "E", "B", "F#", "C#" };
110
 
111
    public static final String[] SYSTEM_MESSAGE_TEXT = {
112
            "System Exclusive (should not be in ShortMessage!)",
113
            "MTC Quarter Frame: ", "Song Position: ", "Song Select: ",
114
            "Undefined", "Undefined", "Tune Request",
115
            "End of SysEx (should not be in ShortMessage!)", "Timing clock",
116
            "Undefined", "Start", "Continue", "Stop", "Undefined",
117
            "Active Sensing", "System Reset" };
118
 
119
    public static final String[] QUARTER_FRAME_MESSAGE_TEXT = {
120
            "frame count LS: ", "frame count MS: ", "seconds count LS: ",
121
            "seconds count MS: ", "minutes count LS: ", "minutes count MS: ",
122
            "hours count LS: ", "hours count MS: " };
123
 
124
    public static final String[] FRAME_TYPE_TEXT = { "24 frames/second",
125
            "25 frames/second", "30 frames/second (drop)",
126
            "30 frames/second (non-drop)", };
127
 
128
    public static final char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6',
129
            '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
130
 
131
    public static String getKeyName(int nKeyNumber) {
132
        if (nKeyNumber > 127) {
133
            return "illegal value";
134
        } else {
135
            int nNote = nKeyNumber % 12;
136
            int nOctave = nKeyNumber / 12;
137
            return s_astrKeyNames[nNote] + (nOctave - 1);
138
        }
139
    }
140
 
141
    public static int get14bitValue(int nLowerPart, int nHigherPart) {
142
        return (nLowerPart & 0x7F) | ((nHigherPart & 0x7F) << 7);
143
    }
144
 
145
    public static int signedByteToUnsigned(byte b) {
146
        return b & 0xFF;
147
    }
148
 
149
    // convert from microseconds per quarter note to beats per minute and vice
150
    // versa
151
    public static float convertTempo(float value) {
152
        if (value <= 0) {
153
            value = 0.1f;
154
        }
155
        return 60000000.0f / value;
156
    }
157
 
158
    public static String getHexString(byte[] aByte) {
159
        StringBuffer sbuf = new StringBuffer(aByte.length * 3 + 2);
160
        for (int i = 0; i < aByte.length; i++) {
161
            if (i > 0) {
162
                sbuf.append(' ');
163
            }
164
            sbuf.append(getHexString((int) aByte[i]));
165
        }
166
        return new String(sbuf);
167
    }
168
 
169
    public static String getHexString(int num) {
170
        StringBuffer sbuf = new StringBuffer();
171
        sbuf.append(hexDigits[(num & 0xF0) >> 4]);
172
        sbuf.append(hexDigits[num & 0x0F]);
173
 
174
        return new String(sbuf);
175
    }
176
 
177
    public static String getDecimalString(byte[] aByte) {
178
        StringBuffer sbuf = new StringBuffer(aByte.length * 4 + 2);
179
        for (int i = 0; i < aByte.length; i++) {
180
            sbuf.append(' ');
181
            sbuf.append(getDecimalString((int) aByte[i]));
182
        }
183
        return new String(sbuf);
184
    }
185
 
186
    public static String getDecimalString(int num) {
187
        StringBuffer sbuf = new StringBuffer();
188
 
189
        sbuf.append("000");
190
        String value = "" + (num & 0xFF);
191
        sbuf.replace(sbuf.length() - value.length(), sbuf.length(), value);
192
 
193
        return new String(sbuf);
194
    }
195
 
196
    public static String intToHex(int i) {
197
        return "" + hexDigits[(i & 0xF0) >> 4] + hexDigits[i & 0x0F];
198
    }
199
 
200
    public static String getHexString(ShortMessage sm) {
201
        // bug in J2SDK 1.4.1
202
        // return getHexString(sm.getMessage());
203
        int status = sm.getStatus();
204
        String res = intToHex(sm.getStatus());
205
        // if one-byte message, return
206
        switch (status) {
207
        case 0xF6: // Tune Request
208
        case 0xF7: // EOX
209
        // System real-time messages
210
        case 0xF8: // Timing Clock
211
        case 0xF9: // Undefined
212
        case 0xFA: // Start
213
        case 0xFB: // Continue
214
        case 0xFC: // Stop
215
        case 0xFD: // Undefined
216
        case 0xFE: // Active Sensing
217
        case 0xFF:
218
            return res;
219
        }
220
        res += ' ' + intToHex(sm.getData1());
221
        // if 2-byte message, return
222
        switch (status) {
223
        case 0xF1: // MTC Quarter Frame
224
        case 0xF3: // Song Select
225
            return res;
226
        }
227
        // switch (sm.getCommand()) {
228
        // case 0xC0:
229
        // case 0xD0:
230
        // return res;
231
        // }
232
        // 3-byte messages left
233
        res += ' ' + intToHex(sm.getData2());
234
        return res;
235
    }
236
}