Skip to content

Commit 62a1df9

Browse files
eded
authored andcommitted
refactor(tts): add default implements
1 parent 747b602 commit 62a1df9

File tree

6 files changed

+35
-91
lines changed

6 files changed

+35
-91
lines changed

src/main/java/com/xiaozhi/dialogue/tts/TtsService.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.xiaozhi.dialogue.tts;
22

3+
import java.util.UUID;
34
import java.util.function.Consumer;
45

56
/**
@@ -12,17 +13,28 @@ public interface TtsService {
1213
*/
1314
String getProviderName();
1415

16+
/**
17+
* 音频格式
18+
*/
19+
default String audioFormat() {
20+
return "wav";
21+
}
22+
1523
/**
1624
* 生成文件名称
1725
*
1826
* @return 文件名称
1927
*/
20-
String getAudioFileName();
28+
default String getAudioFileName() {
29+
return UUID.randomUUID().toString().replace("-", "") + "." + audioFormat();
30+
}
2131

2232
/**
2333
*
2434
*/
25-
boolean isSupportStreamTts();
35+
default boolean isSupportStreamTts() {
36+
return false;
37+
}
2638

2739
/**
2840
* 将文本转换为语音(带自定义语音)
@@ -39,6 +51,8 @@ public interface TtsService {
3951
* @param audioDataConsumer 音频数据消费者,接收PCM格式的音频数据块
4052
* @throws Exception 转换过程中可能发生的异常
4153
*/
42-
void streamTextToSpeech(String text, Consumer<byte[]> audioDataConsumer) throws Exception;
54+
default void streamTextToSpeech(String text, Consumer<byte[]> audioDataConsumer) throws Exception {
55+
throw new UnsupportedOperationException("Unimplemented method 'streamTextToSpeech'");
56+
}
4357

4458
}

src/main/java/com/xiaozhi/dialogue/tts/factory/TtsServiceFactory.java

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.springframework.util.ObjectUtils;
1111
import org.springframework.util.StringUtils;
1212

13+
import java.io.File;
1314
import java.util.Map;
1415
import java.util.concurrent.ConcurrentHashMap;
1516

@@ -43,8 +44,7 @@ public TtsService getTtsService() {
4344
}
4445

4546
private TtsService getTtsService(String voiceName) {
46-
TtsService ttsService = new EdgeTtsService(voiceName, OUT_PUT_PATH);
47-
return ttsService;
47+
return getTtsService(null, voiceName);
4848
}
4949

5050
private String createCacheKey(SysConfig config,String provider){
@@ -56,8 +56,7 @@ private String createCacheKey(SysConfig config,String provider){
5656
Integer configId = config.getConfigId();
5757
configIdStr = configId != null ? String.valueOf(configId) : "default";
5858
}
59-
String cacheKey = provider + ":" + configIdStr;
60-
return cacheKey;
59+
return provider + ":" + configIdStr;
6160
}
6261

6362
/**
@@ -107,8 +106,10 @@ public TtsService getTtsService(SysConfig config, String voiceName) {
107106
* 根据配置创建API类型的TTS服务
108107
*/
109108
private TtsService createApiService(SysConfig config, String voiceName, String outputPath) {
110-
String provider = config.getProvider();
109+
// Make sure output dir exists
110+
ensureOutputPath(outputPath);
111111

112+
var provider = config.getProvider();
112113
// 如果是Edge,直接返回Edge服务
113114
if (DEFAULT_PROVIDER.equals(provider)) {
114115
return new EdgeTtsService(voiceName, outputPath);
@@ -118,11 +119,9 @@ private TtsService createApiService(SysConfig config, String voiceName, String o
118119
return new VolcengineTtsService(config, voiceName, outputPath);
119120
} else if ("xfyun".equals(provider)) {
120121
return new XfyunTtsService(config, voiceName, outputPath);
121-
}/*
122-
* else if ("tencent".equals(provider)) {
123-
* return new TencentTtsService(config, voiceName, outputPath);
124-
* }
125-
*/
122+
} else if ("minimax".equals(provider)) {
123+
return new MiniMaxTtsService(config, voiceName, outputPath);
124+
}
126125

127126
logger.warn("不支持的TTS服务提供商: {}", provider);
128127
return null;
@@ -136,4 +135,8 @@ public void removeCache(SysConfig config) {
136135
serviceCache.remove(cacheKey);
137136
}
138137

138+
private void ensureOutputPath(String outputPath) {
139+
File dir = new File(outputPath);
140+
if (!dir.exists()) dir.mkdirs();
141+
}
139142
}

src/main/java/com/xiaozhi/dialogue/tts/providers/AliyunTtsService.java

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,11 @@
2222
import java.io.InputStream;
2323
import java.net.URL;
2424
import java.nio.ByteBuffer;
25-
import java.util.UUID;
2625
import java.util.concurrent.ExecutorService;
2726
import java.util.concurrent.Executors;
2827
import java.util.concurrent.Future;
2928
import java.util.concurrent.TimeUnit;
3029
import java.util.concurrent.TimeoutException;
31-
import java.util.function.Consumer;
3230

3331
public class AliyunTtsService implements TtsService {
3432
private static final Logger logger = LoggerFactory.getLogger(AliyunTtsService.class);
@@ -58,24 +56,9 @@ public String getProviderName() {
5856
return PROVIDER_NAME;
5957
}
6058

61-
@Override
62-
public boolean isSupportStreamTts() {
63-
return false;
64-
}
65-
66-
@Override
67-
public String getAudioFileName() {
68-
String uuid = UUID.randomUUID().toString().replace("-", "");
69-
return uuid + ".wav";
70-
}
71-
7259
@Override
7360
public String textToSpeech(String text) throws Exception {
7461
try {
75-
File outputDir = new File(outputPath);
76-
if (!outputDir.exists()) {
77-
outputDir.mkdirs();
78-
}
7962
if (voiceName.contains("sambert")) {
8063
return ttsSambert(text);
8164
} else if (getVoiceByName(voiceName) != null) {
@@ -394,9 +377,4 @@ public String ttsSambert(String text) {
394377
return StrUtil.EMPTY;
395378
}
396379

397-
@Override
398-
public void streamTextToSpeech(String text, Consumer<byte[]> audioDataConsumer) throws Exception {
399-
// TODO Auto-generated method stub
400-
throw new UnsupportedOperationException("Unimplemented method 'streamTextToSpeech'");
401-
}
402380
}

src/main/java/com/xiaozhi/dialogue/tts/providers/EdgeTtsService.java

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66

77
import java.nio.file.Files;
88
import java.nio.file.Paths;
9-
import java.util.UUID;
10-
import java.util.function.Consumer;
119
import java.util.stream.Collectors;
1210

1311
import org.slf4j.Logger;
@@ -38,14 +36,8 @@ public String getProviderName() {
3836
}
3937

4038
@Override
41-
public boolean isSupportStreamTts() {
42-
return false;
43-
}
44-
45-
@Override
46-
public String getAudioFileName() {
47-
String uuid = UUID.randomUUID().toString().replace("-", "");
48-
return uuid + ".mp3";
39+
public String audioFormat() {
40+
return "mp3";
4941
}
5042

5143
@Override
@@ -80,9 +72,4 @@ public String textToSpeech(String text) throws Exception {
8072
return AudioUtils.AUDIO_PATH + resampledFileName;
8173
}
8274

83-
@Override
84-
public void streamTextToSpeech(String text, Consumer<byte[]> audioDataConsumer) throws Exception {
85-
// TODO Auto-generated method stub
86-
throw new UnsupportedOperationException("Unimplemented method 'streamTextToSpeech'");
87-
}
8875
}

src/main/java/com/xiaozhi/dialogue/tts/providers/VolcengineTtsService.java

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import java.io.FileOutputStream;
1616
import java.util.Base64;
1717
import java.util.UUID;
18-
import java.util.function.Consumer;
1918

2019
public class VolcengineTtsService implements TtsService {
2120
private static final Logger logger = LoggerFactory.getLogger(VolcengineTtsService.class);
@@ -34,7 +33,7 @@ public class VolcengineTtsService implements TtsService {
3433
private String appId;
3534
private String accessToken; // 对应 apiKey
3635

37-
private final OkHttpClient client = HttpUtil.client;;
36+
private final OkHttpClient client = HttpUtil.client;
3837

3938
public VolcengineTtsService(SysConfig config, String voiceName, String outputPath) {
4039
this.voiceName = voiceName;
@@ -48,17 +47,6 @@ public String getProviderName() {
4847
return PROVIDER_NAME;
4948
}
5049

51-
@Override
52-
public boolean isSupportStreamTts() {
53-
return false;
54-
}
55-
56-
@Override
57-
public String getAudioFileName() {
58-
String uuid = UUID.randomUUID().toString().replace("-", "");
59-
return uuid + ".wav";
60-
}
61-
6250
@Override
6351
public String textToSpeech(String text) throws Exception {
6452
if (text == null || text.isEmpty()) {
@@ -163,12 +151,6 @@ private boolean sendRequest(String text, String audioFilePath) throws Exception
163151
String base64Audio = jsonResponse.get("data").getAsString();
164152
byte[] audioData = Base64.getDecoder().decode(base64Audio);
165153

166-
// 确保目录存在
167-
File audioFileDir = new File(outputPath);
168-
if (!audioFileDir.exists()) {
169-
audioFileDir.mkdirs();
170-
}
171-
172154
// 保存音频文件
173155
File audioFile = new File(audioFilePath);
174156
try (FileOutputStream fout = new FileOutputStream(audioFile)) {
@@ -191,10 +173,4 @@ private boolean sendRequest(String text, String audioFilePath) throws Exception
191173
}
192174
}
193175

194-
@Override
195-
public void streamTextToSpeech(String text, Consumer<byte[]> audioDataConsumer) throws Exception {
196-
// TODO Auto-generated method stub
197-
throw new UnsupportedOperationException("Unimplemented method 'streamTextToSpeech'");
198-
}
199-
200176
}

src/main/java/com/xiaozhi/dialogue/tts/providers/XfyunTtsService.java

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,8 @@
1313
import java.io.File;
1414
import java.io.FileOutputStream;
1515
import java.io.IOException;
16-
import java.util.UUID;
1716
import java.util.concurrent.CountDownLatch;
1817
import java.util.concurrent.TimeUnit;
19-
import java.util.function.Consumer;
2018

2119
public class XfyunTtsService implements TtsService {
2220
private static final Logger logger = LoggerFactory.getLogger(XfyunTtsService.class);
@@ -50,14 +48,8 @@ public String getProviderName() {
5048
}
5149

5250
@Override
53-
public boolean isSupportStreamTts() {
54-
return false;
55-
}
56-
57-
@Override
58-
public String getAudioFileName() {
59-
String uuid = UUID.randomUUID().toString().replace("-", "");
60-
return uuid + ".mp3";
51+
public String audioFormat() {
52+
return "mp3";
6153
}
6254

6355
@Override
@@ -139,10 +131,4 @@ public void onBusinessFail(WebSocket webSocket, TtsResponse ttsResponse) {
139131
return true;
140132
}
141133

142-
@Override
143-
public void streamTextToSpeech(String text, Consumer<byte[]> audioDataConsumer) throws Exception {
144-
// TODO Auto-generated method stub
145-
throw new UnsupportedOperationException("Unimplemented method 'streamTextToSpeech'");
146-
}
147-
148134
}

0 commit comments

Comments
 (0)