fixed a gain

This commit is contained in:
2026-08-18 21:27:20 +08:00
parent 7556fd0a7e
commit aa8baab7c0
3 changed files with 55 additions and 3 deletions

View File

@@ -7,6 +7,10 @@ import numpy as np
SAMPLE_RATE = 16_000
FRAME_SAMPLES = 480
# A silence run at least this long (in 30 ms frames) separates two sentences.
# speech_frame_mask bridges gaps of up to 5 frames, so use 6 frames (~180 ms).
MIN_PAUSE_FRAMES = 6
ONSET_MARGIN_MS = 50
class AudioAnalysisError(RuntimeError):
@@ -108,6 +112,44 @@ def speech_frame_mask(samples: np.ndarray, sample_rate: int = SAMPLE_RATE) -> np
return speech
def refine_sentence_end_ms(
samples: np.ndarray,
sample_rate: int = SAMPLE_RATE,
*,
raw_end_ms: int,
padded_end_ms: int,
) -> int:
"""Pull a padded sentence end back to just before the next sentence's speech.
Whisper's timestamps are not always aligned with the real audio: the next
segment's start can be later than the actual speech onset, so a fixed
end-padding may occasionally run into the next sentence's beginning. This
finds the first silence run of at least MIN_PAUSE_FRAMES inside the padded
region and stops the sentence just before the speech that follows it.
"""
if padded_end_ms <= raw_end_ms:
return padded_end_ms
frame_ms = 1000 * max(1, int(round(sample_rate * 0.03))) / sample_rate
start_sample = max(0, int(raw_end_ms / 1000 * sample_rate))
end_sample = min(samples.size, int(padded_end_ms / 1000 * sample_rate))
if end_sample <= start_sample:
return padded_end_ms
try:
speech = speech_frame_mask(samples[start_sample:end_sample], sample_rate)
except AudioAnalysisError:
return padded_end_ms
silence_frames = 0
for index, is_speech in enumerate(speech):
if not is_speech:
silence_frames += 1
continue
if silence_frames >= MIN_PAUSE_FRAMES:
onset_ms = int(raw_end_ms + index * frame_ms)
return max(raw_end_ms, min(padded_end_ms, onset_ms - ONSET_MARGIN_MS))
silence_frames = 0
return padded_end_ms
def _bridge_false_runs(values: np.ndarray, max_frames: int) -> None:
start = None
for index, value in enumerate(values):