From fc9331d411f4c6ffc9328d7864cdfa2c9d6b3a50 Mon Sep 17 00:00:00 2001 From: pricelees Date: Wed, 20 Aug 2025 21:14:50 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=ED=95=98=EB=82=98=EC=9D=98=20=EB=AC=B8?= =?UTF-8?q?=EC=9E=A5(=ED=95=9C=20=EC=A4=84)=EC=9D=84=20=EB=B0=9B=EC=95=84?= =?UTF-8?q?=20=EC=9D=8C=EC=84=B1=EC=9C=BC=EB=A1=9C=20=EB=B3=80=ED=99=98?= =?UTF-8?q?=ED=95=98=EB=8A=94=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../text_to_speech/SingleTextConverter.kt | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 src/main/kotlin/com/sangdol/text_to_speech/SingleTextConverter.kt diff --git a/src/main/kotlin/com/sangdol/text_to_speech/SingleTextConverter.kt b/src/main/kotlin/com/sangdol/text_to_speech/SingleTextConverter.kt new file mode 100644 index 0000000..66cb1ba --- /dev/null +++ b/src/main/kotlin/com/sangdol/text_to_speech/SingleTextConverter.kt @@ -0,0 +1,44 @@ +package com.sangdol.text_to_speech + +import com.google.cloud.texttospeech.v1.* +import com.google.protobuf.ByteString +import org.springframework.stereotype.Component +import java.io.File +import java.io.FileOutputStream + +@Component +class SingleTextConverter( + private val ttsClient: TextToSpeechClient +) { + fun convert( + request: ConvertSingleTextRequest, + filePath: String, + language: TtsLanguageCode = TtsLanguageCode.ENGLISH_US, + encodingType: AudioEncoding = AudioEncoding.MP3 + ) { + val synthesisInput = SynthesisInput.newBuilder() + .setText(request.text) + .build() + + val voiceOptions = VoiceSelectionParams.newBuilder() + .setLanguageCode(language.code) + .setName(request.type.identifier) + .build() + + val audioConfig = AudioConfig.newBuilder() + .setAudioEncoding(encodingType) + .setSpeakingRate(request.speakingRate) + .build() + + val synthesizeResponse: SynthesizeSpeechResponse = ttsClient.synthesizeSpeech(synthesisInput, voiceOptions, audioConfig) + val content: ByteString = synthesizeResponse.audioContent + + val file = File(filePath).also { + it.parentFile + .takeIf { dir -> !dir.exists() } + ?.mkdirs() + } + + FileOutputStream(file).use { it.write(content.toByteArray()) } + } +}