This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
user:jr018429:portfolio:orca [2011/09/11 17:58] – jr018429 | user:jr018429:portfolio:orca [2011/10/11 16:57] (current) – jr018429 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== ORCA (MIDI Networked Orchestra) ====== | ||
+ | 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:// | ||
+ | <file text diagram.txt> | ||
+ | |||
+ | Orca | ||
+ | | | ||
+ | | | ||
+ | V | ||
+ | Client< | ||
+ | (Connect-Input Stream) | ||
+ | Socket Ports | ||
+ | | ||
+ | Server | ||
+ | | ||
+ | | ||
+ | | | ||
+ | V | ||
+ | | ||
+ | | | ||
+ | | | ||
+ | | | ||
+ | V | ||
+ | | ||
+ | (Documentation) | ||
+ | | ||
+ | Drawn by John Rine | ||
+ | </ | ||
+ | |||
+ | |||
+ | ===client/ | ||
+ | **Python Client/ | ||
+ | \\ | ||
+ | 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/ | ||
+ | Example Python TCP client code.\\ | ||
+ | |||
+ | <file python client.py> | ||
+ | # simple illustration client/ | ||
+ | # 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, | ||
+ | |||
+ | # connect to server | ||
+ | host = sys.argv[1] # server address | ||
+ | port = int(sys.argv[2]) # server port | ||
+ | s.connect((host, | ||
+ | |||
+ | 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 ' | ||
+ | |||
+ | # close the connection | ||
+ | s.close() | ||
+ | </ | ||
+ | \\ | ||
+ | Example Python TCP client code. | ||
+ | <file python server.py> | ||
+ | # simple illustration client/ | ||
+ | # 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, | ||
+ | # associate the socket with a port | ||
+ | host = '' | ||
+ | port = int(sys.argv[1]) | ||
+ | s.bind((host, | ||
+ | # accept " | ||
+ | s.listen(1) | ||
+ | conn, addr = s.accept() | ||
+ | print ' | ||
+ | # 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 | ||
+ | # " | ||
+ | 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\\ | ||
+ | <cli> | ||
+ | C: | ||
+ | client is at (' | ||
+ | </ | ||
+ | Client execution\\ | ||
+ | <cli> | ||
+ | C: | ||
+ | </ | ||
+ | \\ | ||
+ | **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-> | ||
+ | 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** | ||
+ | <file bat compile.bat> | ||
+ | REM Written by John T. Rine | ||
+ | REM 04-10-2011 | ||
+ | REM javac MyFirstApp.java | ||
+ | SET /P M=What program to compile? | ||
+ | javac %M% | ||
+ | pause | ||
+ | </ | ||
+ | <file bat run.bat> | ||
+ | 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/ | ||
+ | \\ | ||
+ | Java TCP Client\\ | ||
+ | <file java TCPClient.java> | ||
+ | //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(" | ||
+ | DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream()); | ||
+ | BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); | ||
+ | sentence = inFromUser.readLine(); | ||
+ | outToServer.writeBytes(sentence + ' | ||
+ | modifiedSentence = inFromServer.readLine(); | ||
+ | System.out.println(" | ||
+ | } | ||
+ | // | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | \\ | ||
+ | Java server code\\ | ||
+ | <file java TCPServerTwo.java> | ||
+ | //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 = " | ||
+ | String capitalizedSentence; | ||
+ | ServerSocket welcomeSocket = new ServerSocket(4000); | ||
+ | |||
+ | while(!clientSentence.equals(" | ||
+ | { | ||
+ | Socket connectionSocket = welcomeSocket.accept(); | ||
+ | BufferedReader inFromClient = | ||
+ | new BufferedReader(new InputStreamReader(connectionSocket.getInputStream())); | ||
+ | DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream()); | ||
+ | clientSentence = inFromClient.readLine(); | ||
+ | System.out.println(" | ||
+ | capitalizedSentence = clientSentence.toUpperCase() + ' | ||
+ | outToClient.writeBytes(capitalizedSentence); | ||
+ | if (clientSentence.equals(" | ||
+ | { | ||
+ | try | ||
+ | { | ||
+ | Receiver rec = MidiSystem.getReceiver(); | ||
+ | ShortMessage mmess = new ShortMessage(); | ||
+ | mmess.setMessage(144, | ||
+ | rec.send(mmess, | ||
+ | Thread.sleep(1000); | ||
+ | } | ||
+ | catch(Exception e) | ||
+ | { | ||
+ | e.printStackTrace(); | ||
+ | } | ||
+ | |||
+ | } | ||
+ | } | ||
+ | // | ||
+ | welcomeSocket.close(); | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | \\ | ||
+ | Java client execution.\\ | ||
+ | <cli> | ||
+ | C: | ||
+ | k>REM java MyFirstApp | ||
+ | |||
+ | C: | ||
+ | k>SET /P M=What program to run? | ||
+ | What program to run? | ||
+ | |||
+ | C: | ||
+ | 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.\\ | ||
+ | <file java simpleMidiNote.java> | ||
+ | //Author: John T. Rine | ||
+ | // | ||
+ | |||
+ | import javax.sound.midi.*; | ||
+ | public class simpleMidiNote | ||
+ | { | ||
+ | |||
+ | public static void main (String args[]) throws Exception | ||
+ | { | ||
+ | // | ||
+ | ShortMessage myMsg = new ShortMessage(); | ||
+ | // Start playing the note Middle C (60), | ||
+ | // moderately loud (velocity = 93). | ||
+ | | ||
+ | long timeStamp = -1; | ||
+ | Receiver rcvr = MidiSystem.getReceiver(); | ||
+ | rcvr.send(myMsg, | ||
+ | |||
+ | // | ||
+ | //Pause for 4 seconds | ||
+ | Thread.sleep(4000); | ||
+ | |||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ==File Parsing== | ||
+ | \\ | ||
+ | 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, | ||
+ | <file java orca_Protocol.java> | ||
+ | //Author: John T. Rine | ||
+ | // | ||
+ | import java.io.*; | ||
+ | |||
+ | public class orca_Protocol | ||
+ | { | ||
+ | public static void main(String[] args) throws IOException | ||
+ | { | ||
+ | FileInputStream in = null; | ||
+ | try | ||
+ | { | ||
+ | System.out.print(" | ||
+ | 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(" | ||
+ | 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(" | ||
+ | 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 = " | ||
+ | break; | ||
+ | case 1: | ||
+ | fileType = " | ||
+ | break; | ||
+ | case 2: | ||
+ | fileType = " | ||
+ | break; | ||
+ | default: | ||
+ | fileType = " | ||
+ | |||
+ | } | ||
+ | System.out.println(" | ||
+ | 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(" | ||
+ | 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(" | ||
+ | System.out.println(" | ||
+ | System.out.println(" | ||
+ | System.out.println(" | ||
+ | System.out.println(" | ||
+ | } | ||
+ | finally | ||
+ | { | ||
+ | if (in != null) | ||
+ | { | ||
+ | in.close(); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
+ | } | ||
+ | </ | ||
+ | Execution of the MIDI file reading application. At present, it can only read and display MIDI header data. | ||
+ | |||
+ | <cli> | ||
+ | C: | ||
+ | java MyFirstApp | ||
+ | |||
+ | C: | ||
+ | /P M=What program to run? | ||
+ | What program to run? | ||
+ | |||
+ | C: | ||
+ | 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 | ||
+ | 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: | ||
+ | se | ||
+ | Press any key to continue . . . | ||
+ | </ | ||