Skip to content

Commit 8e9ed8d

Browse files
committed
Align the silence scale range between Validate() and ScaleSilence
After #3745, ScaleSilence accepts [0.01, 2] but OfflineTtsConfig::Validate() still only rejects silence_scale < 0.001. Values in [0.001, 0.01) and above 2 therefore pass validation and are then silently skipped at run time, so the user asks for scaling and gets unscaled audio. Use one pair of constants for both, document the range in the --tts-silence-scale help text, and raise the upper bound from 2 to 10. Rejecting in Validate() also makes bad input fail before synthesis rather than after it. The check in ScaleSilence stays, because callers of the Generate API pass GeneratedAudioConfig::silence_scale directly and bypass Validate().
1 parent 986a386 commit 8e9ed8d

1 file changed

Lines changed: 20 additions & 11 deletions

File tree

sherpa-onnx/csrc/offline-tts.cc

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,26 @@ struct SilenceInterval {
3131
int32_t end;
3232
};
3333

34+
// Supported range for --tts-silence-scale. The lower bound keeps a pause from
35+
// collapsing to nothing; the upper bound is a sanity limit, far below the
36+
// value at which interval_length * scale would overflow an int32_t.
37+
static constexpr float kMinSilenceScale = 0.01f;
38+
static constexpr float kMaxSilenceScale = 10.0f;
39+
3440
GeneratedAudio GeneratedAudio::ScaleSilence(float scale) const {
3541
if (scale == 1) {
3642
return *this;
3743
}
3844

39-
// scale is used to shorten long pauses, so it is normally within (0, 1).
40-
// Values outside this range are rejected: n below is computed as
41-
// interval_length * scale and converted to an int32_t, and NaN, infinity or
42-
// a very large scale make that conversion undefined. Note that any
43-
// comparison with NaN is false, so NaN is rejected here as well.
44-
if (!(scale >= 0.01f && scale <= 2.0f)) {
45-
SHERPA_ONNX_LOGE("Silence scale %f is not in [0.01, 2]. Skip scaling.",
46-
scale);
45+
// scale is normally within (0, 1), since it is used to shorten long pauses,
46+
// but scaling a pause up is also useful. Values outside the supported range
47+
// are rejected: n below is computed as interval_length * scale and converted
48+
// to an int32_t, and NaN, infinity or a very large scale make that
49+
// conversion undefined. Note that any comparison with NaN is false, so NaN
50+
// is rejected here as well.
51+
if (!(scale >= kMinSilenceScale && scale <= kMaxSilenceScale)) {
52+
SHERPA_ONNX_LOGE("Silence scale %f is not in [%.2f, %.2f]. Skip scaling.",
53+
scale, kMinSilenceScale, kMaxSilenceScale);
4754
return *this;
4855
}
4956
// if the interval is larger than 0.2 second, then we assume it is a pause
@@ -195,7 +202,7 @@ void OfflineTtsConfig::Register(ParseOptions *po) {
195202

196203
po->Register("tts-silence-scale", &silence_scale,
197204
"Duration of the pause is scaled by this number. So a smaller "
198-
"value leads to a shorter pause.");
205+
"value leads to a shorter pause. Must be in [0.01, 10].");
199206
}
200207

201208
bool OfflineTtsConfig::Validate() const {
@@ -221,8 +228,10 @@ bool OfflineTtsConfig::Validate() const {
221228
}
222229
}
223230

224-
if (silence_scale < 0.001) {
225-
SHERPA_ONNX_LOGE("--tts-silence-scale '%.3f' is too small", silence_scale);
231+
if (!(silence_scale >= kMinSilenceScale &&
232+
silence_scale <= kMaxSilenceScale)) {
233+
SHERPA_ONNX_LOGE("--tts-silence-scale '%.3f' is not in [%.2f, %.2f]",
234+
silence_scale, kMinSilenceScale, kMaxSilenceScale);
226235
return false;
227236
}
228237

0 commit comments

Comments
 (0)