Skip to content

Commit 64e0073

Browse files
lengjiayikevinlin09
authored andcommitted
[Add] cosyvoice enable ssml
1 parent f60c9e0 commit 64e0073

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

src/main/java/com/alibaba/dashscope/audio/ttsv2/SpeechSynthesizer.java

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@
1616
import io.reactivex.BackpressureStrategy;
1717
import io.reactivex.Emitter;
1818
import io.reactivex.Flowable;
19+
import lombok.Builder;
20+
import lombok.Getter;
21+
import lombok.NonNull;
22+
import lombok.experimental.SuperBuilder;
23+
import lombok.extern.slf4j.Slf4j;
24+
1925
import java.io.ByteArrayOutputStream;
2026
import java.io.IOException;
2127
import java.nio.ByteBuffer;
@@ -29,11 +35,6 @@
2935
import java.util.concurrent.TimeUnit;
3036
import java.util.concurrent.atomic.AtomicBoolean;
3137
import java.util.concurrent.atomic.AtomicReference;
32-
import lombok.Builder;
33-
import lombok.Getter;
34-
import lombok.NonNull;
35-
import lombok.experimental.SuperBuilder;
36-
import lombok.extern.slf4j.Slf4j;
3738

3839
/** @author lengjiayi */
3940
@Slf4j
@@ -226,7 +227,7 @@ public Flowable<SpeechSynthesisResult> streamingCallAsFlowable(Flowable<String>
226227
return duplexApi
227228
.duplexCall(
228229
StreamInputTtsParamWithStream.fromStreamInputTtsParam(
229-
this.parameters, textStream, preRequestId))
230+
this.parameters, textStream, preRequestId, false))
230231
.map(SpeechSynthesisResult::fromDashScopeResult)
231232
.filter(item -> !canceled.get())
232233
.doOnNext(
@@ -277,7 +278,8 @@ public Flowable<SpeechSynthesisResult> callAsFlowable(String text)
277278
.start();
278279
},
279280
BackpressureStrategy.BUFFER),
280-
preRequestId))
281+
preRequestId,
282+
true))
281283
.map(SpeechSynthesisResult::fromDashScopeResult)
282284
.doOnNext(
283285
result -> {
@@ -306,7 +308,7 @@ public Flowable<SpeechSynthesisResult> callAsFlowable(String text)
306308
* Start voice transcription: Establish a connection with the server, send a voice transcription
307309
* request, and synchronously receive confirmation from the server.
308310
*/
309-
private void startStream() {
311+
private void startStream(boolean enableSsml) {
310312

311313
startStreamTimeStamp = System.currentTimeMillis();
312314
recvAudioLength = 0;
@@ -350,7 +352,7 @@ private void startStream() {
350352
try {
351353
duplexApi.duplexCall(
352354
SpeechSynthesizer.StreamInputTtsParamWithStream.fromStreamInputTtsParam(
353-
this.parameters, textFrames, preRequestId),
355+
this.parameters, textFrames, preRequestId, enableSsml),
354356
new ResultCallback<DashScopeResult>() {
355357
// private Sentence lastSentence = null;
356358

@@ -409,6 +411,9 @@ public void onEvent(DashScopeResult message) {
409411
log.error("Failed to parse response: {}", message, e);
410412
callback.onError(e);
411413
}
414+
if (speechSynthesisResult.getRequestId() == null) {
415+
speechSynthesisResult.setRequestId(preRequestId);
416+
}
412417
callback.onEvent(speechSynthesisResult);
413418
}
414419

@@ -583,7 +588,7 @@ public void streamingCancel() {
583588
public void streamingCall(String text) {
584589
if (isFirst) {
585590
isFirst = false;
586-
this.startStream();
591+
this.startStream(false);
587592
}
588593
this.submitText(text);
589594
}
@@ -614,7 +619,7 @@ public void onComplete() {}
614619
public void onError(Exception e) {}
615620
};
616621
}
617-
this.startStream();
622+
this.startStream(true);
618623
this.submitText(text);
619624
if (this.asyncCall) {
620625
this.asyncStreamingComplete();
@@ -650,11 +655,12 @@ private static class StreamInputTtsParamWithStream extends SpeechSynthesisParam
650655
@NonNull private Flowable<String> textStream;
651656

652657
public static StreamInputTtsParamWithStream fromStreamInputTtsParam(
653-
SpeechSynthesisParam param, Flowable<String> textStream, String preRequestId) {
658+
SpeechSynthesisParam param, Flowable<String> textStream, String preRequestId, boolean enableSsml) {
654659
return StreamInputTtsParamWithStream.builder()
655660
.headers(param.getHeaders())
656661
.parameters(param.getParameters())
657662
.parameter("pre_task_id", preRequestId)
663+
.parameter("enable_ssml", enableSsml)
658664
.format(param.getFormat())
659665
.textStream(textStream)
660666
.model(param.getModel())

0 commit comments

Comments
 (0)