def runCommand( string ): cmd = vConfig.getConfig( string ) textString = '' if ( cmd is not None ): if ( cmd == "class:weather" ): VoiceWeather.speakWeather( vConfig, string ) elif ( cmd == "class:download" ): downloadString = string[string.find(' '):] dl_result = VoiceDownloader.download( vConfig, downloadString ) GoogleSpeech.tts( dl_result ) elif ( cmd == "class:hue_lights" ): hueController = VoiceHue() hueController.controlByCommand( string ) lights = hueController.getLights() print lights groups = hueController.getGroups() print groups #hueController.toggleByName( "kitchen" ) hueController.dimByName( "kitchen" , -50) hueController.dimByName( "kitchen" , +50) else: p = Popen(cmd, shell=True, stdout=PIPE) textString = p.communicate()[0].rstrip() return textString
def speakWeather( config, string ): location_code = config.get( "weather", "weather_location_code" ) weather_com_result = pywapi.get_weather_from_weather_com( str(location_code )) print weather_com_result print string lookupString = '' ### TODAY if ( re.compile( "today", re.IGNORECASE ).findall( string ,1 )): todayData = weather_com_result['forecasts'][0] if ( todayData['day']['text'] != 'N/A' ): if ( int( todayData['day']['chance_precip'] ) > 40 ): lookupString = "Today will be " + str( todayData['day']['text'] ) + " with a chance of showers and a high of " + str( todayData['high'] ) + "degrees" else: lookupString = "Today will be " + str( todayData['day']['text'] ) + " with a high of " + str( todayData['high'] ) + "degrees" else: if ( int(todayData['night']['chance_precip'] ) > 40 ): lookupString = "Tonight will be " + str( todayData['night']['text'] ) + " with a chance of showers" else: lookupString = "Tonight will be " + str( todayData['night']['text'] ) ### TONIGHT elif ( re.compile( "tonight", re.IGNORECASE).findall( string ,1 )): todayData = weather_com_result['forecasts'][0] if ( int(todayData['night']['chance_precip'] ) > 40 ): lookupString = "Tonight will be " + str( todayData['night']['text'] ) + " with a chance of showers" else: lookupString = "Tonight will be " + str( todayData['night']['text'] ) ### Tomorrow Night elif ( re.compile( "tomorrow night", re.IGNORECASE).findall( string ,1 )): todayData = weather_com_result['forecasts'][1] if ( int(todayData['night']['chance_precip'] ) > 40 ): lookupString = "Tomorrow night will be " + str( todayData['night']['text'] ) + " with a chance of showers" else: lookupString = "Tomorrow night will be " + str( todayData['night']['text'] ) ### TODAY elif ( re.compile( "tomorrow", re.IGNORECASE ).findall( string ,1 )): todayData = weather_com_result['forecasts'][1] if ( todayData['day']['text'] != 'N/A' ): if (( int( todayData['day']['chance_precip'] ) > 40 ) or ( int( todayData['night']['chance_precip'] ) > 40 )): lookupString = "Tomorrow will be " + str( todayData['day']['text'] ) + " with a chance of showers and a high of " + str( todayData['high'] ) + " degrees" else: lookupString = "Tomorrow will be " + str( todayData['day']['text'] ) + " with a high of " + str( todayData['high'] ) + "degrees" else: lookupString = "It is currently " + str(weather_com_result['current_conditions']['text']) + " and " + weather_com_result['current_conditions']['temperature'] + "degrees.\n\n" print lookupString ## Work our magic if ( lookupString != '' ): GoogleSpeech.tts( lookupString ) else: GoogleSpeech.tts( "Sorry, Weather information un-available at this time, please try again later" )
def listen_for_speech(): """ Does speech recognition using Google's speech recognition service. Records sound from microphone until silence is found and save it as WAV and then converts it to FLAC. Finally, the file is sent to Google and the result is returned. """ stream = initStream() print "* listening. CTRL+C to finish." all_m = [] data = '' #SILENCE_LIMIT = 2 rel = vConfig.RATE/vConfig.INPUT_FRAMES_PER_BLOCK slid_win = deque(maxlen=vConfig.SILENCE_LIMIT*rel) started = False while (True): data = stream.read(vConfig.INPUT_FRAMES_PER_BLOCK) slid_win.append (abs(audioop.avg(data, 2))) if(True in [ x>vConfig.THRESHOLD for x in slid_win]): if(not started): print "starting record" started = True all_m.append(data) elif (started==True): print "finished" #the limit was reached, finish capture and deliver filename = save_speech(all_m,p) print filename textString = GoogleSpeech.stt(filename, vConfig.RATE) if ( textString != '' ): #os.system( "say " + str(textString) ) print "Initiating Configuration Lookup" #cmd = vConfig.getConfig( textString ) #if ( cmd is not None ): runCommand(textString) #reset all started = False slid_win = deque(maxlen=vConfig.SILENCE_LIMIT*rel) all_m= [] stream = initStream() print stream print "listening ... again" print "* done recording" stream.close()