Subversion Repositories svn.mios

Rev

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

Rev 846 Rev 850
Line 20... Line 20...
20
20
21
package org.midibox.midi;
21
package org.midibox.midi;
22
22
23
import java.io.File;
23
import java.io.File;
24
import java.io.FileOutputStream;
24
import java.io.FileOutputStream;
25
import java.io.IOException;
-
 
26
import java.io.OutputStream;
25
import java.io.OutputStream;
27
import java.util.LinkedList;
26
import java.util.LinkedList;
28
import java.util.Observable;
27
import java.util.Observable;
29
28
30
import javax.sound.midi.InvalidMidiDataException;
29
import javax.sound.midi.InvalidMidiDataException;
Line 33... Line 32...
33
import javax.sound.midi.SysexMessage;
32
import javax.sound.midi.SysexMessage;
34
33
35
public class SysexSendReceive extends Observable implements Receiver {
34
public class SysexSendReceive extends Observable implements Receiver {
36
35
37
    public final static Object WORKER = new Object();
36
    public final static Object WORKER = new Object();
-
 
37
-
 
38
    public final static Object SEND_SYSEX_FILE = new Object();
-
 
39
-
 
40
    public final static Object RECEIVE_SYSEX_FILE = new Object();
-
 
41
-
 
42
    public final static Object SEND_DELAY_TIME = new Object();
-
 
43
-
 
44
    public final static Object RECEIVED_BYTES = new Object();
38
45
39
    protected Receiver receiver;
46
    protected Receiver receiver;
40
47
41
    protected File sendFile;
48
    protected File sendFile;
42
    protected long sendFileLastModified;
49
    protected long sendFileLastModified;
43
50
44
    protected File receiveFile;
51
    protected File receiveFile;
45
52
46
    protected SysexFile sysexFile;
53
    protected SysexFile sysexFile;
47
-
 
48
    protected SysexSendReceiveWorkerTask sysexSendReceiveWorkerTask;
-
 
49
54
50
    protected int sendBufferSize;
55
    protected int sendBufferSize;
51
56
52
    protected int sendDelayTime;
57
    protected int sendDelayTime;
53
58
Line 59... Line 64...
59
    protected boolean receiveStopped = true;
64
    protected boolean receiveStopped = true;
60
65
61
    public SysexSendReceive(Receiver receiver) {
66
    public SysexSendReceive(Receiver receiver) {
62
        super();
67
        super();
63
        this.receiver = receiver;
68
        this.receiver = receiver;
64
        sendBufferSize = 100000; // TK: shouldn't be changed by user since Java doesn't allow to split SysEx streams properly
69
        sendBufferSize = 100000; // TK: shouldn't be changed by user since Java
-
 
70
                                    // doesn't allow to split SysEx streams
-
 
71
                                    // properly
65
        sendDelayTime = 750; // delay between F0
72
        sendDelayTime = 750; // delay between F0
66
        receivedBytes = new LinkedList();
73
        receivedBytes = new LinkedList();
67
    }
74
    }
68
75
69
    public File getSendFile() {
76
    public File getSendFile() {
Line 73... Line 80...
73
    public boolean sendFileChanged() {
80
    public boolean sendFileChanged() {
74
        return sendFile.lastModified() != sendFileLastModified;
81
        return sendFile.lastModified() != sendFileLastModified;
75
    }
82
    }
76
83
77
    public void setSendFile(File file) {
84
    public void setSendFile(File file) {
-
 
85
78
        this.sendFile = file;
86
        this.sendFile = file;
-
 
87
79
        sendFileLastModified = sendFile.lastModified();
88
        sendFileLastModified = sendFile.lastModified();
80
89
81
        readSysexFile();
90
        readSysexFile();
82
91
83
        setChanged();
92
        setChanged();
84
        notifyObservers(sendFile);
93
        notifyObservers(SEND_SYSEX_FILE);
85
        clearChanged();
94
        clearChanged();
86
    }
95
    }
87
96
88
    public void readSysexFile() {
97
    public void readSysexFile() {
89
        sysexFile = new SysexFile();
98
        sysexFile = new SysexFile();
90
        if (!sysexFile.read(sendFile.getPath())) {
99
        if (!sysexFile.read(sendFile.getPath())) {
91
            // TODO
100
            // TODO
92
        }
101
        }
93
    }
102
    }
94
-
 
95
103
96
    public File getReceiveFile() {
104
    public File getReceiveFile() {
97
        return receiveFile;
105
        return receiveFile;
98
    }
106
    }
99
107
100
    public void setReceiveFile(File file) {
108
    public void setReceiveFile(File file) {
-
 
109
101
        this.receiveFile = file;
110
        this.receiveFile = file;
102
111
103
        setChanged();
112
        setChanged();
104
        notifyObservers(receiveFile);
113
        notifyObservers(RECEIVE_SYSEX_FILE);
105
        clearChanged();
114
        clearChanged();
106
    }
115
    }
107
-
 
108
-
 
109
-
 
110
116
111
    public SysexFile getSysexFile() {
117
    public SysexFile getSysexFile() {
112
        return sysexFile;
118
        return sysexFile;
113
    }
119
    }
114
120
Line 119... Line 125...
119
    public int getSendDelayTime() {
125
    public int getSendDelayTime() {
120
        return sendDelayTime;
126
        return sendDelayTime;
121
    }
127
    }
122
128
123
    public void setSendDelayTime(int sendDelayTime) {
129
    public void setSendDelayTime(int sendDelayTime) {
-
 
130
124
        this.sendDelayTime = sendDelayTime;
131
        this.sendDelayTime = sendDelayTime;
-
 
132
-
 
133
        setChanged();
-
 
134
        notifyObservers(SEND_DELAY_TIME);
-
 
135
        clearChanged();
125
    }
136
    }
126
-
 
127
-
 
128
137
129
    public LinkedList getReceivedBytes() {
138
    public LinkedList getReceivedBytes() {
130
        return receivedBytes;
139
        return receivedBytes;
131
    }
140
    }
-
 
141
132
    public void clearReceivedBytes() {
142
    public void clearReceivedBytes() {
133
        receivedBytes.clear();
143
        receivedBytes.clear();
134
    }
144
    }
135
-
 
136
145
137
    public boolean writeSysexFile(File file, byte[] data, int dataLen) {
146
    public boolean writeSysexFile(File file, byte[] data, int dataLen) {
138
        // could also be located in SysexFile.java?
147
        // could also be located in SysexFile.java?
139
148
140
        try {
149
        try {
Line 145... Line 154...
145
            return false;
154
            return false;
146
        }
155
        }
147
156
148
        return true;
157
        return true;
149
    }
158
    }
150
-
 
151
159
152
    public boolean isSendDone() {
160
    public boolean isSendDone() {
153
        return sendDone;
161
        return sendDone;
154
    }
162
    }
155
163
Line 171... Line 179...
171
    }
179
    }
172
180
173
    public void setReceiveStopped(boolean receiveStopped ) {
181
    public void setReceiveStopped(boolean receiveStopped) {
174
        this.receiveStopped = receiveStopped;
182
        this.receiveStopped = receiveStopped;
175
    }
183
    }
176
-
 
177
184
178
    public void close() {
185
    public void close() {
179
186
180
    }
187
    }
181
188
182
    public void setReceiver(Receiver receiver) {
189
    public void setReceiver(Receiver receiver) {
183
        this.receiver = receiver;
190
        this.receiver = receiver;
184
    }
191
    }
185
-
 
186
    public class SysexSendReceiveWorkerTask extends Thread {
-
 
187
-
 
188
        public void run() {
-
 
189
-
 
190
        }
-
 
191
    }
-
 
192
-
 
193
192
194
    public void send(MidiMessage message, long timestamp) {
193
    public void send(MidiMessage message, long timestamp) {
195
        if (!isReceiveStopped()) {
194
        if (!isReceiveStopped()) {
196
            if (message instanceof SysexMessage) {
195
            if (message instanceof SysexMessage) {
197
                receivedBytes.add(((SysexMessage) message).getData());
196
                receivedBytes.add(((SysexMessage) message).getData());
198
197
199
                setChanged();
198
                setChanged();
200
                notifyObservers(receivedBytes);
199
                notifyObservers(RECEIVED_BYTES);
201
                clearChanged();
200
                clearChanged();
202
            }
201
            }
203
        }
202
        }
204
    }
203
    }
205
-
 
206
204
207
    /////////////////////////////////////////////////////////////////////////////////
205
    // ///////////////////////////////////////////////////////////////////////////////
208
    // SysEx File Sender Functions
206
    // SysEx File Sender Functions
209
    /////////////////////////////////////////////////////////////////////////////////
207
    // ///////////////////////////////////////////////////////////////////////////////
210
-
 
211
208
212
    public void startSend(byte[] sysexData, int sysexDataLen) {
209
    public void startSend(byte[] sysexData, int sysexDataLen) {
213
        int offset = 0;
210
        int offset = 0;
214
211
215
        sendCancelled = false;
212
        sendCancelled = false;
Line 224... Line 221...
224
                // update GUI
221
                // update GUI
225
                setChanged();
222
                setChanged();
226
                notifyObservers(WORKER);
223
                notifyObservers(WORKER);
227
                clearChanged();
224
                clearChanged();
228
225
229
                for(i=1; i<sendBufferSize && (i+offset)<sysexDataLen && sysexData[i+offset] != (byte)0xf0; ++i) {
226
                for (i = 1; i < sendBufferSize && (i + offset) < sysexDataLen
-
 
227
                        && sysexData[i + offset] != (byte) 0xf0; ++i) {
230
                    ++sendLen;
228
                    ++sendLen;
231
                }
229
                }
232
230
233
                if( sendLen == 0 ) {
231
                if (sendLen == 0) {
234
                    sendDone = true;
232
                    sendDone = true;