|
|
|
@ -106,13 +106,16 @@ class TranslationController extends GetxController { |
|
|
|
_asrService.setContinue(); |
|
|
|
lastActiveSpeaker.value = id; |
|
|
|
} else { |
|
|
|
_asrService.setPause(); |
|
|
|
if (fTFTranslationResult != '' && !isPlayback) { |
|
|
|
isPlayback = true; |
|
|
|
await _ttsService.setAudioOutputDevice(lastActiveSpeaker.value); |
|
|
|
await Future.delayed(const Duration(milliseconds: 1300)); |
|
|
|
await playTranslatedText(fTFTranslationResult!); |
|
|
|
fTFTranslationResult = ''; |
|
|
|
} else if ((translationHistory.isEmpty || |
|
|
|
!translationHistory.last.isIntermediate) && |
|
|
|
currentMode.value == 'faceToFace') { |
|
|
|
_asrService.setPause(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
@ -165,7 +168,7 @@ class TranslationController extends GetxController { |
|
|
|
} |
|
|
|
|
|
|
|
// 加载历史记录 |
|
|
|
//loadTranslationHistory(); |
|
|
|
loadTranslationHistory(); |
|
|
|
|
|
|
|
// 初始化服务 |
|
|
|
_initServices(); |
|
|
|
@ -462,6 +465,7 @@ class TranslationController extends GetxController { |
|
|
|
|
|
|
|
if (translationHistory.isEmpty || !translationHistory.last.isIntermediate) { |
|
|
|
currentSessionId ??= DateTime.now().millisecondsSinceEpoch.toString(); |
|
|
|
|
|
|
|
final newItem = TranslationItem( |
|
|
|
sourceText: text, |
|
|
|
translatedText: '', |
|
|
|
@ -474,10 +478,15 @@ class TranslationController extends GetxController { |
|
|
|
isIntermediate: true, |
|
|
|
); |
|
|
|
// 添加新项后滚动到底部 |
|
|
|
translationHistory.add(newItem); |
|
|
|
if (currentMode.value == 'faceToFace' && activeSpeaker.value == 0) { |
|
|
|
_asrService.setPause(); |
|
|
|
} else { |
|
|
|
translationHistory.add(newItem); |
|
|
|
} |
|
|
|
_scrollToBottom(); |
|
|
|
} else { |
|
|
|
// 更新当前项 |
|
|
|
|
|
|
|
translationHistory.last.sourceText = text; |
|
|
|
translationHistory.refresh(); |
|
|
|
// 更新后滚动到底部 |
|
|
|
@ -509,7 +518,9 @@ class TranslationController extends GetxController { |
|
|
|
// 更新后滚动到底部 |
|
|
|
_scrollToBottom(); |
|
|
|
saveTranslationHistory(); |
|
|
|
|
|
|
|
if (activeSpeaker.value == 0 && currentMode.value == 'faceToFace') { |
|
|
|
_asrService.setPause(); |
|
|
|
} |
|
|
|
// 使用项目的时间戳进行翻译 |
|
|
|
translateText(text, translationHistory.last.timestamp, isFinal: true); |
|
|
|
} else { |
|
|
|
@ -526,14 +537,16 @@ class TranslationController extends GetxController { |
|
|
|
translationHistory.last.sessionId != currentSessionId, |
|
|
|
isIntermediate: false, |
|
|
|
); |
|
|
|
|
|
|
|
translationHistory.add(newItem); |
|
|
|
translationHistory.refresh(); |
|
|
|
_scrollToBottom(); |
|
|
|
saveTranslationHistory(); |
|
|
|
|
|
|
|
// 使用新项目的时间戳进行翻译 |
|
|
|
translateText(text, newItem.timestamp, isFinal: true); |
|
|
|
if (currentMode.value == 'faceToFace' && activeSpeaker.value == 0) { |
|
|
|
_asrService.setPause(); |
|
|
|
} else { |
|
|
|
translationHistory.add(newItem); |
|
|
|
translationHistory.refresh(); |
|
|
|
_scrollToBottom(); |
|
|
|
saveTranslationHistory(); |
|
|
|
// 使用新项目的时间戳进行翻译 |
|
|
|
translateText(text, newItem.timestamp, isFinal: true); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@ -546,13 +559,33 @@ class TranslationController extends GetxController { |
|
|
|
String? translationResult; |
|
|
|
|
|
|
|
try { |
|
|
|
// 根据检测到的语言确定源语言和目标语言 |
|
|
|
final bool shouldSwap = detectedLanguageCode == sourceLanguageCode.value; |
|
|
|
final detectedSourceLanguageCode = |
|
|
|
shouldSwap ? sourceLanguageCode.value : targetLanguageCode.value; |
|
|
|
final detectedTargetLanguageCode = |
|
|
|
shouldSwap ? targetLanguageCode.value : sourceLanguageCode.value; |
|
|
|
|
|
|
|
var detectedSourceLanguageCode; |
|
|
|
var detectedTargetLanguageCode; |
|
|
|
if ((currentMode.value == 'faceToFace' && activeSpeaker.value == 1) || |
|
|
|
(currentMode.value == 'faceToFace' && |
|
|
|
activeSpeaker.value == 0 && |
|
|
|
lastActiveSpeaker.value == 1)) { |
|
|
|
detectedLanguageCode = sourceLanguageCode.value; |
|
|
|
detectedSourceLanguageCode = sourceLanguageCode.value; |
|
|
|
detectedTargetLanguageCode = targetLanguageCode.value; |
|
|
|
} else if ((currentMode.value == 'faceToFace' && |
|
|
|
activeSpeaker.value == 2) || |
|
|
|
(currentMode.value == 'faceToFace' && |
|
|
|
activeSpeaker.value == 0 && |
|
|
|
lastActiveSpeaker.value == 2)) { |
|
|
|
// 根据检测到的语言确定源语言和目标语言 |
|
|
|
detectedLanguageCode = targetLanguageCode.value; |
|
|
|
detectedSourceLanguageCode = targetLanguageCode.value; |
|
|
|
detectedTargetLanguageCode = sourceLanguageCode.value; |
|
|
|
} else { |
|
|
|
// 根据检测到的语言确定源语言和目标语言 |
|
|
|
final bool shouldSwap = |
|
|
|
detectedLanguageCode == sourceLanguageCode.value; |
|
|
|
detectedSourceLanguageCode = |
|
|
|
shouldSwap ? sourceLanguageCode.value : targetLanguageCode.value; |
|
|
|
detectedTargetLanguageCode = |
|
|
|
shouldSwap ? targetLanguageCode.value : sourceLanguageCode.value; |
|
|
|
} |
|
|
|
// 调用翻译服务 |
|
|
|
translationResult = await _translationService.translateText( |
|
|
|
text: sourceText, |
|
|
|
@ -560,6 +593,7 @@ class TranslationController extends GetxController { |
|
|
|
targetLanguageCode: detectedTargetLanguageCode, |
|
|
|
); |
|
|
|
|
|
|
|
/// } |
|
|
|
if (translationResult != null) { |
|
|
|
// 查找匹配的项目 - 优先使用timestamp查找 |
|
|
|
int index = -1; |
|
|
|
@ -577,14 +611,16 @@ class TranslationController extends GetxController { |
|
|
|
saveTranslationHistory(); |
|
|
|
// 更新后滚动到底部 |
|
|
|
_scrollToBottom(); |
|
|
|
if (isFinal && currentMode.value == 'faceToFace') { |
|
|
|
print("音频输出文本2$translationResult"); |
|
|
|
// 播放TTS - 只在最终结果时播放 |
|
|
|
if (isTtsEnabled.value && |
|
|
|
isFinal && |
|
|
|
currentMode.value != 'faceToFace') { |
|
|
|
await playTranslatedText(translationResult); |
|
|
|
} else { |
|
|
|
fTFTranslationResult = |
|
|
|
(fTFTranslationResult ?? '') + translationResult; |
|
|
|
|
|
|
|
if (activeSpeaker.value == 0 && |
|
|
|
!isPlayback && |
|
|
|
currentMode.value == 'faceToFace') { |
|
|
|
if (activeSpeaker.value == 0 && !isPlayback) { |
|
|
|
isPlayback = true; |
|
|
|
await _ttsService.setAudioOutputDevice(lastActiveSpeaker.value); |
|
|
|
await Future.delayed(const Duration(milliseconds: 1300)); |
|
|
|
@ -592,12 +628,6 @@ class TranslationController extends GetxController { |
|
|
|
fTFTranslationResult = ''; |
|
|
|
} |
|
|
|
} |
|
|
|
// 播放TTS - 只在最终结果时播放 |
|
|
|
if (isTtsEnabled.value && |
|
|
|
isFinal && |
|
|
|
currentMode.value != 'faceToFace') { |
|
|
|
await playTranslatedText(translationResult); |
|
|
|
} |
|
|
|
} else { |
|
|
|
Logger.info( |
|
|
|
'未找到匹配的历史项 ${sourceText} ${timestamp.toIso8601String() ?? "无时间戳"}'); |
|
|
|
@ -623,7 +653,6 @@ class TranslationController extends GetxController { |
|
|
|
final voiceName = _languageManager |
|
|
|
.getTtsVoiceNameByAsrCode(detectedTargetLanguageCode) ?? |
|
|
|
'en-US-AriaNeural'; |
|
|
|
|
|
|
|
await _ttsService.setVoice(voiceName); |
|
|
|
await _ttsService.speakOnce(text); |
|
|
|
} catch (e) { |
|
|
|
|