@@ -21,6 +21,11 @@ def __init__(self):
2121
2222 self .service_group = MutuallyExclusiveCallbackGroup ()
2323 self .timer_group = MutuallyExclusiveCallbackGroup ()
24+
25+ # Cooldown settings
26+ self .cooldown_timer = None
27+ self .in_cooldown = False
28+ self .COOLDOWN_DURATION = 1.0 # seconds
2429
2530 # Initialize Eleven Labs SDK client
2631 api_key = os .environ .get ('ELEVEN_LABS_API_KEY' )
@@ -78,13 +83,18 @@ def publish_status(self):
7883 self .status_pub .publish (msg )
7984
8085 def tts_query_callback (self , request , response ):
81- """
82- Generate a behavior response based on the user's intent and prompt.
83- """
84- text = request .text
85- self .get_logger ().info (f"Streaming audio for text: { text } " )
86-
86+ """Handle TTS query service requests."""
8787 try :
88+ # Check if we're already playing audio or in cooldown
89+ if self .is_playing or self .in_cooldown :
90+ state = "playing" if self .is_playing else "cooling down"
91+ self .get_logger ().warn (f"TTS is { state } , request rejected" )
92+ response .success = False
93+ return response
94+
95+ text = request .text
96+ self .get_logger ().info (f"Streaming audio for text: { text } " )
97+
8898 # Start a new thread for audio streaming and playback
8999 playback_thread = threading .Thread (
90100 target = self .stream_audio_playback ,
@@ -166,9 +176,39 @@ def stream_audio_playback(self, text):
166176 except Exception as e :
167177 self .get_logger ().error (f"Error publishing completion state: { str (e )} " )
168178
179+ # Start cooldown period
180+ self .in_cooldown = True
181+ msg = String ()
182+ msg .data = 'cooldown'
183+ self .audio_state_pub .publish (msg )
184+
185+ if self .cooldown_timer :
186+ self .cooldown_timer .cancel ()
187+ self .cooldown_timer = self .create_timer (
188+ self .COOLDOWN_DURATION ,
189+ self ._end_cooldown ,
190+ callback_group = self .timer_group
191+ )
192+
193+ self .get_logger ().info (f"Starting { self .COOLDOWN_DURATION } s cooldown" )
169194 self .is_playing = False
195+
170196 self .get_logger ().info ("Audio playback completed" )
171197
198+ def _end_cooldown (self ):
199+ """End the cooldown period and signal that the system is ready."""
200+ self .in_cooldown = False
201+ if self .cooldown_timer :
202+ self .cooldown_timer .cancel ()
203+ self .cooldown_timer = None
204+
205+ # Publish completion state
206+ msg = String ()
207+ msg .data = 'done'
208+ self .audio_state_pub .publish (msg )
209+
210+ self .get_logger ().info ("Cooldown complete, system ready" )
211+
172212
173213def main (args = None ):
174214 rclpy .init (args = args )
0 commit comments