88import org .slf4j .LoggerFactory ;
99import org .springframework .stereotype .Component ;
1010import org .springframework .util .ObjectUtils ;
11- import org .springframework .util .StringUtils ;
1211
12+ import java .io .File ;
1313import java .util .Map ;
1414import java .util .concurrent .ConcurrentHashMap ;
1515
@@ -34,98 +34,49 @@ public class TtsServiceFactory {
3434 * 获取默认TTS服务
3535 */
3636 public TtsService getDefaultTtsService () {
37- // 如果缓存中没有默认服务,则创建一个
38- return getTtsService (DEFAULT_VOICE );
37+ var config = new SysConfig (). setProvider ( DEFAULT_PROVIDER );
38+ return getTtsService (config , TtsServiceFactory . DEFAULT_VOICE );
3939 }
4040
41- public TtsService getTtsService () {
42- return getTtsService (DEFAULT_VOICE );
43- }
44-
45- private TtsService getTtsService (String voiceName ) {
46- TtsService ttsService = new EdgeTtsService (voiceName , OUT_PUT_PATH );
47- return ttsService ;
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- }
59- String cacheKey = provider + ":" + configIdStr ;
60- return cacheKey ;
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" ;
45+ return provider + ":" + configIdStr ;
6146 }
6247
6348 /**
6449 * 根据配置获取TTS服务
6550 */
6651 public TtsService getTtsService (SysConfig config , String voiceName ) {
67-
68- String provider ;
6952 // 如果提供商为空,则使用默认提供商
70- if (ObjectUtils .isEmpty (config )) {
71- provider = DEFAULT_PROVIDER ;
72- } else {
73- provider = config .getProvider ();
74- }
75- String cacheKey = createCacheKey (config ,provider );
53+ var provider = ObjectUtils .isEmpty (config ) ? DEFAULT_PROVIDER : config .getProvider ();
54+ var cacheKey = createCacheKey (config , provider );
55+
7656 // 检查是否已有该配置的服务实例
7757 if (serviceCache .containsKey (cacheKey )) {
7858 return serviceCache .get (cacheKey );
79- }else {
80- // 如果是默认提供商且尚未初始化,则初始化
81- if (DEFAULT_PROVIDER .equals (provider )) {
82- if (StringUtils .hasText (voiceName )){
83- TtsService ttsService = getTtsService (voiceName );
84- serviceCache .put (cacheKey , ttsService );
85- return ttsService ;
86- }else {
87- TtsService ttsService = getTtsService ();
88- serviceCache .put (cacheKey , ttsService );
89- return ttsService ;
90- }
91- }
92- // 创建新的服务实例
93- try {
94- TtsService service ;
95- // 创建其他API服务
96- service = createApiService (config , voiceName , OUT_PUT_PATH );
97- serviceCache .put (cacheKey , service );
98- return service ;
99- } catch (Exception e ) {
100- logger .error ("创建{}服务失败" , provider , e );
101- return getDefaultTtsService (); // 失败时返回默认服务
102- }
10359 }
60+
61+ var service = createApiService (config , voiceName , OUT_PUT_PATH );
62+ serviceCache .put (cacheKey , service );
63+ return service ;
10464 }
10565
10666 /**
10767 * 根据配置创建API类型的TTS服务
10868 */
10969 private TtsService createApiService (SysConfig config , String voiceName , String outputPath ) {
110- String provider = config .getProvider ();
111-
112- // 如果是Edge,直接返回Edge服务
113- if (DEFAULT_PROVIDER .equals (provider )) {
114- return new EdgeTtsService (voiceName , outputPath );
115- } else if ("aliyun" .equals (provider )) {
116- return new AliyunTtsService (config , voiceName , outputPath );
117- } else if ("volcengine" .equals (provider )) {
118- return new VolcengineTtsService (config , voiceName , outputPath );
119- } else if ("xfyun" .equals (provider )) {
120- return new XfyunTtsService (config , voiceName , outputPath );
121- }/*
122- * else if ("tencent".equals(provider)) {
123- * return new TencentTtsService(config, voiceName, outputPath);
124- * }
125- */
126-
127- logger .warn ("不支持的TTS服务提供商: {}" , provider );
128- return null ;
70+ // Make sure output dir exists
71+ ensureOutputPath (outputPath );
72+
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+ };
12980 }
13081
13182 public void removeCache (SysConfig config ) {
@@ -136,4 +87,8 @@ public void removeCache(SysConfig config) {
13687 serviceCache .remove (cacheKey );
13788 }
13889
90+ private void ensureOutputPath (String outputPath ) {
91+ File dir = new File (outputPath );
92+ if (!dir .exists ()) dir .mkdirs ();
93+ }
13994}
0 commit comments