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)