Spring Boot
[Google API + Springboot] TTS(Text-to-Speech) 기능을 구현
도쿄아재
2023. 2. 9. 03:11
반응형
Spring Boot 응용 프로그램에서 TTS(Text-to-Speech) 기능을 구현하려면 Google Cloud Text-to-Speech API를 사용할 수 있습니다. API는 Google Cloud Console에서 얻을 수 있는 API 키를 사용하여 액세스할 수 있습니다.
1. 다음 종속성을 pom.xml 파일에 추가합니다.
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-texttospeech</artifactId>
<version>1.0.0</version>
</dependency>
2. 응용 프로그램에서 API 키를 설정합니다.
@Value("${google.cloud.api.key}")
private String apiKey;
@Bean
public TextToSpeechClient textToSpeechClient() throws IOException {
return TextToSpeechClient.create().setCredentials(
ServiceAccountCredentials.fromStream(new ByteArrayInputStream(apiKey.getBytes())));
}
3. 코드에서 API를 사용합니다.
@Autowired
private TextToSpeechClient textToSpeechClient;
public byte[] generateSpeech(String text, String voice) throws Exception {
SynthesisInput input = SynthesisInput.newBuilder().setText(text).build();
VoiceSelectionParams voiceParams =
VoiceSelectionParams.newBuilder().setLanguageCode("en-US").setName(voice).build();
AudioConfig audioConfig =
AudioConfig.newBuilder().setAudioEncoding(AudioEncoding.MP3).build();
SynthesizeSpeechResponse response =
textToSpeechClient.synthesizeSpeech(input, voiceParams, audioConfig);
return response.getAudioContent().toByteArray();
}
반응형