The Orchestra project or “Orca” for short is a computer networking project which when realized will be the networking of several computers to form an orchestra which will play some form of a musical score. This idea here is that one of the computers will function as the conductor of the orchestra, and the other computers will function as the individual instrument players in some sort of server, client relationship. The Java language was ultimately chosen to implement this project for two reasons. First, Java is a network-centric language and has rich support for socket programming. Second, Java has rich support for MIDI.
Got to the ORCA project page for details: http://lab46.corning-cc.edu/notes/sysnet/spring2011?s[]=orca .
Orca | | V Client<--------------------Networking<--------Java---------------------->Midi (Connect-Input Stream) | | | Socket Ports | | | V | V Server | File Format (Listen-Output Stream) | (Structure of midi) Working Example | | V Programming JDK | | | V Sources (Documentation) Drawn by John Rine
Python Client/Server
by John Rine
Early in the semester, the class was challanged to find some example code to perform socket communications. I volunteered to find Python examples. The client/server code listed below is from a tutorial. The tutorial is located at: http://heather.cs.ucdavis.edu/nmahoff/python/pynet.pdf .
Example Python TCP client code.
# simple illustration client/server pair; client program sends a string # to server, which echoes it back to the client (in multiple copies), # and the latter prints to the screen # this is the client import socket import sys # create a socket s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # connect to server host = sys.argv[1] # server address port = int(sys.argv[2]) # server port s.connect((host, port)) s.send(sys.argv[3]) # send test string # read echo i = 0 while(1): data = s.recv(1000000) # read up to 1000000 bytes i += 1 if (i < 5): # look only at the first part of the message print data if not data: # if end of data, leave loop break print 'received', len(data), 'bytes' # close the connection s.close()
Example Python TCP client code.
# simple illustration client/server pair; client program sends a string # to server, which echoes it back to the client (in multiple copies), # and the latter prints to the screen # this is the server import socket import sys # create a socket s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # associate the socket with a port host = '' # can leave this blank on the server side port = int(sys.argv[1]) s.bind((host, port)) # accept "call" from client s.listen(1) conn, addr = s.accept() print 'client is at', addr # read string from client (assumed here to be so short that one call to # recv() is enough), and make multiple copies (to show the need for the # "while" loop on the client side) data = conn.recv(1000000) data = 10000 * data # concatenate data with itself 999 times # wait for the go-ahead signal from the keyboard (to demonstrate that # recv() at the client will block until server sends) z = raw_input() # now send conn.send(data) # close the connection conn.close()
Server execution
C:\Python27>python tms.py 2000 client is at ('127.0.0.1', 52539)
Client execution
C:\Python27>python tmc.py localhost 2000 abc
Installing the Java SDK
Getting path to the Java SDK bin directory.
First search for the path to the Java SDK bin directory. The java compliler, javac.exe is in this directory so from the root directory, C:, search for this file. once found, record the path to the directory. Append this path to the Windows path statement (see below).
Adding path to Java JDK Bin Directory
Windows XP Operating System
Navigate to the Windows path as follows:
Control Panel→System Icon→Advanced Tab→Environment Variables Button→Path (Double-click item)
Add the directory path to Java bin directory to the end of the Windows path, in this case added the following path on to the end of the Windows path: ;C:\Program Files\Java\jdk1.7.0\bin . The semicolon is a separator between paths in the Windows path statement.
Windows Batch Files to Automate Compilation and Execution Of Java Programs
REM Written by John T. Rine REM 04-10-2011 REM javac MyFirstApp.java SET /P M=What program to compile? javac %M% pause
REM Written by John T. Rine REM 04-10-2011 REM java MyFirstApp SET /P M=What program to run? java %M% pause
Java Client/Server Plays Notes Across a Network
Java TCP Client
//Author John T. Rine import java.io.*; import java.net.*; class TCPClient { public static void main(String argv[]) throws Exception { String sentence; String modifiedSentence; while(true) { BufferedReader inFromUser = new BufferedReader( new InputStreamReader(System.in)); Socket clientSocket = new Socket("localhost", 4000); //new Socket("localhost", 4000); DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream()); BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); sentence = inFromUser.readLine(); outToServer.writeBytes(sentence + '\n'); modifiedSentence = inFromServer.readLine(); System.out.println("FROM SERVER: " + modifiedSentence); } //clientSocket.close(); } }
Java server code
//Author John Rine import java.io.*; import java.net.*; import javax.sound.midi.*; class TCPServerTwo { public static void main(String argv[]) throws Exception { String clientSentence = "go"; String capitalizedSentence; ServerSocket welcomeSocket = new ServerSocket(4000); //new ServerSocket(6789); while(!clientSentence.equals("stop")) { Socket connectionSocket = welcomeSocket.accept(); BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream())); DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream()); clientSentence = inFromClient.readLine(); System.out.println("Received: " + clientSentence); capitalizedSentence = clientSentence.toUpperCase() + '\n'; outToClient.writeBytes(capitalizedSentence); if (clientSentence.equals("play")) { try { Receiver rec = MidiSystem.getReceiver(); ShortMessage mmess = new ShortMessage(); mmess.setMessage(144, 0, 60, 93); rec.send(mmess, -1); Thread.sleep(1000); } catch(Exception e) { e.printStackTrace(); } } } // synth.close(); welcomeSocket.close(); } }
Java client execution.
C:\Users\John\Desktop\Spring 2011 School\CSIT 2320\Play a note across the networ k>REM java MyFirstApp C:\Users\John\Desktop\Spring 2011 School\CSIT 2320\Play a note across the networ k>SET /P M=What program to run? What program to run?TCPClient C:\Users\John\Desktop\Spring 2011 School\CSIT 2320\Play a note across the networ k>java TCPClient play FROM SERVER: PLAY play FROM SERVER: PLAY
This is an example of playing a single note by sending a MIDI message to a MIDI receiver.
//Author: John T. Rine //04-10-2011 import javax.sound.midi.*; public class simpleMidiNote { public static void main (String args[]) throws Exception { //http://download.oracle.com/javase/tutorial/sound/MIDI-messages.html ShortMessage myMsg = new ShortMessage(); // Start playing the note Middle C (60), // moderately loud (velocity = 93). myMsg.setMessage(ShortMessage.NOTE_ON, 0, 60, 93); long timeStamp = -1; Receiver rcvr = MidiSystem.getReceiver(); rcvr.send(myMsg, timeStamp); //http://download.oracle.com/javase/tutorial/essential/concurrency/sleep.html //Pause for 4 seconds Thread.sleep(4000); } }
Reading the MIDI file header
by John Rine
Listed below is the code to read and display the header of a midi format file. The code is written in “long hand” (no methods) as it is in development. The data conversion sections could be placed into methods to make tthe code more compact. Remember, it is still under development, but it works.
//Author: John T. Rine //05-02-2011 import java.io.*; public class orca_Protocol { public static void main(String[] args) throws IOException { FileInputStream in = null; try { System.out.print("Enter filename: "); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String fileName = null; fileName = br.readLine(); in = new FileInputStream(fileName); int c; StringBuffer strContent = new StringBuffer(); int count = 0; while (count != 4) { c = in.read(); strContent.append((char)c); count++; } System.out.println(strContent); String temp = strContent.toString(); if(temp.equals("MThd")) System.out.println("Midi Header"); long dummy = 0; c = 0; int shift = 0; long result = 0; for (int i = 0; i != 4; i++) { c = in.read(); dummy = 0; dummy = (long) c; switch(i) { case 0: shift = 24; break; case 1: shift = 16; break; case 2: shift = 8; break; case 3: shift = 0; break; } dummy = dummy << shift; result = result | dummy; } System.out.println("Header Length = " + result); c = 0; shift = 0; int resultt = 0; for (int i = 0; i != 2; i++) { c = in.read(); switch(i) { case 0: shift = 8; break; case 1: shift = 0; break; } c = c << shift; resultt = resultt | c; } String fileType; switch(c) { case 0: fileType = "single track file format"; break; case 1: fileType = "multiple track file format"; break; case 2: fileType = "multiple song file format (i.e., a series of type 0 files)"; break; default: fileType = "Error"; } System.out.println("Midi File Type = " + c + " " + "File Type: " + fileType); c = 0; shift = 0; resultt = 0; for (int i = 0; i != 2; i++) { c = in.read(); switch(i) { case 0: shift = 8; break; case 1: shift = 0; break; } c = c << shift; resultt = resultt | c; } System.out.println("Number of track chunks that follow the header chunk = " + c); c = 0; shift = 0; resultt = 0; for (int i = 0; i != 2; i++) { c = in.read(); switch(i) { case 0: shift = 8; break; case 1: shift = 0; break; } c = c << shift; resultt = resultt | c; } System.out.println("Division = " + c); System.out.println("Unit of time for delta timing."); System.out.println("If the value is positive, then it represents the units per beat."); System.out.println("For example, +96 would mean 96 ticks per beat."); System.out.println("If the value is negative, delta times are in SMPTE compatible units."); } finally { if (in != null) { in.close(); } } } }
Execution of the MIDI file reading application. At present, it can only read and display MIDI header data.
C:\Users\John\Desktop\Spring 2011 School\CSIT 2320\Assignment_Due_05-02-2011>REM java MyFirstApp C:\Users\John\Desktop\Spring 2011 School\CSIT 2320\Assignment_Due_05-02-2011>SET /P M=What program to run? What program to run?orca_Protocol C:\Users\John\Desktop\Spring 2011 School\CSIT 2320\Assignment_Due_05-02-2011>jav a orca_Protocol Enter filename: the_white_stripes-icky_thump.mid MThd Midi Header Header Length = 6 Midi File Type = 1 File Type: multiple track file format Number of track chunks that follow the header chunk = 4 Division = 224 Unit of time for delta timing. If the value is positive, then it represents the units per beat. For example, +96 would mean 96 ticks per beat. If the value is negative, delta times are in SMPTE compatible units. C:\Users\John\Desktop\Spring 2011 School\CSIT 2320\Assignment_Due_05-02-2011>pau se Press any key to continue . . .