Skip to content

@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.

Defined in: src/chunker.ts:45

Options for text chunking

optional characterLimit?: number;

Defined in: src/chunker.ts:47

Maximum characters per chunk (default: 100, clamped: 50-300)

optional preserveSentences?: boolean;

Defined in: src/chunker.ts:57

Whether to prioritize sentence boundaries over character limit


Defined in: src/chunker.ts:31

Represents a single chunk of text

index: number;

Defined in: src/chunker.ts:35

Zero-based index of this chunk

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

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.


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


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


const MAX_CHARACTER_LIMIT: 300 = 300;

Defined in: src/config.ts:14

Maximum allowed character limit


const MIN_CHARACTER_LIMIT: 50 = 50;

Defined in: src/config.ts:19

Minimum character limit


const WORDS_PER_MINUTE: 130 = 130;

Defined in: src/config.ts:35

Estimated words per minute for duration calculations

function chunkText(text, options?): TextChunk[];

Defined in: src/chunker.ts:351

Splits text into chunks for speech synthesis

ParameterTypeDescription
textstringThe text to chunk
optionsChunkerOptionsChunking options

TextChunk[]

Array of text chunks with metadata

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 }
// ]

function createTextChunker(defaultOptions?): (text, options?) => TextChunk[];

Defined in: src/chunker.ts:418

Creates a text chunker instance with preset options

ParameterTypeDescription
defaultOptionsChunkerOptionsDefault options for all chunk operations

A chunker function with the preset options

(text, options?) => TextChunk[]

const chunker = createTextChunker({ characterLimit: 150 });
const chunks = chunker('Long text here...');

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.

ParameterType
strstring

string


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.

ParameterTypeDefault valueDescription
textstringundefinedThe text to estimate duration for
multipliernumber1Optional multiplier for the duration (default: 1)
options?{ baseTimeout?: number; }undefinedOptional 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?numberundefined-

number

Estimated duration in milliseconds

// Estimate duration for a sentence
const 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 timeout
const fallbackDuration = getEstimatedTimeLength("Hi", 1, { baseTimeout: 1300 });

function getEstimatedTimeLengthWithRate(text, rate?): number;

Defined in: src/duration.ts:107

Estimate speech duration with rate adjustment

ParameterTypeDefault valueDescription
textstringundefinedThe text to estimate duration for
ratenumber1Speech rate (0.1 to 10, where 1 is normal)

number

Estimated duration in milliseconds


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).

ParameterType
textstring

boolean