//import the libraries import guru.ttslib.*; import processing.serial.*; //give our instances names Serial treePort; TTS tts; //A string for holding things to say String message = "Ho Ho Ho"; //gregorian calendar for determining the day GregorianCalendar gcal = new GregorianCalendar(); //the newsfeed to load String url = "http://rss.theweathernetwork.com/weather/caon0696"; void setup(){ //the following initiates the voce library voce.SpeechInterface.init("libraries/voce-0.9.1/lib", true, true,"libraries/voce-0.9.1/lib/gram","tree"); //start our port and also tts treePort = new Serial(this,Serial.list()[0],9600); tts = new TTS(); //the following settings control the voice sound tts.setPitch( 180 ); tts.setPitchRange( 90 ); //tts.setPitchShift( -10.5 ); treePort.write("73"); //send command to turn on the lights and open the eyes } void draw(){ if (voce.SpeechInterface.getRecognizerQueueSize()>0){ //if voce recognizes anything being said String s = voce.SpeechInterface.popRecognizedString(); //assign the string that voce heard to the variable s println("you said: " + s); //print what was heard to the debug window. // respond(s); if(s.equals("tree what time is it")){ getTime(); } if(s.equals("tree what day is it")){ whatDay(); } if(s.equals("tree get the weather")){ getWeather(); } if(s.equals("hello tree")){ getAnswer("greetings"); } if(s.equals("merry christmas")){ getAnswer("christmas"); } if(s.equals("thank you")){ getAnswer("thanks"); } } } //This function will split the text up into multiple words, and decide how to animate depending on the length of each word and also pauses which are denoted by "!" void respond(String input){ if (input.length() > 0){ //we actually have something to say voce.SpeechInterface.setRecognizerEnabled(false); //stop listening, otherwise we will hear ourselves and go into a loop //this just splits up all the words sends motion String[] words = split(input," "); int howMany = words.length; for(int i=0;i60){ treePort.write("5"); } else{ treePort.write("7"); delay(500); } } else{ treePort.write("1"); } } tts.speak(input); voce.SpeechInterface.setRecognizerEnabled(true); } } // Function for getting the time void getTime(){ int m = minute(); // Values from 0 - 59 int h = hour(); // Values from 0 - 23 boolean dn = false; String time; String daynight = "Ay em"; //A.M. is read as a single word with regard to our animation function so we cheat here. if(h>12){ dn = true; h = h - 12; daynight = "pee em"; //P.M. is read as a single word with regard to our animation function so we cheat here. } if(h==0){ h=12; } if(m<10){ //if minutes are less than ten, process it to sound natural, we don't say 5 zero one pm if(m==0){ time = "It is now " + h + daynight; //if minutes are at zero just say 5 pm } else{ time = "It is now " + h + "! oh " + m + daynight; // else lets say oh instead of zero println(time); } } else{ //if minutes are greater than ten just say them normal time = "It is now "+ h + "! " + m + daynight; println(time); } message = time; respond(message); } //get the day of the week void whatDay(){ int week = gcal.getActualMaximum(Calendar.DAY_OF_WEEK); println("Day of week: " + week); int first = gcal.getFirstDayOfWeek() ; switch(first){ case 1: println("Sunday"); respond("Sunday"); break; case 2: println("Monday"); respond("Monday"); break; case 3: println("Tuesday"); respond("Tuesday"); break; case 4: println("Wednesday"); respond("Wednesday"); break; case 5: println("Thrusday"); respond("Thursday"); break; case 6: println("Friday"); respond("Friday"); break; case 7: println("Saturday"); respond("Saturday"); break; } } //get the weather void getWeather(){ String currentWeather; //load the feed XMLElement rss = new XMLElement(this,url); XMLElement[] titleXMLElements = rss.getChildren("channel/item/description"); String weather = titleXMLElements[0].getContent(); int index = weather.indexOf(","); currentWeather = weather.substring(0,index); index = weather.indexOf("&"); String temp = weather.substring(index-2,index); int minus = temp.indexOf("-"); currentWeather = "the current weather is " + currentWeather + "! , with a temperature of " + temp + " degrees celcius"; println(currentWeather); message = currentWeather; respond(message); } //generic get answer...loads a line from file void getAnswer(String fileName){ String lines[] = loadStrings(fileName + ".txt"); int index = int(random(lines.length)); // same as int(random(4)) println(lines[index]); // prints one of the lines from greetings.txt message = lines[index]; respond(message); }