Skip to content

Commit

Permalink
Merge pull request #857 from pipecat-ai/aleix/fix-riva-tts-audio-stut…
Browse files Browse the repository at this point in the history
…tering

riva: fix FastPitchTTSService audio stuttering
  • Loading branch information
aconchillo authored Dec 13, 2024
2 parents 2b8c35c + 420ce16 commit 8f24ca4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed

- Fixed an audio stuttering issue in `FastPitchTTSService`.

- Fixed a `BaseOutputTransport` issue that was causing non-audio frames being
processed before the previous audio frames were played. This will allow, for
example, sending a frame `A` after a `TTSSpeakFrame` and the frame `A` will
Expand Down
21 changes: 15 additions & 6 deletions src/pipecat/services/riva.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,10 @@ def __init__(
)

async def run_tts(self, text: str) -> AsyncGenerator[Frame, None]:
def read_audio_responses():
def read_audio_responses(queue: asyncio.Queue):
def add_response(r):
asyncio.run_coroutine_threadsafe(queue.put(r), self.get_event_loop())

try:
responses = self._service.synthesize_online(
text,
Expand All @@ -87,26 +90,32 @@ def read_audio_responses():
quality=self._quality,
custom_dictionary={},
)
return responses
for r in responses:
add_response(r)
add_response(None)
except Exception as e:
logger.error(f"{self} exception: {e}")
return []
add_response(None)

await self.start_ttfb_metrics()
yield TTSStartedFrame()

logger.debug(f"Generating TTS: [{text}]")
responses = await asyncio.to_thread(read_audio_responses)

for resp in responses:
await self.stop_ttfb_metrics()
queue = asyncio.Queue()
await asyncio.to_thread(read_audio_responses, queue)

# Wait for the thread to start.
resp = await queue.get()
while resp:
await self.stop_ttfb_metrics()
frame = TTSAudioRawFrame(
audio=resp.audio,
sample_rate=self._sample_rate,
num_channels=1,
)
yield frame
resp = await queue.get()

await self.start_tts_usage_metrics(text)
yield TTSStoppedFrame()
Expand Down

0 comments on commit 8f24ca4

Please sign in to comment.