Example #1
0
def main():
    # start to listen background with another thread.
    start_listen_background()

    while not os.path.exists(BG_WAV_PATH):
        print('ready for bg wav ...')
        time.sleep(1)

    # initialize recognizer
    recognizer = Recognizer()
    recognizer.speaking_duration = 0.1
    recognizer.phrase_threshold = 0.1
    with Microphone(sample_rate=SAMPLE_RATE) as source:
        # listen for 1 second to calibrate the energy threshold for ambient noise levels
        recognizer.adjust_for_ambient_noise(source)
        print("Calibrated. Say something!")

    source = Microphone(sample_rate=SAMPLE_RATE)
    with Pool(THREAD_NUM) as pool:
        callback_with_model = partial(callback,
                                      model_map=ModelMap(),
                                      pool=pool)
        recognizer.listen_in_background(source, callback_with_model,
                                        PHRASE_TIME_LIMIT)
        while True:
            time.sleep(10)
Example #2
0
class MicrophoneMonitor(object):
    def __init__(self) -> None:
        self.micro = Recognizer()
        self.micro.energy_threshold = 4000
        self.micro.pause_threshold = 0.5

    def monitor_microphone(self, hotword='jarvis'):
        with Microphone() as source:
            self.micro.adjust_for_ambient_noise(source, duration=0.5)
            while True:
                print('Aguardando comando: ')
                audio = self.micro.listen(source)
                try:
                    trigger = self.micro.recognize_google(audio, language='pt')
                    # with open("microphone-results.wav", "wb") as f:
                    #     f.write(audio.get_wav_data())
                    print(trigger)
                    if hotword.lower() in trigger or hotword.capitalize(
                    ) in trigger:
                        print('Comando: ', trigger)
                        return trigger
                except UnknownValueError:
                    print(
                        "Google Speech Recognition could not understand audio")
                except RequestError as e:
                    print(
                        "Could not request results from Google Speech Recognition service; {0}"
                        .format(e))
Example #3
0
class Ear:  # pylint: disable=too-few-public-methods
    """
    listen() -- take audio from microphone, recognize it and return as string
    """
    def __init__(self):
        self.recognizer = Recognizer()

    def listen(self):
        """
        listen audio from microphone and convert to the string.
        :return:data
        """
        self.recognizer = Recognizer()
        with Microphone() as source:
            self.recognizer.adjust_for_ambient_noise(source)
            print("I am listening you...")
            audio = self.recognizer.listen(source, phrase_time_limit=6)

        data = ""
        try:
            # Uses the default API key
            # To use another API key:
            # `r.recognize_google(audio, key="GOOGLE_SPEECH_RECOGNITION_API_KEY")`
            data = self.recognizer.recognize_google(audio, language="tr")
            print("You said : " + data)
        except UnknownValueError:
            print("Google Speech Recognition could not understand audio")
            return self.listen()
        except RequestError as exception:
            print("Could not request results from "
                  "Google Speech Recognition service; {0}".format(exception))
            return self.listen()

        return data.lower()
Example #4
0
def escuchar():
    print("Escuchando...")
    recognizer = Recognizer()
    microfono = Microphone()

    with microfono:
        recognizer.adjust_for_ambient_noise(microfono)

    recognizer.listen_in_background(microfono, callback)
 def listen(self):
     try:
         with Microphone() as source:
             recognizer = Recognizer()
             recognizer.adjust_for_ambient_noise(source)
             audio = recognizer.listen(source)
             return recognizer.recognize_google(audio,
                                                language=self.lang).lower()
     except (UnknownValueError, RequestError):
         return ''
Example #6
0
class SphinxDecoder():
    def __init__(self):
        self.MODELDIR = 'speech/'
        self.wav_name = 'media/temp.wav'
        self.raw_name = 'media/temp.raw'

        config = Decoder.default_config()
        config.set_string('-hmm', self.MODELDIR + 'ru_ru/')
        config.set_string('-dict', self.MODELDIR + 'ru.dic')
        self.decoder = Decoder(config)

        jsgf = Jsgf(self.MODELDIR + 'gr.gram')
        rule = jsgf.get_rule('gr.rule')
        fsg = jsgf.build_fsg(rule, self.decoder.get_logmath(), 7.5)
        fsg.writefile('gr.fsg')

        self.decoder.set_fsg('gr', fsg)
        self.decoder.set_search('gr')

        self.rec = Recognizer()
        self.mic = Microphone()

    def wav_to_raw(self):
        audio_file = AudioSegment.from_wav(self.wav_name)
        audio_file = audio_file.set_frame_rate(16000)
        audio_file.export(self.raw_name, format='raw')

    def record_audio(self):
        with self.mic as source:
            self.rec.adjust_for_ambient_noise(source)

            system('aplay media/beep.wav')
            audio = self.rec.listen(source)
            with open(self.wav_name, 'wb') as new_audio:
                new_audio.write(audio.get_wav_data())

        self.wav_to_raw()

    def get_from_audio(self):
        self.record_audio()

        self.decoder.start_utt()
        stream = open(self.raw_name, 'rb')
        while True:
            buf = stream.read(1024)
            if buf:
                self.decoder.process_raw(buf, False, False)
            else:
                break
        self.decoder.end_utt()
        stream.close()
        try:
            return self.decoder.hyp().hypstr
        except:
            return None
Example #7
0
def gettingWordsFromMic():
    mic = Microphone()
    recognizer = Recognizer()
    print("Say something...")
    print(Microphone.list_microphone_names())
    with mic as source:
        recognizer.adjust_for_ambient_noise(source)
        audio = recognizer.listen(source)

    input("Press a key to process")
    print(recognizer.recognize_google(audio))
Example #8
0
def calibrate_microphone(mic: sr.Microphone, r: sr.Recognizer, duration=5, warmup_duration=0):

    print(f"Calibrating microphone for {duration} seconds.")
    with mic as source:
        if warmup_duration > 0:
            time.sleep(warmup_duration)
            empty_stream(source, time_step=0.1)

        # listen for "duration" seconds and create the ambient noise energy level
        r.adjust_for_ambient_noise(source, duration=duration)

    print("Calibrated energy threshold: ", r.energy_threshold)
def listen():
    print("Say something!")
    r = Recognizer()
    m = Microphone()
    with m as src:
        # Deal with ambient noise since microphone output is rather unpredictable
        r.adjust_for_ambient_noise(src)
    # Start listening
    r.listen_in_background(m, callback)
    # Keep listening even though main thread is blocked
    while True:
        sleep(1)
Example #10
0
class Listener:
    def __init__(self) -> None:
        super(Listener, self).__init__()
        self._recognizer = Recognizer()
        self._recognizer.energy_threshold = 4000

    def listen(self) -> None:
        """Listens while the microphone is open and turns the audio into readable text."""
        with Microphone() as source:
            print('listening...')
            self._recognizer.adjust_for_ambient_noise(source)
            audio_listened = self._recognizer.listen(source)
            text_listened: Any = self._recognizer.recognize_google(
                audio_listened, )
            self.text: str = text_listened.lower()
Example #11
0
 def myCommand(self):
     #listens for commands
     r = Recognizer()
     with Microphone() as source:
         print('Say something...')
         r.pause_threshold = 1
         r.adjust_for_ambient_noise(source, duration=1)
         audio = r.listen(source)
     try:
         command = r.recognize_google(audio).lower()
         print('You said: ' + command + '\n')
     # loop back to continue to listen for commands if unrecognizable speech is received
     except UnknownValueError:
         print('....')
         command = speech.myCommand(self)
     return command
Example #12
0
class Listener(object):
    def __init__(self):
        self.__recognizer = Recognizer()

    def __get_audio_from_file(self, filename, timeout=None, on_listen=None):
        with AudioFile(filename) as source:
            return self.__get_audio_from_source(source, timeout, on_listen)

    def __get_audio_from_microphone(self, timeout=None, on_listen=None):
        with Microphone() as source:
            self.__recognizer.adjust_for_ambient_noise(source, duration=1.0)
            return self.__get_audio_from_source(source, timeout, on_listen)

    def __get_audio_from_source(self, source, timeout=None, on_listen=None):
        if callable(on_listen): on_listen()
        return self.__recognizer.listen(source, timeout=timeout)

    def __recognize(self, audio, language, on_recognition=None):
        if callable(on_recognition): on_recognition()
        return self.__recognizer.recognize_google(audio, language=language)

    def get_text_from_audio(self,
                            filename,
                            language="en-us",
                            timeout=None,
                            on_listen=None,
                            on_recognition=None):
        try:
            audio = self.__get_audio_from_file(filename, timeout, on_listen)
            return self.__recognize(audio, language, on_recognition)
        except FileNotFoundError as error:
            raise error
        except:
            return str()

    def speech_to_text(self,
                       language="en-us",
                       timeout=None,
                       on_listen=None,
                       on_recognition=None):
        try:
            audio = self.__get_audio_from_microphone(timeout, on_listen)
            return self.__recognize(audio, language, on_recognition)
        except:
            return str()
Example #13
0
class Assistant():

    def __init__(self, name,  mic):
        self.name = name
        self.mic = mic
        self.recognizer = Recognizer()
        self.awake = False

    def listen(self):
        with self.mic:
            self.recognizer.adjust_for_ambient_noise(self.mic, 2)
            print("Starting listener...")
        self.stop = self.recognizer.listen_in_background(self.mic, self._proc_audio)

    def stop(self):
        raise RuntimeError("Attempted to stop assistant before starting it")

    def wake_up(self):
        self.awake = True
        def _timeout():
            self.awake = False
        timer = Timer(30, _timeout)
        timer.start()

    def _proc_audio(self, recog, audio):
        try:
            # decoded = recog.recognize_sphinx(audio, show_all=True)
            # txt = decoded.hyp().hypstr
            txt = recog.recognize_google(audio, show_all=True)
            print(f"recognized utterance: {txt}")
        except UnknownValueError as e:
            # txt = recog.recognize_google(audio)
            print("Did not recognize utterance")
            # print(txt)
        if self.name in txt:
            self.wake_up()
        if self.awake and not txt.endswith(self.name):
            print(f"Sending commands: {txt}")
            pixel_ring.think()
            # send command to HA here
            pixel_ring.off()
            self.awake = False
        else:
            print(f"Not awake or name was give without command")
        return
Example #14
0
def read_mic_input(r: sr.Recognizer, mic: sr.Microphone) -> Dict:
    """Use r to transcribe speech recorded from mic, and return a dictionary
    containing three keys:

    "success": boolean value indicating whether or not the recognizer's speech
               transcription was successful or not
    "error": 'None' if no error occured, or a string containing the error
              message if an error occured.
    "transcription": 'None' if speech could not be transcribed, or a string
                      containing the transcribed text.
    """

    with mic as source:
        # adjust the recognizer sensitivity to account for ambient noise
        r.adjust_for_ambient_noise(source, duration=0.3)
        # Record voice input from microhpone
        audio = r.listen(source)

    # intialize the response dictionary to be returned
    response = {"success": True, "error": None, "transcription": None}

    # Attempt to recognize speech in the recording
    try:
        response["transcription"] = r.recognize_google(audio).lower()

        # clean up the transcription of coordinates and measurements
        response["transcription"] = response["transcription"].replace("-", " ")
        response["transcription"] = response["transcription"].replace("/", " ")
        response["transcription"] = response["transcription"].replace(
            "\\", " ")
        response["transcription"] = response["transcription"].replace(
            " 00", " 0 0")

    # Update response object if a RequestError or UnknownValueError exception is
    #   caught
    except sr.RequestError:
        # API was unreachable or unresponsive
        response["success"] = False
        response["error"] = "Error occurred with the API request."
    except sr.UnknownValueError:
        # speech could not be transcribed
        response["error"] = "Unable to recognize speech."

    return response
Example #15
0
class TriggerListener():
    def __init__(self):
        self.recognizer = Recognizer()
        self.microphone = Microphone(device_index=0, sample_rate=16000, chunk_size=256)
        self.is_trigger_listener_active = None
        self.start_listener()

    def stop_listen_for_trigger(self):
        print("Stopping listener for trigger")
        self.stop_listening(wait_for_stop=False)
        self.is_trigger_listener_active = False

    def process_trigger(self, data):
        if "computer" in data.lower():
            print("computer found")
            self.stop_listen_for_trigger()

    def listen_for_trigger(self, recognizer, audio):
        try:
            print("Sound detected...")
            trigger = recognizer.recognize_sphinx(audio, keyword_entries=[('computer', 0.5)])
            if trigger:
                print(f"Trigger detected! - {trigger}")
                self.process_trigger(trigger)
        except sr.UnknownValueError:
            print("Sphinx could not understand audio")
        except sr.RequestError as e:
            print("Sphinx error; {0}".format(e))

    def start_listener(self):
        print("Starting trigger listener")
        self.is_trigger_listener_active = True
        self.recognizer.pause_threshold = 0.5

        with self.microphone as source:
            self.recognizer.adjust_for_ambient_noise(source, duration=2.0)

        self.stop_listening = self.recognizer.listen_in_background(self.microphone, self.listen_for_trigger, phrase_time_limit=1.5)

        while self.is_trigger_listener_active:
            print("Listening...")
            time.sleep(1.0)

        print("TriggerListener finished!")
Example #16
0
def listener(arg):
    r = None
    while True:
        if r is not None:
            nn_print('INIT', Color.gray)
        r = Recognizer()
        with Microphone(device_index=None if arg.M == -1 else arg.M,
                        sample_rate=arg.R) as source:
            r.adjust_for_ambient_noise(source)
            nn_print('RECORD', Color.red)
            audio = r.listen(source,
                             phrase_time_limit=arg.L if arg.L else None)

        nn_print('RECOGNITION', Color.blue, sp=False)
        data = audio.get_raw_data(SAMPLE_RATE, 2)
        print(' {}'.format(pretty_size(len(data))), end='\r', flush=True)
        text = stt(data, arg.S)
        nn_print('RESULT', Color.green, sp=False)
        print(' {}'.format(text))
Example #17
0
def listen():
    r = Recognizer()  # less writing
    with Microphone() as source:  # using microphone to detect audio
        r.adjust_for_ambient_noise(
            source, duration=0.5)  #adjust for ambiet sounds for 1 second
        audio = r.listen(source)  # listen for audio
    data = ''  # set data ad nothing
    try:  # in case of errors
        data = r.recognize_google(
            audio)  # recognize with google's speech recognition
        print('You said: ' + data)  # write what was heard
    except UnknownValueError:  # unknown audio
        print('I didn\'t get that')  # when google api didn't understand audio
        data = 'None'  # return none
    except RequestError as e:  # request error
        print('Api or connection is not working.\n The error is {0}'.format(
            e))  # when connection or Api offline
        data = 'Broken'  # return broken
    return data  # return recognized audio as string
Example #18
0
class RecognizerOfAudioSource(AudioSource):
    """Represent recogniser of audio source."""

    def __init__(self) -> None:
        self._recogniser = Recognizer()

    def __enter__(self) -> Recognizer:
        return self._recogniser.__enter__()

    def __exit__(self, exc_type, exc_val, exc_tb) -> None:
        return self._recogniser.__exit__(exc_type, exc_val, exc_tb)

    @property
    def pause(self) -> int:
        """Return seconds of non-speaking audio before a phrase is considered complete"""
        return self._recogniser.pause_threshold

    @pause.setter
    def pause(self, seconds: int) -> None:
        if not isinstance(seconds, int):
            raise TypeError('Seconds should be <int> data type.')

    def configure_noise(self, source: Microphone, duration: int = 1) -> None:
        self._recogniser.adjust_for_ambient_noise(source, duration)

    def listen(
            self,
            source: Microphone,
            timeout: int = None,
            phrase_time_limit: int = None,
            config: str= None
    ) -> AudioData:
        return self._recogniser.listen(source, timeout, phrase_time_limit, config)

    def recognise_google(
            self,
            audio_data: AudioData,
            key: int = None,
            language: str = "en-US",
            show_all: bool = False
    ) -> str:
        return self._recogniser.recognize_google(audio_data, key, language, show_all).lower()
def record_and_recognise(r: sr.Recognizer) -> str:
    with sr.Microphone() as source:
        print("Слушаю..")
        r.pause_threshold = 1
        r.adjust_for_ambient_noise(source, duration=1)
        audio = r.listen(source, phrase_time_limit=5)

    try:
        recognized = r.recognize_google(audio, language='ru-RU')
        if type(recognized) == list:
            recognized_string = " ".join(recognized)
            return recognized_string
        else:
            return recognized
    except sr.UnknownValueError:
        print(
            "Unknown sounds came from your mouth. Or perhaps we can't hear you."
        )
    except sr.RequestError as e:
        print("Sound recognition exception; {0}".format(e))
Example #20
0
def gettingWordsFromAudio():
    print(version)

    r = Recognizer()

    print("captures any speech")
    harvard = AudioFile('harvard.wav')

    with harvard as source:
        audio = r.record(source)

    print(type(audio))
    print(r.recognize_google(audio))

    print("")
    print("")
    print("captures any speech in the first four seconds of the file")
    with harvard as source:
        audio = r.record(source, duration=4)

    print(r.recognize_google(audio))

    print("")
    print("")
    print(
        "The record() method, when used inside a with block, always moves ahead in the file stream."
    )
    with harvard as source:
        audio1 = r.record(source, duration=4)
        audio2 = r.record(source, duration=4)

    print(r.recognize_google(audio1))
    print(r.recognize_google(audio2))

    print("")
    print("")
    print(
        "To capture only the second phrase in the file, you could start with an offset of four seconds and record for, say, three seconds."
    )
    with harvard as source:
        audio = r.record(source, offset=4, duration=3)

    print(r.recognize_google(audio))

    print("")
    print("")
    print("****************")
    print("noisy audio")
    jackhammer = AudioFile('jackhammer.wav')
    with jackhammer as source:
        audio = r.record(source)

    print(r.recognize_google(audio))

    print("")
    print("")
    print(
        "The adjust_for_ambient_noise() method reads the first second of the file stream and calibrates the recognizer to the noise level of the audio."
    )
    with jackhammer as source:
        r.adjust_for_ambient_noise(source, duration=1)
        audio = r.record(source)

    print(r.recognize_google(audio))

    print("")
    print("")
    print("Prints all json alternatives")
    print(r.recognize_google(audio, show_all=True))
Example #21
0
class Speech(object):
    def __init__(self):
        rospy.init_node("speech")
        self.pub = rospy.Publisher("speech_recognizer",
                                   String,
                                   latch=True,
                                   queue_size=1)
        self.recognizer = Recognizer()
        # self.recognizer.energy_threshold = 1000
        # self.recognizer.pause_threshold = .7
        self.microphone = Microphone()

    @staticmethod
    def check_internet_connection():
        connection = httplib2.HTTPConnectionWithTimeout("www.google.com",
                                                        timeout=5)
        try:
            connection.request("HEAD", "/")
            connection.close()
            return True
        except httplib2.HttpLib2Error:
            connection.close()
            return False

    def __recognize_helper(self, recognized_speech):
        '''
        Once speech obtained do TODO:something with it
        '''
        print(recognized_speech)
        if recognized_speech != "":
            rospy.loginfo("You said: " + recognized_speech)
            self.pub.publish(recognized_speech)

    def __microphone_helper(self, ):
        rospy.loginfo('Listening...')
        with self.microphone as source:
            # If phase_time_limit is not set to 5 it will
            # take a really long time every 3-4th attempt
            audio = self.recognizer.listen(source, phrase_time_limit=5)
        rospy.loginfo('Got a sound; recognizing...')
        return audio

    def recognize_google(self):
        with self.microphone as source:
            self.recognizer.adjust_for_ambient_noise(source)

        try:
            while not rospy.is_shutdown():
                audio = self.__microphone_helper()
                recognized_speech = ""
                if Speech.check_internet_connection():
                    try:
                        recognized_speech = self.recognizer.recognize_google(
                            audio)
                    except sr.UnknownValueError:
                        rospy.logerr("Could not understand audio.")
                    except sr.RequestError:
                        rospy.logerr("Could not request results.")
                else:
                    rospy.logerr("No internet conneciton for Google API")

                self.__recognize_helper(recognized_speech)

        except Exception as exc:
            rospy.logerr(exc)

    def recognize_sphynix(self):
        with self.microphone as source:
            self.recognizer.adjust_for_ambient_noise(source)
        try:
            while not rospy.is_shutdown():
                audio = self.__microphone_helper()
                recognized_speech = ""

                try:
                    recognized_speech = self.recognizer.recognize_sphinx(audio)
                except sr.UnknownValueError:
                    rospy.logerr("Could not understand audio.")
                except sr.RequestError:
                    rospy.logerr("""Could not request results. Do you have
                     pocket Sphynx installed?""")
                self.__recognize_helper(recognized_speech)
        except Exception as exc:
            rospy.logerr(exc)
Example #22
0
def get_audio():
    """
    Função que resolve a fala do usuário.
    """
    # Função que reconhece a entrada da fala.
    mic_in = Recognizer()
    # Pega a entrada do microfone.
    # Ele vai ouvir o comando e traduzir em escrita para que as demais funções possam iterar com o comando.
    with Microphone() as source:
        # Ajusta a entrada eliminando ruidos.
        mic_in.adjust_for_ambient_noise(source)

        # Limpa informações de inicialização do terminal.
        system('clear')

        # Mostra na tela os comandos padrões.
        print('Comandos do sistema:')
        for system_commands in COMMAND_LIST:
            print(system_commands)

        user_commands = [info for info in get_command_list()]

        if user_commands:
            print('Comandos do usuário:')
            for comand in user_commands:
                print(comand)

        # Armazena a informação de audio na variável.
        audio = mic_in.listen(source)

    try:
        system('clear')
        print('Processando ...')
        # Passa o aúdio para o reconhecedor de padrões do google trasformar em frase.
        phrase = mic_in.recognize_google(audio, language='pt-BR')
        print(f'Entrada de aúdio: {phrase}')

        # Condição para o caso de o usuário use o comando ADD.
        if str(phrase).upper() == COMMAND_LIST[0]:
            set_command()

            return get_audio()

        if str(phrase).upper() == COMMAND_LIST[1]:
            return system('exit')

        if str(phrase).upper() not in get_command_list():
            insult = get_insult(phrase)
            record_audio(insult)

            return get_audio()

        record_audio(get_response(phrase))

        return get_audio()

    except UnknownValueError:
        insult = get_insult('Vox')
        record_audio(insult)

        return get_audio()
Example #23
0
class Listener:
    def __init__(self,
                 pin: int = 17,
                 language: str = "en",
                 api: str = "",
                 mic=None) -> None:
        """
        Initialize LED lights, button, language, microphone, and recognizer.

        pin: tell Pi what pin to use for LED and set it's default value
        to 0 (off)
        language: set language to the language you are translating from
        microphone: initialize microphone so it can be used in this program
        recognizer: take input from microphone and decipher it into text

        Microphone.list_microphone_names() to list all microphones
        Pass the needed device as device_index=n if program can't pick
        up device automatically
        """
        self.microphone = Microphone(
            device_index=mic) if mic else Listener.get_mic()
        self.recognizer = Recognizer()
        self.led = PWMLED(pin)
        self.src = language
        self.target = "es"
        self.api = api

    @staticmethod
    def get_mic():
        for index, mic in enumerate(Microphone.list_microphone_names()):
            if "usb" in mic.lower():
                return Microphone(device_index=index)

        raise ValueError("USB microphone required to run.")

    def breathe(self,
                step: float = 0.0001,
                iterations: int = 2,
                rest: float = 0.5) -> None:
        """
        Pulsating effect for LED button.

        step: amount to increase/decrease LED brightness by
        iterations: number of pulses
        rest: time between each pulse
        """

        if step < 0 or step > 1:
            raise ValueError("Step needs to be a value between zero and one.")

        if rest <= 0:
            raise ValueError(
                "Rest time needs to be a positive value greater than zero.")

        if iterations < 0:
            raise ValueError("Iterations needs to be zero or greater.")

        # First set LED to zero (off)
        self.led.value = 0

        for _ in range(iterations):

            while self.led.value <= 1 - step:
                self.led.value = round(self.led.value + step, 5)
                print(self.led.value)

            self.led.value = 0.90
            sleep(rest)

            while self.led.value - step >= 0.15:
                self.led.value = round(self.led.value - step, 5)
                print(self.led.value)

            sleep(rest)
            self.led.value = 0.15

        self.led.value = 0
        sleep(1)

    def confirmation(self) -> None:
        """
        Blinks twice and beeps when ready.
        """
        def _confirmation():
            while self.led.value < 1:
                self.led.value = round(self.led.value + 0.005, 5)

            self.led.value = 1
            sleep(0.15)
            while self.led.value > 0:
                self.led.value = round(self.led.value - 0.005, 5)

            self.led.value = 0
            sleep(0.15)

        self.led.value = 0

        _confirmation()
        _confirmation()

    def listen(self) -> None:
        """Read in voice and send it to Google. Once Google transcribes it, sanitize the result."""

        print("Listening to user voice now...")

        with self.microphone as source:
            self.recognizer.adjust_for_ambient_noise(source)
            audio = self.recognizer.listen(source)

        print("Now trying to interpret user voice")

        # If Google cloud API is explicitly given, use that. (Gives users
        # control over logging or not)
        if self.api:
            text = self.recognizer.recognize_google_cloud(audio, self.api)
        # Otherwise use Google Web API
        else:
            text = self.recognizer.recognize_google(audio)

        print("Sanitizing transciption")
        # This may be unnecessary
        # text = "".join([char for char in text if char.isalnum() or char.isspace()])

        print("Returning transcription back to main.py")

        return text

    def set_brightness(self,
                       target: float,
                       increase: bool,
                       step: float = 0.001):
        """
        Set the brightness to LED. Brightness can go either way: up (starting
        at zero) or down (starting at one) and 'work' it's way into the desired
        target brightness.
        """

        if target < 0 or target > 1:
            raise ValueError("Please enter valid target value.")

        if step < -1 or step > 1:
            raise ValueError("Please enter valid step value.")

        # Setting step to absolute makes the if/else statement easier
        # to understand
        step = abs(step)

        if increase:
            self.led.value = 0
            while self.led.value < target:
                self.led.value = round(self.led.value + step, 5)
        else:
            self.led.value = 1
            while self.led.value > target:
                self.led.value = round(self.led.value - step, 5)

    def set_src(self, new_lang: str) -> None:
        try:
            self.src = googletrans.LANGUAGES[new_lang]
        except IndexError as e:
            print(
                f"Error: Unable to find language.Keeping current source language to {self.src}.\n{e}"
            )

    def set_target(self, new_lang: str) -> None:
        try:
            self.target = googletrans.LANGUAGES[new_lang]
        except IndexError as e:
            print(
                f"Error: Unable to find language. Keeping current target language to {self.target}.\n{e}"
            )
Example #24
0
# Setup microphone and speech recognizer
r = Recognizer()
m = None
input_mic = 'Scarlett 2i4 USB'  # Use whatever is your desired input
for i, microphone_name in enumerate(Microphone.list_microphone_names()):
    if microphone_name == input_mic:
        m = Microphone(device_index=i)

while True:
    """
    Commands will be entered in the specific format explained here:
     - the first word will be one of: 'album', 'artist', 'play'
     - then the name of whatever item is wanted
    """
    with m as source:
        r.adjust_for_ambient_noise(source=source)
        audio = r.listen(source=source)

    command = None
    try:
        command = r.recognize_google(audio_data=audio).lower()
    except UnknownValueError:
        continue

    print(command)
    words = command.split()
    if len(words) <= 1:
        print('Could not understand. Try again')
        continue

    name = ' '.join(words[1:])
Example #25
0
    except sr.UnknownValueError:
        print("Google Speech Recognition could not understand audio")
    except sr.RequestError as e:
        print(
            "Could not request results from Google Speech Recognition service; {0}"
            .format(e))


# Listen for voice audio in a loop and wait for the word 'Dance party'
if __name__ == '__main__':
    try:
        dance_party_audio.load_audio()
        recognizer = Recognizer()
        recognizer.energy_threshold = 200

        microphone = Microphone()
        with microphone as source:
            recognizer.adjust_for_ambient_noise(source)

        # start listening on the mic
        stop_listening = recognizer.listen_in_background(
            microphone, onAudioReceived)
        while True:
            # "Block" the main thread ... will run additional logic to handle
            # Hardware here - TODO
            time.sleep(0.1)
        # Kill the background listener stop listening
        stop_listening()
    except DancePartyAudioLoadError as error:
        print(error)
class Assistant(object):

    __defaultCommands = {
        "open": 1,
        "search": 2,
        "go to": 3,
        "calculate": 4,
        "add path": 5,
        "add directory": 5,
        "remove path": 6,
        "remove directory": 6,
        "command list": 7,
        "minimize": 8,
        "maximize": 9,
        "close": 10,
        "add command": 11,
        "edit command": 12,
    }

    __defaultCommands_PT_BR = {
        "abrir": __defaultCommands["open"],
        "pesquisar": __defaultCommands["search"],
        "procurar": __defaultCommands["search"],
        "ir para": __defaultCommands["go to"],
        "calcular": __defaultCommands["calculate"],
        "adicionar caminho": __defaultCommands["add path"],
        "adicionar diretório": __defaultCommands["add directory"],
        "remover caminho": __defaultCommands["remove path"],
        "remover diretório": __defaultCommands["remove directory"],
        "lista de comandos": __defaultCommands["command list"],
        "minimizar": __defaultCommands["minimize"],
        "maximizar": __defaultCommands["maximize"],
        "fechar": __defaultCommands["close"],
        "adicionar comando": __defaultCommands["add command"],
        "editar comando": __defaultCommands["edit command"]
    }

    __defaultLanguage = "en-us"

    __info = [
        "Open:Run a program or open a file or folder.",
        "Search:Opens the browser with a search.",
        "Go to:Opens the browser directly to a site.",
        "Calculate:Calculates a simple arithmetic expression.",
        "Add path:Adds a directory to locate programs, files, and folders.",
        "Remove path:Removes a directory added by the user.",
        "Command list:Opens a list of all voice commands.",
        "Minimize:This minimizes an active window or a window with a title you specify.",
        "Maximize:This maximizes a window with a title you specify.",
        "Close:This closes an active window or a window with a title you specify.",
        "Add command:Opens a window for defining a command and associating a program with it.",
        "Edit command:Opens a window for editing the information of a command."
    ]

    __info_PT_BR = [
        "Abrir:Executa um programa ou abre um arquivo ou pasta.",
        "Pesquisar:Abre o navegador com uma busca.",
        "Vá para:Abre o navegador diretamente em um site.",
        "Calcular:Calcula uma expressão aritmética simples.",
        "Adicionar caminho:Adiciona um diretório para o assistente localizar arquivos e pastas.",
        "Remover caminho:Remove um diretório adicionado pelo usuário.",
        "Lista de comandos:Abre uma lista com todos os comandos de voz.",
        "Minimizar:Minimiza uma janela ativa ou uma janela com um título específico.",
        "Maximizar:Maximiza uma janela com um título específico.",
        "Fechar:Fecha uma janela ativa ou uma janela com um título específico.",
        "Adicionar comando:Abre uma janela para definir um comando e um programa associado a ele.",
        "Editar comando:Abre uma janela para editar as informações de um comando."
    ]

    __settings = {
        "Assistant": {
            "Language": "EN-US",
            "Name": "Jarvis",
        },
        "System": {
            "Paths": [],
            "Press to speak": "left windows + s",
            "Sounds": True,
            "ShowInput": True
        }
    }

    __userCommands = {}

    __settingsFile = "settings.json"
    __commandsFile = "commands.json"

    __help = False

    def __init__(self, messageBox, settingsPath, icon=None):
        """
        O parâmetro "messageBox" deve ser um objeto de MessageBox ou de uma subclasse dela.

        O parâmetro "settingsPath" deve ser um diretório para 
        carregar e salvar as configurações do assistente.
        """

        # Verifica se messageBox é um objeto que vem de MessageBox ou sua subclasse.
        if not issubclass(type(messageBox), MessageBox):
            raise TypeError("a MessageBox object is required (got {})".format(
                type(messageBox)))
        self.__messageBox = messageBox
        self.__icon = icon

        # Carrega as configurações do assistente.
        self.__settingsFile = os.path.join(settingsPath, self.__settingsFile)
        self.__commandsFile = os.path.join(settingsPath, self.__commandsFile)
        self.loadSettings(self.__settingsFile)
        self.loadUserCommands(self.__commandsFile)

        # Salva as configurações do assistente.
        # Isso faz com que as informações do arquivo fiquem atualizadas caso alguma informação esteja faltando.
        self.saveSettings(self.__settingsFile)

        # Inicializa um objeto para reconhecimento de voz e tradução de texto.
        self.__recognizer = Recognizer()
        self.__translator = Translator()

        # Inicializa objeto para gerar sons.
        self.__sounds = Sounds()

        # Cria objeto da classe AssistantCommands para realizar as ações do assistente.
        self.__assistantCommands = AssistantCommands(self, self.__settings,
                                                     self.__userCommands)

        # Define o idioma do assistente.
        language = self.__settings["Assistant"]["Language"]
        self.changeLanguage(language)

    def changeLanguage(self, language):
        """
        Método para trocar o idioma do assistente.
        """

        if language.lower() == "pt-br":
            self.__settings["Assistant"]["Language"] = "PT-BR"
            self.__commands = self.__defaultCommands_PT_BR
        else:
            self.__settings["Assistant"]["Language"] = "EN-US"
            self.__commands = self.__defaultCommands

    def getAssistantCommands(self):
        """
        Método para retornar uma instância de AssistantCommands.
        """
        return self.__assistantCommands

    @staticmethod
    def getCommandsFile():
        """
        Método para retornar o nome do arquivo que guarda os comandos adicionados pelo usuário.
        """
        return Assistant.__commandsFile

    @staticmethod
    def getDefaultSettings():
        """
        Retorna as configurações padrão do assistente.
        """
        return Assistant.__settings

    def getInfo(self):
        """
        Método para retornar as informações de todos os comandos do assistente.
        """

        # Obtém o idioma atual do assistente.
        language = self.getLanguage().lower()

        # Obtém as informações no idioma atual.
        if language == "pt-br":
            default_info = self.__info_PT_BR.copy()
        else:
            default_info = self.__info.copy()
        info = []

        # Adiciona um espaço no texto.
        for command in default_info:
            info.append(" " + command)

        # Adiciona à lista os comandos criados pelo usuário.
        for command in self.__userCommands.keys():
            info.append(
                " " + self.__userCommands[command]["Command"].capitalize() +
                ":" + self.__userCommands[command]["Description"].capitalize())
        return info

    def getLanguage(self):
        """
        Retorna o idioma atual do assistente.
        """
        return self.__settings["Assistant"]["Language"]

    @staticmethod
    def getSettingsFile():
        """
        Método para retornar o nome do arquivo de configurações do assistente.
        """
        return Assistant.__settingsFile

    def getTranslator(self):
        """
        Método para retornar uma instância de Translator.
        """
        return self.__translator

    def getStatus(self):
        """
        Informa se foi solicitado ou não a parada do método run().
        """
        return self.__stop

    def help(self):
        """
        Método para abrir uma lista com todos os comandos do assistente.
        """
        self.__help = True

    def __helpList(self, info, sep=":"):
        """
        Método para abrir lista de ajuda.
        """

        # Separa os títulos e as descrições.
        commands = []
        for item in info:
            commands.append(item.split(sep, maxsplit=1))
        commands.sort(reverse=True)

        # Ajusta o tamanho da lista.
        height = len(commands) if len(commands) < 10 else 10

        itemList = ItemList("Assistant Commands",
                            height=height,
                            icon=self.__icon)
        itemList.run(commands, self.getStatus)
        self.__help = False

    def isRunning(self):
        """
        Informa se o método run() está em execução ou não.
        """
        return self.__running

    def loadSettings(self, settingsFile):
        """
        Método para carregar as configurações do assistente.
        """

        # Cria um arquivo com as configurações padrão caso o mesmo não exista.
        if not os.path.exists(settingsFile):
            self.saveSettings(settingsFile)
            return self.__settings

        # Carrega as configurações do arquivo.
        with open(settingsFile) as file:
            settings = json.loads(file.read())

            if "Assistant" in settings:
                self.__settings["Assistant"].update(settings["Assistant"])
            if "System" in settings:
                self.__settings["System"].update(settings["System"])

        return self.__settings

    def loadUserCommands(self, commandsFile):
        """
        Método para carregar os comandos adicionados pelo usuário.
        """

        # Cria um arquivo para que os comandos criados pelo usuário possam ser salvos.
        if not os.path.exists(commandsFile):
            self.saveUserCommands(commandsFile)
            return self.__userCommands

        # Carrega os comandos do arquivo.
        with open(commandsFile) as file:
            self.__userCommands = json.loads(file.read())
        return self.__userCommands

    def __press_to_speak(self):
        """
        Espera até que o usuário aperte uma determinada tecla para falar com o assistente.
        """

        # Obtém a tecla que o usuário deve pressionar para falar com o assistente.
        press_to_speak = self.__settings["System"]["Press to speak"]

        while True:
            keyboard.wait(press_to_speak)
            if self.__stop: break
            if not self.__listening:
                self.__speak = True

    def run(self):
        """
        Método para iniciar o assistente.
        """

        self.__stop = False
        self.__running = True
        self.__speak = False
        self.__listening = False

        # Obtém a tecla que o usuário deve pressionar para falar com o assistente.
        press_to_speak = self.__settings["System"]["Press to speak"]

        # Obtém a opção de som habilitado ou não.
        sounds = self.__settings["System"]["Sounds"]

        # Obtém o idioma e o nome do assistente.
        language = self.getLanguage()
        name = self.__settings["Assistant"]["Name"]

        # Informa o que o usuário deve fazer para chamar o assistente.
        if press_to_speak:

            # Inicializa uma thread para esperar até que o usuário aperte uma determinada tecla.
            Thread(target=self.__press_to_speak).start()
            text, substring = 'Press "{}" to speak.', press_to_speak.replace(
                "+", " + ")
        else:
            text, substring = "Speak my name to talk to me.", ""

        self.speak(name,
                   text,
                   language.split("-")[0],
                   substrings=[
                       substring,
                   ],
                   wait=True,
                   sound=sounds)

        # Ouve o usuário enquanto não for pedido para o método run() parar.
        while not self.__stop:

            # Informa que o assistente não está ouvindo.
            self.__listening = False

            if self.__stop: break

            if self.__help:
                self.__helpList(self.getInfo())
                self.__speak = False

            # Aguarda um tempo de milisegundos para que o programa não consuma muito processamento.
            time.sleep(0.1)

            # Verifica se o usuário apertou o botão ou não para falar.
            if press_to_speak and not self.__speak:
                continue

            elif press_to_speak:
                self.__speak = False
                self.__listening = True

            # Inicializa o microfone para ouvir o aúdio.
            with Microphone() as microphone:

                # Obtém as configurações do assistente.
                language = self.getLanguage()
                name = self.__settings["Assistant"]["Name"]

                # Obtém as configurações do sistema.
                paths = self.__settings["System"]["Paths"]
                showInput = self.__settings["System"]["ShowInput"]
                sounds = self.__settings["System"]["Sounds"]
                showInput = self.__settings["System"]["ShowInput"]

                # Ajusta o Recognizer para o som do ambiente atual.
                self.__recognizer.adjust_for_ambient_noise(microphone)

                # Reproduz um som, informando que o assistente está ouvindo o usuário.
                if sounds: self.__sounds.play_sound(self.__sounds.listening_fn)

                # Informa que o assistente está ouvindo.
                self.speak(name,
                           "I'm listening...",
                           language.split("-")[0],
                           duration=2,
                           wait=False,
                           sound=None)

                # Tenta ouvir o usuário e realizar o reconhecimento de voz.
                try:

                    # Obtém o aúdio do microfone.
                    audio = self.__recognizer.listen(microphone)

                    if self.__stop: break

                    # Realiza o reconhecimento de voz.
                    text = self.__recognizer.recognize_google(
                        audio, language=language)

                except Exception as e:

                    if press_to_speak:

                        # Informa que houve algum problema ao realizar o reconhecimento de voz.
                        self.speak(
                            name,
                            "I'm sorry, but I can't understand what you said.",
                            language.split("-")[0],
                            wait=True,
                            sound=sounds)
                    continue

                # Obtém o idioma para realizar a tradução do assistente para o usuário.
                language = language.split("-")[0]

                # Mostra o que o reconhecimento entendeu do que foi dito pelo usuário.
                if showInput:
                    title = self.__translator.translate(
                        "You said:", language,
                        self.__defaultLanguage.split("-")[0]).text
                    self.speak(title, '"%s"' % text, duration=2, wait=True)

                # Caso a opção "Aperte para falar" esteja desativada, o assistente entrará
                # em ação somente se o usuário falar seu nome.

                if not press_to_speak:

                    # Verifica se o usuário chamou o assistente.
                    if text.lower().find(name.lower()) == -1:
                        continue

                # Caso o usuário tenha chamado pelo assistente, será obtido o texto sem o nome dele.
                if text.lower().find(name.lower()) != -1:
                    text = text[text.lower().find(name.lower()) + len(name) +
                                1:]

                # Verifica se é um comando definido pelo usuário.
                userCommand = False

                for key in self.__userCommands.keys():

                    # Se sim, o programa obtém as informações deste comando.
                    if text.lower() == key.lower():
                        command = self.__userCommands[key]
                        userCommand = True

                        # Verifica se o caminho do programa existe.
                        if os.path.exists(command["Path"]):

                            # Executa o programa.
                            file = os.path.split(command["Path"])[-1]
                            self.speak(name,
                                       'Starting "{}"...',
                                       language, [
                                           file,
                                       ],
                                       sound=sounds)
                            self.__assistantCommands.start(
                                command["Path"], command["Arguments"])

                        else:

                            # Informa que não foi possível executar o programa.
                            self.speak(
                                name,
                                "I'm sorry, but I can't run the program associated with this command.",
                                language,
                                duration=3,
                                wait=True,
                                sound=sounds)
                        break

                # Volta caso tenha sido executado um comando definido pelo usuário.
                if userCommand:
                    continue

                # Comando 0 significa que não existe um comando do assistente para o pedido do usuário.
                command = 0

                # Tenta tranformar a primeira palavra (comando) do texto em um verbo no infinitivo.
                word = text.split(maxsplit=1)[0]
                verb = self.__translator.getVerbs(word, language, 0)
                if verb:
                    text = text.replace(word, verb[0].lower())

                # Obtém uma chave para executar um comando do assistente.
                for key in self.__commands.keys():

                    if text.lower().find(key) == 0:
                        command = self.__commands[key]

                        # Separa o comando do resto do texto.
                        text = text[len(key) + 1:]
                        break

                # Se o comando for "open", ele tentará abrir um programa, arquivo ou pasta
                # com o nome que o usuário disse.
                if command == self.__defaultCommands["open"]:

                    try:
                        # Procura por um programa, arquivo ou pasta com o nome que o usuário disse.
                        path = self.__assistantCommands.find(
                            text, paths.copy())

                        # Informa que o programa está sendo aberto.
                        self.speak(name,
                                   'Starting "{}"...',
                                   language, [
                                       text,
                                   ],
                                   sound=sounds)

                        # Abre o programa.
                        AssistantCommands.start(path, "")

                    except:

                        # Informa que não foi possível encontrar o programa.
                        self.speak(
                            name,
                            """I'm sorry, but I can't find "{}" on your computer.""",
                            language, [
                                text,
                            ],
                            wait=True,
                            sound=sounds)

                # Se o comando for "go to", ele irá abrir o site no navegador.
                # Caso não seja possível, ele irá pesquisar na internet pelo mesmo.
                elif command == self.__defaultCommands["go to"]:

                    # Informa que ele está pesquisando pelo site.
                    self.speak(name,
                               'Searching for "{}"...',
                               language, [
                                   text,
                               ],
                               sound=sounds)
                    self.__assistantCommands.searchOnTheInternet(text)

                # Se o comando for "search", ele irá abrir o navegador com a busca.
                elif command == self.__defaultCommands["search"]:

                    # Informa que ele está pesquisando na internet.
                    self.speak(name,
                               'Searching for "{}"...',
                               language, [
                                   text,
                               ],
                               sound=sounds)
                    self.__assistantCommands.searchOnTheInternet(text, False)

                # Se o comando for "calculate", ele irá tentar calcular uma expressão e dizer o resultado.
                elif command == self.__defaultCommands["calculate"]:
                    try:

                        # Tenta obter o resultado.
                        result = self.__assistantCommands.calculate(text)

                        # Informa o resultado.
                        if int(result) != result:
                            self.speak(name,
                                       "The result is {}.",
                                       language, [
                                           str(result),
                                       ],
                                       sound=sounds)
                        else:
                            self.speak(name,
                                       "The result is {}.",
                                       language, [
                                           str(int(result)),
                                       ],
                                       sound=sounds)

                    except Exception:
                        # Informa que não foi possível realizar o cálculo.
                        self.speak(
                            name,
                            "Oops, I can't seem to do that kind of calculation.",
                            language,
                            wait=True,
                            sound=sounds)

                # Se o comando for "add path", será aberto uma janela para o usuário definir um caminho.
                elif command == self.__defaultCommands["add path"]:

                    # Se o usuário selecionou um diretório para ser adicionado, esse diretório será salvo.
                    if self.__assistantCommands.addPath():
                        self.saveSettings(self.__settingsFile)
                        self.speak(
                            name,
                            "This directory has been successfully added to the system.",
                            language,
                            sound=sounds)

                # Se o comando for "remove path", será aberto uma janela para o usuário
                # remover um diretório da lista de diretórios.
                elif command == self.__defaultCommands["remove path"]:

                    # Verifica se o usuário selecionou um diretório para ser deletado.
                    if self.__assistantCommands.deletePath():
                        self.saveSettings(self.__settingsFile)
                        self.speak(
                            name,
                            "This directory has been successfully deleted from the system.",
                            language,
                            sound=sounds)

                # Se o comando for "command list", será aberto uma janela com a lista de todos os comandos.
                elif command == self.__defaultCommands["command list"]:
                    self.help()

                # Se o comando for "minimize", uma janela do computador do usuário será minimizada.
                elif command == self.__defaultCommands["minimize"]:

                    # Caso não exista um nome, será minimizado a janela ativa.
                    if not text:
                        text = None

                        # Caso exista um nome, será verificado se existe uma janela com este nome.
                    else:
                        if not self.__assistantCommands.isWindow(text):
                            self.speak(
                                name,
                                """I'm sorry,' but I can't find a window named "{}".""",
                                [text],
                                sound=sounds)
                            return
                    self.__assistantCommands.minimizeProgram(title=text)

                # Se o comando for "maximize", uma janela do computador do usuário será maximizada.
                elif command == self.__defaultCommands["maximize"]:
                    if not text: return

                    # Verifica se existe uma janela com o nome que o usuário disse.
                    if not self.__assistantCommands.isWindow(text):
                        self.speak(
                            name,
                            """I'm sorry,' but I can't find a window named "{}".""",
                            [text],
                            sound=sounds)
                    self.__assistantCommands.maximizeProgram(title=text)

                # Se o comando for "close", será fechado um programa do computador do usuário.
                elif command == self.__defaultCommands["close"]:

                    # Caso não exista um nome, será fechado a janela ativa.
                    if not text:
                        text = None

                    # Caso exista um nome, será verificado se existe uma janela com este nome.
                    else:
                        if not self.__assistantCommands.isWindow(text):
                            self.speak(
                                name,
                                """I'm sorry,' but I can't find a window named "{}".""",
                                [text],
                                sound=sounds)
                            return
                    self.__assistantCommands.closeProgram(text)

                # Se o comando for "add command", será aberto uma janela para que o usuário defina um comando.
                elif command == self.__defaultCommands["add command"]:
                    self.__assistantCommands.setCommand(icon=self.__icon)

                # Se o comando for "edit command", será aberto uma janela para que o usuário edite um comando.
                elif command == self.__defaultCommands["edit command"]:

                    # Define título da janela para selecionar o comando.
                    title = self.__translator.translate(
                        "Select a command", language, "en").text

                    # Cria uma lista para que o usuário selecione o comando que ele deseja editar.
                    itemList = ItemList(title,
                                        width=40,
                                        height=10,
                                        icon=self.__icon,
                                        selection=True)
                    command = itemList.run([
                        " " + key.capitalize()
                        for key in self.__userCommands.keys()
                    ], self.getStatus)
                    # Caso um comando tenha sido selecionado, uma janela será aberta
                    # com as informações deste comando para edição.
                    if command:
                        self.__assistantCommands.setCommand(
                            self.__userCommands[command.lower()],
                            icon=self.__icon)

                else:
                    # Informa que não foi possível executar nenhum comando.
                    self.speak(
                        name,
                        "I'm sorry, but I didn't understand what you said.",
                        language,
                        wait=True,
                        sound=sounds)

        # Informa que acabou a execução do método.
        self.__running = False

    def save(self):
        """
        Método para savar todas as informações.
        """
        self.saveSettings()
        self.saveUserCommands()

    def saveSettings(self, settingsFile=None):
        """
        Método para salvar as configurações atuais do assistente.
        """

        if not settingsFile:
            settingsFile = self.__settingsFile

        # Cria diretório caso não exista.
        path = os.path.split(settingsFile)[0]
        if not os.path.exists(path):
            os.mkdir(path)

        # Salva o arquivo.
        with open(settingsFile, 'w') as file:
            file.write(json.dumps(self.__settings, indent=2))

    def saveUserCommands(self, commandsFile=None):
        """
        Método para salvar os comandos criados pelo usuário.
        """

        if not commandsFile:
            commandsFile = self.__commandsFile

        # Cria diretório caso não exista.
        path = os.path.split(commandsFile)[0]
        if not os.path.exists(path):
            os.mkdir(path)

        # Salva o arquivo.
        with open(commandsFile, 'w') as file:
            file.write(json.dumps(self.__userCommands, indent=2))

    def speak(self,
              title,
              text,
              language=None,
              substrings=None,
              duration=5,
              wait=False,
              sound=False):
        """
        Método para o assistente se comunicar com o usuário.
        """

        # Traduz a linguagem caso exista algo no parâmetro language.
        if language:
            try:
                msg = self.__translator.translate(
                    text, language,
                    self.__defaultLanguage.split("-")[0]).text
            except:
                msg = text
        else:
            msg = text

        # Adiciona substrings que não foram traduzidas.
        if substrings:
            msg = msg.format(*substrings)
        self.__messageBox.send(title=title,
                               message=msg,
                               duration=duration,
                               threaded=True)

        # Executa um som de mensagem.
        if sound: self.__sounds.play_sound(self.__sounds.message_fn)

        # Aguarda a mensagem fechar para prosseguir.
        if wait:
            time.sleep(duration)

    def stop(self):
        """
        Encerra o método run().
        """
        self.__stop = True
        self.save()
class CommandsRecognition(ApiState):
    def __init__(self, callback, language="pl-PL", api_option=ApiOption.GOOGLE):
        super().__init__()
        self.callback_command_detected = callback
        self.api_option = api_option
        self.language = language
        self.listen_thread = None
        self.phrase_time_limit = 3

        try:
            self.recognizer = Recognizer()
            self.microphone = Microphone()
            """
                adjust the recognizer sensitivity to ambient noise and record audio
                from the microphone
            """
            with self.microphone as source:
                self.recognizer.adjust_for_ambient_noise(source)

        except OSError as ose_err:
            Logger.critical("OSError: {0}".format(ose_err))
            self.api_runs = False
            self.api_info = GLO_MSG['MICROPHONE_FAILURE']
            return
        except Exception as err:
            Logger.critical("Exception: {0}".format(err))
            self.api_runs = False
            self.api_info = GLO_MSG['MICROPHONE_FAILURE']
            return
        Logger.debug("Initialization of CommandsRecognition class")
        self.api_info = GLO_MSG['MICROPHONE_INITIALIZED']

    def validate_command(self, command):
        if command.lower() in GLO_CMD.values():
            detected_command_id = GET_COMMAND(command.lower())
            Logger.info("GLO_CMD command available -> {0}".format(command.lower()))
            self.callback_command_detected(detected_command_id)
        else:
            Logger.info("Detected command: {0}".format(command.lower()))

    def callback_recognition(self, recognizer, audio):
        try:
            command = self._api_recognition(audio)
            self.validate_command(command)
        except UnboundLocalError as err:
            Logger.warning("UnboundLocalError : {0} ".format(err))
        except RequestError as err:
            Logger.warning("RequestError : {0} ".format(err))
        except UnknownValueError as err:
            Logger.debug("UnknownValueError : {0} ".format(err))

    def background_listen(self):
        self.listen_thread = self.recognizer.listen_in_background(
            source=self.microphone, callback=self.callback_recognition, phrase_time_limit=self.phrase_time_limit)

    def _api_recognition(self, audio):
        if self.api_option is ApiOption.GOOGLE:
            return self.recognizer.recognize_google(audio, language=self.language)
        elif self.api_option is ApiOption.GOOGLE_CLOUD:
            # Support languages: https://cloud.google.com/speech-to-text/docs/languages
            return self.recognizer.recognize_google_cloud(audio, credentials_json='', language=self.language)
        elif self.api_option is ApiOption.SPHINX:
            # Support languages : https://sourceforge.net/projects/cmusphinx/files/Acoustic%20and%20Language%20Models/
            return self.recognizer.recognize_sphinx(audio, language=self.language)
        elif self.api_option is ApiOption.WIT:
            # Support languages : https://wit.ai/faq, login required
            return self.recognizer.recognize_wit(audio, key='',)
        elif self.api_option is ApiOption.AZURE_BING:
            # Support languages : https://docs.microsoft.com/en-us/azure/cognitive-services/bing-web-search/language-support, login required
            self.recognizer.recognize_bing(audio, key='', language=self.language)
        elif self.api_option is ApiOption.LEX:
            # Support languages: ONLY ENG -> https://docs.aws.amazon.com/lex/latest/dg/gl-limits.html
            return self.recognizer.recognize_lex(audio)
        elif self.api_option is ApiOption.HOUNDIFY:
            # Support languages: ONLY ENG, login required
            return self.recognizer.recognize_houndify(audio, client_id='', client_key='')
        elif self.api_option is ApiOption.IBM:
            # Support languages : https://www.ibm.com/watson/services/language-translator/, login required
            return self.recognizer.recognize_ibm(audio, username='', password='', language=self.language)
        else:
            Logger.error("Api recognition option is not defined")
Example #28
0
from speech_recognition import Recognizer, Microphone, UnknownValueError
from Techmodule.MusicPlayer import *
from Techmodule.FunModule import *

R = Recognizer()

while True:
    try:
        with Microphone() as source:
            print("Adjust-noise...")
            R.adjust_for_ambient_noise(source)
            print('ready to listen!')
            S1 = R.listen(source)
            print('Done!')
            Lis = R.recognize_google(S1, language="th-TH")
            T = DialogFlow(Lis)
            if T == "Ok":
                playSound("InYourCommand.wav")
                print("Adjust-noise...")
                R.adjust_for_ambient_noise(source)
                print("In your command!")
                S2 = R.listen(source)
                print('Done!')
                T2 = R.recognize_google(S2, language="th-TH")
                CommandLine(DialogFlow(T2))
            elif T == "M-ON":
                print("Adjust-noise...")
                R.adjust_for_ambient_noise(source)
                print("Listen Musicname")
                S3 = R.listen(source)
                print('Done!')