def add(user): talker.say("Speak new task") task = getVoice.getVoice(prompt=False) user.get_project('Inbox').add_task(task) user.get_project('Inbox').update() db.log_action("todoist", "added task " + task) go()
def getVoice(prompt=True): print "getting voice..." r = sr.Recognizer() with sr.Microphone() as source: print "in microphone" r.adjust_for_ambient_noise(source, duration=1) if prompt: talker.say("How can I help you") else: ding_wav = wave.open("/home/pi/BrYOUno/utils/ding.wav", 'rb') ding_data = ding_wav.readframes(ding_wav.getnframes()) audio = pyaudio.PyAudio() stream_out = audio.open(format=audio.get_format_from_width( ding_wav.getsampwidth()), channels=ding_wav.getnchannels(), rate=ding_wav.getframerate(), input=False, output=True) stream_out.start_stream() stream_out.write(ding_data) time.sleep(0.2) stream_out.stop_stream() stream_out.close() print "listening" audio = r.listen(source) print "out of microphone" speech = r.recognize_google(audio) db.log_action("voice", speech.lower()) return speech.lower()
def lst(user): db.log_action("todoist", "list tasks") tasks = user.get_project('Inbox').get_tasks() talker.say("You have " + str(len(tasks)) + " open tasks.") for task in user.get_project('Inbox').get_tasks(): talker.say(task.content) go()
def check(user): tasks = user.get_project('Inbox').get_tasks() option = getVoice.select(map(lambda x: x.content, tasks), selector="task") for task in tasks: if task.content == option: db.log_action("todoist", "checked off task " + task.content) task.complete() talker.say("Task completed") user.get_project('Inbox').update() go()
def go(): # Incomplete, as Brown Dining API needs to be updated to use cafebonappetit's new API talker.say("I'm sorry, but this feature is currently not functional.") option = getVoice.select(["Get current menu", "Get tomorrow's menu"]) eatery = getVoice.select(["ratty", "vdub"], selector="eatery") if option == "Get current menu": requests.get( "https://api.students.brown.edu/dining/menu?client_id=4283555c-e705-46e3-81b9-25d3db38b6e1&eatery=" + eatery).json elif option == "Get tomorrow's menu": requests.get( "https://api.students.brown.edu/dining/menu?client_id=4283555c-e705-46e3-81b9-25d3db38b6e1&eatery=" + eatery + "&day=" + datetime.datetime.today().day + 1).json
def search(option): global player url = "http://0.0.0.0:9999/get_by_search?type=" + option talker.say("Speak search query:") query = getVoice.getVoice(prompt=False) if option == "song": url += "&title=" + urllib.quote(query) db.log_action("gplaymusic", "played song " + query) player = setPlayer(["mplayer", url + "&num_tracks=1"]) elif option == "artist": url += "&artist=" + urllib.quote(query) db.log_action("gplaymusic", "played artist " + query) player = setPlayer(["mplayer", "-playlist", url])
def play(): global player if player == "": try: print "playing" option = getVoice.select(["song", "artist", "station", "playlist"]) if option == "song" or option == "artist": search(option) elif option == "station": station() elif option == "playlist": playlist() except Exception as e: print("error encountered") talker.say("Encountered error.") db.log_error(type(e).__name__, str(e), __file__)
def start(): global player if player == "": print "Connecting to MCParks Audio Server..." ws = create_connection("ws://main.mcparks.us:8887") print "Sending opening message..." ws.send("name:BrYOUno") print "Sent." ws.send("playplay:gfpiano") res = ws.recv() ws.close() talker.say("Playing music.") if res.startswith("loop-"): splt = res.split("-") pos = int(splt[1]) / 1000 toplay = splt[2] player = subprocess.Popen(["mplayer", "http://mcparks.us/audio_files/gfpiano.mp3", "-ss", str(pos)], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
def go(): try: option = getVoice.select( ['list tasks', 'add task', 'check off task', 'cancel'], selector="action") with open("/home/pi/todoist.txt", "r") as pw_file: pw = pw_file.read().replace('\n', '') user = todoist.login('*****@*****.**', pw) if option == 'list tasks': lst(user) elif option == "add task": add(user) elif option == "check off task": check(user) except Exception as e: print("Encountered error") talker.say("Encountered error") db.log_error(type(e).__name__, str(e), __file__)
def playlist(): global player talker.say("Say playlist keyword:") query = getVoice.getVoice(prompt=False) playlists_text = requests.get( "http://pi.ryanhecht.net:9999/get_all_playlists?format=text").text[:-1] all_playlists = playlists_text.split("\n") index = 0 playlist_url = "" for pl in all_playlists: if query in pl[0:pl.index("|")].lower(): to_trim = pl.index("|") + 1 playlist_url = pl[to_trim:] break index = index + 1 if playlist_url == "": talker.say("Playlist not found. Sorry.") else: db.log_action("gplaymusic", "played playlist " + query) player = setPlayer(["mplayer", "-shuffle", "-playlist", playlist_url])
def select(choices, selector="Option"): talker.say("Select a " + selector) index = 0 for choice in choices: talker.say(str(index)) talker.say(choice) index = index + 1 voice = getVoice(prompt=False) if "repeat" in voice: return select(choices, selector) else: try: res_index = int(voice) except ValueError: nums = { 'zero': 0, 'one': 1, 'two': 2, 'to': 2, 'too': 2, 'three': 3, 'four': 4, 'for': 4, 'five': 5, 'six': 6, 'seven': 7, 'eight': 8, 'nine': 9, 'ten': 10 } res_index = nums[voice] db.log_action("voice", choices[res_index]) return choices[res_index]
def onBruno(detector): # The detector is stopped so we can get more input detector.terminate() voice="" try: # Asks the user what they want, with exception handling voice = getVoice.getVoice() except sr.UnknownValueError: talker.say("I did not understand that, sorry") except sr.RequestError as e: talker.say("Could not decode speech. You probably used up your API credits, idiot") # Parses and runs given command via API call, or has a speech interaction with the user. # In a try block so to catch any errors and make the user aware of them/log them try: print voice voice = voice.lower() if "lights" in voice: talker.say("Toggling lights") r = requests.get("http://0.0.0.0/lights") elif "radio" in voice: if "stop" in voice: talker.say("Stopping music") r = requests.get("http://0.0.0.0/mcp/stop") else: talker.say("Starting MCParks radio") r = requests.get("http://0.0.0.0/mcp/start") elif "google" in voice: if "music" in voice: if "stop" in voice: talker.say("Stopping Google Play Music") r = requests.get("http://0.0.0.0/music/stop") else: talker.say("Playing Google Play Music") r = requests.get("http://0.0.0.0/music/start") elif "tasks" in voice: talker.say("Opening Toodoo ist") r = requests.get("http://0.0.0.0/todoist") elif voice == "how are you doing": talker.say("I am doing well.") talker.say("Thank you for asking!") elif "help" in voice: talker.say("Help menu coming soon.") elif voice == "what can you do": talker.say("I can perform a variety of functions") talker.say("First, say 'Hey Bruno' to activate me") talker.say("Then, ask me to turn off the lights,") talker.say("play Christmas Music,") talker.say("Or to open MCParks radio") talker.say("Or, ask me how I'm doing!") else: talker.say("No command found.") except Exception as e: db.log_error(type(e).__name__, str(e), __file__) print "Encountered an error in command handling" talker.say("Encountered an error.") talker.say("Sorry.") finally: listen_loop()
def choose_song(): talker.say("Choose a song:") talker.say("1: Main Street, USA") talker.say("2: Epcot Entrance") talker.say("3: Grand Floridian Piano") talker.say("4: Space Mountain 1")
def stop(): global player player.terminate() player = "" talker.say("Music stopped.")
def stop(): global player if player != "": player.terminate() player = "" talker.say("Music stopped")