Subversion Repositories svn.mios

Rev

Rev 724 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 724 Rev 808
1
/*
1
/*
2
 * @(#)MidiFilter.java  beta8   2006/04/23
2
 * @(#)MidiFilter.java  beta8   2006/04/23
3
 *
3
 *
4
 * Copyright (C) 2008    Adam King (adamjking@optusnet.com.au)
4
 * Copyright (C) 2008    Adam King (adamjking@optusnet.com.au)
5
 *
5
 *
6
 * This application is free software; you can redistribute it and/or modify
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
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
8
 * the Free Software Foundation; either version 2 of the License, or
9
 * (at your option) any later version.
9
 * (at your option) any later version.
10
 *
10
 *
11
 * This application is distributed in the hope that it will be useful,
11
 * This application is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
14
 * GNU General Public License for more details.
15
 *
15
 *
16
 * You should have received a copy of the GNU General Public License
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
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
18
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
 */
19
 */
20
20
21
package org.midibox.midi;
21
package org.midibox.midi;
22
22
23
import java.util.Observable;
23
import java.util.Observable;
24
24
25
import javax.sound.midi.MetaMessage;
25
import javax.sound.midi.MetaMessage;
26
import javax.sound.midi.MidiMessage;
26
import javax.sound.midi.MidiMessage;
27
import javax.sound.midi.Receiver;
27
import javax.sound.midi.Receiver;
28
import javax.sound.midi.ShortMessage;
28
import javax.sound.midi.ShortMessage;
29
import javax.sound.midi.SysexMessage;
29
import javax.sound.midi.SysexMessage;
30
30
31
public class MidiFilter extends Observable implements Receiver {
31
public class MidiFilter extends Observable implements Receiver {
32
32
33
    public Object VOICE_MESSAGES = new Object();
33
    public Object VOICE_MESSAGES = new Object();
34
34
35
    public Object VOICE_MESSAGE = new Object();
35
    public Object VOICE_MESSAGE = new Object();
36
36
37
    public Object CONTROL_CHANGE_MESSAGE = new Object();
37
    public Object CONTROL_CHANGE_MESSAGE = new Object();
38
38
39
    public Object CHANNEL = new Object();
39
    public Object CHANNEL = new Object();
40
40
41
    public Object SYSTEM_COMMON_MESSAGES = new Object();
41
    public Object SYSTEM_COMMON_MESSAGES = new Object();
42
42
43
    public Object SYSTEM_COMMON_MESSAGE = new Object();
43
    public Object SYSTEM_COMMON_MESSAGE = new Object();
44
44
45
    public Object SYSTEM_REALTIME_MESSAGES = new Object();
45
    public Object SYSTEM_REALTIME_MESSAGES = new Object();
46
46
47
    public Object SYSTEM_REALTIME_MESSAGE = new Object();
47
    public Object SYSTEM_REALTIME_MESSAGE = new Object();
48
48
49
    public Object SYSEX_MESSAGES = new Object();
49
    public Object SYSEX_MESSAGES = new Object();
50
50
51
    public Object META_MESSAGES = new Object();
51
    public Object META_MESSAGES = new Object();
52
52
53
    protected Receiver receiver;
53
    protected Receiver receiver;
54
54
55
    // voice messages
55
    // voice messages
56
    private boolean voiceMessages = true;
56
    private boolean voiceMessages = true;
57
57
58
    private boolean[] voiceMessage = new boolean[8];
58
    private boolean[] voiceMessage = new boolean[8];
59
59
60
    // control change
60
    // control change
61
    private boolean[] controlChangeMessage = new boolean[128];
61
    private boolean[] controlChangeMessage = new boolean[128];
62
62
63
    // channels
63
    // channels
64
    private boolean[] channel = new boolean[16];
64
    private boolean[] channel = new boolean[16];
65
65
66
    // system common
66
    // system common
67
    private boolean systemCommonMessages = true;
67
    private boolean systemCommonMessages = true;
68
68
69
    private boolean[] systemCommonMessage = new boolean[8];
69
    private boolean[] systemCommonMessage = new boolean[8];
70
70
71
    // system realtime
71
    // system realtime
72
    private boolean systemRealtimeMessages = true;
72
    private boolean systemRealtimeMessages = true;
73
73
74
    private boolean[] systemRealtimeMessage = new boolean[8];
74
    private boolean[] systemRealtimeMessage = new boolean[8];
75
75
76
    // sysex messages
76
    // sysex messages
77
    private boolean sysexMessages = true;
77
    private boolean sysexMessages = true;
78
78
79
    // meta messages
79
    // meta messages
80
    private boolean metaMessages = true;
80
    private boolean metaMessages = true;
81
81
82
    public MidiFilter(Receiver receiver) {
82
    public MidiFilter(Receiver receiver) {
83
83
84
        this.receiver = receiver;
84
        this.receiver = receiver;
85
85
86
        for (int i = 0; i < voiceMessage.length; i++) {
86
        for (int i = 0; i < voiceMessage.length; i++) {
87
            voiceMessage[i] = true;
87
            voiceMessage[i] = true;
88
        }
88
        }
89
89
90
        for (int i = 0; i < controlChangeMessage.length; i++) {
90
        for (int i = 0; i < controlChangeMessage.length; i++) {
91
            controlChangeMessage[i] = true;
91
            controlChangeMessage[i] = true;
92
        }
92
        }
93
93
94
        for (int i = 0; i < channel.length; i++) {
94
        for (int i = 0; i < channel.length; i++) {
95
            channel[i] = true;
95
            channel[i] = true;
96
        }
96
        }
97
97
98
        for (int i = 0; i < systemCommonMessage.length; i++) {
98
        for (int i = 0; i < systemCommonMessage.length; i++) {
99
            systemCommonMessage[i] = true;
99
            systemCommonMessage[i] = true;
100
        }
100
        }
101
101
102
        for (int i = 0; i < systemRealtimeMessage.length; i++) {
102
        for (int i = 0; i < systemRealtimeMessage.length; i++) {
103
            systemRealtimeMessage[i] = true;
103
            systemRealtimeMessage[i] = true;
104
        }
104
        }
105
    }
105
    }
106
106
107
    public void setVoiceMessage(int statusNumber, boolean enabled) {
107
    public void setVoiceMessage(int statusNumber, boolean enabled) {
108
108
109
        setStatus(statusNumber, enabled);
109
        setStatus(statusNumber, enabled);
110
110
111
        setChanged();
111
        setChanged();
112
112
113
        notifyObservers(VOICE_MESSAGE);
113
        notifyObservers(VOICE_MESSAGE);
114
114
115
        clearChanged();
115
        clearChanged();
116
    }
116
    }
117
117
118
    public void setSystemCommonMessage(int statusNumber, boolean enabled) {
118
    public void setSystemCommonMessage(int statusNumber, boolean enabled) {
119
119
120
        setStatus(statusNumber, enabled);
120
        setStatus(statusNumber, enabled);
121
121
122
        setChanged();
122
        setChanged();
123
123
124
        notifyObservers(SYSTEM_COMMON_MESSAGE);
124
        notifyObservers(SYSTEM_COMMON_MESSAGE);
125
125
126
        clearChanged();
126
        clearChanged();
127
    }
127
    }
128
128
129
    public void setSystemRealtimeMessage(int statusNumber, boolean enabled) {
129
    public void setSystemRealtimeMessage(int statusNumber, boolean enabled) {
130
130
131
        setStatus(statusNumber, enabled);
131
        setStatus(statusNumber, enabled);
132
132
133
        setChanged();
133
        setChanged();
134
134
135
        notifyObservers(SYSTEM_REALTIME_MESSAGE);
135
        notifyObservers(SYSTEM_REALTIME_MESSAGE);
136
136
137
        clearChanged();
137
        clearChanged();
138
    }
138
    }
139
139
140
    protected void setStatus(int statusNumber, boolean enabled) {
140
    protected void setStatus(int statusNumber, boolean enabled) {
141
141
142
        if (statusNumber >= 0x80 && statusNumber <= 0xEF) {
142
        if (statusNumber >= 0x80 && statusNumber <= 0xEF) {
143
            voiceMessage[(statusNumber >> 4) & 0x7] = enabled;
143
            voiceMessage[(statusNumber >> 4) & 0x7] = enabled;
144
        }
144
        }
145
145
146
        if (statusNumber >= 0xF0 && statusNumber <= 0xF7) {
146
        if (statusNumber >= 0xF0 && statusNumber <= 0xF7) {
147
            systemCommonMessage[(statusNumber & 0xF)] = enabled;
147
            systemCommonMessage[(statusNumber & 0xF)] = enabled;
148
        }
148
        }
149
149
150
        if (statusNumber >= 0xF8 && statusNumber <= 0xFF) {
150
        if (statusNumber >= 0xF8 && statusNumber <= 0xFF) {
151
            systemRealtimeMessage[(statusNumber & 0xF) - 8] = enabled;
151
            systemRealtimeMessage[(statusNumber & 0xF) - 8] = enabled;
152
        }
152
        }
153
    }
153
    }
154
154
155
    public void setChannel(int channelNumber, boolean enabled) {
155
    public void setChannel(int channelNumber, boolean enabled) {
156
156
157
        channel[channelNumber] = enabled;
157
        channel[channelNumber] = enabled;
158
158
159
        setChanged();
159
        setChanged();
160
160
161
        notifyObservers(CHANNEL);
161
        notifyObservers(CHANNEL);
162
162
163
        clearChanged();
163
        clearChanged();
164
    }
164
    }
165
165
166
    public void setControlChange(int controlChangeNumber, boolean enabled) {
166
    public void setControlChange(int controlChangeNumber, boolean enabled) {
167
167
168
        controlChangeMessage[controlChangeNumber] = enabled;
168
        controlChangeMessage[controlChangeNumber] = enabled;
169
169
170
        setChanged();
170
        setChanged();
171
171
172
        notifyObservers(CONTROL_CHANGE_MESSAGE);
172
        notifyObservers(CONTROL_CHANGE_MESSAGE);
173
173
174
        clearChanged();
174
        clearChanged();
175
    }
175
    }
176
176
177
    public void setVoiceMessages(boolean voiceMessages) {
177
    public void setVoiceMessages(boolean voiceMessages) {
178
178
179
        this.voiceMessages = voiceMessages;
179
        this.voiceMessages = voiceMessages;
180
180
181
        setChanged();
181
        setChanged();
182
182
183
        notifyObservers(VOICE_MESSAGES);
183
        notifyObservers(VOICE_MESSAGES);
184
184
185
        clearChanged();
185
        clearChanged();
186
    }
186
    }
187
187
188
    public void setSystemCommonMessages(boolean systemCommonMessages) {
188
    public void setSystemCommonMessages(boolean systemCommonMessages) {
189
189
190
        this.systemCommonMessages = systemCommonMessages;
190
        this.systemCommonMessages = systemCommonMessages;
191
191
192
        setChanged();
192
        setChanged();
193
193
194
        notifyObservers(SYSTEM_COMMON_MESSAGES);
194
        notifyObservers(SYSTEM_COMMON_MESSAGES);
195
195
196
        clearChanged();
196
        clearChanged();
197
    }
197
    }
198
198
199
    public void setSystemRealtimeMessages(boolean systemRealtimeMessages) {
199
    public void setSystemRealtimeMessages(boolean systemRealtimeMessages) {
200
200
201
        this.systemRealtimeMessages = systemRealtimeMessages;
201
        this.systemRealtimeMessages = systemRealtimeMessages;
202
202
203
        setChanged();
203
        setChanged();
204
204
205
        notifyObservers(SYSTEM_REALTIME_MESSAGES);
205
        notifyObservers(SYSTEM_REALTIME_MESSAGES);
206
206
207
        clearChanged();
207
        clearChanged();
208
    }
208
    }
209
209
210
    public void setSysexMessages(boolean sysexMessages) {
210
    public void setSysexMessages(boolean sysexMessages) {
211
        this.sysexMessages = sysexMessages;
211
        this.sysexMessages = sysexMessages;
212
212
213
        setChanged();
213
        setChanged();
214
214
215
        notifyObservers(SYSEX_MESSAGES);
215
        notifyObservers(SYSEX_MESSAGES);
216
216
217
        clearChanged();
217
        clearChanged();
218
    }
218
    }
219
219
220
    public void setMetaMessages(boolean metaMessages) {
220
    public void setMetaMessages(boolean metaMessages) {
221
        this.metaMessages = metaMessages;
221
        this.metaMessages = metaMessages;
222
222
223
        setChanged();
223
        setChanged();
224
224
225
        notifyObservers(META_MESSAGES);
225
        notifyObservers(META_MESSAGES);
226
226
227
        clearChanged();
227
        clearChanged();
228
    }
228
    }
229
229
230
    public boolean getVoiceMessage(int statusNumber) {
230
    public boolean getVoiceMessage(int statusNumber) {
231
231
232
        return voiceMessage[(statusNumber >> 4) & 0x7];
232
        return voiceMessage[(statusNumber >> 4) & 0x7];
233
    }
233
    }
234
234
235
    public boolean getSystemCommonMessage(int statusNumber) {
235
    public boolean getSystemCommonMessage(int statusNumber) {
236
236
237
        return systemCommonMessage[(statusNumber & 0xF)];
237
        return systemCommonMessage[(statusNumber & 0xF)];
238
    }
238
    }
239
239
240
    public boolean getSystemRealtimeMessage(int statusNumber) {
240
    public boolean getSystemRealtimeMessage(int statusNumber) {
241
241
242
        return systemRealtimeMessage[(statusNumber & 0xF) - 8];
242
        return systemRealtimeMessage[(statusNumber & 0xF) - 8];
243
    }
243
    }
244
244
245
    public boolean getChannel(int channelNumber) {
245
    public boolean getChannel(int channelNumber) {
246
246
247
        return channel[channelNumber];
247
        return channel[channelNumber];
248
    }
248
    }
249
249
250
    public boolean getControlChange(int controlChangeNumber) {
250
    public boolean getControlChange(int controlChangeNumber) {
251
251
252
        return controlChangeMessage[controlChangeNumber];
252
        return controlChangeMessage[controlChangeNumber];
253
    }
253
    }
254
254
255
    public boolean[] getVoiceMessages() {
255
    public boolean[] getVoiceMessages() {
256
        return voiceMessage.clone();
256
        return voiceMessage.clone();
257
    }
257
    }
258
258
259
    public boolean[] getControlChangeMessages() {
259
    public boolean[] getControlChangeMessages() {
260
        return controlChangeMessage.clone();
260
        return controlChangeMessage.clone();
261
    }
261
    }
262
262
263
    public boolean[] getChannels() {
263
    public boolean[] getChannels() {
264
        return channel.clone();
264
        return channel.clone();
265
    }
265
    }
266
266
267
    public boolean[] getSystemRealtimeMessages() {
267
    public boolean[] getSystemRealtimeMessages() {
268
        return systemRealtimeMessage.clone();
268
        return systemRealtimeMessage.clone();
269
    }
269
    }
270
270
271
    public boolean[] getSystemCommonMessages() {
271
    public boolean[] getSystemCommonMessages() {
272
        return systemCommonMessage.clone();
272
        return systemCommonMessage.clone();
273
    }
273
    }
274
274
275
    public boolean isVoiceMessages() {
275
    public boolean isVoiceMessages() {
276
        return voiceMessages;
276
        return voiceMessages;
277
    }
277
    }
278
278
279
    public boolean isSystemCommonMessages() {
279
    public boolean isSystemCommonMessages() {
280
        return systemCommonMessages;
280
        return systemCommonMessages;
281
    }
281
    }
282
282
283
    public boolean isSystemRealtimeMessages() {
283
    public boolean isSystemRealtimeMessages() {
284
        return systemRealtimeMessages;
284
        return systemRealtimeMessages;
285
    }
285
    }
286
286
287
    public boolean isSysexMessages() {
287
    public boolean isSysexMessages() {
288
        return sysexMessages;
288
        return sysexMessages;
289
    }
289
    }
290
290
291
    public boolean isMetaMessages() {
291
    public boolean isMetaMessages() {
292
        return metaMessages;
292
        return metaMessages;
293
    }
293
    }
294
294
295
    public void close() {
295
    public void close() {
296
296
297
    }
297
    }
298
298
299
    public void send(MidiMessage message, long timestamp) {
299
    public void send(MidiMessage message, long timestamp) {
300
300
301
        // sysex messages
301
        // sysex messages
302
        if (message instanceof SysexMessage) {
302
        if (message instanceof SysexMessage) {
303
            if (!sysexMessages) {
303
            if (!sysexMessages) {
304
                return;
304
                return;
305
            }
305
            }
306
        }
306
        }
307
307
308
        // meta messages
308
        // meta messages
309
        if (message instanceof MetaMessage) {
309
        if (message instanceof MetaMessage) {
310
            if (!metaMessages) {
310
            if (!metaMessages) {
311
                return;
311
                return;
312
            }
312
            }
313
        }
313
        }
314
314
315
        if (message instanceof ShortMessage) {
315
        if (message instanceof ShortMessage) {
316
316
317
            int statusNo = (int) (message.getStatus() & 0xFF);
317
            int statusNo = (int) (message.getStatus() & 0xFF);
318
318
319
            if (statusNo >= 0x80 && statusNo <= 0xEF) {
319
            if (statusNo >= 0x80 && statusNo <= 0xEF) {
320
320
321
                int channelNo = statusNo & 0x0F;
321
                int channelNo = statusNo & 0x0F;
322
322
323
                statusNo = statusNo & 0xF0;
323
                statusNo = statusNo & 0xF0;
324
324
325
                if (!getVoiceMessage(statusNo) || !voiceMessages) {
325
                if (!getVoiceMessage(statusNo) || !voiceMessages) {
326
                    return;
326
                    return;
327
                }
327
                }
328
328
329
                // control change
329
                // control change
330
                if (statusNo == ShortMessage.CONTROL_CHANGE
330
                if (statusNo == ShortMessage.CONTROL_CHANGE
331
                        && !controlChangeMessage[((ShortMessage) message)
331
                        && !controlChangeMessage[((ShortMessage) message)
332
                                .getData1()]) {
332
                                .getData1()]) {
333
                    return;
333
                    return;
334
                }
334
                }
335
335
336
                // channels
336
                // channels
337
                if (!channel[channelNo]) {
337
                if (!channel[channelNo]) {
338
                    return;
338
                    return;
339
                }
339
                }
340
            }
340
            }
341
341
342
            if (statusNo >= 0xF0 && statusNo <= 0xF7) {
342
            if (statusNo >= 0xF0 && statusNo <= 0xF7) {
343
                if (!getSystemCommonMessage(statusNo) || !systemCommonMessages) {
343
                if (!getSystemCommonMessage(statusNo) || !systemCommonMessages) {
344
                    return;
344
                    return;
345
                }
345
                }
346
            }
346
            }
347
347
348
            if (statusNo >= 0xF8 && statusNo <= 0xFF) {
348
            if (statusNo >= 0xF8 && statusNo <= 0xFF) {
349
                if (!getSystemRealtimeMessage(statusNo)
349
                if (!getSystemRealtimeMessage(statusNo)
350
                        || !systemRealtimeMessages) {
350
                        || !systemRealtimeMessages) {
351
                    return;
351
                    return;
352
                }
352
                }
353
            }
353
            }
354
354
355
        }
355
        }
356
356
357
        if (receiver != null) {
357
        if (receiver != null) {
358
            receiver.send(message, timestamp);
358
            receiver.send(message, timestamp);
359
        }
359
        }
-
 
360
    }
-
 
361
-
 
362
    public void setReceiver(Receiver receiver) {
-
 
363
        this.receiver = receiver;
360
    }
364
    }
361
}
365
}
362
 
366