基本代码的第一部分运行良好,当尝试实现第二部分时,它不运行

问题描述 投票:0回答:0

我试图让自己成为一个程序来听输入语音命令。

意图是对这些命令以文本和语音响应的形式输出。

有时文字和语音同时出现,有时完全不同。

我知道文本命令很愚蠢。

我是一个完全的初学者,我在进行过程中进行自由泳,以保持有趣并在遇到问题时学习。

我可以让基本的脚本运行起来,这是肯定的,,

我想做什么:

  • 将文本从即时打印更改为打字机样式,一次一个字符。
  • 我希望能够在打印时具体确定时间,以便我可以 将它与喜剧时间的声音很好地同步。

记住,我是用这个来学习的,不是很认真

最终,我什至希望能够实现预先录制的音频文件作为对特定输入的响应。

我制作了这个初始程序,效果很好(但文本没有精确同步,因此喜剧时间安排有效):

import speech_recognition as sr
import pyttsx3
import random
import time
import threading

#Remember to install pyaudio
#Also, speech_recognition installs as SpeechRecoginition for some brilliant reason


engine = pyttsx3.init()
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[0].id)

engine.setProperty('rate', 150)


sr.Recognizer().pause_threshold = 3


recognizer = sr.Recognizer()



def speak(text):
    engine.say(text)
    engine.runAndWait()


def print_text(text, end='\n'):
    for char in text:
        print(char, end='', flush=True)
        time.sleep(0.02)
    print(end, end='')


exclude_command = "don't include this"




if __name__ == '__main__':
    # Create and start the first thread for the first part of text and speech
    t1 = threading.Thread(target=print_text, args=[
        "\n\n\n\nHello brother! \n\nI am Khabib Nurmagomedov!\n\nTry saying: \n\nHello, goodbye, jiha...... uhm.........."])
    t1.start()
    speak('Hello brother, i am habib nurmagomedoff. Try saying hello, goodbye, jihad, or just tell me to fuck off')

    # Wait for the first thread to finish
    t1.join()

    # Create and start the second thread for the second part of text
    t2 = threading.Thread(target=print_text, args=[
        "\n\n\n\nok, sure. ...yeah. that. \n\n....whatever... \n\n...cba with this voice \n\n\n"])
    t2.start()
    time.sleep(4)
    speak('Whatever you do, dont mention the irishman, you know who, in-shal-lah.')
    print_text('For real, he goes ballistic if you mention the irishman.\n\nHello goes a long way!')

    # Wait for the second thread to finish
    t2.join()

    while True:
        # Listen for a command
        def parseCommand():
            listener = sr.Recognizer()
        print_text('\n\n"Listening for voice..."\n\n')


        with sr.Microphone() as source:
            recognizer.adjust_for_ambient_noise(source)
            audio = recognizer.listen(source)

        # Convert the user's speech to text
        try:
            command = recognizer.recognize_google(audio).lower()

            # Check for the command "hello, also exclude certain statements from YOU SAID"
            if  "haha" not in command and "shut up" not in command:
                print('You said: ' + command + '\n')
                speak('You said: ' + command)


            if "hello" in command.lower():
                print_text('Hello to you too, friendo!')
                speak('Hello to you too, nerd')
            elif "goodbye" in command.lower():
                print_text('Feel free to leave, dont mind the voice\n')
                speak('Nah, youre staying right here, youll do nuthin you little weasel')
            elif "fuck off" in command.lower():
                print_text('Okily Dokily neighbour :)')
                speak('Ok, al-ham-dulillah, Send me message, location, location. Anywhere you want, Oslo, Finnmark, Brooklyn, doesnt matter, tell me, where? Im here. Send location')       
            elif "comma" in command.lower():
                speak('Almost had it, you mumbling fuckhead, speak up next time bitch, put on your big boy pants, touch my camera through the fence, fuckface')
            elif "child" in command.lower():
                speak('Almost had it, you mumbling fuckhead, speak up next time bitch, put on your big boy pants, touch my camera through the fence, fuckface')
            elif "jihad" in command.lower():
                speak('in-shal-lah my brother, Ireland 6 million people, Russian 150 million people')
            elif "haha" in command.lower():
                speak('keep laughing bitch, you think its funny? funny how? How am I funny? How the fuck am I funny, what the fuck is so funny about me? Tell me, tell me whats funny!')
            elif "shut up" in command.lower():
                 speak('shut up yourself')

            else:
                print_text('Nice try, doing great there buddy! Keep talking back :)\n')
            # Randomly add extra derogatory comment to the output
            if random.randint(0, 1) == 1:
                print_text('\n\nGreat job friendo, keep speaking with a normal tone and volume, dont mind the voice')
                speak('did you also say that you are a massive homosexual? Kind of hard to hear you with that cheap ass microphone')



        except sr.UnknownValueError:
            speak("uhm, Dis guy i tink is maybe little bit stupid guy, is voice recognition program, speak brother, i am listen")

所以我做了这个

import pyttsx3
import threading
import time


engine = pyttsx3.init()


rate = engine.getProperty('rate')
engine.setProperty('rate', rate - 50)


def speak(text):
    engine.say(text)
    engine.runAndWait()










def print_text(text, end='\n'):
    for char in text:
        print(char, end='', flush=True)
        time.sleep(0.02)
    print(end, end='')











def thread_func():
    print_text("\n\n\n\nHello brother!\n\nI am Khabib Nurmagomedov!\n\nTry saying: ")
    speak("Hello brother, i am habib nurmagomedoff. Try saying hello, goodbye, jihad, or just tell me to fuck off, its up to you really")


t = threading.Thread(target=thread_func)
t.start()


t2 = threading.Timer(5, print_text, args=["\n\nHello, ", ' '])
t2.start()

t3 = threading.Timer(6, print_text, args=["goodbye, ", ' '])
t3.start()

t4 = threading.Timer(6.5, print_text, args=["jiha...... uhm...", ''])
t4.start()



t5 = threading.Timer(7.5, print_text, args=[" or just tell me to........."])
t5.start()

t6 = threading.Timer(9, print_text, args=[" \n...FIND something. ITS SUPPOSED TO BE ''FIND SOMETHING''. jesus christ."])
t6.start()


t7 = threading.Timer(13.0, print_text, args=["\n\n\n\nok, sure. ...yeah. that. \n\n....whatever..."])
t7.start()

运行良好,使我能够很好地控制文本时间,但如果我随后尝试在 t7 之后添加原始脚本的响应“

AKA 在“好的,当然。......是的。那。随便什么。”

然后它拒绝运行。

我试过用 900 万种不同的方式询问 chatgpt,我知道这可能是一个非常简单的解决方案,但我想不出来。

我只是想学习。

multithreading text-to-speech timing basic simultaneous
© www.soinside.com 2019 - 2024. All rights reserved.