Skip to content

Commit b0f1fe2

Browse files
committed
Fix issue with playback
1 parent fd52428 commit b0f1fe2

File tree

1 file changed

+20
-15
lines changed
  • coffee_ws/src/effector_nodes/effector_nodes/tts_node

1 file changed

+20
-15
lines changed

coffee_ws/src/effector_nodes/effector_nodes/tts_node/node.py

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ def tts_query_callback(self, request, response):
103103

104104
def stream_audio_playback(self, text):
105105
"""Stream audio directly from ElevenLabs API to PyAudio"""
106+
stream = None
106107
try:
107108
with self.stream_lock:
108109
self.is_playing = True
@@ -118,9 +119,6 @@ def stream_audio_playback(self, text):
118119
msg.data = 'playing'
119120
self.audio_state_pub.publish(msg)
120121

121-
# Set up audio buffer for processing
122-
audio_buffer = bytearray()
123-
124122
# Generate and stream audio
125123
audio_stream = self.eleven_labs_client.generate(
126124
text=text,
@@ -143,26 +141,33 @@ def stream_audio_playback(self, text):
143141

144142
# Process and play each chunk as it arrives
145143
for chunk in audio_stream:
144+
if not self.is_playing: # Check if we should stop playing
145+
break
146146
if chunk: # Skip empty chunks
147147
# For PCM format, we can write directly to the stream
148148
stream.write(chunk)
149149

150-
# Close the audio stream
151-
stream.stop_stream()
152-
stream.close()
153-
154-
# Publish audio completion state
150+
except Exception as e:
151+
self.get_logger().error(f"Error in audio streaming: {str(e)}")
152+
finally:
153+
# Always clean up resources and update state
154+
if stream:
155+
try:
156+
stream.stop_stream()
157+
stream.close()
158+
except Exception as e:
159+
self.get_logger().error(f"Error closing audio stream: {str(e)}")
160+
161+
# Publish completion state
162+
try:
155163
msg = String()
156164
msg.data = 'done'
157165
self.audio_state_pub.publish(msg)
158-
159-
self.get_logger().info("Audio playback completed")
160-
161-
self.is_playing = False
162-
163-
except Exception as e:
164-
self.get_logger().error(f"Error in audio streaming: {str(e)}")
166+
except Exception as e:
167+
self.get_logger().error(f"Error publishing completion state: {str(e)}")
168+
165169
self.is_playing = False
170+
self.get_logger().info("Audio playback completed")
166171

167172

168173
def main(args=None):

0 commit comments

Comments
 (0)