Access the Typecast API with our official Kotlin SDK.
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.
Latest registered version: 1.2.7 on Maven Central. Make sure you have version 1.2.7 or higher installed. If you have an older version, update your dependency version.
Use recommendVoices when you know the desired style but not the exact voice_id.
val voices = client.recommendVoices( "warm female voice for a product tutorial", count = 3,)voices.forEach { voice -> println("${voice.voiceId} ${voice.voiceName} ${voice.score}")}
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.
Set your API key via environment variable, .env file, or builder:
// Using environment variable// export TYPECAST_API_KEY="your-api-key-here"val client = TypecastClient.create()// Or pass directlyval client = TypecastClient.create("your-api-key-here")// Or use builder for custom configurationval client = TypecastClient.builder() .apiKey("your-api-key-here") .baseUrl("https://custom-api.example.com") .build()
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
val client = TypecastClient.builder() .baseUrl("https://your-proxy.example.com") .build()
ssfm-v30 offers two emotion control modes: Preset and Smart.
Smart Mode
Preset Mode
Let the AI infer emotion from context:
val request = TTSRequest.builder() .voiceId("tc_672c5f5ce59fac2a48faeaee") .text("Everything is going to be okay.") .model(TTSModel.SSFM_V30) .prompt(SmartPrompt.builder() .previousText("I just got the best news!") // Optional context .nextText("I can't wait to celebrate!") // Optional context .build()) .build()val response = client.textToSpeech(request)
Explicitly set emotion with preset values:
val request = TTSRequest.builder() .voiceId("tc_672c5f5ce59fac2a48faeaee") .text("I am so excited to show you these features!") .model(TTSModel.SSFM_V30) .prompt(PresetPrompt.builder() .emotionPreset(EmotionPreset.HAPPY) // normal, happy, sad, angry, whisper, toneup, tonedown .emotionIntensity(1.5) // Range: 0.0 to 2.0 .build()) .build()val response = client.textToSpeech(request)
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.
client.generateToFile( "output.mp3", GenerateToFileRequest( text = "Hello from Typecast.", voiceId = "tc_672c5f5ce59fac2a48faeaee", // Find voice IDs at https://typecast.ai/developers/api/voices ))
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.
val audio = client.composeSpeech() .defaults(ComposerSettings(voiceId = "tc_672c5f5ce59fac2a48faeaee", model = TTSModel.SSFM_V30)) .say("Hello<|5s|>Nice to meet you<|1s|>Today<|2s|>how does the weather feel?") .generate()
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.
val client = TypecastClient.create("YOUR_API_KEY")val audio = client.composeSpeech() .defaults(ComposerSettings(voiceId = "tc_672c5f5ce59fac2a48faeaee", model = TTSModel.SSFM_V30)) .say("Hello there") .pause(5.0) .say("Nice to meet you", ComposerSettings(voiceId = "tc_60e5426de8b95f1d3000d7b5", output = Output(audioPitch = 2))) .say("Today") .pause(2.0) .say("How does the weather feel?") .generate()File("conversation.wav").writeBytes(audio.audioData)
Stream audio chunks in real-time for low-latency playback:
import javax.sound.sampled.*// Set up audio playback: 32000 Hz, 16-bit, mono, little-endianval format = AudioFormat(32000f, 16, 1, true, false)val line = AudioSystem.getSourceDataLine(format).apply { open(format, 8192) start()}val stream = client.textToSpeechStream(request)val buf = ByteArray(4096)var first = truewhile (true) { val bytesRead = stream.read(buf) if (bytesRead == -1) break var offset = 0 var len = bytesRead if (first) { offset = 44 // Skip 44-byte WAV header len -= 44 first = false } line.write(buf, offset, len)}line.drain()line.close()stream.close()client.close()
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.
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.
Pass .granularity(Granularity.WORD) (default) or .granularity(Granularity.CHAR) to control the alignment unit.
val request = TTSRequestWithTimestamps.builder() .voiceId("tc_60e5426de8b95f1d3000d7b5") .text("Hello. How are you?") .model(TTSModel.SSFM_V30) .granularity(Granularity.CHAR) // required for Japanese / Chinese .build()
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.