Search:
Menu VisualVoice / Step2

Visual Voice Home


About Artistic Vision People Teams Contact us


Activities



Publications Media Images/Movies


Opportunities


Related Links


Local Only


Website problems?

edit SideBar

Sending PC values from MAX to the KuraFace

Once the KuraFace is up and running in its own window, the next step is to try and connect to it. A simple MXJ object named "TryAgain", was coded with the following behaviour:

i) Attempts a socket connection on port 4444 to the KuraFace, waiting as server.
ii) Upon receiving values through the inlets, attempts to send them through the socket connection to the KuraFace.

TryAgain is located in the SocketTest patcher, as shown here:

The file TryAgain.java is shown in its entirety below -- comments are added at important points.

	import com.cycling74.max.*;
	import java.io.*;
	import java.net.*;

	public class TryAgain extends MaxObject {

		public TryAgain() {

			declareTypedIO("iffffffff", "m");

			// initialize for safety
			for( int i = 0; i< 8; i++ ) {

				mults[i] = 0;
			}

		}

		// processes inlet data
		protected void inlet( int i ) {

sending a 1 to the leftmost inlet triggers socket connection

			// left-most inlet opens or closes socket connection
			if( getInlet() == 0 ) {

				if( i == 1 && !isSending ) {

			        try {

			            kkSocket = new Socket( hostname, port);
			            out = new PrintWriter(kkSocket.getOutputStream(), true);
			            in = new BufferedReader(new InputStreamReader(kkSocket.getInputStream()));

			            isSending = true;
			            System.out.println( "connected on port " + port );
			        }

			        catch (UnknownHostException e) {

			            System.err.println("Don't know about host: " + hostname);
			        }

			        catch (IOException e) {

			            System.err.println("Couldn't get I/O for the connection to: " + hostname);
			        }
				}
				else if( i == 0 && isSending ) {

					out.println( "Bye." );
					out.close();

			        try {

				        in.close();
				        kkSocket.close();
				        isSending = false;

				        System.out.println( "disconnected from socket on port: " + port );
			        }

			        catch(IOException io) {

			        	System.out.println("Could not close socket connection: " + io);
			        }
				}
			}
		}

receiving a value in one of the eight other inlets triggers an attempt to send values through the socket

		protected void inlet( float f ) {

			// other inlets
			if( isSending ) {

				int whichInlet = getInlet() - 1;

				mults[ whichInlet ] = f;

				String outputLine = "";

				for( int i=0; i<8; i++ ) {

					outputLine += mults[i] + ";";
				}

				out.println( outputLine );

				try {

					String fromServer = in.readLine();
					System.out.println( fromServer );
				}

				catch(IOException io ) {

					System.out.println("could not read from server socket:" + io);
				}
			}
		}

		boolean isSending = false;

		// give these better default values?
		float mults[] = new float[8];

	    Socket kkSocket = null;
	    PrintWriter out = null;
	    BufferedReader in = null;
		int port = 4444;
	    String hostname = "BrahmsOSX.local";
	}

The next step is to bring in a phoneme --> PC vector mapping, so that the sender object can process phoneme data, and convert it to facial parameters.