|
|
@ -155,6 +155,7 @@ object AgentService : CoroutineScope { |
|
|
//是否保留最终结果 |
|
|
//是否保留最终结果 |
|
|
private var isKeepResult = true; |
|
|
private var isKeepResult = true; |
|
|
// 识别结果 |
|
|
// 识别结果 |
|
|
|
|
|
private var currentRecognitionResult = "" |
|
|
private var recognitionResult = "" |
|
|
private var recognitionResult = "" |
|
|
private var pendingProcessJob: Job? = null |
|
|
private var pendingProcessJob: Job? = null |
|
|
|
|
|
|
|
|
@ -633,26 +634,7 @@ object AgentService : CoroutineScope { |
|
|
return false |
|
|
return false |
|
|
} |
|
|
} |
|
|
Log.d("TAG", "开始语音识别startRecognition?") |
|
|
Log.d("TAG", "开始语音识别startRecognition?") |
|
|
stopRecognition() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// if (isRecognitionActive&&audioSourceType==AzureAsrHelper.AudioSourceType.MICROPHONE){ |
|
|
|
|
|
// Log.d(TAG, "开始语音识别stopRecognition") |
|
|
|
|
|
// // BleService.closeCodec() |
|
|
|
|
|
// stopRecognition() |
|
|
|
|
|
|
|
|
|
|
|
// } |
|
|
|
|
|
// if((isExternalActive&&audioSourceType==AzureAsrHelper.AudioSourceType.MICROPHONE)||!isRecognitionActive) |
|
|
|
|
|
// { |
|
|
|
|
|
// Log.d(TAG, "开始语音识别BleService.openEncoder") |
|
|
|
|
|
// BleService.openEncoder() |
|
|
|
|
|
|
|
|
|
|
|
// } |
|
|
|
|
|
// if(isRecognitionActive) |
|
|
|
|
|
// { |
|
|
|
|
|
// Log.d(TAG, "开始语音识别azureAsrHelper?.startContinuousRecognition(audioSourceType)") |
|
|
|
|
|
// return true |
|
|
|
|
|
// } |
|
|
|
|
|
// 检查录音权限 |
|
|
// 检查录音权限 |
|
|
if (!isExternalActive && !checkRecordAudioPermission()) { |
|
|
if (!isExternalActive && !checkRecordAudioPermission()) { |
|
|
Log.e(TAG, "需要录音权限") |
|
|
Log.e(TAG, "需要录音权限") |
|
|
@ -712,7 +694,9 @@ object AgentService : CoroutineScope { |
|
|
if(currsessionAsrId.isEmpty()) { |
|
|
if(currsessionAsrId.isEmpty()) { |
|
|
currsessionAsrId = sessionid |
|
|
currsessionAsrId = sessionid |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
var text = recognitionResult + recognizing |
|
|
var text = recognitionResult + recognizing |
|
|
|
|
|
currentRecognitionResult = text |
|
|
sendEvent( |
|
|
sendEvent( |
|
|
"recognizing", mapOf( |
|
|
"recognizing", mapOf( |
|
|
"sessionid" to currsessionAsrId, |
|
|
"sessionid" to currsessionAsrId, |
|
|
@ -755,11 +739,19 @@ object AgentService : CoroutineScope { |
|
|
if (currsessionAsrId.isEmpty()) { |
|
|
if (currsessionAsrId.isEmpty()) { |
|
|
currsessionAsrId = sessionid |
|
|
currsessionAsrId = sessionid |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
var text = result |
|
|
var text = result |
|
|
if(recognitionResult.isEmpty()) { |
|
|
if(text.isNotEmpty()) |
|
|
|
|
|
{ |
|
|
text = text.replace(Regex("^[,。?、!, . ? !]+"), "") |
|
|
text = text.replace(Regex("^[,。?、!, . ? !]+"), "") |
|
|
|
|
|
recognitionResult += text |
|
|
|
|
|
} |
|
|
|
|
|
else{ |
|
|
|
|
|
recognitionResult= currentRecognitionResult |
|
|
|
|
|
currentRecognitionResult= "" |
|
|
} |
|
|
} |
|
|
recognitionResult += text |
|
|
if (recognitionResult.isNotEmpty()) |
|
|
|
|
|
{ |
|
|
if (currentRecognitionMode == "push_to_talk") { //按住说话 |
|
|
if (currentRecognitionMode == "push_to_talk") { //按住说话 |
|
|
sendEvent( |
|
|
sendEvent( |
|
|
"recognizing", mapOf( |
|
|
"recognizing", mapOf( |
|
|
@ -779,21 +771,26 @@ object AgentService : CoroutineScope { |
|
|
) |
|
|
) |
|
|
pendingProcessJob = launch { |
|
|
pendingProcessJob = launch { |
|
|
delay(1500) |
|
|
delay(1500) |
|
|
if (recognitionResult.isNotEmpty()) { |
|
|
sendEvent( |
|
|
sendEvent( |
|
|
|
|
|
"recognition_result", mapOf( |
|
|
"recognition_result", mapOf( |
|
|
"sessionid" to currsessionAsrId, |
|
|
"sessionid" to currsessionAsrId, |
|
|
"text" to recognitionResult, |
|
|
"text" to recognitionResult, |
|
|
"language" to detectedLanguage |
|
|
"language" to detectedLanguage |
|
|
) |
|
|
) |
|
|
) |
|
|
) |
|
|
processWithChatApi(currsessionAsrId, recognitionResult) |
|
|
recognitionResult = "" |
|
|
recognitionResult = "" |
|
|
|
|
|
currsessionAsrId = "" |
|
|
currsessionAsrId = "" |
|
|
} |
|
|
|
|
|
|
|
|
processWithChatApi(currsessionAsrId, recognitionResult) |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
else |
|
|
|
|
|
{ |
|
|
|
|
|
Log.d(TAG, "liwei-------- onResult: $sessionid $result $currentRecognitionMode 空字符串") |
|
|
|
|
|
} |
|
|
|
|
|
// } |
|
|
|
|
|
|
|
|
_hasSpeechDetected.set(false) |
|
|
_hasSpeechDetected.set(false) |
|
|
restartIdleCheck() |
|
|
restartIdleCheck() |
|
|
@ -805,6 +802,7 @@ object AgentService : CoroutineScope { |
|
|
sendEvent("recognition_started", mapOf("sessionid" to sessionid, "status" to "started")) |
|
|
sendEvent("recognition_started", mapOf("sessionid" to sessionid, "status" to "started")) |
|
|
// 使用模式感知的空闲检测启动方法 |
|
|
// 使用模式感知的空闲检测启动方法 |
|
|
recognitionResult = "" |
|
|
recognitionResult = "" |
|
|
|
|
|
currentRecognitionResult = "" |
|
|
currsessionAsrId = "" |
|
|
currsessionAsrId = "" |
|
|
pendingProcessJob?.cancel() |
|
|
pendingProcessJob?.cancel() |
|
|
audioPlayer?.playAudio(R.raw.start) |
|
|
audioPlayer?.playAudio(R.raw.start) |
|
|
@ -815,6 +813,7 @@ object AgentService : CoroutineScope { |
|
|
override fun onSessionStopped(sessionid:String) { |
|
|
override fun onSessionStopped(sessionid:String) { |
|
|
Log.d(TAG, "liwei-------- onSessionStopped: $sessionid $currsessionAsrId $recognitionResult $currentRecognitionMode") |
|
|
Log.d(TAG, "liwei-------- onSessionStopped: $sessionid $currsessionAsrId $recognitionResult $currentRecognitionMode") |
|
|
if (currentRecognitionMode == "push_to_talk") { //按住说话 |
|
|
if (currentRecognitionMode == "push_to_talk") { //按住说话 |
|
|
|
|
|
|
|
|
if (recognitionResult.isNotEmpty()){ |
|
|
if (recognitionResult.isNotEmpty()){ |
|
|
sendEvent( |
|
|
sendEvent( |
|
|
"recognition_result", mapOf( |
|
|
"recognition_result", mapOf( |
|
|
@ -824,6 +823,9 @@ object AgentService : CoroutineScope { |
|
|
) |
|
|
) |
|
|
processWithChatApi(currsessionAsrId, recognitionResult) |
|
|
processWithChatApi(currsessionAsrId, recognitionResult) |
|
|
} |
|
|
} |
|
|
|
|
|
else{ |
|
|
|
|
|
Log.d(TAG, "liwei-------- onSessionStopped: $sessionid $currsessionAsrId $recognitionResult $currentRecognitionMode 空字符串") |
|
|
|
|
|
} |
|
|
sendEvent("recognition_stopped", mapOf("sessionid" to currsessionAsrId, "status" to "stopped")) |
|
|
sendEvent("recognition_stopped", mapOf("sessionid" to currsessionAsrId, "status" to "stopped")) |
|
|
}else{ |
|
|
}else{ |
|
|
sendEvent("recognition_stopped", mapOf("sessionid" to sessionid, "status" to "stopped")) |
|
|
sendEvent("recognition_stopped", mapOf("sessionid" to sessionid, "status" to "stopped")) |
|
|
@ -908,7 +910,7 @@ object AgentService : CoroutineScope { |
|
|
fun interruptCurrentResponse() { |
|
|
fun interruptCurrentResponse() { |
|
|
if (isAiStreaming || isTtsSpeaking) { |
|
|
if (isAiStreaming || isTtsSpeaking) { |
|
|
val startTime = System.currentTimeMillis() |
|
|
val startTime = System.currentTimeMillis() |
|
|
|
|
|
stopRecognition() |
|
|
// 并行执行停止操作,加快响应速度 |
|
|
// 并行执行停止操作,加快响应速度 |
|
|
stopTts() |
|
|
stopTts() |
|
|
stopAiStream() |
|
|
stopAiStream() |
|
|
|