Skip to content

feat: support minimax t2a #96

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 11, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
fix(tts): tts service factory
  • Loading branch information
vritser authored and vritser committed Jun 10, 2025
commit 2321e96da32f63b258fb4b10542356a855f909e2
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ public void handleUnboundDevice(String sessionId, SysDevice device) {
if (device.getDeviceName() != null && device.getModelId() == null) {
String message = "设备未配置对话模型,请到配置页面完成配置后开始对话";

String audioFilePath = ttsService.getTtsService().textToSpeech(message);
String audioFilePath = ttsService.getDefaultTtsService().textToSpeech(message);
audioService.sendAudioMessage(chatSession, new DialogueService.Sentence(message, audioFilePath), true,
true);

Expand All @@ -230,7 +230,7 @@ public void handleUnboundDevice(String sessionId, SysDevice device) {
String audioFilePath;
if (!StringUtils.hasText(codeResult.getAudioPath())) {
String codeMessage = "请到设备管理页面添加设备,输入验证码" + codeResult.getCode();
audioFilePath = ttsService.getTtsService().textToSpeech(codeMessage);
audioFilePath = ttsService.getDefaultTtsService().textToSpeech(codeMessage);
codeResult.setDeviceId(deviceId);
codeResult.setSessionId(sessionId);
codeResult.setAudioPath(audioFilePath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.io.File;
import java.util.Map;
Expand All @@ -35,71 +34,33 @@ public class TtsServiceFactory {
* 获取默认TTS服务
*/
public TtsService getDefaultTtsService() {
// 如果缓存中没有默认服务,则创建一个
return getTtsService(DEFAULT_VOICE);
var config = new SysConfig().setProvider(DEFAULT_PROVIDER);
return getTtsService(config, TtsServiceFactory.DEFAULT_VOICE);
}

public TtsService getTtsService() {
return getTtsService(DEFAULT_VOICE);
}

private TtsService getTtsService(String voiceName) {
return getTtsService(null, voiceName);
}

private String createCacheKey(SysConfig config,String provider){
// 对于API服务,使用"provider:configId"作为缓存键,确保每个配置使用独立的服务实例
String configIdStr;
if (config == null) {
configIdStr = "default";
}else{
Integer configId = config.getConfigId();
configIdStr = configId != null ? String.valueOf(configId) : "default";
}
// 对于API服务,使用"provider:configId"作为缓存键,确保每个配置使用独立的服务实例
private String createCacheKey(SysConfig config, String provider) {
var configId = config.getConfigId();
var configIdStr = configId != null ? String.valueOf(configId) : "default";
return provider + ":" + configIdStr;
}

/**
* 根据配置获取TTS服务
*/
public TtsService getTtsService(SysConfig config, String voiceName) {

String provider;
// 如果提供商为空,则使用默认提供商
if (ObjectUtils.isEmpty(config)) {
provider = DEFAULT_PROVIDER;
} else {
provider = config.getProvider();
}
String cacheKey = createCacheKey(config,provider);
var provider = ObjectUtils.isEmpty(config) ? DEFAULT_PROVIDER : config.getProvider();
var cacheKey = createCacheKey(config, provider);

// 检查是否已有该配置的服务实例
if (serviceCache.containsKey(cacheKey)) {
return serviceCache.get(cacheKey);
}else{
// 如果是默认提供商且尚未初始化,则初始化
if (DEFAULT_PROVIDER.equals(provider)) {
if(StringUtils.hasText(voiceName)){
TtsService ttsService = getTtsService(voiceName);
serviceCache.put(cacheKey, ttsService);
return ttsService;
}else{
TtsService ttsService = getTtsService();
serviceCache.put(cacheKey, ttsService);
return ttsService;
}
}
// 创建新的服务实例
try {
TtsService service;
// 创建其他API服务
service = createApiService(config, voiceName, OUT_PUT_PATH);
serviceCache.put(cacheKey, service);
return service;
} catch (Exception e) {
logger.error("创建{}服务失败", provider, e);
return getDefaultTtsService(); // 失败时返回默认服务
}
}

var service = createApiService(config, voiceName, OUT_PUT_PATH);
serviceCache.put(cacheKey, service);
return service;
}

/**
Expand All @@ -109,22 +70,13 @@ private TtsService createApiService(SysConfig config, String voiceName, String o
// Make sure output dir exists
ensureOutputPath(outputPath);

var provider = config.getProvider();
// 如果是Edge,直接返回Edge服务
if (DEFAULT_PROVIDER.equals(provider)) {
return new EdgeTtsService(voiceName, outputPath);
} else if ("aliyun".equals(provider)) {
return new AliyunTtsService(config, voiceName, outputPath);
} else if ("volcengine".equals(provider)) {
return new VolcengineTtsService(config, voiceName, outputPath);
} else if ("xfyun".equals(provider)) {
return new XfyunTtsService(config, voiceName, outputPath);
} else if ("minimax".equals(provider)) {
return new MiniMaxTtsService(config, voiceName, outputPath);
}

logger.warn("不支持的TTS服务提供商: {}", provider);
return null;
return switch (config.getProvider()) {
case "aliyun" -> new AliyunTtsService(config, voiceName, outputPath);
case "volcengine" -> new VolcengineTtsService(config, voiceName, outputPath);
case "xfyun" -> new XfyunTtsService(config, voiceName, outputPath);
case "minimax" -> new MiniMaxTtsService(config, voiceName, outputPath);
default -> new EdgeTtsService(voiceName, outputPath);
};
}

public void removeCache(SysConfig config) {
Expand Down