Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix player voicelines #513

Merged
merged 1 commit into from
Feb 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 19 additions & 11 deletions src/conversation/conversation.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ def continue_conversation(self) -> tuple[str, sentence | None]:
return comm_consts.KEY_REPLYTYPE_PLAYERTALK, None

@utils.time_it
def process_player_input(self, player_text: str) -> tuple[str, bool]:
def process_player_input(self, player_text: str) -> tuple[str, bool, sentence|None]:
"""Submit the input of the player to the conversation

Args:
Expand All @@ -178,7 +178,7 @@ def process_player_input(self, player_text: str) -> tuple[str, bool]:
"""
player_character = self.__context.npcs_in_conversation.get_player_character()
if not player_character:
return '', False # If there is no player in the conversation, exit here
return '', False, None # If there is no player in the conversation, exit here

events_need_updating: bool = False

Expand All @@ -200,7 +200,7 @@ def process_player_input(self, player_text: str) -> tuple[str, bool]:
# If too much time has passed, in-game events need to be updated
events_need_updating = True
logging.debug('Updating game events...')
return player_text, events_need_updating
return player_text, events_need_updating, None

if self.__mic_ptt:
# only start listening when push-to-talk button pressed again
Expand All @@ -209,13 +209,8 @@ def process_player_input(self, player_text: str) -> tuple[str, bool]:
new_message: user_message = user_message(player_text, player_character.name, False)
new_message.is_multi_npc_message = self.__context.npcs_in_conversation.contains_multiple_npcs()
new_message = self.update_game_events(new_message)
self.__messages.add_message(new_message)
if self.__should_voice_player_input(player_character):
player__character_voiced_sentence = self.__output_manager.generate_sentence(sentence_content(player_character, player_text, False, False))
if player__character_voiced_sentence.error_message:
player_message_content: sentence_content = sentence_content(player_character, player_text, False, False)
player__character_voiced_sentence = sentence(player_message_content, "" , 2.0)
self.__sentences.put(player__character_voiced_sentence)
self.__messages.add_message(new_message)
player_voiceline = self.__get_player_voiceline(player_character, player_text)
text = new_message.text
logging.log(23, f"Text passed to NPC: {text}")

Expand All @@ -228,12 +223,25 @@ def process_player_input(self, player_text: str) -> tuple[str, bool]:
else:
self.__start_generating_npc_sentences()

return player_text, events_need_updating
return player_text, events_need_updating, player_voiceline

def __get_mic_prompt(self):
mic_prompt = f"This is a conversation with {self.__context.get_character_names_as_text(False)} in {self.__context.location}."
#logging.log(23, f'Context for mic transcription: {mic_prompt}')
return mic_prompt

@utils.time_it
def __get_player_voiceline(self, player_character: Character | None, player_text: str) -> sentence | None:
"""Synthesizes the player's input if player voice input is enabled, or else returns None
"""
player_character_voiced_sentence: sentence | None = None
if self.__should_voice_player_input(player_character):
player_character_voiced_sentence = self.__output_manager.generate_sentence(sentence_content(player_character, player_text, False, False))
if player_character_voiced_sentence.error_message:
player_message_content: sentence_content = sentence_content(player_character, player_text, False, False)
player_character_voiced_sentence = sentence(player_message_content, "" , 2.0)

return player_character_voiced_sentence

@utils.time_it
def update_context(self, location: str | None, time: int, custom_ingame_events: list[str], weather: str, custom_context_values: dict[str, Any]):
Expand Down
8 changes: 6 additions & 2 deletions src/game_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def player_input(self, input_json: dict[str, Any]) -> dict[str, Any]:

player_text: str = input_json.get(comm_consts.KEY_REQUESTTYPE_PLAYERINPUT, '')
self.__update_context(input_json)
updated_player_text, update_events = self.__talk.process_player_input(player_text)
updated_player_text, update_events, player_spoken_sentence = self.__talk.process_player_input(player_text)
if update_events:
return {comm_consts.KEY_REPLYTYPE: comm_consts.KEY_REQUESTTYPE_TTS, comm_consts.KEY_TRANSCRIBE: updated_player_text}

Expand All @@ -128,7 +128,11 @@ def player_input(self, input_json: dict[str, Any]) -> dict[str, Any]:
}

# if the player response is not an action command, return a regular player reply type
return {comm_consts.KEY_REPLYTYPE: comm_consts.KEY_REPLYTYPE_NPCTALK}
if player_spoken_sentence:
self.__game.prepare_sentence_for_game(player_spoken_sentence, self.__talk.context, self.__config)
return {comm_consts.KEY_REPLYTYPE: comm_consts.KEY_REPLYTYPE_NPCTALK, comm_consts.KEY_REPLYTYPE_NPCTALK: self.sentence_to_json(player_spoken_sentence)}
else:
return {comm_consts.KEY_REPLYTYPE: comm_consts.KEY_REPLYTYPE_NPCTALK}

@utils.time_it
def end_conversation(self, input_json: dict[str, Any]) -> dict[str, Any]:
Expand Down