// 환경 변수 사용// export TYPECAST_API_KEY="your-api-key-here"TypecastClient client = new TypecastClient();// 또는 직접 전달TypecastClient client = new TypecastClient("your-api-key-here");// 또는 사용자 정의 base URL과 함께TypecastClient client = new TypecastClient("your-api-key-here", "https://custom-api.example.com");
자체 프록시를 통해 요청하는 경우 프록시 base URL을 전달하고 API 키는 null 또는 빈 문자열로 생략할 수 있습니다. API 키가 비어 있거나 없으면 SDK는 X-API-KEY 헤더를 보내지 않습니다. 기본 Typecast 호스트로 요청할 때는 API 키가 계속 필요합니다.
API 키 없는 프록시
TypecastClient client = new TypecastClient(null, "https://your-proxy.example.com");
TTSRequest request = TTSRequest.builder() .voiceId("tc_672c5f5ce59fac2a48faeaee") .text("모든 것이 잘 될 거예요.") .model(TTSModel.SSFM_V30) .prompt(SmartPrompt.builder() .previousText("방금 최고의 소식을 들었어요!") // 선택적 문맥 .nextText("축하할 수 있어서 너무 기다려져요!") // 선택적 문맥 .build()) .build();TTSResponse response = client.textToSpeech(request);
한 voice로 읽는 문장 안에 쉼만 넣고 싶다면 텍스트에 pause markup을 직접 작성합니다. <|5s|>, <|1s|>, <|0.3s|>, <|0.34413s|>처럼 쓰며 값은 초 단위이고 반드시 s로 끝납니다. 별도 pause 함수를 호출하지 않아도 텍스트만 보고 쉼 위치를 확인할 수 있습니다.
TTSResponse audio = client.composeSpeech() .defaults(new ComposerSettings().voiceId("tc_672c5f5ce59fac2a48faeaee").model(TTSModel.SSFM_V30)) .say("안녕하세요<|5s|>반갑습니다<|1s|>오늘<|2s|>날씨는 어떤 것 같으세요?") .generate();
한 파일 안에서 서로 다른 voice나 구간별 pitch, tempo, prompt, seed 같은 옵션을 조합해야 할 때 사용합니다. composer는 각 구간을 WAV로 생성하고 앞뒤 무음 PCM 샘플을 trim한 뒤 합성합니다. MP3가 필요하면 먼저 WAV를 생성한 다음 앱 또는 서버 파이프라인에서 변환하세요.
TypecastClient client = new TypecastClient("YOUR_API_KEY");TTSResponse audio = client.composeSpeech() .defaults(new ComposerSettings().setVoiceId("tc_672c5f5ce59fac2a48faeaee").setModel(TTSModel.SSFM_V30)) .say("Hello there") .pause(5) .say("Nice to meet you", new ComposerSettings() .setVoiceId("tc_60e5426de8b95f1d3000d7b5") .setOutput(Output.builder().volume(null).audioPitch(2).build())) .say("Today") .pause(2) .say("How does the weather feel?") .generate();Files.write(Path.of("conversation.wav"), audio.getAudioData());
import javax.sound.sampled.*;// 오디오 재생 설정: 32000 Hz, 16비트, 모노, 리틀엔디안AudioFormat format = new AudioFormat(32000, 16, 1, true, false);SourceDataLine line = AudioSystem.getSourceDataLine(format);line.open(format, 8192);line.start();try (InputStream stream = client.textToSpeechStream(request)) { byte[] buf = new byte[4096]; boolean first = true; int bytesRead; while ((bytesRead = stream.read(buf)) != -1) { int offset = 0; if (first) { offset = 44; // 44바이트 WAV 헤더 건너뛰기 bytesRead -= 44; first = false; } line.write(buf, offset, bytesRead); }}line.drain();line.close();client.close();
WAV 스트리밍 형식: 32000 Hz, 16비트, 모노 PCM. 첫 번째 청크에 44바이트 WAV 헤더(size = 0xFFFFFFFF)가 포함되며, 이후 청크는 원시 PCM 데이터만 포함합니다. MP3 형식: 320 kbps, 44100 Hz, 각 청크는 독립적으로 디코딩 가능합니다. java.io.OutputStream과의 이름 충돌을 피하려면 com.neosapience.models.OutputStream으로 정규화된 이름을 사용하세요.
textToSpeechWithTimestamps()는 POST /v1/text-to-speech/with-timestamps를 래핑하며, 오디오와 함께 단어·문자 단위 정렬 데이터를 반환합니다. 가라오케 하이라이트, 자막 생성, 립싱크 애플리케이션에 활용할 수 있습니다.
granularity("word")(기본값) 또는 granularity("char")을 설정해 정렬 단위를 제어합니다.
// 문자 단위 정렬 — 일본어·중국어에 필수TTSWithTimestampsResponse result = client.textToSpeechWithTimestamps( TTSRequestWithTimestamps.builder() .voiceId("tc_60e5426de8b95f1d3000d7b5") .text("Hello. How are you?") .model("ssfm-v30") .granularity("char") .build());