Subversion Repositories svn.mios

Rev

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

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