Simplify _calculate_word_times
This commit is contained in:
@@ -229,7 +229,7 @@ class RimeTTSService(WordTTSService, WebsocketService):
|
|||||||
ends: List of end times for each word.
|
ends: List of end times for each word.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
List of (word, timestamp) pairs with proper spacing and timing.
|
List of (word, timestamp) pairs with proper timing.
|
||||||
"""
|
"""
|
||||||
word_pairs = []
|
word_pairs = []
|
||||||
for i, (word, start_time, end_time) in enumerate(zip(words, starts, ends)):
|
for i, (word, start_time, end_time) in enumerate(zip(words, starts, ends)):
|
||||||
@@ -239,18 +239,12 @@ class RimeTTSService(WordTTSService, WebsocketService):
|
|||||||
# Adjust timing by adding cumulative time
|
# Adjust timing by adding cumulative time
|
||||||
adjusted_start = start_time + self._cumulative_time
|
adjusted_start = start_time + self._cumulative_time
|
||||||
|
|
||||||
# Handle spacing and punctuation
|
# Handle punctuation by appending to previous word
|
||||||
is_punctuation = bool(word.strip(",.!?") == "")
|
is_punctuation = bool(word.strip(",.!?") == "")
|
||||||
if is_punctuation:
|
if is_punctuation and word_pairs:
|
||||||
# Append punctuation to previous word
|
prev_word, prev_time = word_pairs[-1]
|
||||||
if word_pairs:
|
word_pairs[-1] = (prev_word + word, prev_time)
|
||||||
prev_word, prev_time = word_pairs[-1]
|
|
||||||
word_pairs[-1] = (prev_word + word, prev_time)
|
|
||||||
else:
|
else:
|
||||||
# Add space between words (not before punctuation)
|
|
||||||
needs_space = word_pairs and not words[i - 1].strip(",.!?") == ""
|
|
||||||
if needs_space:
|
|
||||||
word = " " + word
|
|
||||||
word_pairs.append((word, adjusted_start))
|
word_pairs.append((word, adjusted_start))
|
||||||
|
|
||||||
return word_pairs
|
return word_pairs
|
||||||
@@ -259,6 +253,7 @@ class RimeTTSService(WordTTSService, WebsocketService):
|
|||||||
"""Process incoming websocket messages."""
|
"""Process incoming websocket messages."""
|
||||||
async for message in self._get_websocket():
|
async for message in self._get_websocket():
|
||||||
msg = json.loads(message)
|
msg = json.loads(message)
|
||||||
|
|
||||||
if not msg or msg["contextId"] != self._context_id:
|
if not msg or msg["contextId"] != self._context_id:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
@@ -286,6 +281,7 @@ class RimeTTSService(WordTTSService, WebsocketService):
|
|||||||
if word_pairs:
|
if word_pairs:
|
||||||
await self.add_word_timestamps(word_pairs)
|
await self.add_word_timestamps(word_pairs)
|
||||||
self._cumulative_time = ends[-1] + self._cumulative_time
|
self._cumulative_time = ends[-1] + self._cumulative_time
|
||||||
|
logger.debug(f"Updated cumulative time to: {self._cumulative_time}")
|
||||||
|
|
||||||
elif msg["type"] == "error":
|
elif msg["type"] == "error":
|
||||||
logger.error(f"{self} error: {msg}")
|
logger.error(f"{self} error: {msg}")
|
||||||
|
|||||||
Reference in New Issue
Block a user