-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetworkReceiver.java
More file actions
56 lines (50 loc) · 1.41 KB
/
NetworkReceiver.java
File metadata and controls
56 lines (50 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import javax.sound.midi.*;
import java.io.*;
import java.nio.*;
import java.net.*;
/**
* Implements a network-based MIDI receiver that
* can be accessed remotely.
*/
public class NetworkReceiver implements Receiver {
private DatagramSocket _socket;
private InetAddress _address;
/**
* Sets the remote address to the specified string.
* @param addressStr the remote address
*/
public void setAddress (String addressStr) throws UnknownHostException {
_address = InetAddress.getByName(addressStr);
}
public NetworkReceiver () throws SocketException {
_socket = new DatagramSocket();
}
@Override
/**
* Closes the receiver and its associated network socket.
*/
public void close () {
_socket.close();
}
@Override
/**
* Sends the MIDI message to a remote receiver over the network.
* @param midiMessage the message to send.
* @param the associated timestamp of the message.
*/
public void send (MidiMessage midiMessage, long timeStamp) {
ShortMessage message = (ShortMessage) midiMessage;
try {
int capacity = 4 * Integer.BYTES;
ByteBuffer b = ByteBuffer.allocate(capacity);
b.putInt(message.getCommand());
b.putInt(message.getChannel());
b.putInt(message.getData1());
b.putInt(message.getData2());
DatagramPacket packet = new DatagramPacket(b.array(), capacity, _address, Server.PORT);
_socket.send(packet);
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}