Subversion Repositories svn.mios

Rev

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

Rev Author Line No. Line
213 tk 1
/*
2
 * @(#)SIDV2librarian.java  beta1   2008/01/21
3
 *
4
 * Copyright (C) 2008    Rutger Vlek (rutgervlek@hotmail.com)
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.SIDV2librarian;
22
 
628 adamjking 23
import javax.sound.midi.MidiDevice;
24
 
213 tk 25
import org.midibox.midi.MidiDeviceRouting;
628 adamjking 26
import org.midibox.sidlibr.SIDLibController;
213 tk 27
import org.midibox.sidlibr.SysExControllerDevice;
28
 
628 adamjking 29
public class SIDV2librarian {
213 tk 30
 
31
    private MidiDeviceRouting midiDeviceRouting;
32
    private SysExControllerDevice sysexControllerDevice;
33
    private SIDLibController sidLibController;
34
 
628 adamjking 35
    private MidiDevice localMidiDevice;
36
    private MidiDevice inputMidiDevice;
37
    private MidiDevice outputMidiDevice;
38
 
213 tk 39
    public SIDV2librarian() {
628 adamjking 40
 
41
        this(new MidiDeviceRouting());
213 tk 42
    }
628 adamjking 43
 
44
    public SIDV2librarian(MidiDeviceRouting midiDeviceRouting) {
45
 
46
        this.midiDeviceRouting = midiDeviceRouting;
47
 
48
        sysexControllerDevice = new SysExControllerDevice(
49
                "MidiBox SID V2 Editor");
50
 
51
        this.localMidiDevice = sysexControllerDevice;
52
 
53
        sidLibController = new SIDLibController(sysexControllerDevice
54
                .getSysExController());
55
 
56
        if (midiDeviceRouting != null) {
57
            midiDeviceRouting.getMidiDeviceManager().rescanDevices();
58
 
59
            midiDeviceRouting.reorder();
60
        }
61
    }
62
 
213 tk 63
    public MidiDeviceRouting getMidiDeviceRouting() {
64
        return midiDeviceRouting;
65
    }
66
 
67
    public SysExControllerDevice getSysExControllerDevice() {
68
        return sysexControllerDevice;
69
    }
628 adamjking 70
 
213 tk 71
    public SIDLibController getSIDLibController() {
72
        return sidLibController;
73
    }
628 adamjking 74
 
75
    public int getInputDeviceIndex() {
76
        return midiDeviceRouting.getMidiReadDevices().indexOf(inputMidiDevice);
213 tk 77
    }
628 adamjking 78
 
79
    public int getOutputDeviceIndex() {
80
        return midiDeviceRouting.getMidiWriteDevices()
81
                .indexOf(outputMidiDevice);
82
    }
83
 
84
    public MidiDevice getLocalMidiDevice() {
85
        return localMidiDevice;
86
    }
87
 
88
    public MidiDevice getInputMidiDevice() {
89
        return inputMidiDevice;
90
    }
91
 
92
    public MidiDevice getOutputMidiDevice() {
93
        return outputMidiDevice;
94
    }
95
 
96
    public void reconnectAllDevices() { // This function is a workaround for the
97
        // SysEx (string length doesn't reset)
98
        // bug in the javax.sound.midi class
99
 
100
        if (midiDeviceRouting != null) {
101
 
102
            System.out.println("Now reconnecting!");
103
            midiDeviceRouting.disconnectDevices(inputMidiDevice,
104
                    localMidiDevice);
105
            System.out.println("Disconnection of "
106
                    + inputMidiDevice.getDeviceInfo() + " succesfull");
107
            midiDeviceRouting.disconnectDevices(localMidiDevice,
108
                    outputMidiDevice);
109
            System.out.println("Disconnection of "
110
                    + outputMidiDevice.getDeviceInfo() + " succesfull");
111
 
112
            midiDeviceRouting.connectDevices(inputMidiDevice, localMidiDevice);
113
            System.out.println("Reconnection of "
114
                    + inputMidiDevice.getDeviceInfo() + " succesfull");
115
            midiDeviceRouting.connectDevices(localMidiDevice, outputMidiDevice);
116
            System.out.println("Reconnection of "
117
                    + outputMidiDevice.getDeviceInfo() + " succesfull");
118
        }
119
 
120
    }
121
 
122
    public int findInputDeviceHash(int hash) {
123
        int temp = -1;
124
        for (int i = 0; i < midiDeviceRouting.getMidiReadDevices().size(); i++) {
125
            if (((MidiDevice) midiDeviceRouting.getMidiReadDevices().elementAt(
126
                    i)).getDeviceInfo().toString().hashCode() == hash) {
127
                temp = i;
128
                break;
129
            }
130
        }
131
        return temp;
132
    }
133
 
134
    public int findOuputDeviceHash(int hash) {
135
        int temp = -1;
136
        for (int i = 0; i < midiDeviceRouting.getMidiWriteDevices().size(); i++) {
137
            if (((MidiDevice) midiDeviceRouting.getMidiWriteDevices()
138
                    .elementAt(i)).getDeviceInfo().toString().hashCode() == hash) {
139
                temp = i;
140
                break;
141
            }
142
        }
143
        return temp;
144
    }
145
 
146
    public int getInputDeviceHash() {
147
        return inputMidiDevice.getDeviceInfo().toString().hashCode();
148
    }
149
 
150
    public int getOutputDeviceHash() {
151
        return outputMidiDevice.getDeviceInfo().toString().hashCode();
152
    }
153
 
293 rutgerv 154
    public void closeMidi() {
628 adamjking 155
        midiDeviceRouting.disconnectDevices(inputMidiDevice, localMidiDevice);
156
        midiDeviceRouting.disconnectDevices(localMidiDevice, outputMidiDevice);
293 rutgerv 157
    }
628 adamjking 158
 
159
    public void setInputDeviceIndex(int index) {
160
        if (index != getInputDeviceIndex()) {
161
            midiDeviceRouting.disconnectDevices(inputMidiDevice,
162
                    localMidiDevice);
163
            inputMidiDevice = (MidiDevice) midiDeviceRouting
164
                    .getMidiReadDevices().elementAt(index);
165
            midiDeviceRouting.connectDevices(inputMidiDevice, localMidiDevice);
166
            System.out.println("Connection of "
167
                    + inputMidiDevice.getDeviceInfo() + " succesfull");
168
        }
213 tk 169
    }
628 adamjking 170
 
171
    public void setOutputDeviceIndex(int index) {
172
        if (index != getOutputDeviceIndex()) {
173
            midiDeviceRouting.disconnectDevices(localMidiDevice,
174
                    outputMidiDevice);
175
            outputMidiDevice = (MidiDevice) midiDeviceRouting
176
                    .getMidiWriteDevices().elementAt(index);
177
            midiDeviceRouting.connectDevices(localMidiDevice, outputMidiDevice);
178
            System.out.println("Connection of "
179
                    + outputMidiDevice.getDeviceInfo() + " succesfull");
180
        }
181
    }
213 tk 182
}