import java.io.BufferedReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import java.net.PortUnreachableException; import java.net.SocketException; import java.net.UnknownHostException; import java.util.LinkedList; import java.util.List; import javax.sound.midi.MidiUnavailableException; import be.ac.ulg.montefiore.run.jahmm.Hmm; import be.ac.ulg.montefiore.run.jahmm.ObservationInteger; import be.ac.ulg.montefiore.run.jahmm.OpdfIntegerFactory; import be.ac.ulg.montefiore.run.jahmm.io.FileFormatException; import be.ac.ulg.montefiore.run.jahmm.io.HmmWriter; import be.ac.ulg.montefiore.run.jahmm.io.OpdfIntegerWriter; import be.ac.ulg.montefiore.run.jahmm.learn.BaumWelchLearner; import be.ac.ulg.montefiore.run.jahmm.learn.KMeansLearner; public class Record { // MotionStar Comms private final static byte MSG_RUN_CONTINUOUS = 104; private final static int PORT_NUMBER = 5000; private InetAddress address; private DatagramSocket socket; private final static String IP_ADDRESS = "10.74.192.9"; MotionStar m = new MotionStar(); // Lists of lists of values from multiple iterations (observations), used to create HMMs List> toHmmX; List> toHmmY; List> toHmmZ; // Lists of values from one full iteration/gesture training performance List sequencesX ; List sequencesY ; List sequencesZ; // Current received value from sensor to add to list public int receivedX = 0; public int receivedY = 0; public int receivedZ = 0; // Other variables public boolean firstElement = true, answer = true; public String gestureName, input, instrumentName; public FileWriter writerX, writerY, writerZ, gWriter, iWriter; public OpdfIntegerWriter opdfWriterX, opdfWriterY, opdfWriterZ; BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); //HMM public OpdfIntegerFactory factory; public Hmm hmmX, hmmY, hmmZ; public KMeansLearner kml; public static void main (String args[]) throws MotionStarException, InterruptedException, IOException, FileFormatException, MidiUnavailableException{ Record one = new Record(); } public Record() throws MotionStarException, InterruptedException, IOException, FileFormatException, MidiUnavailableException{ m.connect(); while (answer){ getGesture(); System.out.println("Gesture data succesfully saved."); System.out.println("Would you like to create another gesture? (Y/N)"); input = br.readLine(); while (true){ if (input.equalsIgnoreCase("y")){ answer = true; break; } else if (input.equalsIgnoreCase("n")){ answer = false; System.out.println(""); Recognise r = new Recognise(); r.Rec(); break; } else { System.out.println("Incorrect value detected. New gesture? (Y/N)"); input = br.readLine(); } } } } public void getGesture() throws IOException, MotionStarException, InterruptedException{ System.out.println("Welcome!"); System.out.println("You may record up to 5 different gestures."); System.out.print("Please state the name of the gesture you wish to create: "); gestureName = br.readLine(); System.out.println(""); gWriter = new FileWriter("GestureList.txt", true); // Save the gesture name gWriter.write(gestureName + "\r\n"); // Writes the gesture name to a reference file gWriter.close(); System.out.println("Choose an instrument from the following"); System.out.println("1 - Guitar"); System.out.println("2 - Piano"); System.out.println("3 - Bass"); System.out.println("4 - Synth"); System.out.println("5 - Organ"); System.out.print("Instrument: "); getInstrument(); learnGesture(gestureName); } //Ensures a correct instrument was typed public void getInstrument() throws IOException{ instrumentName = br.readLine(); if (instrumentName.equalsIgnoreCase("guitar")) writeInstrument(instrumentName); else if (instrumentName.equalsIgnoreCase("piano")) writeInstrument(instrumentName); else if (instrumentName.equalsIgnoreCase("bass")) writeInstrument(instrumentName); else if (instrumentName.equalsIgnoreCase("synth")) writeInstrument(instrumentName); else if (instrumentName.equalsIgnoreCase("organ")) writeInstrument(instrumentName); else{ System.out.println(""); System.out.println("Invalid instrument, please try again using the above list"); System.out.print("Instrument: "); getInstrument(); } } //Save instrument name to file public void writeInstrument(String instrumentName) throws IOException{ iWriter = new FileWriter("MIDI.txt", true); iWriter.write(instrumentName + "\r\n"); iWriter.close(); } //Repeat gesture to generate observation sequences and then generate and save HMMs to text files public void learnGesture(String gestureName) throws MotionStarException, IOException, InterruptedException { toHmmX = new LinkedList>(); toHmmY = new LinkedList>(); toHmmZ = new LinkedList>(); for(int i=1;i<11;i++){ sequencesX = new LinkedList(); sequencesY = new LinkedList(); sequencesZ = new LinkedList(); System.out.println(""); System.out.println("Training iteration " +i); System.out.println("Press Enter, and perform gesture '"+gestureName+"'"); System.in.read(); runContinuous(); toHmmX = createData(toHmmX, sequencesX); toHmmY = createData(toHmmY, sequencesY); toHmmZ = createData(toHmmZ, sequencesZ); System.in.read(); } hmmX = createHmm(toHmmX); writerX = new FileWriter(gestureName + " X.txt"); opdfWriterX = new OpdfIntegerWriter(); HmmWriter.write(writerX, opdfWriterX, hmmX); writerX.close(); hmmY = createHmm(toHmmY); writerY = new FileWriter(gestureName + " Y.txt"); opdfWriterY = new OpdfIntegerWriter(); HmmWriter.write(writerY, opdfWriterY, hmmY); writerY.close(); hmmZ = createHmm(toHmmZ); writerZ = new FileWriter(gestureName + " Z.txt"); opdfWriterZ = new OpdfIntegerWriter(); HmmWriter.write(writerZ, opdfWriterZ, hmmZ); writerZ.close(); } //Uses JAHMM to calculate HMMs public Hmm createHmm(List> toHmm){ factory = new OpdfIntegerFactory(100); kml = new KMeansLearner(8,factory,toHmm); Hmmhmm = kml.iterate(); BaumWelchLearner bwl = new BaumWelchLearner(); bwl.setNbIterations(20); bwl.learn(hmm, toHmm); return hmm; } //Tells device to continually send data and retrieves it, generating a sequence public void runContinuous() throws MotionStarException, InterruptedException{ setSocket(); sendMessage(MSG_RUN_CONTINUOUS); System.out.println("Capturing..."); for (int i=0;i<101;i++){ receiveMessage(); if (firstElement == true){ firstElement = false; } else { ObservationInteger x = new ObservationInteger(receivedX); sequencesX.add(x); ObservationInteger y = new ObservationInteger(receivedY); sequencesY.add(y); ObservationInteger z = new ObservationInteger(receivedZ); sequencesZ.add(z); Thread.sleep(50); } } firstElement = true; System.out.println(""); System.out.println("Stopped capturing"); System.out.println(""); System.out.println("Press Enter to continue"); } public List> createData (List> toHmm, List sequences){ toHmm.add(sequences); return toHmm; } private void setSocket() throws MotionStarException { try { socket = new DatagramSocket(); socket.setSoTimeout(300); } catch (SocketException e) { throw new MotionStarException("Cannot connect socket on port " + PORT_NUMBER); } } public void sendMessage(byte Message) throws MotionStarException { byte [] message = new byte [16]; message [8] = Message; message [10] = 3; try { address = InetAddress.getByName(IP_ADDRESS); } catch (UnknownHostException e) { throw new MotionStarException("You do not appear to be connected to a network"); } try { DatagramPacket packet = new DatagramPacket(message, message.length, address, PORT_NUMBER); socket.send(packet); } catch (PortUnreachableException e) { throw new MotionStarException("Port unreachable error with sending"); } catch (IOException e) { throw new MotionStarException("IO error with sending"); } } public void receiveMessage() throws MotionStarException { byte [] received = new byte [128]; try { address = InetAddress.getByName(IP_ADDRESS); } catch (UnknownHostException e) { throw new MotionStarException("You do not appear to be connected to a network"); } try { DatagramPacket packet = new DatagramPacket(received, received.length, address, PORT_NUMBER); socket.receive(packet); received = packet.getData(); } catch (PortUnreachableException e) { throw new MotionStarException("Port unreachable error with sending"); } catch (IOException e) { throw new MotionStarException("IO error with sending"); } if (firstElement == false){ //Doesn't capture first value of 0 receivedX = (received[18]+20); receivedY = (received[20]+20); receivedZ = (received[22]+20); } } }