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:
dependencies {
    implementation("com.neosapience:typecast-kotlin:1.0.1")
}
Make sure you have version 1.0.1 or higher installed. If you have an older version, update your dependency version.

Quick Start

import com.neosapience.TypecastClient
import com.neosapience.models.*
import java.io.File

fun main() {
    // Initialize client
    val client = TypecastClient.create("YOUR_API_KEY")

    // Convert text to speech
    val request = TTSRequest.builder()
        .voiceId("tc_672c5f5ce59fac2a48faeaee")
        .text("Hello there! I'm your friendly text-to-speech agent.")
        .model(TTSModel.SSFM_V30)
        .build()

    val response = client.textToSpeech(request)

    // Save audio file
    File("output.${response.format}").writeBytes(response.audioData)

    println("Audio saved! Duration: ${response.duration}s, Format: ${response.format}")

    // Clean up
    client.close()
}

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
  • Idiomatic Kotlin: Builder pattern with Kotlin-friendly syntax using data classes
  • Comprehensive Error Handling: Specific exception classes for each error type

Configuration

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 directly
val client = TypecastClient.create("your-api-key-here")

// Or use builder for custom configuration
val client = TypecastClient.builder()
    .apiKey("your-api-key-here")
    .baseUrl("https://custom-api.example.com")
    .build()

Environment File

Create a .env file in your project root:
TYPECAST_API_KEY=your-api-key-here

Advanced Usage

Emotion Control (ssfm-v30)

ssfm-v30 offers two emotion control modes: Preset and Smart.
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)

Audio Customization

Control loudness, pitch, tempo, and output format:
val request = TTSRequest.builder()
    .voiceId("tc_672c5f5ce59fac2a48faeaee")
    .text("Customized audio output!")
    .model(TTSModel.SSFM_V30)
    .output(Output.builder()
        .targetLufs(-14.0)                 // Range: -70 to 0 (LUFS)
        .audioPitch(2)                  // Range: -12 to +12 semitones
        .audioTempo(1.2)                // Range: 0.5x to 2.0x
        .audioFormat(AudioFormat.MP3)   // Options: WAV, MP3
        .build())
    .seed(42)  // For reproducible results
    .build()

val response = client.textToSpeech(request)

File("output.${response.format}").writeBytes(response.audioData)
println("Duration: ${response.duration}s, Format: ${response.format}")

Voice Discovery (V2 API)

List and filter available voices with enhanced metadata:
// Get all voices
val voices = client.getVoicesV2()

// Filter by criteria
val filter = VoicesV2Filter.builder()
    .model(TTSModel.SSFM_V30)
    .gender(GenderEnum.FEMALE)
    .age(AgeEnum.YOUNG_ADULT)
    .build()

val filtered = client.getVoicesV2(filter)

// Display voice info
voices.forEach { voice ->
    println("ID: ${voice.voiceId}, Name: ${voice.voiceName}")
    println("Gender: ${voice.gender}, Age: ${voice.age}")
    
    voice.models.forEach { model ->
        println("Model: ${model.version}, Emotions: ${model.emotions}")
    }
    
    voice.useCases?.let { useCases ->
        println("Use cases: ${useCases.joinToString(", ")}")
    }
}

Multilingual Content

The SDK supports 37 languages with automatic language detection:
// Auto-detect language (recommended)
val request = TTSRequest.builder()
    .voiceId("tc_672c5f5ce59fac2a48faeaee")
    .text("こんにちは。お元気ですか。")
    .model(TTSModel.SSFM_V30)
    .build()

val response = client.textToSpeech(request)

// Or specify language explicitly
val koreanRequest = TTSRequest.builder()
    .voiceId("tc_672c5f5ce59fac2a48faeaee")
    .text("안녕하세요. 반갑습니다.")
    .model(TTSModel.SSFM_V30)
    .language(LanguageCode.KOR)  // ISO 639-3 language code
    .build()

val koreanResponse = client.textToSpeech(koreanRequest)

File("output.${response.format}").writeBytes(response.audioData)

Supported Languages

The SDK supports 37 languages with automatic language detection:
CodeLanguageCodeLanguageCodeLanguage
ENGEnglishJPNJapaneseUKRUkrainian
KORKoreanELLGreekINDIndonesian
SPASpanishTAMTamilDANDanish
DEUGermanTGLTagalogSWESwedish
FRAFrenchFINFinnishMSAMalay
ITAItalianZHOChineseCESCzech
POLPolishSLKSlovakPORPortuguese
NLDDutchARAArabicBULBulgarian
RUSRussianHRVCroatianRONRomanian
BENBengaliHINHindiHUNHungarian
NANHokkienNORNorwegianPANPunjabi
THAThaiTURTurkishVIEVietnamese
YUECantonese
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:
import com.neosapience.TypecastClient
import com.neosapience.exceptions.*

try {
    val response = client.textToSpeech(request)
} catch (e: UnauthorizedException) {
    // 401: Invalid API key
    println("Invalid API key: ${e.message}")
} catch (e: PaymentRequiredException) {
    // 402: Insufficient credits
    println("Insufficient credits: ${e.message}")
} catch (e: ForbiddenException) {
    // 403: Access denied
    println("Access denied: ${e.message}")
} catch (e: NotFoundException) {
    // 404: Resource not found
    println("Voice not found: ${e.message}")
} catch (e: UnprocessableEntityException) {
    // 422: Validation error
    println("Validation error: ${e.message}")
} catch (e: RateLimitException) {
    // 429: Rate limit exceeded
    println("Rate limit exceeded - please retry later")
} catch (e: InternalServerException) {
    // 500: Server error
    println("Server error: ${e.message}")
} catch (e: TypecastException) {
    // Generic error
    println("API error (${e.statusCode}): ${e.message}")
}

Exception Hierarchy

ExceptionStatus CodeDescription
BadRequestException400Invalid request parameters
UnauthorizedException401Invalid or missing API key
PaymentRequiredException402Insufficient credits
ForbiddenException403Access denied
NotFoundException404Resource not found
UnprocessableEntityException422Validation error
RateLimitException429Rate limit exceeded
InternalServerException500Server error
TypecastException*Base exception class

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:
dependencies {
    implementation("com.neosapience:typecast-kotlin:1.0.1")
}
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:
dependencies {
    implementation("com.neosapience:typecast-kotlin:1.0.1")
}
2

Add Internet Permission

Add to your AndroidManifest.xml:
<uses-permission android:name="android.permission.INTERNET" />
3

Use in Background Thread

Make API calls from a coroutine or background thread:
lifecycleScope.launch(Dispatchers.IO) {
    val client = TypecastClient.create("YOUR_API_KEY")
    val response = client.textToSpeech(request)
    // Handle response
}

API Reference

TypecastClient Methods

MethodDescription
textToSpeech(TTSRequest)Convert text to speech audio
getVoicesV2()Get all available voices
getVoicesV2(VoicesV2Filter)Get filtered voices
getVoiceV2(voiceId: String)Get a specific voice by ID
close()Release resources

TTSRequest Fields

FieldTypeRequiredDescription
voiceIdStringVoice ID (format: tc_* or uc_*)
textStringText to synthesize (max 2000 chars)
modelTTSModelTTS model (SSFM_V21 or SSFM_V30)
languageLanguageCodeISO 639-3 code (auto-detected if omitted)
promptPrompt / PresetPrompt / SmartPromptEmotion settings
outputOutputAudio output settings
seedIntRandom seed for reproducibility

TTSResponse Fields

FieldTypeDescription
audioDataByteArrayGenerated audio data
durationDoubleAudio duration in seconds
formatStringAudio format (wav or mp3)