Beispiel #1
0
 def __init__(self, device_index=None, sample_rate=16000, chunk_size=1024,
              mute=False):
     Microphone.__init__(self, device_index=device_index,
                         sample_rate=sample_rate, chunk_size=chunk_size)
     self.muted = False
     if mute:
         self.mute()
Beispiel #2
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)
Beispiel #3
0
 def __init__(self, device_index=None, sample_rate=16000, chunk_size=1024,
              mute=False):
     Microphone.__init__(
         self, device_index=device_index, sample_rate=sample_rate,
         chunk_size=chunk_size)
     self.muted = False
     if mute:
         self.mute()
Beispiel #4
0
class UserAudioSource(AudioSource):
    """Represent user audio input."""

    def __init__(self) -> None:
        self._micro = Microphone()

    def __enter__(self) -> Microphone:
        return self._micro.__enter__()

    def __exit__(self, exc_type, exc_val, exc_tb) -> None:
        return self._micro.__exit__(exc_type, exc_val, exc_tb)
Beispiel #5
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))
def audio_to_text(message_input):
    # initialise the recognizer
    r = Recognizer()
    # Use the sysdefault microphone
    for i, microphone_name in enumerate(Microphone.list_microphone_names()):
        if microphone_name == "sysdefault":
            micro = Microphone(device_index=i)
    with micro as source:
        # Extract the audio and convert it to text
        audio = r.listen(source)
    # recognize speech using Google Speech Recognition and add it to the text input area
    try:
        message_input.setText(r.recognize_google(audio))
    except UnknownValueError:
        message_input.setText('The audio was not understood')
Beispiel #7
0
    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
Beispiel #8
0
def listen_continuously() -> Callable[[bool], None]:
    """
    Listen for speech continuously, send each sentence to a speech-to-text converter, then send the text to the server.
    Sentences are detected by pauses in the speech.
    """

    # look for the microphone specified in config file
    mics_lst = sr.Microphone.list_microphone_names()
    mic = None
    for i, m in enumerate(mics_lst):
        if m == config.mic_name:
            mic = Microphone(device_index=i)
    if mic is None:
        msg = "No microphone can be found!"
        log(msg, level=logging.ERROR, remote=False)
        raise Exception(msg)

    recognizer = sr.Recognizer()
    log("adjusting to noise for {0} sec".format(config.noise_adjustment_time),
        remote=False)
    with mic as source:
        recognizer.adjust_for_ambient_noise(
            duration=config.noise_adjustment_time, source=source)

    log("listening!", remote=True)
    # stop_listening is a function that when called stops the background listening
    stop_listening_func = recognizer.listen_in_background(
        mic,
        callback=listening_callback,
        phrase_time_limit=config.recognizer_phrase_time_limit)
    return stop_listening_func
    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']
Beispiel #10
0
def listen_background():
    background_listener = noisered.BackgroundListener()
    with Microphone(sample_rate=SAMPLE_RATE) as source:
        background_listener.adjust_for_ambient_noise(source)
        while os.path.exists(SHARED_MEM_DIR):
            audio_data = background_listener.listen(source, pause_time_limit=5)
            if not audio_data:
                print("background listener: no audio data")
                time.sleep(1)
                continue

            segment = extract_silence(audio_data.get_wav_data())
            if not segment:
                print("background listener: no silence")
                time.sleep(1)
                continue
            segment.export(BG_WAV_PATH + '.tmp', format='wav', bitrate=256)

            with noisered.SEMAPHORE:
                try:
                    os.remove(BG_WAV_PATH)
                except:
                    pass
                    # create wav file
                os.rename(BG_WAV_PATH + '.tmp', BG_WAV_PATH)
                print(f"export bgm. {BG_WAV_PATH}. size={len(segment)}")
Beispiel #11
0
def listen(timeout: Union[int, float], phrase_limit: Union[int, float], sound: bool = True,
           stdout: bool = True) -> Union[str, None]:
    """Function to activate listener, this function will be called by most upcoming functions to listen to user input.

    Args:
        timeout: Time in seconds for the overall listener to be active.
        phrase_limit: Time in seconds for the listener to actively listen to a sound.
        sound: Flag whether to play the listener indicator sound. Defaults to True unless set to False.
        stdout: Flag whether to print the listener status on screen.

    Returns:
        str:
         - Returns recognized statement from the microphone.
    """
    with Microphone() as source:
        try:
            playsound(sound=indicators.start, block=False) if sound else None
            sys.stdout.write("\rListener activated...") if stdout else None
            listened = recognizer.listen(source=source, timeout=timeout, phrase_time_limit=phrase_limit)
            playsound(sound=indicators.end, block=False) if sound else None
            support.flush_screen()
            recognized = recognizer.recognize_google(audio_data=listened)
            logger.info(recognized)
            return recognized
        except (UnknownValueError, RequestError, WaitTimeoutError):
            return
        except (ConnectionError, TimeoutError, requests.exceptions.RequestException, requests.exceptions.Timeout) as \
                error:
            logger.error(error)
Beispiel #12
0
    def listen(self) -> str:
        # Record Audio
        data = ''
        recognizer = Recognizer()
        with Microphone() as source:
            self.log.debug('Listening on microphone...')
            try:
                audio = recognizer.listen(source, timeout=10)
            except WaitTimeoutError:
                self.log.debug('Listen timed out.')
                return data
        # Speech recognition using Google Speech Recognition
        try:
            # Uses the default API key
            # To use another API key: `r.recognize_google(audio, key='GOOGLE_SPEECH_RECOGNITION_API_KEY')`
            data = recognizer.recognize_google(audio)
            self.log.debug(f'You said: "{data}"')
        except UnknownValueError:
            self.log.debug(
                'Google Speech Recognition could not understand audio')
        except RequestError as e:
            self.log.error(
                f'Could not request results from Google Speech Recognition service: {e}'
            )

        return str(data)
Beispiel #13
0
    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()
Beispiel #14
0
    def __init__(self):
        recognizer = Recognizer()
        recognizer.dynamic_energy_threshold = False
        recognizer.energy_threshold = 1000
        self.recognizer = recognizer
        self.microphone = Microphone()
        self.susi = susi

        try:
            res = requests.get('http://ip-api.com/json').json()
            self.susi.update_location(longitude=res['lon'],
                                      latitude=res['lat'])

        except ConnectionError as e:
            logging.error(e)

        self.config = json_config.connect('config.json')

        if self.config['hotword_engine'] == 'Snowboy':
            from main.hotword_engine import SnowboyDetector
            self.hotword_detector = SnowboyDetector()
        else:
            from main.hotword_engine import PocketSphinxDetector
            self.hotword_detector = PocketSphinxDetector()

        if self.config['wake_button'] == 'enabled':
            if self.config['device'] == 'RaspberryPi':
                from ..hardware_components import RaspberryPiWakeButton
                self.wake_button = RaspberryPiWakeButton()
            else:
                self.wake_button = None
        else:
            self.wake_button = None
Beispiel #15
0
 def __init__(self):
     Thread.__init__(self, daemon=True);
     self.rec = Recognizer();
     self.rec.energy_threshold=110;
     self.rec.pause_threshold=0.5;
     self.rec.operation_timeout=5;
     self.mic = Microphone(device_index = 2);
     self.answer = AnswerBot('de.txt');
Beispiel #16
0
 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()
Beispiel #17
0
def escuchar():
    print("Escuchando...")
    recognizer = Recognizer()
    microfono = Microphone()

    with microfono:
        recognizer.adjust_for_ambient_noise(microfono)

    recognizer.listen_in_background(microfono, callback)
Beispiel #18
0
    def __init__(self, renderer=None):
        try:
            import RPi.GPIO as GPIO
            GPIO.setmode(GPIO.BCM)
            GPIO.setup(17, GPIO.OUT)
            GPIO.setup(27, GPIO.OUT)
            GPIO.setup(22, GPIO.OUT)
        except ImportError:
            print("Only available for devices with GPIO ports ")
        except RuntimeError:
            pass

        recognizer = Recognizer()
        recognizer.dynamic_energy_threshold = False
        recognizer.energy_threshold = 1000
        self.recognizer = recognizer
        self.microphone = Microphone()
        self.susi = susi
        self.renderer = renderer

        try:
            res = requests.get('http://ip-api.com/json').json()
            self.susi.update_location(
                longitude=res['lon'], latitude=res['lat'], country_name=res['country'], country_code=res['countryCode'])

        except ConnectionError as e:
            logging.error(e)

        self.config = json_config.connect('config.json')

        if self.config['usage_mode'] == 'authenticated':
            try:
                susi.sign_in(email=self.config['login_credentials']['email'],
                             password=self.config['login_credentials']['password'])
            except Exception:
                print('Some error occurred in login. Check you login details in config.json')

        if self.config['hotword_engine'] == 'Snowboy':
            from main.hotword_engine.snowboy_detector import SnowboyDetector
            self.hotword_detector = SnowboyDetector()
        else:
            from main.hotword_engine.sphinx_detector import PocketSphinxDetector
            self.hotword_detector = PocketSphinxDetector()

        if self.config['WakeButton'] == 'enabled':
            print("\nSusi has the wake button enabled")
            if self.config['Device'] == 'RaspberryPi':
                print("\nSusi runs on a RaspberryPi")
                from ..hardware_components import RaspberryPiWakeButton
                self.wake_button = RaspberryPiWakeButton()
            else:
                print("\nSusi is not running on a RaspberryPi")
                self.wake_button = None
        else:
            print("\nSusi has the wake button disabled")
            self.wake_button = None
Beispiel #19
0
 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()
 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 ''
Beispiel #21
0
 def __init__(self, recognizer, queue):
     self.recognizer = recognizer
     self.microphone = Microphone()
     self.indicator = 0
     self.queue = queue
     self.is_actually_run = None
     self.err_no_net = False
     self.service_json = None
     self.load_service_json()
     self.service_key = 'AIzaSyBOti4mM-6x9WDnZIjIeyEU21OpBXqWBgw'
     self.for_free = False
Beispiel #22
0
    def __init__(self, width, height):
        super().__init__(width, height)
        arcade.set_background_color(arcade.color.AMAZON)
        self.microphone = Microphone()
        self.recognizer = Recognizer()

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

        self.stop_listening = None
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)
Beispiel #24
0
def listen(audio_queue, recognize):
    engine = pyttsx3.init()
    rate = engine.getProperty('rate')
    engine.setProperty('rate', rate - 40)
    engine.say("I am listening")
    engine.runAndWait()
    with Microphone() as source:
        while True:
            print("Слушаю Вас")
            audio = recognize.listen(source)
            audio_queue.put(audio)
            engine.say("Ok")
            engine.runAndWait()
Beispiel #25
0
    def __init__(self, renderer=None):
        GPIO.setmode(GPIO.BCM)
        GPIO.setup(17, GPIO.OUT)
        GPIO.setup(27, GPIO.OUT)
        GPIO.setup(22, GPIO.OUT)

        recognizer = Recognizer()
        recognizer.dynamic_energy_threshold = False
        recognizer.energy_threshold = 1000
        self.recognizer = recognizer
        self.microphone = Microphone()
        self.susi = susi
        self.renderer = renderer

        try:
            res = requests.get('http://ip-api.com/json').json()
            self.susi.update_location(longitude=res['lon'],
                                      latitude=res['lat'])

        except ConnectionError as e:
            logging.error(e)

        self.config = json_config.connect('config.json')

        if self.config['usage_mode'] == 'authenticated':
            try:
                susi.sign_in(
                    email=self.config['login_credentials']['email'],
                    password=self.config['login_credentials']['password'])
            except Exception:
                print(
                    'Some error occurred in login. Check you login details in config.json'
                )

        if self.config['hotword_engine'] == 'Snowboy':
            from main.hotword_engine import SnowboyDetector
            self.hotword_detector = SnowboyDetector()
        else:
            from main.hotword_engine import PocketSphinxDetector
            self.hotword_detector = PocketSphinxDetector()

        if self.config['wake_button'] == 'enabled':
            if self.config['device'] == 'RaspberryPi':
                from ..hardware_components import RaspberryPiWakeButton
                self.wake_button = RaspberryPiWakeButton()
            else:
                self.wake_button = None
        else:
            self.wake_button = None
Beispiel #26
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
def get_audio():
    rObject = Recognizer()
    audio = ''
    with Microphone() as source:
        print("Speak...")

        # recording the audio using speech recognition
        audio = rObject.listen(source, phrase_time_limit=5)
    print("Stop.")  # limit 5 secs
    try:
        text = rObject.recognize_google(audio, language='en-US')
        print("You: ", text)
        return text
    except:
        chatbot_speaks("Could not understand your audio, Please try again !")
        return 0
Beispiel #28
0
def ears_setup():
    p = PyAudio()
    count = p.get_device_count()
    device = [i for i in range(count) if "Logitech" in p.get_device_info_by_index(i)["name"]][0]

    source = Microphone(device_index=device)
    # yup, I'm playing with the internals of this class.
    source.CHUNK = 512
    source.RATE = 8000
    source.CHANNELS = 1
    try:
        source.__enter__()
        source.stream.stop_stream()
    except:
        vprint(1, "Microphone initialization failed.")
        source.__exit__()

    return source
Beispiel #29
0
def robot_listen(request):
    wit_key = ''
    try:
        recogniser = Recognizer()
        microphone = Microphone()

        print('Listening')

        with microphone as source:
            # recogniser.adjust_for_ambient_noise(source)
            audio = recogniser.listen(source)

        return str(recogniser.recognize_wit(audio, key=wit_key, show_all=True))
    except (UnknownValueError, RequestError) as e:
        print(e)
        print('\n')
        return str({'_text': '', 'entities': {}})
Beispiel #30
0
def VoiceInput():

    recog = Recognizer()
    mic = Microphone(device_index=1)

    with mic:
        audio = recog.listen(mic)
    try:
        recognized = recog.recognize_google(audio)
    except UnknownValueError:
        Response("Try Again")
        with mic:
            audio = recog.listen(mic)
            recognized = recog.recognize_google(audio)
    except RequestError as exc:
        Response("Sorry my service is down")
    print(recognized)
    return (recognized)
Beispiel #31
0
def record_audio(ask=False):
    if ask:
        speak_electrica(ask)
    with Microphone() as source:
        print("Speak Up! I am listning..")
        audio = sr_engine.listen(source)
        audio_text = ""
        try:
            print("Now hold-on ! I'm recognising..")
            audio_text = sr_engine.recognize_google(audio, language="en-in")
            print(audio_text)
            return audio_text.lower()
        except UnknownValueError:
            speak_electrica("Sorry I didn't get that. kindly speak up again")
            say_again()

        except RequestError:
            speak_electrica("Sorry, unable to connect with engine")
Beispiel #32
0
def record_audio():
    with Microphone() as source:
        r.adjust_for_ambient_noise(source, duration=0.2)
        audio = r.listen(source, phrase_time_limit=4)
        voice_data = ' '
        try:
            voice_data = r.recognize_google(audio)
        except sr.UnknownValueError:
            # voices = engine.getProperty('voices')
            # engine.setProperty('voice', voices[1].id)
            # engine.say('Sorry I did not get that')
            # print('Sorry I did not get that')
            # engine.runAndWait()
            pass
        except sr.RequestError:
            # engine.say('my service is down now')
            pass
        return voice_data
Beispiel #33
0
from speech_recognition import Microphone

print(Microphone.list_microphone_names())