Code review feedback
This commit is contained in:
@@ -8,11 +8,11 @@ import asyncio
|
|||||||
import json
|
import json
|
||||||
from typing import AsyncGenerator, List, Literal, Optional
|
from typing import AsyncGenerator, List, Literal, Optional
|
||||||
|
|
||||||
import numpy as np
|
|
||||||
from loguru import logger
|
from loguru import logger
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
|
|
||||||
from pipecat.frames.frames import (
|
from pipecat.frames.frames import (
|
||||||
|
ErrorFrame,
|
||||||
Frame,
|
Frame,
|
||||||
LLMFullResponseEndFrame,
|
LLMFullResponseEndFrame,
|
||||||
LLMFullResponseStartFrame,
|
LLMFullResponseStartFrame,
|
||||||
@@ -307,24 +307,13 @@ class GoogleTTSService(TTSService):
|
|||||||
|
|
||||||
await self.push_frame(TTSStartedFrame())
|
await self.push_frame(TTSStartedFrame())
|
||||||
|
|
||||||
# The audio produced by the TTS service has an audible click or pop at the beginning.
|
# Skip the first 44 bytes to remove the WAV header
|
||||||
# This is due to the abrupt start of the audio waveform. To mitigate this, we apply a
|
audio_content = response.audio_content[44:]
|
||||||
# short fade-in effect to the audio data.
|
|
||||||
|
|
||||||
# Convert the response to a mutable numpy array
|
|
||||||
audio_content = np.frombuffer(response.audio_content, dtype=np.int16).copy()
|
|
||||||
|
|
||||||
# Apply a smooth, short fade-in
|
|
||||||
fade_duration = int(0.01 * self.sample_rate) # 10ms fade-in
|
|
||||||
fade_in = np.square(
|
|
||||||
np.linspace(0, 1, fade_duration)
|
|
||||||
) # Quadratic fade for smoother start
|
|
||||||
audio_content[:fade_duration] = audio_content[:fade_duration] * fade_in
|
|
||||||
|
|
||||||
# Read and yield audio data in chunks
|
# Read and yield audio data in chunks
|
||||||
chunk_size = 8192
|
chunk_size = 8192
|
||||||
for i in range(0, len(audio_content), chunk_size):
|
for i in range(0, len(audio_content), chunk_size):
|
||||||
chunk = audio_content[i : i + chunk_size].tobytes()
|
chunk = audio_content[i : i + chunk_size]
|
||||||
if not chunk:
|
if not chunk:
|
||||||
break
|
break
|
||||||
await self.stop_ttfb_metrics()
|
await self.stop_ttfb_metrics()
|
||||||
@@ -336,3 +325,7 @@ class GoogleTTSService(TTSService):
|
|||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.exception(f"{self} error generating TTS: {e}")
|
logger.exception(f"{self} error generating TTS: {e}")
|
||||||
|
error_message = f"TTS generation error: {str(e)}"
|
||||||
|
yield ErrorFrame(error=error_message)
|
||||||
|
finally:
|
||||||
|
await self.push_frame(TTSStoppedFrame())
|
||||||
|
|||||||
Reference in New Issue
Block a user