[Bugfix] Report sample rate and channels for streamed speech PCM (#8567)
Signed-off-by: Yueqian Lin <linyueqian@outlook.com>
This commit is contained in:
1 parent
c32a26a845
commit
1c71a2c03a
8 files changed
+577
-93
No files matched your search
@@ -30,6 +30,9 @@ Protocol:
|
||||
Server -> Client (default, word_timestamps=false):
|
||||
{"type": "audio.start", "utterance_index": 0, "sentence_index": 0,
|
||||
"sentence_text": "...", "format": "wav"}
|
||||
# With stream_audio=true, audio.start is sent with the first audio
|
||||
# chunk; for format "pcm" it also carries that audio's "sample_rate"
|
||||
# and "channels" (s16le, interleaved, model-native).
|
||||
<binary frame: audio bytes>
|
||||
...
|
||||
{"type": "audio.done", "utterance_index": 0, "sentence_index": 0}
|
||||
@@ -56,6 +59,7 @@ import asyncio
|
||||
import base64
|
||||
import json
|
||||
import time
|
||||
from collections.abc import Awaitable, Callable
|
||||
from contextlib import aclosing
|
||||
|
||||
from fastapi import WebSocket, WebSocketDisconnect
|
||||
@@ -75,7 +79,7 @@ logger = init_logger(__name__)
|
||||
_DEFAULT_IDLE_TIMEOUT = 30.0 # seconds
|
||||
_DEFAULT_CONFIG_TIMEOUT = 10.0 # seconds
|
||||
_PCM_SAMPLE_RATE = 24000
|
||||
_BYTES_PER_SAMPLE = 2 # 16-bit mono PCM
|
||||
_BYTES_PER_SAMPLE = 2 # 16-bit PCM, per channel
|
||||
_MAX_CONFIG_MESSAGE_SIZE = 4 * 1024 * 1024 # allow large ref_audio payloads
|
||||
_MAX_INPUT_TEXT_MESSAGE_SIZE = 128 * 1024
|
||||
|
||||
@@ -336,13 +340,28 @@ class OmniStreamingSpeechHandler:
|
||||
"sentence_text": sentence_text,
|
||||
"format": response_format,
|
||||
}
|
||||
if config.stream_audio and response_format == "pcm":
|
||||
# Nominal stream rate; each audio.chunk carries the authoritative
|
||||
# per-chunk sample_rate.
|
||||
start_payload["sample_rate"] = _PCM_SAMPLE_RATE
|
||||
if config.word_timestamps:
|
||||
start_payload["word_timestamps"] = True
|
||||
await websocket.send_json(start_payload)
|
||||
# Streamed pcm is model-native s16le. Its rate and channel count are
|
||||
# known once the first chunk is encoded, so for pcm audio.start is
|
||||
# deferred to that point and states them; it is sent bare if no audio
|
||||
# arrives. Non-streamed responses still send it before generation.
|
||||
describe_pcm = config.stream_audio and response_format == "pcm"
|
||||
audio_format: dict[str, int] = {}
|
||||
start_sent = False
|
||||
|
||||
async def send_start() -> None:
|
||||
nonlocal start_sent
|
||||
if start_sent:
|
||||
return
|
||||
start_sent = True
|
||||
if describe_pcm and audio_format:
|
||||
start_payload["sample_rate"] = audio_format["sample_rate"]
|
||||
start_payload["channels"] = audio_format["channels"]
|
||||
await websocket.send_json(start_payload)
|
||||
|
||||
if not describe_pcm:
|
||||
await send_start()
|
||||
|
||||
total_bytes = 0
|
||||
generation_failed = False
|
||||
@@ -365,6 +384,8 @@ class OmniStreamingSpeechHandler:
|
||||
request_start_s=request_start_s,
|
||||
request_arrival_ts=request_arrival_ts,
|
||||
tts_params=tts_params,
|
||||
audio_format=audio_format,
|
||||
send_start=send_start,
|
||||
)
|
||||
else:
|
||||
async with aclosing(
|
||||
@@ -374,9 +395,11 @@ class OmniStreamingSpeechHandler:
|
||||
request_start_s=request_start_s,
|
||||
request_arrival_ts=request_arrival_ts,
|
||||
tts_params=tts_params,
|
||||
audio_format=audio_format,
|
||||
)
|
||||
) as stream:
|
||||
async for chunk in stream:
|
||||
await send_start()
|
||||
total_bytes += len(chunk)
|
||||
await websocket.send_bytes(chunk)
|
||||
else:
|
||||
@@ -401,6 +424,10 @@ class OmniStreamingSpeechHandler:
|
||||
sentence_index,
|
||||
e,
|
||||
)
|
||||
try:
|
||||
await send_start()
|
||||
except Exception:
|
||||
logger.debug("Failed to send audio.start for sentence %d", sentence_index, exc_info=True)
|
||||
await self._send_error(
|
||||
websocket,
|
||||
f"Generation failed for utterance {utterance_index}, sentence {sentence_index}: {e}",
|
||||
@@ -408,6 +435,9 @@ class OmniStreamingSpeechHandler:
|
||||
)
|
||||
finally:
|
||||
try:
|
||||
if not start_sent:
|
||||
# Keep audio.start/audio.done paired when no audio was produced.
|
||||
await send_start()
|
||||
await websocket.send_json(
|
||||
{
|
||||
"type": "audio.done",
|
||||
@@ -433,6 +463,8 @@ class OmniStreamingSpeechHandler:
|
||||
request_arrival_ts: float,
|
||||
language: str | None = None,
|
||||
tts_params: dict | None = None,
|
||||
audio_format: dict[str, int] | None = None,
|
||||
send_start: Callable[[], Awaitable[None]] | None = None,
|
||||
) -> int:
|
||||
"""Stream PCM as JSON ``audio.chunk`` frames, aligned per sentence.
|
||||
|
||||
@@ -450,6 +482,10 @@ class OmniStreamingSpeechHandler:
|
||||
chunk_id = 0
|
||||
# Receives the aligner stage's pooling output from the generator.
|
||||
collect: dict = {}
|
||||
audio_format = audio_format if audio_format is not None else {}
|
||||
|
||||
def frame_bytes() -> int:
|
||||
return _BYTES_PER_SAMPLE * int(audio_format.get("channels", 1))
|
||||
|
||||
async def send_chunk(
|
||||
chunk: bytes,
|
||||
@@ -459,6 +495,8 @@ class OmniStreamingSpeechHandler:
|
||||
chunk_end_ms: int,
|
||||
) -> None:
|
||||
nonlocal chunk_id
|
||||
if send_start is not None:
|
||||
await send_start()
|
||||
await websocket.send_json(
|
||||
{
|
||||
"type": "audio.chunk",
|
||||
@@ -468,6 +506,7 @@ class OmniStreamingSpeechHandler:
|
||||
"chunk_start_ms": chunk_start_ms,
|
||||
"chunk_end_ms": chunk_end_ms,
|
||||
"sample_rate": chunk_sample_rate,
|
||||
"channels": int(audio_format.get("channels", 1)),
|
||||
"audio_b64": base64.b64encode(chunk).decode("ascii"),
|
||||
"timestamps": timestamps_payload,
|
||||
}
|
||||
@@ -484,13 +523,14 @@ class OmniStreamingSpeechHandler:
|
||||
tts_params=tts_params,
|
||||
collect=collect,
|
||||
cumulative_audio=True,
|
||||
audio_format=audio_format,
|
||||
)
|
||||
) as stream:
|
||||
async for chunk, chunk_sample_rate in stream:
|
||||
sample_rate = chunk_sample_rate
|
||||
chunk_start_ms = int(round((audio_bytes_seen / _BYTES_PER_SAMPLE / sample_rate) * 1000.0))
|
||||
chunk_start_ms = int(round((audio_bytes_seen / frame_bytes() / sample_rate) * 1000.0))
|
||||
audio_bytes_seen += len(chunk)
|
||||
chunk_end_ms = int(round((audio_bytes_seen / _BYTES_PER_SAMPLE / sample_rate) * 1000.0))
|
||||
chunk_end_ms = int(round((audio_bytes_seen / frame_bytes() / sample_rate) * 1000.0))
|
||||
total_bytes += len(chunk)
|
||||
# Audio first, timestamps after the whole sentence is aligned.
|
||||
await send_chunk(chunk, chunk_sample_rate, None, chunk_start_ms, chunk_end_ms)
|
||||
@@ -502,7 +542,7 @@ class OmniStreamingSpeechHandler:
|
||||
timestamps_payload = (
|
||||
extract_word_timestamps(aligner_res, sentence_text, language) if aligner_res is not None else None
|
||||
)
|
||||
sentence_end_ms = int(round((audio_bytes_seen / _BYTES_PER_SAMPLE / sample_rate) * 1000.0))
|
||||
sentence_end_ms = int(round((audio_bytes_seen / frame_bytes() / sample_rate) * 1000.0))
|
||||
await send_chunk(b"", sample_rate, timestamps_payload, 0, sentence_end_ms)
|
||||
|
||||
return total_bytes
|
||||
|
||||
Reference in new issue
Block a user