openai-kotlin icon indicating copy to clipboard operation
openai-kotlin copied to clipboard

How can I get a List of all voices for TTS?

Open wolfscowl opened this issue 10 months ago • 2 comments

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

wolfscowl avatar Apr 25 '24 00:04 wolfscowl

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"))
}

wolfscowl avatar Apr 25 '24 21:04 wolfscowl

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.

aallam avatar Apr 29 '24 22:04 aallam