Subversion Repositories svn.mios

Rev

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

Rev Author Line No. Line
684 adamjking 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.sidv2editor;
22
 
23
import javax.sound.midi.MidiDevice;
24
 
25
import org.midibox.midi.MidiDeviceManager;
26
import org.midibox.midi.MidiDeviceRouting;
27
import org.midibox.sidlibr.SIDLibController;
28
import org.midibox.sidlibr.SysExControllerDevice;
29
 
30
public class SIDV2Editor {
31
 
32
    private MidiDeviceRouting midiDeviceRouting;
33
    private SysExControllerDevice sysexControllerDevice;
34
    private SIDLibController sidLibController;
35
 
36
    private MidiDevice localMidiDevice;
37
    private MidiDevice inputMidiDevice;
38
    private MidiDevice outputMidiDevice;
39
 
40
    private MidiDeviceManager midiDeviceManager;
41
 
42
    public SIDV2Editor() {
43
 
44
        this(new MidiDeviceManager(), new MidiDeviceRouting());
45
    }
46
 
47
    public SIDV2Editor(MidiDeviceManager midiDeviceManager,
48
            MidiDeviceRouting midiDeviceRouting) {
49
 
50
        this.midiDeviceManager = midiDeviceManager;
51
 
52
        this.midiDeviceRouting = midiDeviceRouting;
53
 
54
        sysexControllerDevice = new SysExControllerDevice(
55
                "MidiBox SID V2 Editor");
56
 
57
        this.localMidiDevice = sysexControllerDevice;
58
 
59
        sidLibController = new SIDLibController(sysexControllerDevice
60
                .getSysExController());
61
 
62
        if (midiDeviceRouting != null) {
63
            midiDeviceManager.rescanDevices();
64
            midiDeviceRouting.addMidiReadDevices(midiDeviceManager
739 adamjking 65
                    .getSelectedMidiReadDevices().values());
684 adamjking 66
            midiDeviceRouting.addMidiWriteDevices(midiDeviceManager
739 adamjking 67
                    .getSelectedMidiWriteDevices().values());
684 adamjking 68
        }
69
    }
70
 
71
    public MidiDeviceManager getMidiDeviceManager() {
72
        return midiDeviceManager;
73
    }
74
 
75
    public MidiDeviceRouting getMidiDeviceRouting() {
76
        return midiDeviceRouting;
77
    }
78
 
79
    public SysExControllerDevice getSysExControllerDevice() {
80
        return sysexControllerDevice;
81
    }
82
 
83
    public SIDLibController getSIDLibController() {
84
        return sidLibController;
85
    }
86
 
87
    public int getInputDeviceIndex() {
88
        return midiDeviceRouting.getMidiReadDevices().indexOf(inputMidiDevice);
89
    }
90
 
91
    public int getOutputDeviceIndex() {
92
        return midiDeviceRouting.getMidiWriteDevices()
93
                .indexOf(outputMidiDevice);
94
    }
95
 
96
    public MidiDevice getLocalMidiDevice() {
97
        return localMidiDevice;
98
    }
99
 
100
    public MidiDevice getInputMidiDevice() {
101
        return inputMidiDevice;
102
    }
103
 
104
    public MidiDevice getOutputMidiDevice() {
105
        return outputMidiDevice;
106
    }
107
 
108
    public void reconnectAllDevices() { // This function is a workaround for the
109
        // SysEx (string length doesn't reset)
110
        // bug in the javax.sound.midi class
111
        if (midiDeviceRouting != null) {
738 adamjking 112
            midiDeviceRouting.disconnectDevices(inputMidiDevice,
113
                    localMidiDevice);
114
            midiDeviceRouting.disconnectDevices(localMidiDevice,
115
                    outputMidiDevice);
684 adamjking 116
            midiDeviceRouting.connectDevices(inputMidiDevice, localMidiDevice);
117
            midiDeviceRouting.connectDevices(localMidiDevice, outputMidiDevice);
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
 
154
    public void closeMidi() {
155
        midiDeviceRouting.disconnectDevices(inputMidiDevice, localMidiDevice);
156
        midiDeviceRouting.disconnectDevices(localMidiDevice, outputMidiDevice);
157
    }
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
        }
167
    }
168
 
169
    public void setOutputDeviceIndex(int index) {
170
        if (index != getOutputDeviceIndex()) {
171
            midiDeviceRouting.disconnectDevices(localMidiDevice,
172
                    outputMidiDevice);
173
            outputMidiDevice = (MidiDevice) midiDeviceRouting
174
                    .getMidiWriteDevices().elementAt(index);
175
            midiDeviceRouting.connectDevices(localMidiDevice, outputMidiDevice);
176
        }
177
    }
178
}