openai-kotlin
openai-kotlin copied to clipboard
How can I get a List of all voices for TTS?
Description
Is there a simple way to get a List of all supported Voices?
Steps to Reproduce
My approach., but no success
val voiceFields = Voice::class.java.declaredFields // size 8
val sortedVoiceFields = voiceFields.filter { it.type == Voice::class.java } // size 0
I would be very grateful for a solution.
Environment
Android Studio
I have found a solution to read out all voices from the voice class with their names as a map:
Voice class
@Serializable
@JvmInline
public value class Voice(public val value: String) {
public companion object {
public val Alloy: Voice = Voice("alloy")
public val Echo: Voice = Voice("echo")
public val Fable: Voice = Voice("fable")
public val Onyx: Voice = Voice("onyx")
public val Nova: Voice = Voice("nova")
public val Shimmer: Voice = Voice("shimmer")
}
}
Function:
fun getAvailableVoices(): Map<String,Voice> {
val voices = mutableMapOf<String, Voice>()
val properties = Voice.Companion::class.memberProperties
properties.forEach {
if (it.returnType.classifier == Voice::class) {
val name = it.name
val voice = it.call(Voice::class.companionObjectInstance) as Voice
voices[name] = voice
}
}
return voices
}
Gradle Dependeny:
dependencies {
...
implementation(kotlin("reflect"))
}
There is no list because the list of voices can change, necessitating updates to the library each time a new voice is added. I suggest manually creating and maintaining the list. This approach applies to other APIs, such as the models list, etc.