feat: 하나의 문장(한 줄)을 받아 음성으로 변환하는 기능 구현

This commit is contained in:
이상진 2025-08-20 21:14:50 +09:00
parent 1abad2258d
commit fc9331d411

View File

@ -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()) }
}
}