88import org .slf4j .LoggerFactory ;
99import org .springframework .stereotype .Component ;
1010import org .springframework .util .ObjectUtils ;
11- import org .springframework .util .StringUtils ;
1211
1312import java .io .File ;
1413import java .util .Map ;
@@ -35,71 +34,33 @@ public class TtsServiceFactory {
3534 * 获取默认TTS服务
3635 */
3736 public TtsService getDefaultTtsService () {
38- // 如果缓存中没有默认服务,则创建一个
39- return getTtsService (DEFAULT_VOICE );
37+ var config = new SysConfig (). setProvider ( DEFAULT_PROVIDER );
38+ return getTtsService (config , TtsServiceFactory . DEFAULT_VOICE );
4039 }
4140
42- public TtsService getTtsService () {
43- return getTtsService (DEFAULT_VOICE );
44- }
45-
46- private TtsService getTtsService (String voiceName ) {
47- return getTtsService (null , voiceName );
48- }
49-
50- private String createCacheKey (SysConfig config ,String provider ){
51- // 对于API服务,使用"provider:configId"作为缓存键,确保每个配置使用独立的服务实例
52- String configIdStr ;
53- if (config == null ) {
54- configIdStr = "default" ;
55- }else {
56- Integer configId = config .getConfigId ();
57- configIdStr = configId != null ? String .valueOf (configId ) : "default" ;
58- }
41+ // 对于API服务,使用"provider:configId"作为缓存键,确保每个配置使用独立的服务实例
42+ private String createCacheKey (SysConfig config , String provider ) {
43+ var configId = config .getConfigId ();
44+ var configIdStr = configId != null ? String .valueOf (configId ) : "default" ;
5945 return provider + ":" + configIdStr ;
6046 }
6147
6248 /**
6349 * 根据配置获取TTS服务
6450 */
6551 public TtsService getTtsService (SysConfig config , String voiceName ) {
66-
67- String provider ;
6852 // 如果提供商为空,则使用默认提供商
69- if (ObjectUtils .isEmpty (config )) {
70- provider = DEFAULT_PROVIDER ;
71- } else {
72- provider = config .getProvider ();
73- }
74- String cacheKey = createCacheKey (config ,provider );
53+ var provider = ObjectUtils .isEmpty (config ) ? DEFAULT_PROVIDER : config .getProvider ();
54+ var cacheKey = createCacheKey (config , provider );
55+
7556 // 检查是否已有该配置的服务实例
7657 if (serviceCache .containsKey (cacheKey )) {
7758 return serviceCache .get (cacheKey );
78- }else {
79- // 如果是默认提供商且尚未初始化,则初始化
80- if (DEFAULT_PROVIDER .equals (provider )) {
81- if (StringUtils .hasText (voiceName )){
82- TtsService ttsService = getTtsService (voiceName );
83- serviceCache .put (cacheKey , ttsService );
84- return ttsService ;
85- }else {
86- TtsService ttsService = getTtsService ();
87- serviceCache .put (cacheKey , ttsService );
88- return ttsService ;
89- }
90- }
91- // 创建新的服务实例
92- try {
93- TtsService service ;
94- // 创建其他API服务
95- service = createApiService (config , voiceName , OUT_PUT_PATH );
96- serviceCache .put (cacheKey , service );
97- return service ;
98- } catch (Exception e ) {
99- logger .error ("创建{}服务失败" , provider , e );
100- return getDefaultTtsService (); // 失败时返回默认服务
101- }
10259 }
60+
61+ var service = createApiService (config , voiceName , OUT_PUT_PATH );
62+ serviceCache .put (cacheKey , service );
63+ return service ;
10364 }
10465
10566 /**
@@ -109,22 +70,13 @@ private TtsService createApiService(SysConfig config, String voiceName, String o
10970 // Make sure output dir exists
11071 ensureOutputPath (outputPath );
11172
112- var provider = config .getProvider ();
113- // 如果是Edge,直接返回Edge服务
114- if (DEFAULT_PROVIDER .equals (provider )) {
115- return new EdgeTtsService (voiceName , outputPath );
116- } else if ("aliyun" .equals (provider )) {
117- return new AliyunTtsService (config , voiceName , outputPath );
118- } else if ("volcengine" .equals (provider )) {
119- return new VolcengineTtsService (config , voiceName , outputPath );
120- } else if ("xfyun" .equals (provider )) {
121- return new XfyunTtsService (config , voiceName , outputPath );
122- } else if ("minimax" .equals (provider )) {
123- return new MiniMaxTtsService (config , voiceName , outputPath );
124- }
125-
126- logger .warn ("不支持的TTS服务提供商: {}" , provider );
127- return null ;
73+ return switch (config .getProvider ()) {
74+ case "aliyun" -> new AliyunTtsService (config , voiceName , outputPath );
75+ case "volcengine" -> new VolcengineTtsService (config , voiceName , outputPath );
76+ case "xfyun" -> new XfyunTtsService (config , voiceName , outputPath );
77+ case "minimax" -> new MiniMaxTtsService (config , voiceName , outputPath );
78+ default -> new EdgeTtsService (voiceName , outputPath );
79+ };
12880 }
12981
13082 public void removeCache (SysConfig config ) {
0 commit comments