Skip to content

Instantly share code, notes, and snippets.

@RobertApikyan
Created July 12, 2022 11:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RobertApikyan/ced45a062c94f9bf30aeebdd99ec2e66 to your computer and use it in GitHub Desktop.
Save RobertApikyan/ced45a062c94f9bf30aeebdd99ec2e66 to your computer and use it in GitHub Desktop.
package com.hfh.wellbe.smartwatch.managers.impl
import android.annotation.SuppressLint
import com.hfh.wellbe.smartwatch.api.apiClient.listeners.ErrorListener
import com.hfh.wellbe.smartwatch.api.apiClient.listeners.ProgressListener
import com.hfh.wellbe.smartwatch.api.apiClient.retrofit.ApiServices
import com.hfh.wellbe.smartwatch.enums.AssistantActionControlType
import com.hfh.wellbe.smartwatch.managers.AssistantManager
import com.hfh.wellbe.smartwatch.models.AssistantTextRequest
import com.hfh.wellbe.smartwatch.models.AssistantTextResponse
import com.hfh.wellbe.smartwatch.utils.ResultSrc
import com.microsoft.cognitiveservices.speech.SpeechConfig
import com.microsoft.cognitiveservices.speech.SpeechRecognizer
import com.microsoft.cognitiveservices.speech.SpeechSynthesizer
import com.microsoft.cognitiveservices.speech.audio.AudioConfig
class AssistantManagerImpl(private val configs: SpeechConfig) : AssistantManager, ApiManager() {
override val localCloseCommands = hashSetOf("cancel","stop","shut up","please stop","turn off","off")
override val localRepeatCommands = hashSetOf("repeat","repeat that","what did you say?")
override val localCommands: Set<String> = localCloseCommands + localRepeatCommands
override val noneCommands: Set<String> = hashSetOf("none")
override val cancelCommands: Set<String> = hashSetOf("cancel")
override val stopCommands: Set<String> = hashSetOf("stop")
override val previousCommands: Set<String> = hashSetOf("previous", "go back")
override val nextCommands: Set<String> = hashSetOf("next", "skip")
override val repeatCommands: Set<String> = hashSetOf("repeat")
override val removeCommands: Set<String> = hashSetOf("remove", "delete")
override val allCommands: Set<String> = noneCommands + cancelCommands +
stopCommands + previousCommands + nextCommands + repeatCommands + removeCommands
override val allCommandsMapping: Map<Set<String>, AssistantActionControlType> =
hashMapOf(
noneCommands to AssistantActionControlType.None,
cancelCommands to AssistantActionControlType.Cancel,
stopCommands to AssistantActionControlType.Stop,
previousCommands to AssistantActionControlType.Previous,
nextCommands to AssistantActionControlType.Next,
repeatCommands to AssistantActionControlType.Repeat,
removeCommands to AssistantActionControlType.Remove
)
private lateinit var speechRecognizer: SpeechRecognizer
private lateinit var speechSynthesizer: SpeechSynthesizer
@SuppressLint("DefaultLocale")
override fun getActionControlTypeByCommand(command: String):AssistantActionControlType {
for (entry in allCommandsMapping.entries) {
if (entry.key.contains(command.toLowerCase())){
return entry.value
}
}
return AssistantActionControlType.None
}
override fun createSpeechRecognizer(): SpeechRecognizer {
if (!::speechRecognizer.isInitialized) {
speechRecognizer = SpeechRecognizer(configs)
}
return speechRecognizer
}
override fun createSpeechSynthesizer(): SpeechSynthesizer {
if (!::speechSynthesizer.isInitialized) {
speechSynthesizer = SpeechSynthesizer(
configs,
AudioConfig.fromDefaultSpeakerOutput()
)
}
return speechSynthesizer
}
override fun assistantTextRequest(
request: AssistantTextRequest,
authorized: Boolean,
progressListener: ProgressListener,
errorListener: ErrorListener
) = ResultSrc<AssistantTextResponse> {
val call = if (authorized) {
ApiServices.user.textRequestLimited(request)
} else {
ApiServices.genericUser.textRequestLimited(request)
}
apiClient.enqueue(call,progressListener,errorListener).onSuccess { response ->
setResultIfNotNull(response)
}.bindFailure(this)
}
override fun createSSML(text: String) = ("<speak version=\"1.0\" " +
"xmlns=\"https://www.w3.org/2001/10/synthesis\" " +
"xml:lang=\"en-US\">" +
"<voice name=\"en-US-JessaRUS\">"
+ text +
"</voice>" +
"</speak>")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment