def static_send(bot: BotBase,
                    global_state: State,
                    text: str,
                    callback: callable,
                    mode: DialogMode = DialogMode.REGULAR,
                    delay: int = 1):

        chat_id = global_state.chat_id
        _text = ImmediateNextAction.get_text(text.strip(), global_state)

        if mode == DialogMode.REGULAR:
            if delay < 10:
                bot.send_chat_action(chat_id, ChatAction.TYPING)
            bot.schedule_message(chat_id, _text, delay, lambda *x: callback())

        elif mode == DialogMode.DELAYED:
            bot.delayed_type_message(chat_id, _text, lambda *x: callback())

        elif mode == DialogMode.ITERATIVE:
            bot.send_iteratively_edited_message(chat_id, _text.split())
            callback()

        elif mode == DialogMode.VOICE:
            speech = Speech()
            bot.send_chat_action(chat_id, ChatAction.RECORD_AUDIO)
            audio_message = speech.text_to_speech(_text)
            bot.send_voice_message(chat_id, audio_message)
            callback()
Exemple #2
0
    def accept_voice_message(self, bot: BotBase, chat_id: str, voice_message,
                             global_state: State) -> 'LevelBase':
        global_state.chat_id = chat_id

        speech = Speech()
        text = speech.speech_to_text(voice_message)

        return self.resume(bot, global_state, ChatType.VOICE, text)
    def accept_voice_message(self, bot: BotBase, chat_id: str, voice_message, global_state: State) -> 'LevelBase':

        speech = Speech()

        text = speech.speech_to_text(voice_message)

        bot.send_text(chat_id, text)

        return self
    def accept_text_message(self, bot: BotBase, chat_id: str, text: str, global_state: State) -> 'LevelBase':
        global_state.name = text

        speech = Speech()

        file = speech.text_to_speech(f'Hallo {global_state.name}!')

        bot.send_voice_message(chat_id, file)

        return self
Exemple #5
0
def main():
    recog = sr.Recognizer()
    trigger_map = load_trigger_map()
    triggers = sorted(trigger_map.keys(), key=lambda key: -len(key))

    with sr.Microphone(device_index=0) as source:
        print("Adjusting energy_threshold")
        recog.adjust_for_ambient_noise(source)
        print(recog.energy_threshold)

        print("Listening...")
        # obtain audio from the microphone
        audio = recog.listen(source)
        print("Thinking...")

        try:
            recognized_speech = recog.recognize_google(audio)
            print("I think I heard " + recognized_speech)

            speech = None
            for trigger in triggers:
                if trigger in recognized_speech.lower():
                    entry = trigger_map[trigger]
                    print('Entry', entry)
                    processor = entry.get(PROCESSOR_KEY)

                    processor_module = importlib.import_module(
                        f'processors.{processor}')
                    processor_class = getattr(processor_module, processor)

                    if not issubclass(processor_class, SpeechProcessor):
                        continue

                    trigger_speech = random.choice(entry.get(SPEECHES_KEY))
                    speech = Speech(
                        trigger_key=trigger,
                        trigger_speech=recognized_speech,
                        speech=trigger_speech.get(SPEECH_KEY),
                        language=trigger_speech.get(LANG_KEY),
                    )

            if speech:
                # text to speech
                tts = speech.processed(processor_class())
                filename = f'media/{hash((speech.trigger_key, speech.speech, processor_class))}.mp3'
                if not Path(filename).is_file():
                    gTTS(tts, lang=speech.language).save(filename)

                playsound(filename)

        except sr.UnknownValueError:
            print("I could not understand the audio")
        except sr.RequestError as e:
            print("Error; {0}".format(e))
 def __init__(self):
     QtWidgets.QMainWindow.__init__(self, parent=None)
     self.ui = uic.loadUi('main.ui', self)
     self.ui.show()
     self.btn_array = ButtonArray()
     self.speech = Speech()
     # Add button to the btn array
     for btn in self.create_buttons():
         self.btn_array.add_btn(btn)
Exemple #7
0
def main():
    # sys.argv +=['--style', 'material`']
    QGuiApplication.setAttribute(Qt.AA_EnableHighDpiScaling)
    app = QGuiApplication(sys.argv)
    speech = Speech()
    settings = Settings()
    engine = QQmlApplicationEngine()
    engine.rootContext().setContextProperty('speech', speech)
    engine.rootContext().setContextProperty('settings', settings)
    engine.load(os.path.join(os.path.dirname(__file__), 'qml/main.qml'))
    if not engine.rootObjects():
        sys.exit(-1)
    sys.exit(app.exec_())
    def __init__(self, session, ip, motion, leds):
        self.__session = session
        self.__speech = Speech(self.__session)
        self.__showScreen = ShowScreen(self.__session, ip)
        self.__showScreen.show_screen("")
        self.__game = TicTacToe(self.__session, self.__speech,
                                self.__showScreen)
        self.__moveReader = MoveReader(self.__session, self.__game)
        self.__speechDetection = SpeechDetection(self.__session,
                                                 ["Let's Play", "Stop"],
                                                 self.speech_callback)
        self.__animationPlayer = AnimationPlayer(self.__session, motion)
        self.__waiting_game_start = 1
        self.__playing = 1
        self.__program_running = 1
        self.__motion = motion
        self.__leds = leds
        while self.__program_running:
            while self.__waiting_game_start:
                # Add the following for starting on Enter
                #input = raw_input('Say ´lets play´ or press enter to start a game.')
                #if input == "":
                #    self.__speechDetection.unsubscribe()
                #    self.look_at_player_and_say("Okay, Let's do this!", False)
                #    self.__waiting_game_start = 0
                #    self.__showScreen.show_screen("")
                #    self.__game.restart()
                #    self.__playing = 1
                pass
            while self.__playing:
                delta = 0
                self.__speech.say("Now make your move")
                while delta != 1:
                    time.sleep(0.5)
                    delta = self.__moveReader.read_move()
                    if delta > 1:
                        self.look_at_player_and_say(
                            "Too many moves detected! Restore my image board",
                            True)
                        time.sleep(1)

                self.__showScreen.show_screen(self.__game.get_url_params())
                print self.__game.get_url_params()

                delta2 = 1
                while delta2 != 0:
                    delta2 = self.__moveReader.check_correct_board()
                    if delta2 > 0:
                        self.look_at_player_and_say("Wrong move.", True)
                        self.__game.say_move(self.__game.move_player2)
                    time.sleep(1)

                self.__playing = self.__game.player_won() == 0
            winner = self.__game.player_won()
            if winner == 3:
                self.__animationPlayer.play_draw_animation()
            if winner == 2:
                self.__animationPlayer.play_win_animation()
            if winner == 1:
                self.__animationPlayer.play_lose_animation()
            print "End of Game"
            self.__waiting_game_start = 1
            self.__playing = 1
            self.__speechDetection = SpeechDetection(self.__session,
                                                     ["Let's Play", "Stop"],
                                                     self.speech_callback)
        self.__speechDetection.unsubscribe()
class GameHandler(object):
    def __init__(self, session, ip, motion, leds):
        self.__session = session
        self.__speech = Speech(self.__session)
        self.__showScreen = ShowScreen(self.__session, ip)
        self.__showScreen.show_screen("")
        self.__game = TicTacToe(self.__session, self.__speech,
                                self.__showScreen)
        self.__moveReader = MoveReader(self.__session, self.__game)
        self.__speechDetection = SpeechDetection(self.__session,
                                                 ["Let's Play", "Stop"],
                                                 self.speech_callback)
        self.__animationPlayer = AnimationPlayer(self.__session, motion)
        self.__waiting_game_start = 1
        self.__playing = 1
        self.__program_running = 1
        self.__motion = motion
        self.__leds = leds
        while self.__program_running:
            while self.__waiting_game_start:
                # Add the following for starting on Enter
                #input = raw_input('Say ´lets play´ or press enter to start a game.')
                #if input == "":
                #    self.__speechDetection.unsubscribe()
                #    self.look_at_player_and_say("Okay, Let's do this!", False)
                #    self.__waiting_game_start = 0
                #    self.__showScreen.show_screen("")
                #    self.__game.restart()
                #    self.__playing = 1
                pass
            while self.__playing:
                delta = 0
                self.__speech.say("Now make your move")
                while delta != 1:
                    time.sleep(0.5)
                    delta = self.__moveReader.read_move()
                    if delta > 1:
                        self.look_at_player_and_say(
                            "Too many moves detected! Restore my image board",
                            True)
                        time.sleep(1)

                self.__showScreen.show_screen(self.__game.get_url_params())
                print self.__game.get_url_params()

                delta2 = 1
                while delta2 != 0:
                    delta2 = self.__moveReader.check_correct_board()
                    if delta2 > 0:
                        self.look_at_player_and_say("Wrong move.", True)
                        self.__game.say_move(self.__game.move_player2)
                    time.sleep(1)

                self.__playing = self.__game.player_won() == 0
            winner = self.__game.player_won()
            if winner == 3:
                self.__animationPlayer.play_draw_animation()
            if winner == 2:
                self.__animationPlayer.play_win_animation()
            if winner == 1:
                self.__animationPlayer.play_lose_animation()
            print "End of Game"
            self.__waiting_game_start = 1
            self.__playing = 1
            self.__speechDetection = SpeechDetection(self.__session,
                                                     ["Let's Play", "Stop"],
                                                     self.speech_callback)
        self.__speechDetection.unsubscribe()

    def say_reaction(self, reaction_text):
        self.__speech.say(text=reaction_text)
        return True

    def do_animation(self, animation):
        return animation

    def speech_callback(self, value):
        print(value)
        if value[0] == "Let's Play":
            if value[1] > 0.3:
                self.__speechDetection.unsubscribe()
                self.look_at_player_and_say("Okay, Let's do this!", False)
                self.__waiting_game_start = 0
                self.__showScreen.show_screen("")
                self.__game.restart()
        if value[0] == "Stop":
            if value[1] > 0.4:
                self.__playing = 0
                self.__waiting_game_start = 0
                self.__program_running = 0

    def look_at_player_and_say(self, text, warning_led):
        self.__motion.setStiffnesses("Head", 1.0)
        names = ["HeadYaw", "HeadPitch"]
        angles = [78. * almath.TO_RAD, -30. * almath.TO_RAD]
        times = [1., 1.]
        is_absolute = True
        self.__motion.angleInterpolation(names, angles, times, is_absolute)
        if warning_led:
            qi. async (self.rotate_eyes, delay=0)
        self.__speech.say(text=text)
        angles = [0., 0.]
        times = [1., 1.]
        is_absolute = True
        self.__motion.angleInterpolation(names, angles, times, is_absolute)

    def rotate_eyes(self):
        self.__leds.setIntensity('RightFaceLedsRed', 1.0)
        self.__leds.setIntensity('LeftFaceLedsRed', 1.0)
        self.__leds.rotateEyes(16711680, 1, 6)
Exemple #10
0
class Main(QtWidgets.QMainWindow):
    def __init__(self):
        QtWidgets.QMainWindow.__init__(self, parent=None)
        self.ui = uic.loadUi('main.ui', self)
        self.ui.show()
        self.btn_array = ButtonArray()
        self.speech = Speech()
        # Add button to the btn array
        for btn in self.create_buttons():
            self.btn_array.add_btn(btn)

    def on_btn_clicked(self, text):
        """
        Common slot for all
        :return:
        """
        db_logger = LogData()
        db_logger.add_data(text)
        self.speech.speak(text)
        print (text)

    ### All methods RPC methods ###
    def get_btns(self):
        debug_logger.debug(str(self.btn_array))
        return str(self.btn_array)

    def set_btn_text(self, id, text):
        try:
            self.btn_array[id].setText(text)
        except KeyError:
            pass

    ### RPC Calback Methods End ###

    def create_dispatcher(self):
        dispatcher = {'get_btns':     self.get_btns,
                      'set_btn_text': self.set_btn_text,
                      'update_image': self.update_btn,
                      'get_log_data': self.get_log_data}
        return dispatcher

    def update_btn(self, id, text, file_name):
        # Add to the database
        btn_db = BtnDb()
        btn_db.add_data(id, text, file_name)
        btn = self.btn_array.get_btn(int(id))
        btn.set_text(text)
        btn.set_pic(QtGui.QIcon(file_name), QtCore.QSize(btn.btn_width(), btn.btn_height()))
        btn.set_uri(file_name)

    def recreate_btn(self, id):
        pass

    def get_log_data(self):
        db_logger = LogData()
        return db_logger.get_data()

    def create_buttons(self):
        qbtns = [self.ui.btn1,
                 self.ui.btn2,
                 self.ui.btn3,
                 self.ui.btn4,
                 self.ui.btn5,
                 self.ui.btn6,
                 self.ui.btn7,
                 self.ui.btn8,
                 self.ui.btn9,
                 self.ui.btn10,
                 self.ui.btn11,
                 self.ui.btn12]

        btn_db = BtnDb()
        row = btn_db.get_btns()
        btns = []
        for i, btn in enumerate(row):
            btns.append(Button.builder()
                        .btn(qbtns[int(btn[0])])
                        .id(int(btn[0]))
                        .text(btn[1])
                        .pic(QtGui.QIcon(btn[2]))
                        .uri(btn[2])
                        .icon_size(QtCore.QSize(qbtns[i].width(), qbtns[i].height()))
                        .callback(self.on_btn_clicked)
                        .build())

        return btns
Exemple #11
0
 def __init__(self, session, motion):
     self.session = session
     self.animation_service = session.service("ALAnimationPlayer")
     self.animation_path = 'animations/Stand/'
     self.__speech = Speech(session=session)
     self.__motion = motion
Exemple #12
0
class AnimationPlayer:
    def __init__(self, session, motion):
        self.session = session
        self.animation_service = session.service("ALAnimationPlayer")
        self.animation_path = 'animations/Stand/'
        self.__speech = Speech(session=session)
        self.__motion = motion

    def reaction_won(self):
        self.animation_service.run(self.animation_path +
                                   'Emotions/Positive/Happy_4')
        rest_position(session=self.session)
        return True

    def reaction_lost(self):
        self.animation_service.run(self.animation_path +
                                   'Emotions/Neutral/Embarrassed_1')
        rest_position(session=self.session)
        return True

    def reaction_draw(self):
        self.animation_service.run(self.animation_path +
                                   'Gestures/IDontKnow_1')
        rest_position(session=self.session)
        return True

    def play_win_animation(self):
        future_say = qi. async (self.say_reaction,
                                "I won the game!",
                                delay=500)
        future_animation = qi. async (self.do_animation,
                                      self.reaction_won(),
                                      delay=0)
        while future_say == False or future_animation == False:
            time.sleep(0.5)
        self.look_at_player_and_say("Good luck next time!")

    def play_draw_animation(self):
        future_say = qi. async (self.say_reaction,
                                "Sadly no one won.",
                                delay=500)
        future_animation = qi. async (self.do_animation,
                                      self.reaction_draw(),
                                      delay=0)
        while future_say == False or future_animation == False:
            time.sleep(0.5)
        self.look_at_player_and_say("Let's try again!")

    def play_lose_animation(self):
        future_say = qi. async (self.say_reaction,
                                "You won the Game!",
                                delay=500)
        future_animation = qi. async (self.do_animation,
                                      self.reaction_draw(),
                                      delay=0)
        while future_say == False or future_animation == False:
            time.sleep(0.5)
        self.look_at_player_and_say("Congratulations!")

    def look_at_player_and_say(self, text):
        self.__motion.setStiffnesses("Head", 1.0)
        names = ["HeadYaw", "HeadPitch"]
        angles = [78. * almath.TO_RAD, -30. * almath.TO_RAD]
        times = [1., 1.]
        is_absolute = True
        self.__motion.angleInterpolation(names, angles, times, is_absolute)
        self.__speech.say(text=text)
        angles = [0., 0.]
        times = [1., 1.]
        is_absolute = True
        self.__motion.angleInterpolation(names, angles, times, is_absolute)

    def say_reaction(self, reaction_text):
        self.__speech.say(text=reaction_text)
        return True

    def do_animation(self, animation):
        return animation
Exemple #13
0
from Speech import Speech
from Bomb import Bomb
import modules.Wires as Wires
import modules.Button as Button
import modules.Simon as Simon
import modules.Memory as Memory

r = Speech()
r.calibrate(duration=5)

b = Bomb()

while True:
    print("1. On the Subject of Wires (defuse wires)")
    print("2. On the Subject of The Button (defuse button)")
    print("4. On the Subject of Simon Says (defuse simon says)")
    print("6. On the Subject of Memory (defuse memory)")
    print("12. Update Strikes (strikes #)")
    print("13. Run Setup (bomb setup)")
    print("14. Undo Memory (undo memory)")
    print("15. Reset Memory (reset memory)")
    r.say("Select a module")
    choice = r.listen("grammars/menu.gram")
    # select module
    if choice == "defuse wires":
        Wires.solve(r, b)
    elif choice == "defuse button":
        Button.solve(r, b)
    elif choice == "defuse simon says":
        Simon.solve(r, b)
    elif choice == "defuse memory":