Subversion Repositories svn.mios

Rev

Rev 294 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 294 Rev 628
Line 18... Line 18...
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.apps.SIDV2librarian;
21
package org.midibox.apps.SIDV2librarian;
22
22
23
import java.util.Observable;
-
 
24
import java.util.prefs.Preferences;
-
 
25
import java.util.Observer;
-
 
26
import javax.swing.JOptionPane;
23
import javax.sound.midi.MidiDevice;
-
 
24
27
import org.midibox.midi.MidiDeviceRouting;
25
import org.midibox.midi.MidiDeviceRouting;
28
import org.midibox.midi.gui.MidiDeviceRoutingGUI;
-
 
29
import org.midibox.sidlibr.SysExControllerDevice;
-
 
30
import org.midibox.sidlibr.SIDLibController;
26
import org.midibox.sidlibr.SIDLibController;
31
-
 
-
 
27
import org.midibox.sidlibr.SysExControllerDevice;
32
28
33
public class SIDV2librarian{
29
public class SIDV2librarian {
34
    private Preferences prefs = Preferences.userNodeForPackage(this.getClass());
-
 
-
 
30
35
    private MidiDeviceRouting midiDeviceRouting;
31
    private MidiDeviceRouting midiDeviceRouting;
36
    private SysExControllerDevice sysexControllerDevice;
32
    private SysExControllerDevice sysexControllerDevice;
37
    private SIDLibController sidLibController;
33
    private SIDLibController sidLibController;
-
 
34
38
    private MidiDeviceRoutingGUI midiDeviceRoutingGUI;
35
    private MidiDevice localMidiDevice;
39
    private boolean allowPrefs = false;
36
    private MidiDevice inputMidiDevice;
40
    public boolean showMIDIconfig = false;
37
    private MidiDevice outputMidiDevice;
41
38
42
    public SIDV2librarian() {
39
    public SIDV2librarian() {
-
 
40
43
        sysexControllerDevice = new SysExControllerDevice("MidiBox SID V2 Editor");
41
        this(new MidiDeviceRouting());
-
 
42
    }
-
 
43
44
        midiDeviceRouting = new MidiDeviceRouting(sysexControllerDevice);  
44
    public SIDV2librarian(MidiDeviceRouting midiDeviceRouting) {
-
 
45
45
        midiDeviceRoutingGUI = new MidiDeviceRoutingGUI(midiDeviceRouting);        
46
        this.midiDeviceRouting = midiDeviceRouting;
-
 
47
-
 
48
        sysexControllerDevice = new SysExControllerDevice(
-
 
49
                "MidiBox SID V2 Editor");
-
 
50
-
 
51
        this.localMidiDevice = sysexControllerDevice;
-
 
52
46
        sidLibController = new SIDLibController(sysexControllerDevice.getSysExController());
53
        sidLibController = new SIDLibController(sysexControllerDevice
47
        recallConnections();
54
                .getSysExController());
-
 
55
-
 
56
        if (midiDeviceRouting != null) {
-
 
57
            midiDeviceRouting.getMidiDeviceManager().rescanDevices();
-
 
58
48
        allowPrefs = true;
59
            midiDeviceRouting.reorder();
-
 
60
        }
49
    }
61
    }
50
   
62
51
    public MidiDeviceRouting getMidiDeviceRouting() {
63
    public MidiDeviceRouting getMidiDeviceRouting() {
52
        return midiDeviceRouting;
64
        return midiDeviceRouting;
53
    }
-
 
54
   
-
 
55
    public MidiDeviceRoutingGUI getMidiDeviceRoutingGUI() {
-
 
56
        return midiDeviceRoutingGUI;
-
 
57
    }
65
    }
58
66
59
    public SysExControllerDevice getSysExControllerDevice() {
67
    public SysExControllerDevice getSysExControllerDevice() {
60
        return sysexControllerDevice;
68
        return sysexControllerDevice;
61
    }
69
    }
62
   
70
63
    public SIDLibController getSIDLibController() {
71
    public SIDLibController getSIDLibController() {
64
        return sidLibController;
72
        return sidLibController;
65
    }
73
    }
66
   
74
67
    private void recallConnections() {
75
    public int getInputDeviceIndex() {
68
        int input = prefs.getInt("INPUT_DEVICE", 0);
-
 
69
        int output = prefs.getInt("OUTPUT_DEVICE", 0);
-
 
70
        int in = midiDeviceRouting.findInputDeviceHash(input);
76
        return midiDeviceRouting.getMidiReadDevices().indexOf(inputMidiDevice);
71
        int out = midiDeviceRouting.findOuputDeviceHash(output);
-
 
-
 
77
    }
72
       
78
73
        if (in!=-1 && out!=-1) {
79
    public int getOutputDeviceIndex() {
74
            midiDeviceRouting.setInputDevice(in);
80
        return midiDeviceRouting.getMidiWriteDevices()
75
            midiDeviceRouting.setOutputDevice(out);
81
                .indexOf(outputMidiDevice);
76
        } else {
-
 
77
            showMIDIconfig = true;
-
 
78
            JOptionPane.showMessageDialog(null,"Your MIDI configuration has changed, please check connections!","MIDI configuration changed!",JOptionPane.OK_OPTION);
-
 
79
        }      
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();
80
    }
152
    }
81
   
153
82
    public void closeMidi() {
154
    public void closeMidi() {
-
 
155
        midiDeviceRouting.disconnectDevices(inputMidiDevice, localMidiDevice);
83
        midiDeviceRouting.closeMidi();
156
        midiDeviceRouting.disconnectDevices(localMidiDevice, outputMidiDevice);
84
    }
157
    }
85
       
158
86
    public void storeConnections() {
159
    public void setInputDeviceIndex(int index) {
-
 
160
        if (index != getInputDeviceIndex()) {
-
 
161
            midiDeviceRouting.disconnectDevices(inputMidiDevice,
-
 
162
                    localMidiDevice);
87
        int input = midiDeviceRouting.getInputDeviceHash();
163
            inputMidiDevice = (MidiDevice) midiDeviceRouting
-
 
164
                    .getMidiReadDevices().elementAt(index);
-
 
165
            midiDeviceRouting.connectDevices(inputMidiDevice, localMidiDevice);
-
 
166
            System.out.println("Connection of "
-
 
167
                    + inputMidiDevice.getDeviceInfo() + " succesfull");
-
 
168
        }
-
 
169
    }
-
 
170
-
 
171
    public void setOutputDeviceIndex(int index) {
-
 
172
        if (index != getOutputDeviceIndex()) {
-
 
173
            midiDeviceRouting.disconnectDevices(localMidiDevice,
-
 
174
                    outputMidiDevice);
88
        int output = midiDeviceRouting.getOutputDeviceHash();
175
            outputMidiDevice = (MidiDevice) midiDeviceRouting
89
        prefs.putInt("INPUT_DEVICE", input);
176
                    .getMidiWriteDevices().elementAt(index);
-
 
177
            midiDeviceRouting.connectDevices(localMidiDevice, outputMidiDevice);
90
        prefs.putInt("OUTPUT_DEVICE", output);
178
            System.out.println("Connection of "
-
 
179
                    + outputMidiDevice.getDeviceInfo() + " succesfull");
-
 
180
        }
91
    }
181
    }
92
}
182
}