Skip to main content
The official Kotlin library for the Typecast API. Convert text to lifelike speech using AI-powered voices. Compatible with Kotlin 1.9+ and JDK 17 or later. Works with Gradle (Kotlin DSL or Groovy) and Maven.

Maven Central

Typecast Kotlin SDK

Source Code

Typecast Kotlin SDK Source Code

Installation

Add the following dependency to your build.gradle.kts:
Latest registered version: 1.2.9 on Maven Central. Make sure you have version 1.2.9 or higher installed. If you have an older version, update your dependency version.

Quick Start

Features

The Typecast Kotlin SDK provides powerful features for text-to-speech conversion:
  • Multiple Voice Models: Support for ssfm-v30 (latest) and ssfm-v21 AI voice models
  • Multi-language Support: 37 languages including English, Korean, Spanish, Japanese, Chinese, and more
  • Emotion Control: Preset emotions (normal, happy, sad, angry, whisper, toneup, tonedown) or smart context-aware inference
  • Audio Customization: Control loudness (LUFS -70 to 0), pitch (-12 to +12 semitones), tempo (0.5x to 2.0x), and format (WAV/MP3)
  • Voice Discovery: V2 Voices API with filtering by model, gender, age, and use cases
  • Instant Voice Cloning: Upload a WAV/MP3 sample and create a custom voice ID
  • Timestamp TTS: Word- and character-level alignment data for subtitles, karaoke, and lip-sync
  • Idiomatic Kotlin: Builder pattern with Kotlin-friendly syntax using data classes
  • Comprehensive Error Handling: Specific exception classes for each error type
  • Streaming: Real-time chunked audio delivery for low-latency playback

Voice Recommendations

Use recommendVoices when you know the desired style but not the exact voice_id.
Recommendation results contain only voiceId, voiceName, and score. Use getVoiceV2 or getVoicesV2 when you need detailed metadata such as supported models, emotions, gender, age, or use cases.

Configuration

Set your API key via environment variable, .env file, or builder:
When requests go through your own proxy, set baseUrl to the proxy endpoint and omit apiKey. The SDK will not send the X-API-KEY header for empty or missing keys. Requests to the default Typecast host still require an API key.
Proxy without API key

Environment File

Create a .env file in your project root:

Advanced Usage

Emotion Control (ssfm-v30)

ssfm-v30 offers two emotion control modes: Preset and Smart.
Let the AI infer emotion from context:

Audio Customization

Control loudness, pitch, tempo, and output format:

Generate audio to a file

Use generateToFile when you want the SDK to synthesize speech and write the audio bytes directly to a local file. The model defaults to ssfm-v30, and .mp3 / .wav extensions infer the output format when output.audioFormat is not set. Browse available voice IDs on the Voices page.

Text pauses

Use text pause markup when you only need silent gaps inside one composed text segment. Put <|5s|>, <|1s|>, <|0.3s|>, or <|0.34413s|> directly in the text. The value is interpreted as seconds and must end with s. This keeps the pause expression visible in plain text without adding separate pause calls.

Multi-speaker composition

Use the composer chaining API when one output file needs different voices or per-segment options such as pitch, tempo, prompt, or seed. The composer generates each segment as WAV, trims leading/trailing silent PCM samples, and concatenates the result. If you need MP3, generate WAV first and convert it in your app or server pipeline.

Voice Discovery (V2 API)

List and filter available voices with enhanced metadata:

Multilingual Content

The SDK supports 37 languages with automatic language detection:

Streaming

Stream audio chunks in real-time for low-latency playback:
WAV streaming format: 32000 Hz, 16-bit, mono PCM. The first chunk includes a 44-byte WAV header (size = 0xFFFFFFFF); subsequent chunks are raw PCM only. For MP3: 320 kbps, 44100 Hz, each chunk is independently decodable.

Timestamp TTS

textToSpeechWithTimestamps() wraps POST /v1/text-to-speech/with-timestamps and returns the audio together with per-word and per-character alignment data — useful for karaoke highlights, subtitle generation, and lip-sync applications.

Basic Usage

Granularity

Pass .granularity(Granularity.WORD) (default) or .granularity(Granularity.CHAR) to control the alignment unit.

Subtitle Export

Japanese / Chinese: Word-level segmentation is not meaningful for languages without whitespace delimiters (jpn, zho). Use Granularity.CHAR for these languages to get character-level alignment.

Instant Voice Cloning

Clone a custom voice from a short audio sample, then pass the returned uc_ voice ID directly to TTS.
Voice cloning audio must be 25 MB or smaller, the audio duration must be 5-150 seconds, and the custom voice name must be 1-30 characters.

Supported Languages

The SDK supports 37 languages with automatic language detection:
If not specified, the language will be automatically detected from the input text.

Error Handling

The SDK provides specific exception classes for handling API errors:

Exception Hierarchy

IntelliJ IDEA Setup

1

Create New Project

  1. Open IntelliJ IDEA
  2. Go to FileNewProject...
  3. Select “Kotlin” and “Gradle (Kotlin)”
  4. Set JDK to 17 or higher
2

Add Dependency

Add to your build.gradle.kts:
3

Sync Project

Click the Gradle sync button or right-click build.gradle.ktsReload Gradle Project

Android Setup

1

Add Dependency

Add to your app’s build.gradle.kts:
2

Add Internet Permission

Add to your AndroidManifest.xml:
3

Use in Background Thread

Make API calls from a coroutine or background thread:

API Reference

TypecastClient Methods

TTSRequest Fields

TTSResponse Fields