How do I get this pyttsx3 Youtube TTS code to loop forever?

24 Views Asked by At

Before I begin, forgive me. I'm a relatively ametuer coder, a couple weeks of experience at best.

I wanted to create a youtube TTS chatbot that takes livechat messages and reads them out (with a 1/3 chance).

I whipped up a quick chatbot and it works, but only for a little bit. After a seemingly random amount of messages and with no prompting, it just ends.

I am using DougDoug's ChatGPT code and modified it to add pyttsx3 to read the messages instead of doing anything, and added OBSWebsockets.

I'm not sure if I need to include all of my code, but I'll paste the main portion. The problem is probably really obvious but honestly I have no clue at this point.

while countdown > 0:
    print(countdown)
    countdown -= 1
    time.sleep(1)

if STREAMING_ON_TWITCH:
    t = YTPlays_Connection.Twitch()
    t.twitch_connect(TWITCH_CHANNEL)
else:
    t = YTPlays_Connection.YouTube()
    t.youtube_connect(YOUTUBE_CHANNEL_ID, YOUTUBE_STREAM_URL)

def handle_message(message):
    try:
        msg = message['message'].lower()
        username = message['username'].lower()
        print("Got this message from " + username + ": " + msg)
        if random.randint(1, 1) == 1:
            #Show Chat Source on OBS
            obswebsockets_manager.set_source_visibility("INSERT_SCENE", "INSERT_SOURCE", True)
            #Say Message
            TTSEngine = pyttsx3.init()
            voices = TTSEngine.getProperty('voices')
            TTSEngine.setProperty('voice', voices[1].id)
            rate = TTSEngine.getProperty('rate')
            TTSEngine.setProperty('rate', -10)
            TTSEngine.say(msg)
            TTSEngine.runAndWait()
            TTSEngine = None
            #Remove Source on OBS
            obswebsockets_manager.set_source_visibility("INSERT_SCENE", "INSERT_SOURCE", False)

    except Exception as e:
        print("Encountered exception: " + str(e))

    except KeyboardInterrupt:
        # Proper cleanup when the user manually interrupts the program
        TTSEngine.stop()
    print("Program manually interrupted.")

while True:
    active_tasks = [t for t in active_tasks if not t.done()]

    #Check for new messages
    new_messages = t.twitch_receive_messages();

    if new_messages:
        message_queue += new_messages; # New messages are added to the back of the queue
        message_queue = message_queue[-MAX_QUEUE_LENGTH:] # Shorten the queue to only the most recent X messages
 

    messages_to_handle = []

    if not message_queue:
        # No messages in the queue
        last_time = time.time()
    else:
        # Determine how many messages we should handle now
        r = 1 if MESSAGE_RATE == 0 else (time.time() - last_time) / MESSAGE_RATE
        n = int(r * len(message_queue))
        if n > 0:
            # Pop the messages we want off the front of the queue
            messages_to_handle = message_queue[0:n]
            del message_queue[0:n]
            last_time = time.time();
    # If user presses Shift+Backspace, automatically end the program
    if keyboard.is_pressed('shift+backspace'):
        exit()
    if not messages_to_handle:
        continue
    else:
        for message in messages_to_handle:
            if len(active_tasks) <= MAX_WORKERS:
                active_tasks.append(thread_pool.submit(handle_message, message))
            else:
                print(f'WARNING: active tasks ({len(active_tasks)}) exceeds number of workers ({MAX_WORKERS}). ({len(message_queue)} messages in the queue)')```

I created a version that creates a debug log, then put the error and code into ChatGPT, but there's no error, the program just ends abruptly.
0

There are 0 best solutions below