def listener(self, req):
        rospy.loginfo("Starting Google Speech Recognition")
        is_active = True

        print("A moment of silence, please...")
        with self.m as source:
            self.r.adjust_for_ambient_noise(source)
            rospy.loginfo("Set minimum energy threshold to {}".format(
                self.r.energy_threshold))
            self.r.dynamic_energy_threshold = False

        while is_active:
            print("Say something!")

            try:
                with self.m as source:
                    audio = self.r.listen(source, timeout=5)
                print("Got it! Now to recognize it...")

                try:
                    value = self.r.recognize_google(audio)

                    if str is bytes:
                        print(u"You said (Google) {}".format(value).encode(
                            "UTF-8"))
                        data = u"{}".format(value).encode("UTF-8")

                        if data.lower() == "no":
                            rospy.loginfo("Exiting Q&A")
                            is_active = False
                            break
                        else:
                            get_speech(data)
                    else:
                        print("You said {}".format(value))

                        if value.lower() == "no":
                            rospy.loginfo("Exiting Q&A")
                            is_active = False
                            break
                        else:
                            get_speech(value)

                    play_sound.play("Any_More_Questions")

                except sr.UnknownValueError:
                    print("Oops! Didn't catch that")
                    play_sound.play(
                        random.choice(["Sorry", "Excuse_Me", "Pardon"]))

                except sr.RequestError as e:
                    print(
                        "Uh oh! Couldn't request results from Google "
                        "Speech Recognition service; {0}".format(e))

            except sr.WaitTimeoutError:
                rospy.loginfo("Exiting Q&A")
                is_active = False

        return EmptyResponse(True)
Example #2
0
def check_for_warning_time(dt):
    secs = dt.hour * 3600 + dt.minute * 60 + dt.second
    current = datetime.datetime.now()
    diff = secs - (current.hour * 3600 + current.minute * 60 + current.second)
    print(diff)
    if (diff < 120 and diff > 80):
        print("Warning")
        play_sound.play()
        return (1)
Example #3
0
def read_aloud(text, cache=False, spd=5, pit=5, vol=5, per=0, read_times=1):
    """
        cache: 缓存语音文件,当然,缓存后,后续参数都失去意义
    """
    # log.INFO("开始播报:%s" % text)
    # status, mp3_file = get_mp3_file(text, spd, pit, vol, per)
    if isinstance(text, unicode): text = text.encode('utf-8')
    if cache:
        status, mp3_file = get_mp3_file(text, spd, pit, vol, per)
    else:
        status, mp3_file = download_tts_file(text, None, spd, pit, vol, per)
    retry = 3
    while retry:
        if status == 0:
            tmp = read_times
            while read_times > 0:
                play_sound.play(mp3_file)
                read_times -= 1
            log.info("播报‘%s’%d次完成。" % (urllib2.unquote(text), tmp))
            break
        elif status == 1:
            play_sound.play(mp3_file)
            break
        elif status == 2:
            log.warn("语音转换重试。")
            status, mp3_file = get_mp3_file(text)
            retry -= 1
    else:
        play_sound.play(LOCAL_AUDIOS['TTS_ERROR'])
    if (not cache) \
            and (mp3_file not in LOCAL_AUDIOS.values()) \
            and os.path.exists(mp3_file):
        os.remove(mp3_file)
def save_tour(msg):
    loc = msg.lower().find("add location")

    if loc > -1:
        input_string = msg[loc:].lower().replace("add location ", "", 1)

        if " to tour " in input_string:
            location_name, tour_name = input_string.split(" to tour ", 1)

            is_save_tour_success = save_tour_client.save_tour(
                tour_name, [location_name])

            if is_save_tour_success:
                to_say = location_name + " added to " + tour_name
                sound_file.create_wave(to_say, tour_name)
                play_sound.play("Tour_saved")
                # play_sound.speak_text(to_say)
                play_sound.play(tour_name)
                sound_file.delete_wave(tour_name)
            else:
                play_sound.play("Tour_not_saved")

            return True, is_save_tour_success

    return False, False
def save_location(msg):
    index = msg.lower().find("save this location as")

    if index > -1:
        loc_name = msg[index:].lower().replace("save this location as ", "", 1)

        is_save_loc_success = save_location_client.save_location(loc_name)

        if is_save_loc_success:
            text_to_speak = "Location saved as " + loc_name
            sound_file.create_wave(text_to_speak, loc_name)
            play_sound.play("Location_saved")
            # play_sound.speak_text(text_to_speak)
            # play_sound.play("Location_saved_as")
            play_sound.play(loc_name)
            sound_file.delete_wave(loc_name)

        else:
            play_sound.play("Location_not_saved")

        return True, is_save_loc_success

    return False, False
Example #6
0
    return [
        block_schedule, block_schedule, block_schedule, block_schedule,
        block_schedule, no_schedule, no_schedule
    ][datetime.datetime.today().weekday()]


def determine_schedule():
    f = open('/home/pi/warning_bell/schedule.txt', 'r')
    schedule_command = f.read().strip()
    if ':' in schedule_command:
        schedule = schedule_command.split(',')
    elif schedule_command == 'auto':
        schedule = default_schedule()
    else:
        schedule = {
            'regular': regular_schedule,
            'block': block_schedule,
            'event_block': event_block_schedule,
            'late_block': late_block_schedule,
            'none': no_schedule
        }[schedule_command]
    f.close()
    return schedule


print("Example Sound from Warning Bell")
play_sound.play('OldCarStarting.mp3')
while True:
    check_schedule()
    time.sleep(10)
Example #7
0
def handle_request(req):

    """
    Function to handle requests when a client requests a tour service.
    :param req: The request consists of a tour name as well as a tour level.
    :return:
    """

    result = True

    if req.tourName in tours:

        play_sound.play("Welcome")

        i = 0

        for location_name in tours[req.tourName]:

            rospy.wait_for_service('navigate_to')
            rospy.wait_for_service('do_presentation')
            rospy.wait_for_service('google_recognizer_service')

            # Check if wav file exists

            try:
                poi_service = rospy.ServiceProxy('navigate_to', stdService)
                response = poi_service(location_name)
                if response.result:
                    rospy.loginfo("Successfully reached " + location_name)

                else:
                    rospy.loginfo(
                        "Unsuccessfully: unable to reach " + location_name)
                    result = False
                    break

            except rospy.ServiceException, e:
                print "Service call failed: %s" % e
                result = False

            try:
                presentation_service = rospy.ServiceProxy('do_presentation',
                                                          Presentation)
                response = presentation_service(location_name, req.tourLevel)

                if response.success:
                    rospy.loginfo("Successfully presented " + location_name)

                else:
                    rospy.loginfo(
                        "Unsuccessfully: unable find presentation for " +
                        location_name)

            except rospy.ServiceException, e:
                print "Presentation Service call failed: %s" % e

            time.sleep(1)

            try:

                play_sound.play("Any_Questions")
                qna_service = rospy.ServiceProxy('google_recognizer_service',
                                                 Empty)
                qna_service(True)

            except rospy.ServiceException, e:
                print "Service call failed: %s" % e
Example #8
0
            time.sleep(1)

            try:

                play_sound.play("Any_Questions")
                qna_service = rospy.ServiceProxy('google_recognizer_service',
                                                 Empty)
                qna_service(True)

            except rospy.ServiceException, e:
                print "Service call failed: %s" % e

            i += 1
            if i < len(tours[req.tourName]):
                play_sound.play("Move_On")

            else:
                play_sound.play("End")

    else:
        rospy.loginfo(
            "Tour does not exist" + req.tourName + " : " + req.tourLevel)
        result = False

    return doTourResponse(result)


def initiate_do_tour_service(tours_dict=None):
    """
    Function to start the 'do_tour' service with the with the right tour
Example #9
0
import play_sound

play_sound.play('/home/stephanie/ProyectoFinal/whitenoise.wav')