class Text(object): def __init__(self): self.stt_engine = STT() self.tts_engine = TTS() self.mic = Mic(self.tts_engine, self.stt_engine, self.stt_engine) self.selection = Selection(self.tts_engine) def handle(self): while True: threshold, translate = self.mic.passiveListen("JARVIS") if not translate or not threshold: continue input = self.mic.activeListen(threshold) print input if input: string = self.selection.select(input) else: self.tts_engine.say("Pardon?")
class MusicMode: def __init__(self, PERSONA, mic): self.persona = PERSONA # self.mic - we're actually going to ignore the mic they passed in self.music = Music() # index spotify playlists into new dictionary and language models original = self.music.get_soup_playlist() + [ "STOP", "CLOSE", "PLAY", "PAUSE", "NEXT", "PREVIOUS", "LOUDER", "SOFTER", "LOWER", "HIGHER", "VOLUME", "PLAYLIST" ] pronounced = g2p.translateWords(original) zipped = zip(original, pronounced) lines = ["%s %s" % (x, y) for x, y in zipped] with open("dictionary_spotify.dic", "w") as f: f.write("\n".join(lines) + "\n") with open("sentences_spotify.txt", "w") as f: f.write("\n".join(original) + "\n") f.write("<s> \n </s> \n") f.close() # make language model os.system( "text2idngram -vocab sentences_spotify.txt < sentences_spotify.txt -idngram spotify.idngram" ) os.system( "idngram2lm -idngram spotify.idngram -vocab sentences_spotify.txt -arpa languagemodel_spotify.lm" ) # create a new mic with the new music models self.mic = Mic("languagemodel.lm", "dictionary.dic", "languagemodel_persona.lm", "dictionary_persona.dic", "languagemodel_spotify.lm", "dictionary_spotify.dic") def delegateInput(self, input): command = input.upper() # check if input is meant to start the music module if "PLAYLIST" in command: command = command.replace("PLAYLIST", "") elif "STOP" in command: self.mic.say("Stopping music") self.music.stop() return elif "PLAY" in command: self.mic.say("Playing %s" % self.music.current_song()) self.music.play() return elif "PAUSE" in command: self.mic.say("Pausing music") # not pause because would need a way to keep track of pause/play # state self.music.stop() return elif any(ext in command for ext in ["LOUDER", "HIGHER"]): self.mic.say("Louder") self.music.volume(interval=10) self.music.play() return elif any(ext in command for ext in ["SOFTER", "LOWER"]): self.mic.say("Softer") self.music.volume(interval=-10) self.music.play() return elif "NEXT" in command: self.mic.say("Next song") self.music.play() # backwards necessary to get mopidy to work self.music.next() self.mic.say("Playing %s" % self.music.current_song()) return elif "PREVIOUS" in command: self.mic.say("Previous song") self.music.play() # backwards necessary to get mopidy to work self.music.previous() self.mic.say("Playing %s" % self.music.current_song()) return # SONG SELECTION... requires long-loading dictionary and language model # songs = self.music.fuzzy_songs(query = command.replace("PLAY", "")) # if songs: # self.mic.say("Found songs") # self.music.play(songs = songs) # print "SONG RESULTS" # print "============" # for song in songs: # print "Song: %s Artist: %s" % (song.title, song.artist) # self.mic.say("Playing %s" % self.music.current_song()) # else: # self.mic.say("No songs found. Resuming current song.") # self.music.play() # PLAYLIST SELECTION playlists = self.music.fuzzy_playlists(query=command) if playlists: self.mic.say("Loading playlist %s" % playlists[0]) self.music.play(playlist_name=playlists[0]) self.mic.say("Playing %s" % self.music.current_song()) else: self.mic.say("No playlists found. Resuming current song.") self.music.play() return def handleForever(self): self.music.play() self.mic.say("Playing %s" % self.music.current_song()) while True: try: threshold, transcribed = self.mic.passiveListen(self.persona) except: continue if threshold: self.music.pause() input = self.mic.activeListen(MUSIC=True) if "close" in input.lower(): self.mic.say("Closing Spotify") return if input: self.delegateInput(input) else: self.mic.say("Pardon?") self.music.play()
class MusicMode: def __init__(self, PERSONA, mic): self.persona = PERSONA # self.mic - we're actually going to ignore the mic they passed in self.music = Music() # index spotify playlists into new dictionary and language models original = self.music.get_soup_playlist( ) + ["STOP", "CLOSE", "PLAY", "PAUSE", "NEXT", "PREVIOUS", "LOUDER", "SOFTER", "LOWER", "HIGHER", "VOLUME", "PLAYLIST"] pronounced = g2p.translateWords(original) zipped = zip(original, pronounced) lines = ["%s %s" % (x, y) for x, y in zipped] with open("dictionary_spotify.dic", "w") as f: f.write("\n".join(lines) + "\n") with open("sentences_spotify.txt", "w") as f: f.write("\n".join(original) + "\n") f.write("<s> \n </s> \n") f.close() # make language model os.system( "text2idngram -vocab sentences_spotify.txt < sentences_spotify.txt -idngram spotify.idngram") os.system( "idngram2lm -idngram spotify.idngram -vocab sentences_spotify.txt -arpa languagemodel_spotify.lm") # create a new mic with the new music models self.mic = Mic( speaker.newSpeaker(), stt.PocketSphinxSTT(lmd_music="languagemodel_spotify.lm", dictd_music="dictionary_spotify.dic"), stt.PocketSphinxSTT(lmd_music="languagemodel_spotify.lm", dictd_music="dictionary_spotify.dic") ) def delegateInput(self, input): command = input.upper() # check if input is meant to start the music module if "PLAYLIST" in command: command = command.replace("PLAYLIST", "") elif "STOP" in command: self.mic.say("Stopping music") self.music.stop() return elif "PLAY" in command: self.mic.say("Playing %s" % self.music.current_song()) self.music.play() return elif "PAUSE" in command: self.mic.say("Pausing music") # not pause because would need a way to keep track of pause/play # state self.music.stop() return elif any(ext in command for ext in ["LOUDER", "HIGHER"]): self.mic.say("Louder") self.music.volume(interval=10) self.music.play() return elif any(ext in command for ext in ["SOFTER", "LOWER"]): self.mic.say("Softer") self.music.volume(interval=-10) self.music.play() return elif "NEXT" in command: self.mic.say("Next song") self.music.play() # backwards necessary to get mopidy to work self.music.next() self.mic.say("Playing %s" % self.music.current_song()) return elif "PREVIOUS" in command: self.mic.say("Previous song") self.music.play() # backwards necessary to get mopidy to work self.music.previous() self.mic.say("Playing %s" % self.music.current_song()) return # SONG SELECTION... requires long-loading dictionary and language model # songs = self.music.fuzzy_songs(query = command.replace("PLAY", "")) # if songs: # self.mic.say("Found songs") # self.music.play(songs = songs) # print "SONG RESULTS" # print "============" # for song in songs: # print "Song: %s Artist: %s" % (song.title, song.artist) # self.mic.say("Playing %s" % self.music.current_song()) # else: # self.mic.say("No songs found. Resuming current song.") # self.music.play() # PLAYLIST SELECTION playlists = self.music.fuzzy_playlists(query=command) if playlists: self.mic.say("Loading playlist %s" % playlists[0]) self.music.play(playlist_name=playlists[0]) self.mic.say("Playing %s" % self.music.current_song()) else: self.mic.say("No playlists found. Resuming current song.") self.music.play() return def handleForever(self): self.music.play() self.mic.say("Playing %s" % self.music.current_song()) while True: try: threshold, transcribed = self.mic.passiveListen(self.persona) except: continue if threshold: self.music.pause() input = self.mic.activeListen(MUSIC=True) if "close" in input.lower(): self.mic.say("Closing Spotify") return if input: self.delegateInput(input) else: self.mic.say("Pardon?") self.music.play()
class BibleReader: def __init__(self, PERSONA, mic, lang): self.persona = PERSONA self.lang = lang self.bookName = "" self.chapNum = "" self.client = mpd.MPDClient() self.client.timeout = None self.client.idletimeout= None self.client.connect("localhost", 6600) dictionary = bible_lists.dictList[lang] self.mic = Mic(mic.speaker, "languagemodel_bible.lm", dictionary[0], "languagemodel_persona.lm", "dictionary_persona.dic", lmd_music="languagemodel_playback.lm", dictd_music=dictionary[1], lmd_num="languagemodel_num.lm", dictd_num=dictionary[2]) # def say(self, word, lang): # filename = "audio/" + lang + "/" + word + ".wav" # os.system("aplay -D hw:1,0 " + filename) def lookupBible(self, lang): badInput = True while badInput: badInput = False self.mic.speak("book", self.lang) book = self.mic.activeListen() self.mic.speak("chapter", self.lang) chap = self.mic.activeListen(NUMBER=True) if book == "" or chap == "": badInput = True self.mic.speak("pardon", self.lang) else: book, chap, audio = bible_search.bible_query(book, chap, lang) if audio == "": badInput = True self.mic.speak("repeat", self.lang) else: self.mic.say("Opening " + book + " " + chap) self.mic.speak("confirm", self.lang) input = self.mic.activeListen(MUSIC=True) if "CANCEL" in input: badInput = True self.mic.speak("cancel", self.lang) else: return book, chap, audio def nextBook(self, book): return bible_lists.nextList[book] def handleForever(self): self.mic.speak("opening", self.lang) try: self.client.clear() except mpd.ConnectionError: self.client.disconnect() self.client.connect("localhost", 6600) self.client.clear() self.client.add("file:///home/pi/jasper/client/BibleReader/bible.mp3") self.client.play() isPlaying = True while True: inputFlag = False finishedFlag = False try: i, o, e = select.select([sys.stdin], [], [], 0) for s in i: if s == sys.stdin: input = sys.stdin.read(1) inputFlag = True #if not inputFlag: # threshold, transcribed = self.mic.passiveListen(self.persona) threshold = False stat = self.client.status() if 'songid' not in stat: finishedFlag = True except: continue if inputFlag or threshold: inputFlag = False try: self.client.pause(1) except mpd.ConnectionError: self.client.disconnect() self.client.connect("localhost", 6600) self.client.pause(1) input = self.mic.activeListen(MUSIC=True) if "CLOSE BIBLE" in input: self.mic.speak("closing", self.lang) self.client.stop() self.client.close() self.client.disconnect() return elif "STOP" in input: self.mic.speak("stop", self.lang) self.client.stop() isPlaying = False elif "PAUSE" in input: self.mic.speak("pause", self.lang) isPlaying = False elif "CONTINUE" in input: self.mic.speak("continuing", self.lang) self.client.pause(0) isPlaying = True elif "OPEN" in input: self.bookName, self.chapNum, audio = self.lookupBible(self.lang) self.mic.speak("opening", self.lang) #choose another book try: self.client.clear() except mpd.ConnectionError: self.client.disconnect() self.client.connect("localhost", 6600) self.client.clear() bible_search.audio_download(audio) self.client.add("file:///home/pi/jasper/client/BibleReader/bible.mp3") self.client.play() else: self.mic.speak("pardon", self.lang) if isPlaying: self.client.play() if finishedFlag: finishedFlag = False self.mic.speak("nextchap", self.lang) input = self.mic.activeListen(MUSIC=True) if "CONTINUE" in input: nextChap = str(int(self.chapNum) + 1) self.bookName, self.chapNum, audio = bible_search.bible_query(self.bookName, nextChap, self.lang) if audio == "": #go to next book self.bookName = self.nextBook(self.bookName) nextChap = "1" self.bookName, self.chapNum, audio = bible_search.bible_query(self.bookName, nextChap, self.lang) self.mic.speak("opening", self.lang) #choose another book try: self.client.clear() except mpd.ConnectionError: self.client.disconnect() self.client.connect("localhost", 6600) self.client.clear() bible_search.audio_download(audio) self.client.add("file:///home/pi/jasper/client/BibleReader/bible.mp3") self.client.play() else: self.mic.speak("closing", self.lang) try: self.client.close() self.client.disconnect() except mpd.connectionError: self.client.disconnect() return
self.client.close() self.client.disconnect() except mpd.connectionError: self.client.disconnect() return bible = BibleReader("JASPER", mic, lang) mic.speak("welcome", lang) while True: mic.speak("prompt", lang) time.sleep(0.1) command = mic.activeListen() if "COMMAND" in command: mic.speak("command", lang) elif "CHANGE LANGUAGE" in command: mic.speak("changelang", lang) lang = mic.activeListen() if "INDONESIAN" in lang: mic = Mic(mic.speaker, "languagemodel_command.lm", "dictionary_commandindo.dic", "languagemodel_persona.lm", "dictionary_persona.dic") else: mic = Mic(mic.speaker, "languagemodel_command.lm", "dictionary_command.dic", "languagemodel_persona.lm", "dictionary_persona.dic") bible = BibleReader("JASPER", mic, lang) mic.speak("langchange", lang) elif "LIST BOOK" in command: mic.speak("listbook", lang) #example, needs revision elif "RECOMMEND BOOK" in command: mic.speak("recommend", lang)
class MusicMode(object): def __init__(self, PERSONA, mic, mpdwrapper): self._logger = logging.getLogger(__name__) self.persona = PERSONA # self.mic - we're actually going to ignore the mic they passed in self.music = mpdwrapper # index spotify playlists into new dictionary and language models phrases = ["STOP", "CLOSE", "PLAY", "PAUSE", "NEXT", "PREVIOUS", "LOUDER", "SOFTER", "LOWER", "HIGHER", "VOLUME", "PLAYLIST"] phrases.extend(self.music.get_soup_playlist()) music_stt_engine = mic.active_stt_engine.get_instance('music', phrases) self.mic = Mic(mic.speaker, mic.passive_stt_engine, music_stt_engine) def delegateInput(self, input): command = input.upper() # check if input is meant to start the music module if "PLAYLIST" in command: command = command.replace("PLAYLIST", "") elif "STOP" in command: self.mic.say("Stopping music") self.music.stop() return elif "PLAY" in command: self.mic.say("Playing %s" % self.music.current_song()) self.music.play() return elif "PAUSE" in command: self.mic.say("Pausing music") # not pause because would need a way to keep track of pause/play # state self.music.stop() return elif any(ext in command for ext in ["LOUDER", "HIGHER"]): self.mic.say("Louder") self.music.volume(interval=10) self.music.play() return elif any(ext in command for ext in ["SOFTER", "LOWER"]): self.mic.say("Softer") self.music.volume(interval=-10) self.music.play() return elif "NEXT" in command: self.mic.say("Next song") self.music.play() # backwards necessary to get mopidy to work self.music.next() self.mic.say("Playing %s" % self.music.current_song()) return elif "PREVIOUS" in command: self.mic.say("Previous song") self.music.play() # backwards necessary to get mopidy to work self.music.previous() self.mic.say("Playing %s" % self.music.current_song()) return # SONG SELECTION... requires long-loading dictionary and language model # songs = self.music.fuzzy_songs(query = command.replace("PLAY", "")) # if songs: # self.mic.say("Found songs") # self.music.play(songs = songs) # print("SONG RESULTS") # print("============") # for song in songs: # print("Song: %s Artist: %s" % (song.title, song.artist)) # self.mic.say("Playing %s" % self.music.current_song()) # else: # self.mic.say("No songs found. Resuming current song.") # self.music.play() # PLAYLIST SELECTION playlists = self.music.fuzzy_playlists(query=command) if playlists: self.mic.say("Loading playlist %s" % playlists[0]) self.music.play(playlist_name=playlists[0]) self.mic.say("Playing %s" % self.music.current_song()) else: self.mic.say("No playlists found. Resuming current song.") self.music.play() return def handleForever(self): self.music.play() self.mic.say("Playing %s" % self.music.current_song()) while True: threshold, transcribed = self.mic.passiveListen(self.persona) if not transcribed or not threshold: self._logger.info("Nothing has been said or transcribed.") continue self.music.pause() input = self.mic.activeListen(MUSIC=True) if input: if "close" in input.lower(): self.mic.say("Closing Spotify") return self.delegateInput(input) else: self.mic.say("Pardon?") self.music.play()
from mic import Mic print("active listening") mic = Mic("languagemodel.lm", "dictionary.dic", "languagemodel_persona.lm", "dictionary_persona.dic") mic.say("How can I be of service?") b = mic.activeListen() #a = mic.googleTranslate() #print(a)
class MusicMode: def __init__(self, PERSONA, mic): self.persona = PERSONA # self.mic - we're actually going to ignore the mic they passed in self.music = Music() # index spotify playlists into new dictionary and language models words = self.music.get_soup_playlist() + [ "STOP", "CLOSE", "PLAY", "PAUSE", "NEXT", "PREVIOUS", "LOUDER", "SOFTER", "LOWER", "HIGHER", "VOLUME", "PLAYLIST" ] text = "\n".join(["<s> %s </s>" for word in words]) # make language model vocabcompiler.compile_text(text, languagemodel_spotify) # create a new mic with the new music models self.mic = Mic( speaker.newSpeaker(), stt.PocketSphinxSTT(lmd_music=languagemodel_spotify, dictd_music=dictionary_spotify), stt.PocketSphinxSTT(lmd_music=languagemodel_spotify, dictd_music=dictionary_spotify)) def delegateInput(self, input): command = input.upper() # check if input is meant to start the music module if "PLAYLIST" in command: command = command.replace("PLAYLIST", "") elif "STOP" in command: self.mic.say("Stopping music") self.music.stop() return elif "PLAY" in command: self.mic.say("Playing %s" % self.music.current_song()) self.music.play() return elif "PAUSE" in command: self.mic.say("Pausing music") # not pause because would need a way to keep track of pause/play # state self.music.stop() return elif any(ext in command for ext in ["LOUDER", "HIGHER"]): self.mic.say("Louder") self.music.volume(interval=10) self.music.play() return elif any(ext in command for ext in ["SOFTER", "LOWER"]): self.mic.say("Softer") self.music.volume(interval=-10) self.music.play() return elif "NEXT" in command: self.mic.say("Next song") self.music.play() # backwards necessary to get mopidy to work self.music.next() self.mic.say("Playing %s" % self.music.current_song()) return elif "PREVIOUS" in command: self.mic.say("Previous song") self.music.play() # backwards necessary to get mopidy to work self.music.previous() self.mic.say("Playing %s" % self.music.current_song()) return # SONG SELECTION... requires long-loading dictionary and language model # songs = self.music.fuzzy_songs(query = command.replace("PLAY", "")) # if songs: # self.mic.say("Found songs") # self.music.play(songs = songs) # print "SONG RESULTS" # print "============" # for song in songs: # print "Song: %s Artist: %s" % (song.title, song.artist) # self.mic.say("Playing %s" % self.music.current_song()) # else: # self.mic.say("No songs found. Resuming current song.") # self.music.play() # PLAYLIST SELECTION playlists = self.music.fuzzy_playlists(query=command) if playlists: self.mic.say("Loading playlist %s" % playlists[0]) self.music.play(playlist_name=playlists[0]) self.mic.say("Playing %s" % self.music.current_song()) else: self.mic.say("No playlists found. Resuming current song.") self.music.play() return def handleForever(self): self.music.play() self.mic.say("Playing %s" % self.music.current_song()) while True: try: threshold, transcribed = self.mic.passiveListen(self.persona) except: continue if threshold: self.music.pause() input = self.mic.activeListen(MUSIC=True) if "close" in input.lower(): self.mic.say("Closing Spotify") return if input: self.delegateInput(input) else: self.mic.say("Pardon?") self.music.play()
class MusicMode(object): def __init__(self, PERSONA, mic, mpdwrapper): self._logger = logging.getLogger(__name__) self.persona = PERSONA # self.mic - we're actually going to ignore the mic they passed in self.music = mpdwrapper # index spotify playlists into new dictionary and language models phrases = ["STOP", "CLOSE", "PLAY", "PAUSE", "NEXT", "PREVIOUS", "LOUDER", "SOFTER", "LOWER", "HIGHER", "VOLUME", "PLAYLIST"] phrases.extend(self.music.get_soup_playlist()) music_stt_engine = mic.active_stt_engine.get_instance('music', phrases) self.mic = Mic(mic.speaker, mic.passive_stt_engine, music_stt_engine) def delegateInput(self, input): command = input.upper() # check if input is meant to start the music module if "PLAYLIST" in command: command = command.replace("PLAYLIST", "") elif "STOP" in command: self.mic.say("Stopping music") self.music.stop() return elif "PLAY" in command: self.mic.say("Playing %s" % self.music.current_song()) self.music.play() return elif "PAUSE" in command: self.mic.say("Pausing music") # not pause because would need a way to keep track of pause/play # state self.music.stop() return elif any(ext in command for ext in ["LOUDER", "HIGHER"]): self.mic.say("Louder") self.music.volume(interval=10) self.music.play() return elif any(ext in command for ext in ["SOFTER", "LOWER"]): self.mic.say("Softer") self.music.volume(interval=-10) self.music.play() return elif "NEXT" in command: self.mic.say("Next song") self.music.play() # backwards necessary to get mopidy to work self.music.next() self.mic.say("Playing %s" % self.music.current_song()) return elif "PREVIOUS" in command: self.mic.say("Previous song") self.music.play() # backwards necessary to get mopidy to work self.music.previous() self.mic.say("Playing %s" % self.music.current_song()) return # SONG SELECTION... requires long-loading dictionary and language model # songs = self.music.fuzzy_songs(query = command.replace("PLAY", "")) # if songs: # self.mic.say("Found songs") # self.music.play(songs = songs) # print "SONG RESULTS" # print "============" # for song in songs: # print "Song: %s Artist: %s" % (song.title, song.artist) # self.mic.say("Playing %s" % self.music.current_song()) # else: # self.mic.say("No songs found. Resuming current song.") # self.music.play() # PLAYLIST SELECTION playlists = self.music.fuzzy_playlists(query=command) if playlists: self.mic.say("Loading playlist %s" % playlists[0]) self.music.play(playlist_name=playlists[0]) self.mic.say("Playing %s" % self.music.current_song()) else: self.mic.say("No playlists found. Resuming current song.") self.music.play() return def handleForever(self): self.music.play() self.mic.say("Playing %s" % self.music.current_song()) while True: threshold, transcribed = self.mic.passiveListen(self.persona) if not transcribed or not threshold: self._logger.info("Nothing has been said or transcribed.") continue self.music.pause() input = self.mic.activeListen(MUSIC=True) if input: if "close" in input.lower(): self.mic.say("Closing Spotify") return self.delegateInput(input) else: self.mic.say("Pardon?") self.music.play()