From 802cf940a979191c13eac37697a9cbf50d7a6703 Mon Sep 17 00:00:00 2001 From: rdkartono Date: Tue, 25 Nov 2025 11:52:05 +0700 Subject: [PATCH] commit 25/11/2025 --- .idea/libraries/google_cloud_texttospeech.xml | 59 ++++++ AAS_NewGen.iml | 1 + src/google/GoogleTTS.kt | 49 ++++- src/google/GoogleTTSLanguage.kt | 10 + src/google/GoogleTTSSSMLBreakStrength.kt | 11 ++ src/google/GoogleTTSSSMLStrength.kt | 9 + src/google/GoogleTTSVoiceType.kt | 9 + src/google/SSML.kt | 179 ++++++++++++++++++ 8 files changed, 317 insertions(+), 10 deletions(-) create mode 100644 .idea/libraries/google_cloud_texttospeech.xml create mode 100644 src/google/GoogleTTSLanguage.kt create mode 100644 src/google/GoogleTTSSSMLBreakStrength.kt create mode 100644 src/google/GoogleTTSSSMLStrength.kt create mode 100644 src/google/GoogleTTSVoiceType.kt create mode 100644 src/google/SSML.kt diff --git a/.idea/libraries/google_cloud_texttospeech.xml b/.idea/libraries/google_cloud_texttospeech.xml new file mode 100644 index 0000000..1f8d528 --- /dev/null +++ b/.idea/libraries/google_cloud_texttospeech.xml @@ -0,0 +1,59 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/AAS_NewGen.iml b/AAS_NewGen.iml index 5b2e4d8..c6cc4fb 100644 --- a/AAS_NewGen.iml +++ b/AAS_NewGen.iml @@ -36,5 +36,6 @@ + \ No newline at end of file diff --git a/src/google/GoogleTTS.kt b/src/google/GoogleTTS.kt index c69a688..18c3b45 100644 --- a/src/google/GoogleTTS.kt +++ b/src/google/GoogleTTS.kt @@ -1,14 +1,43 @@ package google -class GoogleTTS { +import com.google.cloud.texttospeech.v1.AudioConfig +import com.google.cloud.texttospeech.v1.AudioEncoding +import com.google.cloud.texttospeech.v1.SynthesisInput +import com.google.cloud.texttospeech.v1.TextToSpeechClient +import com.google.cloud.texttospeech.v1.VoiceSelectionParams +import org.tinylog.Logger +import java.nio.file.Files +import kotlin.io.path.Path - // project : Gtc-Cloud-AAS - // project ID : gtc-cloud-aas - // project number : 247742838587 - // location : indonesia-southeast1 - // name : API key 1 - // creation date : 21 Nov 2025 - // read : https://docs.cloud.google.com/vertex-ai/generative-ai/docs/reference/libraries#client-libraries-usage-java - private val BoundAccount = "vertex-express@gtc-cloud-aas.iam.gserviceaccount.com" - private val API_KEYS = "AQ.Ab8RN6K2ZF6wykKVJcuHsfm0po9IXn3alBpO5cfZzPb4lq35Yw" +@Suppress("unused") +class GoogleTTS(credentialJson: String = "c:/googlettsapi/gtc-cloud-aas-ae23c9552e1f.json") { + init{ + System.setProperty("GOOGLE_APPLICATION_CREDENTIALS", credentialJson) + } + + fun Speak(text: String, language : GoogleTTSLanguage, voicetype: GoogleTTSVoiceType, targetfilename: String){ + TextToSpeechClient.create().use { client -> + try{ + val input = SynthesisInput.newBuilder().setText(text).build() + val voice = VoiceSelectionParams.newBuilder() + .setLanguageCode(language.name) + .setName(String.format("%s-%s", language.name, voicetype.name)) + .build() + val audioconfig = AudioConfig.newBuilder() + .setAudioEncoding(AudioEncoding.LINEAR16) + .setSampleRateHertz(44100) + .build() + val response = client.synthesizeSpeech(input, voice, audioconfig) + val bytes = response.audioContent.toByteArray() + val target = Path(targetfilename) + Files.write(target, bytes) + Logger.info { "Speak success, file saved to : $targetfilename" } + } catch (e : Exception){ + Logger.error { "Speak failed, message : ${e.message}" } + } + + + + } + } } \ No newline at end of file diff --git a/src/google/GoogleTTSLanguage.kt b/src/google/GoogleTTSLanguage.kt new file mode 100644 index 0000000..c22b13a --- /dev/null +++ b/src/google/GoogleTTSLanguage.kt @@ -0,0 +1,10 @@ +package google + +@Suppress("unused") +enum class GoogleTTSLanguage(code: String) { + Indonesia("id-ID"), + English("en-US"), + Japanese("ja-JP"), + Chinese("zh-CN"), + Arabic("ar-SA") +} \ No newline at end of file diff --git a/src/google/GoogleTTSSSMLBreakStrength.kt b/src/google/GoogleTTSSSMLBreakStrength.kt new file mode 100644 index 0000000..dbdc638 --- /dev/null +++ b/src/google/GoogleTTSSSMLBreakStrength.kt @@ -0,0 +1,11 @@ +package google + +@Suppress("unused") +enum class GoogleTTSSSMLBreakStrength(code: String) { + None("none"), + ExtraWeak("x-weak"), + Weak("weak"), + Medium("medium"), + Strong("strong"), + ExtraStrong("x-strong") +} \ No newline at end of file diff --git a/src/google/GoogleTTSSSMLStrength.kt b/src/google/GoogleTTSSSMLStrength.kt new file mode 100644 index 0000000..5d1e7d0 --- /dev/null +++ b/src/google/GoogleTTSSSMLStrength.kt @@ -0,0 +1,9 @@ +package google + +@Suppress("unused") +enum class GoogleTTSSSMLStrength(code: String) { + Strong("strong"), + Moderate("moderate"), + None("none"), + Reduced("reduced") +} \ No newline at end of file diff --git a/src/google/GoogleTTSVoiceType.kt b/src/google/GoogleTTSVoiceType.kt new file mode 100644 index 0000000..1011bc2 --- /dev/null +++ b/src/google/GoogleTTSVoiceType.kt @@ -0,0 +1,9 @@ +package google + +@Suppress("unused") +enum class GoogleTTSVoiceType(name: String) { + FemaleA("Wavenet-A"), + MaleA("Wavenet-B"), + MaleB("Wavenet-C"), + FemaleB("Wavenet-D") +} \ No newline at end of file diff --git a/src/google/SSML.kt b/src/google/SSML.kt new file mode 100644 index 0000000..895cdf9 --- /dev/null +++ b/src/google/SSML.kt @@ -0,0 +1,179 @@ +package google + +class SSML private constructor( + private val result : List +) { + + override fun toString(): String = result.joinToString("\n") + @Suppress("unused") + class SSMLBuilder { + + private val result = mutableListOf() + + /** + * Create a new SSMLBuilder + * Source : https://cloud.google.com/text-to-speech/docs/ssml + */ + constructor() + + /** + * Add a text to the SSML + * @param text The text to add + * @return The SSMLBuilder + */ + fun addText(text: String): SSMLBuilder = apply { + result.add(text) + } + + /** + * Add a break to the SSML + * @param ms The duration of the break in milliseconds + * @return The SSMLBuilder + */ + fun addBreak(ms: Int): SSMLBuilder = apply { + result.add("") + } + + /** + * Add a break to the SSML + * @param strength The strength of the break, relative to the surrounding text + * @return the SSMLBuilder + */ + fun addBreak(strength: GoogleTTSSSMLBreakStrength): SSMLBuilder = apply { + result.add("") + } + + /** + * For spelling currency / money + * @param amount The amount to spell + * @param lang The language to use + * @return The SSMLBuilder + */ + fun addSayAsCurrency(amount: String, lang: GoogleTTSLanguage): SSMLBuilder = apply { + result.add( + "$amount" + ) + } + + /** + * For spelling numbers + * Number will be spelled individually + * @param number The number to spell + * @return The SSMLBuilder + */ + fun addSayAsTelephone(number: String): SSMLBuilder = apply { + result.add("$number") + } + + /** + * For spelling letter by letter + * @param text The text to spell + * @return The SSMLBuilder + */ + fun addSayAsVerbatim(text: String): SSMLBuilder = apply { + result.add("$text") + } + + /** + * For spelling date + * @param date The date to spell, example 10-9-2021, or 2021-09-10, or 9-10 + * @param format The format of the date, example dmy, ymd, mdy + * @return The SSMLBuilder + */ + fun addSayAsDate(date: String, format: String): SSMLBuilder = apply { + result.add( + "$date" + ) + } + + /** + * Spelling letter by letter + * @param text The text to spell + * @return the SSMLBuilder + */ + fun addSayAsCharacters(text: String): SSMLBuilder = apply { + result.add("$text") + } + + /** + * Saying number as cardinal + * for example 12345 will be spelled as twelve thousand three hundred forty five + * @param number The number to say + * @return The SSMLBuilder + */ + fun addSayAsCardinal(number: String): SSMLBuilder = apply { + result.add("$number") + } + + /** + * Saying number as ordinal + * for example 1 will be spelled as 'first' + * @param number number to say + * @return The SSMLBuilder + */ + fun addSayAsOrdinal(number: String): SSMLBuilder = apply { + result.add("$number") + } + + /** + * Saying number as fraction + * for example 5+1/2 will be spelled as 'five and a half' + * @param fraction number to say + * @return the SSMLBuilder + */ + fun addSayAsFraction(fraction: String): SSMLBuilder = apply { + result.add("$fraction") + } + + /** + * Saying a word like it was censored 'beep' + * @param text The text to censor + * @return the SSMLBuilder + */ + fun addSayAsExpletive(text: String): SSMLBuilder = apply { + result.add("$text") + } + + /** + * Say unit in singular or plural form + * For example 10 foot will be spelled as 'ten feet' + * @param unit The unit to spell + * @return the SSMLBuilder + */ + fun addSayAsUnit(unit: String): SSMLBuilder = apply { + result.add("$unit") + } + + /** + * Say a time + * for example 12:30 will be spelled as 'twelve thirty' + * @param time The time to spell + * @return the SSMLBuilder + */ + fun addSayAsTime(time: String): SSMLBuilder = apply { + result.add("$time") + } + + /** + * add a full sentence with specific emphasis + * @param text full sentence, not word + * @param strength the strength of the emphasis + * @return the SSMLBuilder + */ + fun addEmphasis(text: String, strength: GoogleTTSSSMLStrength): SSMLBuilder = apply { + result.add("$text") + } + + /** + * Build the SSML + * @return SSML object + */ + fun build(): SSML { + val xx = mutableListOf() + xx.add("") + xx.addAll(result) + xx.add("") + return SSML(xx) + } + } +} \ No newline at end of file