-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.java
More file actions
50 lines (45 loc) · 1.7 KB
/
Server.java
File metadata and controls
50 lines (45 loc) · 1.7 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
import java.io.*;
import java.util.*;
import java.nio.*;
import java.net.*;
import javax.sound.midi.*;
/**
* Implements a server that can receive and produce audio
* for MIDI events received over the computer network.
*/
public class Server {
public static final int PORT = 4567;
private static final int MAX_CAPACITY = 256;
public static void main(String[] args)
throws javax.sound.midi.InvalidMidiDataException, MidiUnavailableException, IOException {
Receiver _receiver = MidiSystem.getReceiver();
HashMap<String, Integer> channelMapping = new HashMap<>();
DatagramSocket socket = new DatagramSocket(PORT);
System.out.println("Receiving messages...");
while (true) {
// Receive message
byte[] buffer = new byte[MAX_CAPACITY];
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
socket.receive(packet);
String addressAndPort = "" + packet.getAddress() + ":" + packet.getPort();
// Extract request
ByteBuffer byteBuffer = ByteBuffer.wrap(buffer);
int command = byteBuffer.getInt();
int channelIgnored = byteBuffer.getInt(); // Throw this away since we'll overwrite it anyhow
int data1 = byteBuffer.getInt();
int data2 = byteBuffer.getInt();
// Determine unique channel for this (address,port) tuple
int channel;
if (channelMapping.containsKey(addressAndPort)) {
channel = channelMapping.get(addressAndPort);
} else {
channel = channelMapping.size(); // Assign an unused channel
channelMapping.put(addressAndPort, channel);
}
// Execute MIDI event
ShortMessage message = new ShortMessage(command, channel, data1, data2);
_receiver.send(message, -1);
System.out.println("src=" + addressAndPort + " channel=" + channel);
}
}
}