Skip to content

Commit 320f157

Browse files
authored
add http sse cosyvocie tts api and omni createItem function (#206)
* feat(model/omni):support item.create api * feat(model/cosyvoice):support http sse api
1 parent 82ea356 commit 320f157

10 files changed

Lines changed: 1272 additions & 3 deletions
Lines changed: 226 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,226 @@
1+
// Copyright (c) Alibaba, Inc. and its affiliates.
2+
3+
import com.alibaba.dashscope.audio.http_tts.AudioInfo;
4+
import com.alibaba.dashscope.audio.http_tts.HttpSpeechSynthesisParam;
5+
import com.alibaba.dashscope.audio.http_tts.HttpSpeechSynthesisResult;
6+
import com.alibaba.dashscope.audio.http_tts.HttpSpeechSynthesizer;
7+
import com.alibaba.dashscope.common.ResultCallback;
8+
import com.alibaba.dashscope.exception.ApiException;
9+
import com.alibaba.dashscope.exception.InputRequiredException;
10+
import com.alibaba.dashscope.exception.NoApiKeyException;
11+
import com.alibaba.dashscope.utils.Constants;
12+
13+
import java.io.FileOutputStream;
14+
import java.io.IOException;
15+
import java.nio.ByteBuffer;
16+
import java.util.concurrent.CountDownLatch;
17+
18+
/**
19+
* Example usage of HttpSpeechSynthesizer for HTTP SSE-based text-to-speech synthesis.
20+
*
21+
* <p>Make sure to set the DASHSCOPE_API_KEY environment variable before running this example.
22+
*
23+
* @author DashScope SDK Team
24+
*/
25+
public class HttpSpeechSynthesizerUsage {
26+
27+
/**
28+
* Demonstrates synchronous call with SSE - blocks until synthesis is complete and returns audio
29+
* data.
30+
*/
31+
public static void syncCall() {
32+
System.out.println("=== Synchronous Call with SSE Example ===");
33+
34+
// Create synthesizer
35+
HttpSpeechSynthesizer synthesizer = new HttpSpeechSynthesizer();
36+
37+
// Build parameters
38+
HttpSpeechSynthesisParam param =
39+
HttpSpeechSynthesisParam.builder()
40+
.model("cosyvoice-v3-flash")
41+
.text("我家的后面有一个很大的园。")
42+
.voice("longanyang")
43+
.format("wav")
44+
.sampleRate(24000)
45+
.build();
46+
47+
try {
48+
// Call and get complete audio data
49+
ByteBuffer audioData = synthesizer.callAndReturnAudio(param);
50+
51+
// Save to file
52+
if (audioData != null && audioData.hasRemaining()) {
53+
byte[] bytes = new byte[audioData.remaining()];
54+
audioData.get(bytes);
55+
56+
try (FileOutputStream fos = new FileOutputStream("sync_output.wav")) {
57+
fos.write(bytes);
58+
System.out.println("Audio saved to sync_output.wav, size: " + bytes.length + " bytes");
59+
} catch (IOException e) {
60+
System.err.println("Failed to save audio: " + e.getMessage());
61+
}
62+
}
63+
} catch (ApiException | NoApiKeyException | InputRequiredException e) {
64+
System.err.println("Synthesis failed: " + e.getMessage());
65+
}
66+
}
67+
68+
/**
69+
* Demonstrates synchronous call without SSE - returns audio URL instead of audio data. This is a
70+
* simpler and faster way to get the synthesized audio.
71+
*/
72+
public static void syncCallWithUrl() {
73+
System.out.println("\n=== Synchronous Call without SSE (returns Audio URL) ===");
74+
75+
HttpSpeechSynthesizer synthesizer = new HttpSpeechSynthesizer();
76+
77+
HttpSpeechSynthesisParam param =
78+
HttpSpeechSynthesisParam.builder()
79+
.model("cosyvoice-v3-flash")
80+
.text("我家的后面有一个很大的园。")
81+
.voice("longanyang")
82+
.format("wav")
83+
.sampleRate(24000)
84+
.build();
85+
86+
try {
87+
// Non-SSE call - returns result with audio URL
88+
HttpSpeechSynthesisResult result = synthesizer.call(param);
89+
90+
System.out.println("Request ID: " + result.getRequestId());
91+
System.out.println("Finish Reason: " + result.getFinishReason());
92+
93+
if (result.hasAudioUrl()) {
94+
AudioInfo audioInfo = result.getAudioInfo();
95+
System.out.println("\nAudio URL: " + audioInfo.getUrl());
96+
System.out.println("Audio ID: " + audioInfo.getId());
97+
System.out.println("Expires At: " + audioInfo.getExpiresAt());
98+
System.out.println("Remaining Time: " + audioInfo.getRemainingSeconds() + " seconds");
99+
System.out.println("URL Expired: " + audioInfo.isExpired());
100+
101+
// You can download the audio from the URL
102+
// Example: use HttpURLConnection or any HTTP client to download
103+
System.out.println("\nTip: You can download the audio file from the URL above.");
104+
}
105+
106+
} catch (ApiException | NoApiKeyException | InputRequiredException e) {
107+
System.err.println("Synthesis failed: " + e.getMessage());
108+
}
109+
}
110+
111+
/** Demonstrates streaming call with callback - receives audio chunks as they arrive. */
112+
public static void streamCallWithCallback() {
113+
System.out.println("\n=== Streaming Call with Callback Example ===");
114+
115+
HttpSpeechSynthesizer synthesizer = new HttpSpeechSynthesizer();
116+
117+
HttpSpeechSynthesisParam param =
118+
HttpSpeechSynthesisParam.builder()
119+
.model("cosyvoice-v3-flash")
120+
.text("今天天气真好,适合出去玩。")
121+
.voice("longanyang")
122+
.format("wav")
123+
.sampleRate(24000)
124+
.build();
125+
126+
// Use CountDownLatch to wait for completion
127+
CountDownLatch latch = new CountDownLatch(1);
128+
129+
try {
130+
synthesizer.streamCall(
131+
param,
132+
new ResultCallback<HttpSpeechSynthesisResult>() {
133+
private int chunkCount = 0;
134+
135+
@Override
136+
public void onEvent(HttpSpeechSynthesisResult result) {
137+
chunkCount++;
138+
if (result.hasAudioData()) {
139+
System.out.println(
140+
"Received chunk #"
141+
+ chunkCount
142+
+ ", size: "
143+
+ result.getAudioDataSize()
144+
+ " bytes");
145+
}
146+
if (result.getRequestId() != null) {
147+
System.out.println("Request ID: " + result.getRequestId());
148+
}
149+
}
150+
151+
@Override
152+
public void onComplete() {
153+
latch.countDown();
154+
}
155+
156+
@Override
157+
public void onError(Exception e) {
158+
System.err.println("✗ Error during synthesis: " + e.getMessage());
159+
latch.countDown();
160+
}
161+
});
162+
163+
// Wait for completion
164+
latch.await();
165+
System.out.println("Done!");
166+
167+
} catch (ApiException | NoApiKeyException | InputRequiredException | InterruptedException e) {
168+
System.err.println("Failed: " + e.getMessage());
169+
}
170+
}
171+
172+
/** Demonstrates custom parameter settings. */
173+
public static void customParameters() {
174+
System.out.println("\n=== Custom Parameters Example ===");
175+
176+
HttpSpeechSynthesizer synthesizer = new HttpSpeechSynthesizer();
177+
178+
// Build parameters with custom voice settings
179+
HttpSpeechSynthesisParam param =
180+
HttpSpeechSynthesisParam.builder()
181+
.model("cosyvoice-v3-flash")
182+
.text("这是一段测试语音合成参数的文本。")
183+
.voice("longanyang")
184+
.format("wav")
185+
.sampleRate(24000)
186+
.volume(80) // Volume: 0-100
187+
.rate(1.2f) // Speech rate: 0.5-2.0
188+
.pitch(1.1f) // Pitch: 0.5-2.0
189+
.build();
190+
191+
System.out.println("Parameters:");
192+
System.out.println(" Model: " + param.getModel());
193+
System.out.println(" Text: " + param.getText());
194+
System.out.println(" Voice: " + param.getVoice());
195+
System.out.println(" Format: " + param.getFormat());
196+
System.out.println(" Sample Rate: " + param.getSampleRate());
197+
System.out.println(" Volume: " + param.getVolume());
198+
System.out.println(" Rate: " + param.getRate());
199+
System.out.println(" Pitch: " + param.getPitch());
200+
201+
try {
202+
ByteBuffer audioData = synthesizer.callAndReturnAudio(param);
203+
if (audioData != null) {
204+
System.out.println(
205+
"✓ Synthesis completed, audio size: " + audioData.remaining() + " bytes");
206+
}
207+
} catch (ApiException | NoApiKeyException | InputRequiredException e) {
208+
System.err.println("Failed: " + e.getMessage());
209+
}
210+
}
211+
212+
public static void main(String[] args) {
213+
Constants.apiKey = System.getenv("DASHSCOPE_API_KEY");
214+
System.out.println("HttpSpeechSynthesizer Usage Examples\n");
215+
System.out.println("====================================\n");
216+
217+
// Run examples
218+
syncCall(); // SSE streaming - returns audio data
219+
syncCallWithUrl(); // Non-SSE - returns audio URL
220+
streamCallWithCallback();
221+
customParameters();
222+
223+
System.out.println("\n====================================");
224+
System.out.println("All examples completed!");
225+
}
226+
}

0 commit comments

Comments
 (0)