DEV Community

Ishaan Verma for Elsewhere

Posted on

kokoro-js cut my text short without an error

Hi, I'm Ishaan and I built Elsewhere, an ebook reader that runs in the browser and reads books aloud. In September its voice was Kokoro, running in the browser through kokoro-js. Long passages stopped mid-thought, no error appeared, and the highlight ran ahead of the audio. It took me three tries to find the real limit, a count of 510 phoneme tokens, past which kokoro-js cuts the input.

First answer: a ceiling of 28.6 seconds

I gave the model longer and longer text and measured how much audio came back:

300 characters  -> 22.0 s
600 characters  -> 28.6 s
900 characters  -> 28.6 s
1200 characters -> 28.6 s
Enter fullscreen mode Exit fullscreen mode

Past a point, twice the words produced the same amount of audio. My length guard allowed 4,500 characters, so it never fired. I estimated the loss at about 2% of one book's text, split passages into segments of at most 800 characters, and added a check that would fail loudly if audio came back at the ceiling. The 800 came from the book's 90th percentile speaking rate on long text, 27.68 ms per character, which put a full segment at about 22 seconds.

Second answer: my speaking rate was wrong

The same day, passages were still being cut off. I measured again on stored audio, using a stretch with paragraph silence I had spliced in on both sides, so its edges were exact. 463 characters took 27.28 seconds. That is 58.9 ms per character, more than twice my figure. An 800-character segment needed about 47 seconds and was cut off at about 28.

For one passage the arithmetic predicted a stop at character 945, and the audio stopped at about 936. By that rate, 56 of the 61 passages I had generated had at least one segment over the ceiling.

The check I had added missed this. Its threshold was 98% of the ceiling, and a truncated segment came back at 27,470 ms, which is 96%.

The new version sized segments from the rate of the audio it had produced so far. A segment that came back at 90% of the ceiling or more was split in two and spoken again, and an error was raised if halving did not help. Audio made before the change stayed truncated and had to be generated again.

Third answer: the limit was counted in tokens

Reading the bundled library settled it:

// kokoro-js 1.2.1, trimmed, with readable names
const { input_ids } = this.tokenizer(phonemes, { truncation: true })
return this.generate_from_ids(input_ids, { voice, speed })
Enter fullscreen mode Exit fullscreen mode

The model takes 510 phoneme tokens. With truncation on, everything past that is thrown away and the shorter audio comes back as if nothing happened. stream() splits text into sentences first and then makes the same call on each, so a single sentence over the limit is cut there too.

Seconds and characters were stand-ins for that count, which is why my 800 characters and my 98% check both failed. Numerals, abbreviations and initialisms can turn into more phonemes per character than ordinary prose.

I tried to get an exact count from stream(), which hands back the phonemes it made, by tokenizing them again with truncation off. Generation hung on the first passage, and I found out by running it. Given a plain string, stream() never closes its sentence splitter, so the splitter holds the last sentence back and the loop waits forever. My first segment was a heading, so nothing came out at all. I went back to generate() the same day.

That left my duration check to catch cuts, and it has a gap. A faster voice reaches 510 tokens in less time, so a cut segment can come back under 90% of the ceiling and pass.

Tests that passed with the fix removed

My first test for re-splitting a segment passed against a build with that code taken out, because the smaller character budget stayed under the ceiling by itself. A test for the measured rate passed with the rate frozen, because the splitting was producing the smaller calls. I rewrote them so that removing the mechanism each one names makes it fail.

Why I moved off Kokoro

I moved to other voices in October, after finding that Kokoro on the CPU made a reader wait about 40 seconds for the first audio. Books already narrated with it still play.

Elsewhere is my project. It opens DRM-free books, so a book bought from the Kindle store will not open. The voice is an AI voice rather than a human narrator. You can listen to a sample without an account at https://listenelsewhere.com/try

Has a model or a library ever cut your input without telling you? Let me know in the comments.

Top comments (0)