@responsivevoice/text
Text processing utilities for text-to-speech: intelligent chunking with
CJK support, speech-duration estimation, and fast hashing for cache keys.
Consumed by @responsivevoice/core, @responsivevoice/api-client, and
services/tts-api.
Interfaces
Section titled “Interfaces”ChunkerOptions
Section titled “ChunkerOptions”Defined in: src/chunker.ts:45
Options for text chunking
Properties
Section titled “Properties”characterLimit?
Section titled “characterLimit?”optional characterLimit?: number;Defined in: src/chunker.ts:47
Maximum characters per chunk (default: 100, clamped: 50-300)
preserveSentences?
Section titled “preserveSentences?”optional preserveSentences?: boolean;Defined in: src/chunker.ts:57
Whether to prioritize sentence boundaries over character limit
TextChunk
Section titled “TextChunk”Defined in: src/chunker.ts:31
Represents a single chunk of text
Properties
Section titled “Properties”index: number;Defined in: src/chunker.ts:35
Zero-based index of this chunk
isLast
Section titled “isLast”isLast: boolean;Defined in: src/chunker.ts:39
Whether this is the last chunk
text: string;Defined in: src/chunker.ts:33
The text content of the chunk
total: number;Defined in: src/chunker.ts:37
Total number of chunks
Variables
Section titled “Variables”BASE_TIMEOUT_FALLBACK
Section titled “BASE_TIMEOUT_FALLBACK”const BASE_TIMEOUT_FALLBACK: 1300 = 1300;Defined in: src/config.ts:46
Base timeout in ms for fallback (HTTP audio) short-text estimation. Higher than native to account for network latency.
BASE_TIMEOUT_NATIVE
Section titled “BASE_TIMEOUT_NATIVE”const BASE_TIMEOUT_NATIVE: 700 = 700;Defined in: src/config.ts:40
Base timeout in ms for native TTS (Web Speech API) short-text estimation
DEFAULT_CHARACTER_LIMIT
Section titled “DEFAULT_CHARACTER_LIMIT”const DEFAULT_CHARACTER_LIMIT: 100 = 100;Defined in: src/config.ts:9
Default character limit for text chunking Text longer than this will be split into multiple utterances
MAX_CHARACTER_LIMIT
Section titled “MAX_CHARACTER_LIMIT”const MAX_CHARACTER_LIMIT: 300 = 300;Defined in: src/config.ts:14
Maximum allowed character limit
MIN_CHARACTER_LIMIT
Section titled “MIN_CHARACTER_LIMIT”const MIN_CHARACTER_LIMIT: 50 = 50;Defined in: src/config.ts:19
Minimum character limit
WORDS_PER_MINUTE
Section titled “WORDS_PER_MINUTE”const WORDS_PER_MINUTE: 130 = 130;Defined in: src/config.ts:35
Estimated words per minute for duration calculations
Functions
Section titled “Functions”chunkText()
Section titled “chunkText()”function chunkText(text, options?): TextChunk[];Defined in: src/chunker.ts:351
Splits text into chunks for speech synthesis
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
text | string | The text to chunk |
options | ChunkerOptions | Chunking options |
Returns
Section titled “Returns”Array of text chunks with metadata
Example
Section titled “Example”const chunks = chunkText('Hello world. How are you today?', { characterLimit: 20 });// Returns:// [// { text: 'Hello world.', index: 0, total: 2, isLast: false },// { text: 'How are you today?', index: 1, total: 2, isLast: true }// ]createTextChunker()
Section titled “createTextChunker()”function createTextChunker(defaultOptions?): (text, options?) => TextChunk[];Defined in: src/chunker.ts:418
Creates a text chunker instance with preset options
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
defaultOptions | ChunkerOptions | Default options for all chunk operations |
Returns
Section titled “Returns”A chunker function with the preset options
(text, options?) => TextChunk[]
Example
Section titled “Example”const chunker = createTextChunker({ characterLimit: 150 });const chunks = chunker('Long text here...');djb2Hash()
Section titled “djb2Hash()”function djb2Hash(str): string;Defined in: src/hash.ts:5
DJB2 hash function — fast, non-cryptographic string hash. Used for scoping storage keys to API keys and for browser voice fingerprinting.
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
str | string |
Returns
Section titled “Returns”string
getEstimatedTimeLength()
Section titled “getEstimatedTimeLength()”function getEstimatedTimeLength( text, multiplier?, options?): number;Defined in: src/duration.ts:55
Estimate the speech duration for a given text
Uses different formulas based on text length:
- Short text (fewer than 5 words): character-based formula with a configurable base timeout.
- Normal text: words-per-minute calculation (130 WPM).
Text is preprocessed (currency/number expansion) before word counting so estimates match what TTS engines actually speak.
Parameters
Section titled “Parameters”| Parameter | Type | Default value | Description |
|---|---|---|---|
text | string | undefined | The text to estimate duration for |
multiplier | number | 1 | Optional multiplier for the duration (default: 1) |
options? | { baseTimeout?: number; } | undefined | Optional configuration. options.baseTimeout is the base timeout in ms for short text (default: 700 for native TTS, use 1300 for fallback/HTTP audio). |
options.baseTimeout? | number | undefined | - |
Returns
Section titled “Returns”number
Estimated duration in milliseconds
Example
Section titled “Example”// Estimate duration for a sentenceconst duration = getEstimatedTimeLength("Hello, how are you today?");
// With a multiplier (e.g., for slower speech rate)const slowerDuration = getEstimatedTimeLength("Hello world", 1.5);
// For fallback engine (HTTP audio) - uses higher base timeoutconst fallbackDuration = getEstimatedTimeLength("Hi", 1, { baseTimeout: 1300 });getEstimatedTimeLengthWithRate()
Section titled “getEstimatedTimeLengthWithRate()”function getEstimatedTimeLengthWithRate(text, rate?): number;Defined in: src/duration.ts:107
Estimate speech duration with rate adjustment
Parameters
Section titled “Parameters”| Parameter | Type | Default value | Description |
|---|---|---|---|
text | string | undefined | The text to estimate duration for |
rate | number | 1 | Speech rate (0.1 to 10, where 1 is normal) |
Returns
Section titled “Returns”number
Estimated duration in milliseconds
hasCJKContent()
Section titled “hasCJKContent()”function hasCJKContent(text): boolean;Defined in: src/chunker.ts:137
Checks if text contains any CJK ideographic characters. Exported for use in voice-aware character limit computation (e.g. reducing the limit for Google remote voices with CJK content).
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
text | string |
Returns
Section titled “Returns”boolean