Subversion Repositories svn.mios

Rev

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

Rev Author Line No. Line
628 adamjking 1
/*
2
 * @(#)MIOSStudio.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.apps.miosstudio;
22
 
23
import java.util.Iterator;
24
import java.util.Observable;
25
import java.util.Observer;
26
 
641 adamjking 27
import javax.sound.midi.MidiDevice;
28
 
29
import org.midibox.midi.MidiDeviceManager;
628 adamjking 30
import org.midibox.midi.MidiDeviceRouting;
31
import org.midibox.midi.MidiFilterDevice;
641 adamjking 32
import org.midibox.midi.MidiFilterDeviceManager;
628 adamjking 33
import org.midibox.midi.MidiKeyboardControllerDevice;
34
import org.midibox.midi.MidiMonitorFiltered;
35
import org.midibox.midi.MidiMonitorFilteredDevice;
36
import org.midibox.midi.MidiRouterDevice;
37
import org.midibox.mios.DebugFunctionDevice;
38
import org.midibox.mios.HexFileUploadDevice;
39
import org.midibox.mios.HexFileUploadDeviceManager;
40
import org.midibox.mios.LCDMessageDevice;
41
import org.midibox.mios.MIOSTerminal;
42
 
721 adamjking 43
public class MIOSStudio extends Observable implements Observer {
628 adamjking 44
 
721 adamjking 45
    public static Object MIDI_THRU_OUT_PORT = new Object();
723 adamjking 46
 
721 adamjking 47
    public static Object ROUTE_INDIVIDUAL_DEVICES = new Object();
723 adamjking 48
 
628 adamjking 49
    protected MidiDeviceRouting midiDeviceRouting;
50
 
51
    protected MidiRouterDevice miosStudioInPort;
52
 
53
    protected MidiFilterDevice midiThruFilterDevice;
54
 
55
    // TODO: implement a map for MIDI thru
56
 
57
    /*
58
     * private MidiMapDevice midiThruMapDevice;
59
     */
60
 
641 adamjking 61
    protected MidiRouterDevice miosStudioThruPort;
62
 
628 adamjking 63
    protected MidiRouterDevice miosStudioOutPort;
64
 
641 adamjking 65
    private MidiDeviceManager midiDeviceManager;
66
 
67
    private MidiFilterDeviceManager midiFilterManager;
68
 
69
    /*
70
     * private MidiMapDeviceManager midiMapManager;
71
     */
72
 
628 adamjking 73
    private MidiMonitorFilteredDevice midiInPortMonitorDevice;
74
 
75
    private MidiMonitorFilteredDevice midiOutPortMonitorDevice;
76
 
77
    // Debuging for command line
78
 
79
    /*
80
     * private DumpReceiverDevice inDumpReceiverDevice;
81
     *
82
     * private DumpReceiverDevice outDumpReceiverDevice;
83
     */
84
 
85
    private MidiKeyboardControllerDevice midiKeyboardControllerDevice;
86
 
87
    // TODO: implement a manager for sysex sending and receing tasks
88
 
89
    /*
90
     * private SysexSendReceiveDeviceManager sysexSendReceiveDeviceManager;
91
     */
92
 
93
    private HexFileUploadDeviceManager hexFileUploadDeviceManager;
94
 
95
    // TODO: implement a device for reading and writing to MIOS
96
 
97
    /*
98
     * private MemoryReadWriteDevice memoryReadWriteDevice;
99
     */
100
 
101
    private LCDMessageDevice lcdMessageDevice;
102
 
103
    private DebugFunctionDevice debugFunctionDevice;
104
 
105
    private MidiMonitorFilteredDevice miosTerminalDevice;
106
 
107
    private boolean routeIndividualDevices;
108
 
639 adamjking 109
    private boolean midiThruOutPort = false;
628 adamjking 110
 
111
    public MIOSStudio() {
112
 
113
        createDevices();
114
 
115
        setRouteIndividualDevices(false);
116
 
117
        connectDevices();
118
    }
119
 
120
    protected void createDevices() {
121
 
122
        midiDeviceRouting = new MidiDeviceRouting();
123
 
124
        miosStudioInPort = new MidiRouterDevice("MIOS Studio In Port");
125
 
641 adamjking 126
        midiThruFilterDevice = new MidiFilterDevice("MIOS Studio Thru Filter");
628 adamjking 127
 
641 adamjking 128
        /*
129
         * midiThruMapDevice = new MidiMapDevice("MIOS Studio Thru Map");
130
         */
131
 
639 adamjking 132
        miosStudioThruPort = new MidiRouterDevice("MIOS Studio Thru Port");
133
 
641 adamjking 134
        miosStudioOutPort = new MidiRouterDevice("MIOS Studio Out Port");
628 adamjking 135
 
641 adamjking 136
        midiDeviceManager = new MidiDeviceManager();
137
        midiDeviceManager.addObserver(this);
138
 
139
        midiFilterManager = new MidiFilterDeviceManager();
140
        midiFilterManager.addObserver(this);
141
 
628 adamjking 142
        /*
641 adamjking 143
         * midiMapManager = new MidiMapDeviceManager();
144
         * midiMapManager.addObserver(this);
628 adamjking 145
         */
146
 
147
        midiInPortMonitorDevice = new MidiMonitorFilteredDevice(
148
                "MIDI Monitor: IN");
149
 
150
        midiOutPortMonitorDevice = new MidiMonitorFilteredDevice(
151
                "MIDI Monitor: OUT");
152
 
153
        /*
154
         * inDumpReceiverDevice = new DumpReceiverDevice("Dump Receiver: IN");
155
         *
156
         * outDumpReceiverDevice = new DumpReceiverDevice("Dump Receiver: OUT");
157
         */
158
 
159
        midiKeyboardControllerDevice = new MidiKeyboardControllerDevice(
160
                "MIDI Keyboard Controller", 0);
161
 
162
        /*
163
         * sysexSendReceiveDeviceManager = new SysexSendReceiveDeviceManager();
164
         * sysexSendReceiveDeviceManager.newSysexSendReceive();
165
         * sysexSendReceiveDeviceManager.addObserver(this);
166
         */
167
 
168
        hexFileUploadDeviceManager = new HexFileUploadDeviceManager();
693 adamjking 169
 
628 adamjking 170
        hexFileUploadDeviceManager.addObserver(this);
723 adamjking 171
 
628 adamjking 172
        /*
173
         * memoryReadWriteDevice = new MemoryReadWriteDevice( "MIOS Memory
174
         * Read/Write");
175
         */
176
 
177
        lcdMessageDevice = new LCDMessageDevice("MIOS LCD Message");
178
 
179
        debugFunctionDevice = new DebugFunctionDevice("MIOS Debug Function");
180
 
181
        MidiMonitorFiltered miosTerminalFiltered = new MidiMonitorFiltered(
182
                new MIOSTerminal());
183
 
184
        miosTerminalDevice = new MidiMonitorFilteredDevice("MIOS Terminal",
185
                miosTerminalFiltered);
186
 
187
        // special for MIOS Terminal:
188
        // disable all messages by default, only allow pass SysEx
189
        // user can enable other MIDI events again if required
190
 
741 adamjking 191
         miosTerminalFiltered.getMidiFilter().setVoiceMessages(false);
192
 
193
         miosTerminalFiltered.getMidiFilter().setSystemCommonMessages(false);
723 adamjking 194
 
741 adamjking 195
         miosTerminalFiltered.getMidiFilter().setSystemRealtimeMessages(false);
196
 
197
         miosTerminalFiltered.getMidiFilter().setSysexMessages(true);
723 adamjking 198
 
741 adamjking 199
         miosTerminalFiltered.getMidiFilter().setMetaMessages(false);
628 adamjking 200
    }
201
 
641 adamjking 202
    public MidiDeviceManager getMidiDeviceManager() {
203
        return midiDeviceManager;
204
    }
205
 
206
    public MidiFilterDeviceManager getMidiFilterManager() {
207
        return midiFilterManager;
208
    }
209
 
210
    /*
211
     * public MidiMapDeviceManager getMidiMapManager() { return midiMapManager;
212
     * }
213
     */
214
 
628 adamjking 215
    public MidiDeviceRouting getMidiDeviceRouting() {
216
        return midiDeviceRouting;
217
    }
218
 
219
    public MidiMonitorFilteredDevice getMidiOutPortMonitorDevice() {
220
        return midiOutPortMonitorDevice;
221
    }
222
 
223
    public MidiMonitorFilteredDevice getMidiInPortMonitorDevice() {
224
        return midiInPortMonitorDevice;
225
    }
226
 
227
    public MidiKeyboardControllerDevice getMidiKeyboardControllerDevice() {
228
        return midiKeyboardControllerDevice;
229
    }
230
 
231
    /*
232
     * public SysexSendReceiveDeviceManager getSysexSendReceiveDeviceManager() {
233
     * return sysexSendReceiveDeviceManager; }
234
     */
235
 
236
    public HexFileUploadDeviceManager getHexFileUploadDeviceManager() {
237
        return hexFileUploadDeviceManager;
238
    }
239
 
240
    public DebugFunctionDevice getDebugFunctionDevice() {
241
        return debugFunctionDevice;
242
    }
243
 
244
    /*
245
     * public MemoryReadWriteDevice getMemoryReadWriteDevice() { return
246
     * memoryReadWriteDevice; }
247
     */
248
 
249
    public LCDMessageDevice getLcdMessageDevice() {
250
        return lcdMessageDevice;
251
    }
252
 
253
    public MidiMonitorFilteredDevice getMIOSTerminalDevice() {
254
        return miosTerminalDevice;
255
    }
256
 
639 adamjking 257
    public boolean isMidiThruOutPort() {
258
        return midiThruOutPort;
628 adamjking 259
    }
260
 
639 adamjking 261
    public void setMidiThruOutPort(boolean midiThru) {
262
        this.midiThruOutPort = midiThru;
628 adamjking 263
 
641 adamjking 264
        midiDeviceRouting
265
                .connectDevices(miosStudioInPort, midiThruFilterDevice);
266
 
628 adamjking 267
        if (midiThru) {
639 adamjking 268
 
628 adamjking 269
            /*
270
             * midiDeviceRouting.connectDevices(midiThruFilterDevice,
271
             * midiThruMapDevice);
272
             */
273
 
639 adamjking 274
            midiDeviceRouting.disconnectDevice(miosStudioThruPort);
641 adamjking 275
 
639 adamjking 276
            midiDeviceRouting.disconnectDevices(midiThruFilterDevice,
641 adamjking 277
                    miosStudioThruPort);
639 adamjking 278
 
628 adamjking 279
            midiDeviceRouting.connectDevices(midiThruFilterDevice,
280
                    miosStudioOutPort);
281
        } else {
641 adamjking 282
 
628 adamjking 283
            /*
284
             * midiDeviceRouting.disconnectDevices(midiThruFilterDevice,
285
             * midiThruMapDevice);
286
             */
641 adamjking 287
 
628 adamjking 288
            midiDeviceRouting.disconnectDevices(midiThruFilterDevice,
641 adamjking 289
                    miosStudioOutPort);
290
 
639 adamjking 291
            midiDeviceRouting.connectDevices(midiThruFilterDevice,
292
                    miosStudioThruPort);
628 adamjking 293
        }
641 adamjking 294
 
639 adamjking 295
        setRouteIndividualDevices(routeIndividualDevices);
723 adamjking 296
 
721 adamjking 297
        setChanged();
723 adamjking 298
 
721 adamjking 299
        notifyObservers(MIDI_THRU_OUT_PORT);
723 adamjking 300
 
721 adamjking 301
        clearChanged();
628 adamjking 302
    }
303
 
304
    public MidiRouterDevice getMiosStudioInPort() {
305
        return miosStudioInPort;
306
    }
307
 
308
    public MidiRouterDevice getMiosStudioOutPort() {
309
        return miosStudioOutPort;
310
    }
311
 
312
    public MidiFilterDevice getMidiThruFilterDevice() {
313
        return midiThruFilterDevice;
314
    }
315
 
316
    /*
317
     * public MidiMapDevice getMidiThruMapDevice() { return midiThruMapDevice; }
318
     */
319
 
320
    public boolean isRouteIndividualDevices() {
321
        return routeIndividualDevices;
322
    }
323
 
324
    public void setRouteIndividualDevices(boolean routeIndividualDevices) {
325
        this.routeIndividualDevices = routeIndividualDevices;
326
 
327
        // remove all devices
328
        midiDeviceRouting.removeAllMidiReadDevices();
329
        midiDeviceRouting.removeAllMidiWriteDevices();
330
 
331
        // add internal MIOSStudio devices if selected
332
        if (routeIndividualDevices) {
333
 
334
            routeIndividualDevices();
335
 
336
        } else {
337
            midiDeviceRouting.addMidiWriteDevice(miosStudioInPort);
338
            midiDeviceRouting.addMidiReadDevice(miosStudioOutPort);
641 adamjking 339
 
639 adamjking 340
            if (!midiThruOutPort) {
341
                midiDeviceRouting.addMidiReadDevice(miosStudioThruPort);
342
            }
628 adamjking 343
        }
344
 
641 adamjking 345
        reorder();
723 adamjking 346
 
721 adamjking 347
        setChanged();
723 adamjking 348
 
721 adamjking 349
        notifyObservers(ROUTE_INDIVIDUAL_DEVICES);
723 adamjking 350
 
721 adamjking 351
        clearChanged();
628 adamjking 352
    }
353
 
641 adamjking 354
    public void reorder() {
355
 
356
        midiDeviceRouting.getMidiReadDevices().removeAll(
357
                midiFilterManager.getMidiFilterDevices());
358
        midiDeviceRouting.getMidiWriteDevices().removeAll(
359
                midiFilterManager.getMidiFilterDevices());
360
 
361
        midiDeviceRouting.getMidiReadDevices().removeAll(
739 adamjking 362
                midiDeviceManager.getSelectedMidiReadDevices().values());
641 adamjking 363
        midiDeviceRouting.getMidiWriteDevices().removeAll(
739 adamjking 364
                midiDeviceManager.getSelectedMidiWriteDevices().values());
641 adamjking 365
 
366
        /*
367
         * midiReadDevices.removeAll(midiMapManager.getMidiMapDevices());
368
         * midiWriteDevices.removeAll(midiMapManager.getMidiMapDevices());
369
         */
370
 
371
        midiDeviceRouting.addMidiReadDevices(midiFilterManager
372
                .getMidiFilterDevices());
373
        midiDeviceRouting.addMidiWriteDevices(midiFilterManager
374
                .getMidiFilterDevices());
375
 
739 adamjking 376
        Iterator it = midiDeviceManager.getMidiReadDevices().values()
377
                .iterator();
641 adamjking 378
 
379
        while (it.hasNext()) {
380
 
381
            Object object = it.next();
382
 
739 adamjking 383
            if (midiDeviceManager.getSelectedMidiReadDevices().values()
384
                    .contains(object)) {
641 adamjking 385
                midiDeviceRouting.addMidiReadDevice((MidiDevice) object);
386
            }
387
        }
388
 
739 adamjking 389
        it = midiDeviceManager.getMidiWriteDevices().values().iterator();
641 adamjking 390
        while (it.hasNext()) {
391
 
392
            Object object = it.next();
393
 
739 adamjking 394
            if (midiDeviceManager.getSelectedMidiWriteDevices().values()
641 adamjking 395
                    .contains(object)) {
396
                midiDeviceRouting.addMidiWriteDevice((MidiDevice) object);
397
            }
398
        }
399
 
400
        /*
401
         * midiReadDevices.addAll(midiMapManager.getMidiMapDevices());
402
         * midiWriteDevices.addAll(midiMapManager.getMidiMapDevices());
403
         */
404
    }
405
 
628 adamjking 406
    protected void routeIndividualDevices() {
407
 
408
        midiDeviceRouting.addMidiWriteDevice(miosStudioInPort);
409
        midiDeviceRouting.addMidiReadDevice(miosStudioInPort);
410
 
411
        midiDeviceRouting.addMidiWriteDevice(midiThruFilterDevice);
412
        midiDeviceRouting.addMidiReadDevice(midiThruFilterDevice);
413
 
414
        /*
415
         * midiDeviceRouting.addMidiWriteDevice(midiThruMapDevice);
416
         * midiDeviceRouting.addMidiReadDevice(midiThruMapDevice);
417
         */
641 adamjking 418
 
639 adamjking 419
        if (!midiThruOutPort) {
641 adamjking 420
 
639 adamjking 421
            midiDeviceRouting.addMidiWriteDevice(miosStudioThruPort);
422
            midiDeviceRouting.addMidiReadDevice(miosStudioThruPort);
423
        }
628 adamjking 424
        midiDeviceRouting.addMidiWriteDevice(miosStudioOutPort);
425
        midiDeviceRouting.addMidiReadDevice(miosStudioOutPort);
426
 
427
        midiDeviceRouting.addMidiWriteDevice(midiOutPortMonitorDevice);
428
 
429
        midiDeviceRouting.addMidiWriteDevice(midiInPortMonitorDevice);
430
 
431
        /*
432
         * midiDeviceRouting.addMidiWriteDevice(inDumpReceiverDevice);
433
         *
434
         * midiDeviceRouting.addMidiWriteDevice(outDumpReceiverDevice);
435
         */
436
 
437
        midiDeviceRouting.addMidiWriteDevice(midiKeyboardControllerDevice);
438
        midiDeviceRouting.addMidiReadDevice(midiKeyboardControllerDevice);
439
 
440
        /*
441
         * midiDeviceRouting.addMidiReadDevices(sysexSendReceiveDeviceManager
442
         * .getSysexSendReceiveDevices()); midiDeviceRouting.addMidiWriteDevices
443
         * (sysexSendReceiveDeviceManager .getSysexSendReceiveDevices());
444
         */
445
 
446
        midiDeviceRouting.addMidiReadDevices(hexFileUploadDeviceManager
447
                .getHexFileUploadDevices());
448
        midiDeviceRouting.addMidiWriteDevices(hexFileUploadDeviceManager
449
                .getHexFileUploadDevices());
450
        /*
451
         * midiDeviceRouting.addMidiWriteDevice(memoryReadWriteDevice);
452
         * midiDeviceRouting.addMidiReadDevice(memoryReadWriteDevice);
453
         */
454
 
455
        midiDeviceRouting.addMidiReadDevice(lcdMessageDevice);
456
 
457
        midiDeviceRouting.addMidiWriteDevice(debugFunctionDevice);
458
        midiDeviceRouting.addMidiReadDevice(debugFunctionDevice);
459
 
460
        midiDeviceRouting.addMidiWriteDevice(miosTerminalDevice);
461
    }
462
 
463
    public void connectDevices() {
464
 
639 adamjking 465
        setMidiThruOutPort(midiThruOutPort);
641 adamjking 466
 
628 adamjking 467
        midiDeviceRouting.connectDevices(miosStudioInPort,
468
                midiInPortMonitorDevice);
469
 
470
        midiDeviceRouting.connectDevices(miosStudioOutPort,
471
                midiOutPortMonitorDevice);
472
 
473
        /*
474
         * midiDeviceRouting.connectDevices(inVirtualMidiPortDevice,
475
         * inDumpReceiverDevice);
476
         * midiDeviceRouting.connectDevices(outVirtualMidiPortDevice,
477
         * outDumpReceiverDevice);
478
         */
641 adamjking 479
 
628 adamjking 480
        midiDeviceRouting.connectDevices(miosStudioInPort,
481
                midiKeyboardControllerDevice);
482
        midiDeviceRouting.connectDevices(midiKeyboardControllerDevice,
483
                miosStudioOutPort);
484
        /*
485
         * Iterator it = sysexSendReceiveDeviceManager
486
         * .getSysexSendReceiveDevices().iterator(); while (it.hasNext()) {
487
         * SysexSendReceiveDevice ssrt = (SysexSendReceiveDevice) it.next();
488
         * midiDeviceRouting.connectDevices(miosStudioInPort, ssrt);
489
         * midiDeviceRouting.connectDevices(ssrt, miosStudioOutPort); }
490
         */
491
        Iterator it = hexFileUploadDeviceManager.getHexFileUploadDevices()
492
                .iterator();
493
        while (it.hasNext()) {
494
            HexFileUploadDevice hutd = (HexFileUploadDevice) it.next();
495
            midiDeviceRouting.connectDevices(miosStudioInPort, hutd);
496
            midiDeviceRouting.connectDevices(hutd, miosStudioOutPort);
497
        }
498
        /*
499
         * midiDeviceRouting.connectDevices(miosStudioInPort,
500
         * memoryReadWriteDevice);
501
         * midiDeviceRouting.connectDevices(memoryReadWriteDevice,
502
         * miosStudioOutPort);
503
         */
504
 
505
        midiDeviceRouting.connectDevices(lcdMessageDevice, miosStudioOutPort);
506
        midiDeviceRouting.connectDevices(miosStudioInPort, debugFunctionDevice);
507
        midiDeviceRouting
508
                .connectDevices(debugFunctionDevice, miosStudioOutPort);
509
 
510
        midiDeviceRouting.connectDevices(miosStudioInPort, miosTerminalDevice);
511
    }
512
 
513
    public void update(Observable observable, Object object) {
723 adamjking 514
 
628 adamjking 515
        if (observable == hexFileUploadDeviceManager) {
516
 
693 adamjking 517
            HexFileUploadDevice hexFileUploadDevice = (HexFileUploadDevice) object;
628 adamjking 518
 
519
            if (hexFileUploadDeviceManager.getHexFileUploadDevices().contains(
693 adamjking 520
                    hexFileUploadDevice)) {
628 adamjking 521
 
693 adamjking 522
                midiDeviceRouting.connectDevices(miosStudioInPort,
523
                        hexFileUploadDevice);
524
                midiDeviceRouting.connectDevices(hexFileUploadDevice,
525
                        miosStudioOutPort);
628 adamjking 526
            } else {
693 adamjking 527
                midiDeviceRouting.disconnectDevice(hexFileUploadDevice);
641 adamjking 528
            }
628 adamjking 529
 
641 adamjking 530
            setRouteIndividualDevices(routeIndividualDevices);
531
        }
532
 
533
        if (observable == midiDeviceManager) {
534
 
535
            MidiDevice midiDevice = (MidiDevice) object;
536
 
739 adamjking 537
            if (!midiDeviceManager.getSelectedMidiReadDevices().values()
538
                    .contains(midiDevice)
642 adamjking 539
                    && !midiDeviceManager.getSelectedMidiWriteDevices()
739 adamjking 540
                            .values().contains(midiDevice)) {
641 adamjking 541
                midiDeviceRouting.disconnectDevice(midiDevice);
628 adamjking 542
            }
641 adamjking 543
 
544
            setRouteIndividualDevices(routeIndividualDevices);
628 adamjking 545
        }
546
 
641 adamjking 547
        if (observable == midiFilterManager) {
628 adamjking 548
 
641 adamjking 549
            MidiDevice midiFilter = (MidiDevice) object;
628 adamjking 550
 
641 adamjking 551
            if (!midiFilterManager.getMidiFilterDevices().contains(midiFilter)) {
552
                midiDeviceRouting.disconnectDevice(midiFilter);
628 adamjking 553
            }
641 adamjking 554
 
555
            setRouteIndividualDevices(routeIndividualDevices);
628 adamjking 556
        }
557
    }
558
}