Skip to content

Commit 4b0cf7f

Browse files
authored
Fix ScaleSilence copying past the pause interval when scale > 1 (#3744)
GeneratedAudio::ScaleSilence() lengthens each detected pause by copying n = (interval.end - interval.start) * scale samples starting at interval.start. When scale > 1 that is n > interval.end - interval.start, so the copy runs past the end of the pause and into the audio that follows it. Two consequences: - The beginning of the next word is duplicated, which is audible as a stutter after every pause. - On the last interval, interval.end == num_samples, so samples.begin() + interval.start + n is past samples.end() and the insert reads out of bounds. Copy the pause once and pad with zeros to the requested length instead. For scale <= 1 the behaviour is unchanged, and scale == 1 still returns early before reaching this code.
1 parent 97293f0 commit 4b0cf7f

1 file changed

Lines changed: 15 additions & 4 deletions

File tree

sherpa-onnx/csrc/offline-tts.cc

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,21 @@ GeneratedAudio GeneratedAudio::ScaleSilence(float scale) const {
7979
ans.samples.insert(ans.samples.end(), samples.begin() + i,
8080
samples.begin() + interval.start);
8181
i = interval.end;
82-
int32_t n = static_cast<int32_t>((interval.end - interval.start) * scale);
83-
84-
ans.samples.insert(ans.samples.end(), samples.begin() + interval.start,
85-
samples.begin() + interval.start + n);
82+
int32_t len = interval.end - interval.start;
83+
int32_t n = static_cast<int32_t>(len * scale);
84+
85+
if (n <= len) {
86+
ans.samples.insert(ans.samples.end(), samples.begin() + interval.start,
87+
samples.begin() + interval.start + n);
88+
} else {
89+
// scale > 1: copying n samples from interval.start would run past the
90+
// end of the pause into the following speech (audible as a repeated
91+
// word onset) and can read out of bounds on the final interval.
92+
// Copy the pause once, then extend it with silence.
93+
ans.samples.insert(ans.samples.end(), samples.begin() + interval.start,
94+
samples.begin() + interval.end);
95+
ans.samples.insert(ans.samples.end(), n - len, 0.0f);
96+
}
8697
}
8798

8899
if (i < num_samples) {

0 commit comments

Comments
 (0)