examples(twilio-chatbot): create sample rate variable

This commit is contained in:
Aleix Conchillo Flaqué
2025-02-03 10:58:06 -08:00
parent 51a86a509c
commit 1cdb66f889
2 changed files with 20 additions and 15 deletions

View File

@@ -36,6 +36,8 @@ load_dotenv(override=True)
logger.remove(0)
logger.add(sys.stderr, level="DEBUG")
SAMPLE_RATE = 8000
async def save_audio(server_name: str, audio: bytes, sample_rate: int, num_channels: int):
if len(audio) > 0:
@@ -61,13 +63,13 @@ async def run_bot(websocket_client: WebSocket, stream_sid: str, testing: bool):
params=FastAPIWebsocketParams(
audio_in_enabled=True,
audio_out_enabled=True,
audio_out_sample_rate=8000,
audio_out_sample_rate=SAMPLE_RATE,
add_wav_header=False,
vad_enabled=True,
vad_analyzer=SileroVADAnalyzer(sample_rate=8000),
vad_analyzer=SileroVADAnalyzer(sample_rate=SAMPLE_RATE),
vad_audio_passthrough=True,
serializer=TwilioFrameSerializer(
stream_sid, TwilioFrameSerializer.InputParams(sample_rate=8000)
stream_sid, TwilioFrameSerializer.InputParams(sample_rate=SAMPLE_RATE)
),
),
)
@@ -76,14 +78,14 @@ async def run_bot(websocket_client: WebSocket, stream_sid: str, testing: bool):
stt = DeepgramSTTService(
api_key=os.getenv("DEEPGRAM_API_KEY"),
live_options=LiveOptions(sample_rate=8000),
live_options=LiveOptions(sample_rate=SAMPLE_RATE),
audio_passthrough=True,
)
tts = CartesiaTTSService(
api_key=os.getenv("CARTESIA_API_KEY"),
voice_id="79a125e8-cd45-4c13-8a67-188112f4dd22", # British Lady
sample_rate=8000,
sample_rate=SAMPLE_RATE,
push_silence_after_stop=testing,
)
@@ -99,7 +101,7 @@ async def run_bot(websocket_client: WebSocket, stream_sid: str, testing: bool):
# NOTE: Watch out! This will save all the conversation in memory. You can
# pass `buffer_size` to get periodic callbacks.
audiobuffer = AudioBufferProcessor(sample_rate=8000)
audiobuffer = AudioBufferProcessor(sample_rate=SAMPLE_RATE)
pipeline = Pipeline(
[

View File

@@ -43,7 +43,8 @@ logger.remove(0)
logger.add(sys.stderr, level="DEBUG")
DEFAULT_DURATION = 30
DEFAULT_CLIENT_DURATION = 30
SAMPLE_RATE = 8000
async def download_twiml(server_url: str) -> str:
@@ -91,13 +92,15 @@ async def run_client(client_name: str, server_url: str, duration_secs: int):
params=WebsocketClientParams(
audio_in_enabled=True,
audio_out_enabled=True,
audio_out_sample_rate=8000,
audio_out_sample_rate=SAMPLE_RATE,
add_wav_header=False,
serializer=TwilioFrameSerializer(
stream_sid, params=TwilioFrameSerializer.InputParams(sample_rate=8000)
stream_sid, params=TwilioFrameSerializer.InputParams(sample_rate=SAMPLE_RATE)
),
vad_enabled=True,
vad_analyzer=SileroVADAnalyzer(params=VADParams(stop_secs=1.5), sample_rate=8000),
vad_analyzer=SileroVADAnalyzer(
params=VADParams(stop_secs=1.5), sample_rate=SAMPLE_RATE
),
vad_audio_passthrough=True,
),
)
@@ -107,14 +110,14 @@ async def run_client(client_name: str, server_url: str, duration_secs: int):
# We let the audio passthrough so we can record the conversation.
stt = DeepgramSTTService(
api_key=os.getenv("DEEPGRAM_API_KEY"),
live_options=LiveOptions(sample_rate=8000),
live_options=LiveOptions(sample_rate=SAMPLE_RATE),
audio_passthrough=True,
)
tts = CartesiaTTSService(
api_key=os.getenv("CARTESIA_API_KEY"),
voice_id="e13cae5c-ec59-4f71-b0a6-266df3c9bb8e", # Madame Mischief
sample_rate=8000,
sample_rate=SAMPLE_RATE,
push_silence_after_stop=True,
)
@@ -130,7 +133,7 @@ async def run_client(client_name: str, server_url: str, duration_secs: int):
# NOTE: Watch out! This will save all the conversation in memory. You can
# pass `buffer_size` to get periodic callbacks.
audiobuffer = AudioBufferProcessor(sample_rate=8000)
audiobuffer = AudioBufferProcessor(sample_rate=SAMPLE_RATE)
pipeline = Pipeline(
[
@@ -185,8 +188,8 @@ async def main():
"-d",
"--duration",
type=int,
default=DEFAULT_DURATION,
help=f"duration of each client in seconds (default: {DEFAULT_DURATION})",
default=DEFAULT_CLIENT_DURATION,
help=f"duration of each client in seconds (default: {DEFAULT_CLIENT_DURATION})",
)
args, _ = parser.parse_known_args()