[Bugfix] Validate Breeze PCM speech content and calibrate HNR guard (#8044)
Signed-off-by: Yueqian Lin <linyueqian@outlook.com>
This commit is contained in:
1 parent
dbd6a35dd2
commit
1d77e96d4d
4 files changed
+216
-12
No files matched your search
@@ -688,7 +688,9 @@ def _compute_pcm_hnr_db(pcm_samples: np.ndarray, sr: int = _PCM_SPEECH_SAMPLE_RA
|
||||
peak = float(np.max(ac[min_lag:max_lag]))
|
||||
if 0 < peak < 1:
|
||||
hnr_values.append(10 * np.log10(peak / (1 - peak + 1e-10)))
|
||||
return float(np.mean(hnr_values)) if hnr_values else 0.0
|
||||
# Silence and constant signals provide no usable harmonic estimate. They
|
||||
# must not pass a model-specific floor of zero or below.
|
||||
return float(np.mean(hnr_values)) if hnr_values else float("-inf")
|
||||
|
||||
|
||||
def _assert_pcm_int16_speech_hnr(
|
||||
@@ -714,10 +716,7 @@ def _assert_pcm_int16_speech_hnr(
|
||||
pcm_samples = np.frombuffer(audio_bytes, dtype=np.int16).astype(np.float32) / 32768.0
|
||||
hnr = _compute_pcm_hnr_db(pcm_samples, sr=sr)
|
||||
print(f"PCM speech HNR: {hnr:.2f} dB (threshold: {min_hnr_db} dB, sr={sr})")
|
||||
assert hnr >= min_hnr_db, (
|
||||
f"Audio distortion detected: HNR={hnr:.2f} dB < {min_hnr_db} dB. "
|
||||
"Voice clone decoder may be losing ref_code speaker context on later chunks."
|
||||
)
|
||||
assert hnr >= min_hnr_db, f"PCM speech HNR={hnr:.2f} dB is below the configured floor of {min_hnr_db} dB."
|
||||
|
||||
|
||||
def _response_has_audio_output(response: Any) -> bool:
|
||||
@@ -754,11 +753,38 @@ def _omni_assertion_needs_audio_transcript(request_config: dict[str, Any], run_l
|
||||
def _speech_assertion_needs_audio_transcript(request_config: dict[str, Any], run_level: str | None) -> bool:
|
||||
if run_level not in {"advanced_model", "full_model"}:
|
||||
return False
|
||||
if request_config.get("response_format") == "pcm":
|
||||
if request_config.get("response_format") == "pcm" and "transcript_pcm_sample_rate" not in request_config:
|
||||
return False
|
||||
return bool(request_config.get("transcript_expected_text", request_config.get("input")))
|
||||
|
||||
|
||||
def _speech_pcm_sample_rate(request_config: dict[str, Any]) -> int:
|
||||
expected = request_config.get("expected_sample_rate")
|
||||
if "transcript_pcm_sample_rate" in request_config:
|
||||
sample_rate = request_config["transcript_pcm_sample_rate"]
|
||||
assert type(sample_rate) is int and sample_rate > 0, "transcript_pcm_sample_rate must be a positive integer"
|
||||
assert expected is None or int(expected) == sample_rate, (
|
||||
"transcript_pcm_sample_rate must match expected_sample_rate"
|
||||
)
|
||||
return sample_rate
|
||||
return int(expected or _PCM_SPEECH_SAMPLE_RATE_HZ)
|
||||
|
||||
|
||||
def _speech_audio_for_transcription(audio_bytes: bytes | None, request_config: dict[str, Any]) -> bytes | None:
|
||||
"""Give ASR a WAV container when a speech test opts into raw PCM transcription."""
|
||||
if not audio_bytes or request_config.get("response_format") != "pcm":
|
||||
return audio_bytes
|
||||
sample_rate = _speech_pcm_sample_rate(request_config)
|
||||
assert len(audio_bytes) % 2 == 0, "PCM byte length must be aligned to int16"
|
||||
with io.BytesIO() as buffer:
|
||||
with wave.open(buffer, "wb") as wav_file:
|
||||
wav_file.setnchannels(1)
|
||||
wav_file.setsampwidth(2)
|
||||
wav_file.setframerate(sample_rate)
|
||||
wav_file.writeframes(audio_bytes)
|
||||
return buffer.getvalue()
|
||||
|
||||
|
||||
def _resolve_audio_transcript(
|
||||
response: Any,
|
||||
request_config: dict[str, Any],
|
||||
@@ -777,7 +803,11 @@ def _resolve_audio_transcript(
|
||||
existing = getattr(response, "audio_content", None)
|
||||
if isinstance(existing, str) and existing.strip():
|
||||
return existing
|
||||
audio_bytes = getattr(response, "audio_bytes", None)
|
||||
audio_bytes = (
|
||||
_speech_audio_for_transcription(getattr(response, "audio_bytes", None), request_config)
|
||||
if speech_api
|
||||
else getattr(response, "audio_bytes", None)
|
||||
)
|
||||
if not audio_bytes:
|
||||
return None
|
||||
return convert_audio_bytes_to_text(
|
||||
@@ -1005,7 +1035,7 @@ def assert_audio_speech_response(response: Any, request_config: dict[str, Any],
|
||||
_assert_pcm_int16_speech_hnr(
|
||||
response.audio_bytes,
|
||||
min_hnr_db=min_hnr_db,
|
||||
sr=int(request_config.get("expected_sample_rate") or _PCM_SPEECH_SAMPLE_RATE_HZ),
|
||||
sr=_speech_pcm_sample_rate(request_config),
|
||||
)
|
||||
|
||||
transcript = _resolve_audio_transcript(response, request_config, run_level, speech_api=True)
|
||||
@@ -1016,7 +1046,7 @@ def assert_audio_speech_response(response: Any, request_config: dict[str, Any],
|
||||
print(f"input text is: {expected_text}")
|
||||
_assert_transcript_matches(
|
||||
transcript,
|
||||
getattr(response, "audio_bytes", None),
|
||||
_speech_audio_for_transcription(getattr(response, "audio_bytes", None), request_config),
|
||||
expected_text,
|
||||
threshold=0.9,
|
||||
escalation_model=request_config.get("transcript_escalation_model"),
|
||||
|
||||
Reference in new issue
Block a user