project_charles / tests /test_talking.py
sohojoe's picture
animate charles
cf5e7f4
# Modifying the code to ensure the mouth is open when the character starts talking
import random
import time
class CharacterFace:
def __init__(self):
self.mouth_open = False
self.last_change_time = 0
self.next_change_in = 0
def update(self, is_talking, start_talking=False):
current_time = time.time()
# Open the mouth when the character starts talking
if start_talking:
self.mouth_open = True
self.next_change_in = current_time + random.uniform(0.1, 0.5)
return self.mouth_open
# Initialize the next change time if it's zero.
if self.next_change_in == 0:
self.next_change_in = current_time + random.uniform(0.1, 0.5)
# Update the mouth state only if the character is talking.
if is_talking:
# Check if it's time to change the mouth state.
if current_time >= self.next_change_in:
self.mouth_open = not self.mouth_open
self.next_change_in = current_time + random.uniform(0.1, 0.5)
else:
# Close the mouth if the character is not talking.
self.mouth_open = False
return self.mouth_open
def _debug_test():
# Example usage
face = CharacterFace()
output = []
# Initialize variables to control talk and pause durations
next_talk_time = 0
next_pause_time = 0
is_talking = False
# Simulate the character talking and not talking with variable durations
for _ in range(500): # Increase the number of iterations for a longer simulation
current_time = time.time()
start_talking = False
if is_talking and current_time >= next_talk_time:
is_talking = False
next_pause_time = current_time + random.uniform(0.5, 3.0)
if not is_talking and current_time >= next_pause_time:
is_talking = True
start_talking = True # Set flag to open mouth at the start of talking
next_talk_time = current_time + random.uniform(1.0, 5.0)
mouth_open = face.update(is_talking, start_talking)
print(f"Is Talking: {is_talking}, Mouth Open: {mouth_open}")
time.sleep(random.uniform(0.1, 0.5))