Ejemplo n.º 1
0
    def conversation(self, deadline=DEFAULT_GRPC_DEADLINE):
        """
        Starts a conversation with the Google Assistant.

        The device begins listening for your query or command and will wait indefinitely.
        Once it completes a query/command, it returns to listening for another.

        Args:
            deadline: The amount of time (in milliseconds) to wait for each gRPC request to
                complete before terminating.
        """
        keep_talking = True
        while keep_talking:
            playing = False
            with Recorder() as recorder:  #removed bytes player
                #play = player.play(AUDIO_FORMAT)

                def wrapped_play(data):
                    nonlocal playing
                    if not playing:
                        self._playing_started()
                        #playing = True
                    #play(data)

                try:
                    keep_talking = self._assist(recorder, wrapped_play,
                                                deadline)
                finally:
                    #play(None)       # Signal end of sound stream.
                    recorder.done()  # Signal stop recording.

            if playing:
                self._playing_stopped()
        return self._user_text
Ejemplo n.º 2
0
    def conversation(self, deadline=DEFAULT_GRPC_DEADLINE):
        listen_flag = False
        keep_talking = True
        while keep_talking:
            playing = False
            with Recorder() as recorder, BytesPlayer() as player:
                play = player.play(AUDIO_FORMAT)

                def wrapped_play(data):
                    nonlocal playing
                    if not playing:
                        self._playing_started()
                        playing = True
                    play(data)

                try:
                    keep_talking, listen_flag = self._assist(
                        recorder, wrapped_play, deadline)
                finally:
                    play(None)  # Signal end of sound stream.
                    recorder.done()  # Signal stop recording.

            if listen_flag:
                return True

            if playing:
                self._playing_stopped()

        return False
Ejemplo n.º 3
0
    def recognize(self,
                  language_code='en-US',
                  alternative_language_codes=None,
                  hint_phrases=None):
        """
        Performs speech-to-text for a single utterance using the default ALSA soundcard driver.
        Once it detects the user is done speaking, it stops listening and delivers the top
        result as text.

        By default, this method calls :meth:`start_listening` and :meth:`stop_listening` as the
        recording begins and ends, respectively.

        Args:
            language_code:  Language expected from the user, in IETF BCP 47 syntax (default is
                "en-US"). See the `list of Cloud's supported languages`_.
            hint_phrase: A list of strings containing words and phrases that may be expected from
                the user. These hints help the speech recognizer identify them in the dialog and
                improve the accuracy of your results.

        Returns:
            The text transcription of the user's dialog.
        """
        streaming_config = speech_v1p1beta1.types.StreamingRecognitionConfig(
            config=self._make_config(language_code, alternative_language_codes,
                                     hint_phrases),
            single_utterance=True)

        with Recorder() as recorder:
            chunks = recorder.record(AUDIO_FORMAT,
                                     chunk_duration_sec=0.1,
                                     on_start=self.start_listening,
                                     on_stop=self.stop_listening)

            requests = (speech_v1p1beta1.types.StreamingRecognizeRequest(
                audio_content=data) for data in chunks)
            responses = self._client.streaming_recognize(
                config=streaming_config, requests=requests)

            for response in responses:
                if response.speech_event_type == END_OF_SINGLE_UTTERANCE:
                    recorder.done()

                for result in response.results:
                    if result.is_final:
                        return result.alternatives[0].transcript

        return None
Ejemplo n.º 4
0
    def conversation2(self, deadline=DEFAULT_GRPC_DEADLINE):
        """
        Starts a conversation with the Google Assistant.

        The device begins listening for your query or command and will wait indefinitely.
        Once it completes a query/command, it returns to listening for another.

        Args:
            deadline: The amount of time (in milliseconds) to wait for each gRPC request to
                complete before terminating.
        """
        keep_talking = True
        while keep_talking:
            playing = False
            with Recorder() as recorder, BytesPlayer() as player:
                play = player.play(AUDIO_FORMAT)

                def wrapped_play(data):
                    nonlocal playing
                    if not playing:
                        self._playing_started()
                        playing = True
                    play(data)

                try:
                    logger.info(
                        "FINALLYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY YO SHIT"
                    )
                    keep_talking = self._assist_2(recorder, wrapped_play,
                                                  deadline)
                finally:
                    #play(None)       # Signal end of sound stream.
                    recorder.done()  # Signal stop recording.

            if playing:
                self._playing_stopped()
        logger.info("WE can start recording now yo f**k")
        self.record_candy()
Ejemplo n.º 5
0
    def recognize(self, language_code='en-US', hint_phrases=None):
        streaming_config=speech.types.StreamingRecognitionConfig(
            config=self._make_config(language_code, hint_phrases),
            single_utterance=True)

        with Recorder() as recorder:
            chunks = recorder.record(AUDIO_FORMAT,
                                     chunk_duration_sec=0.1,
                                     on_start=self.start_listening,
                                     on_stop=self.stop_listening)

            requests = (speech.types.StreamingRecognizeRequest(audio_content=data) for data in chunks)
            responses = self._client.streaming_recognize(config=streaming_config, requests=requests)

            for response in responses:
                if response.speech_event_type == END_OF_SINGLE_UTTERANCE:
                    recorder.done()

                for result in response.results:
                    if result.is_final:
                        return result.alternatives[0].transcript

        return None
Ejemplo n.º 6
0
    def start(self,
              detected_callback=callbacks,
              interrupt_check=interrupt_callback,
              sleep_time=0.02):
        """
        Start the voice detector. For every `sleep_time` second it checks the
        audio buffer for triggering keywords. If detected, then call
        corresponding function in `detected_callback`, which can be a single
        function (single model) or a list of callback functions (multiple
        models). Every loop it also calls `interrupt_check` -- if it returns
        True, then breaks from the loop and return.

        :param detected_callback: a function or list of functions. The number of
                                  items must match the number of models in
                                  `decoder_model`.
        :param interrupt_check: a function that returns True if the main loop
                                needs to stop.
        :param float sleep_time: how much time in second every loop waits.
        :return: None
        """
        self._running = True

        global interrupted
        interrupted = False

        if interrupt_check():
            logger.debug("detect voice return")
            return

        tc = type(detected_callback)
        if tc is not list:
            detected_callback = [detected_callback]
        if len(detected_callback) == 1 and self.num_hotwords > 1:
            detected_callback *= self.num_hotwords

        assert self.num_hotwords == len(detected_callback), \
            "Error: hotwords in your models (%d) do not match the number of " \
            "callbacks (%d)" % (self.num_hotwords, len(detected_callback))

        logger.debug("detecting...")

        while self._running is True:
            with Recorder() as recorder:
                for data in recorder.record(AUDIO_FORMAT,
                                            chunk_duration_sec=0.02,
                                            on_start=self.start_detect,
                                            on_stop=self.stop_detect):

                    ans = self.detector.RunDetection(data)
                    if ans == -1:
                        logger.warning(
                            "Error initializing streams or reading audio data")
                        recorder.done()
                    elif ans > 0:
                        recorder.done()
                        message = "Keyword " + str(ans) + " detected at time: "
                        message += time.strftime("%Y-%m-%d %H:%M:%S",
                                                 time.localtime(time.time()))
                        logger.info(message)
                        callback = detected_callback[ans - 1]
                        if callback is not None:
                            callback()

            if interrupt_check():
                logger.debug("detect voice break")
                recorder.done()
                break
        logger.debug("finished.")