Subversion Repositories svn.mios

Rev

Rev 290 | Details | Compare with Previous | Last modification | View Log | RSS feed

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