Skip to content

Commit

Permalink
fix erroe
Browse files Browse the repository at this point in the history
  • Loading branch information
xqsadness committed Sep 22, 2023
1 parent 8404c62 commit c97dc0b
Show file tree
Hide file tree
Showing 10 changed files with 172 additions and 44 deletions.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,6 @@
type = "0"
version = "2.0">
<Breakpoints>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
uuid = "64F113F6-3F90-410F-A6BE-5380FC5206DE"
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "DefaultProject/DEFAULT_SOURCE/CONTROLL_APP/CONSTANT.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "370"
endingLineNumber = "370"
landmarkName = "parseDataCombine(_:completions:)"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
Expand Down
11 changes: 11 additions & 0 deletions DefaultProject/DEFAULT_SOURCE/CONTROLL_APP/CONSTANT.swift
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,17 @@ struct QUIZ: Codable {
var img: String = ""
}

struct QUIZ_ARRAY: Codable {
var id: String = ""
var answer: [String] = []
var question: String = ""
var a: String = ""
var b: String = ""
var c: String = ""
var d: String = ""
var img: String = ""
}

struct QuizDataCombine: Codable {
var listQuestionsMath: [QUIZ]
var listQuestionsColor: [QUIZ]
Expand Down
2 changes: 2 additions & 0 deletions DefaultProject/View/History/HistoryView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ struct HistoryView: View {
VStack{
HStack{
Button {
synthesizer.stopSpeaking(at: .immediate)
coordinator.pop()
} label: {
Image(systemName: "chevron.left")
Expand Down Expand Up @@ -82,6 +83,7 @@ struct HistoryView: View {
}
.onDisappear{
QuizTimer.shared.reset()
synthesizer.stopSpeaking(at: .immediate)
}
}

Expand Down
2 changes: 2 additions & 0 deletions DefaultProject/View/Listen/ListeningView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ struct ListeningView: View {
VStack{
HStack{
Button {
synthesizer.stopSpeaking(at: .immediate)
coordinator.pop()
} label: {
Image(systemName: "chevron.left")
Expand Down Expand Up @@ -81,6 +82,7 @@ struct ListeningView: View {
}
.onDisappear{
QuizTimer.shared.reset()
synthesizer.stopSpeaking(at: .immediate)
}
}

Expand Down
15 changes: 11 additions & 4 deletions DefaultProject/View/ListenEndRepeat/ListenAndRPContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ struct ListenAndRPContentView: View {
@StateObject var speechRecognizer: SpeechRecognizer
@State var synthesizer: AVSpeechSynthesizer
@State var index: Int
@Binding var isSpeaking: Bool
// @Binding var isSpeaking: Bool
@Binding var isHide: Bool
@Binding var countCorrect: Int
@Binding var countWrong: Int
@Binding var selectedTab: Int

var handleTapToSpeak: (() -> Void)
var handleTapToSpeakOnchange: (() -> Void)

var body: some View {
VStack (spacing: 0) {
Expand Down Expand Up @@ -93,7 +94,7 @@ struct ListenAndRPContentView: View {
.simultaneousGesture(DragGesture())

HStack{
if isSpeaking {
if speechRecognizer.isSpeaking {
LottieView(name: "animation_soundwave", loopMode: .loop)
.frame(height: 57)
} else {
Expand All @@ -111,7 +112,7 @@ struct ListenAndRPContentView: View {
}
}
}
.padding(isSpeaking ? 0 : 15)
.padding(speechRecognizer.isSpeaking ? 0 : 15)
.frame(maxWidth: .infinity)
.overlay{
RoundedRectangle(cornerRadius: 10)
Expand All @@ -125,6 +126,12 @@ struct ListenAndRPContentView: View {
handleTapToSpeak()
}

.onChange(of: speechRecognizer.isSpeaking) { newValue in
if !newValue{
handleTapToSpeakOnchange()
}
}

if speechRecognizer.transcript.isEmpty{
HStack(spacing: 10){
ForEach(0..<3){ _ in
Expand Down Expand Up @@ -152,7 +159,7 @@ struct ListenAndRPContentView: View {
}

func speakText(textToSpeak: String) {
isSpeaking = false
speechRecognizer.isSpeaking = false
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSession.Category.playAndRecord, mode: .default, options: .defaultToSpeaker)
try AVAudioSession.sharedInstance().setActive(true, options: .notifyOthersOnDeactivation)
Expand Down
61 changes: 52 additions & 9 deletions DefaultProject/View/ListenEndRepeat/ListenAndRepeatView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,15 @@ struct ListenAndRepeatView: View {
@State private var isFail = false
@State private var isShowPopup = false
@State private var isHide: Bool = true
@State private var isSpeaking: Bool = false
// @State private var isSpeaking: Bool = false
@State private var checkPermission: Bool = false

var body: some View {
ZStack(alignment: .top){
VStack{
VStack{
HStack{
Button {
synthesizer.stopSpeaking(at: .immediate)
coordinator.pop()
} label: {
Image(systemName: "chevron.left")
Expand All @@ -51,8 +52,10 @@ struct ListenAndRepeatView: View {
VStack{
TabView(selection: $selectedTab) {
ForEach(CONSTANT.SHARED.DATA_LISTEN_AND_REPEAT.indices, id: \.self) { index in
ListenAndRPContentView(speechRecognizer: speechRecognizer,synthesizer: synthesizer,index: index ,isSpeaking: $isSpeaking, isHide: $isHide, countCorrect: $countCorrect, countWrong: $countWrong, selectedTab: $selectedTab){
ListenAndRPContentView(speechRecognizer: speechRecognizer,synthesizer: synthesizer,index: index, isHide: $isHide, countCorrect: $countCorrect, countWrong: $countWrong, selectedTab: $selectedTab){
handleTapToSpeak(answer: CONSTANT.SHARED.DATA_LISTEN_AND_REPEAT[index].answer.cw_localized)
} handleTapToSpeakOnchange: {
handleTapToSpeakOnchange(answer: CONSTANT.SHARED.DATA_LISTEN_AND_REPEAT[index].answer.cw_localized)
}
.tag(index)
.contentShape(Rectangle()).gesture(DragGesture())
Expand All @@ -65,6 +68,14 @@ struct ListenAndRepeatView: View {
synthesizer.stopSpeaking(at: .immediate)
}
})

// DispatchQueue.main.asyncAfter(deadline: .now() + 0.8 ,execute: {
// if !synthesizer.isSpeaking{
// speakText(textToSpeak: CONSTANT.SHARED.DATA_LISTEN_AND_REPEAT[index].answer.cw_localized)
// }else{
// synthesizer.stopSpeaking(at: .immediate)
// }
// })
}
}
}
Expand Down Expand Up @@ -155,7 +166,7 @@ struct ListenAndRepeatView: View {
}
.overlay{
if checkPermission{
PermissionsNoticeView(message: "Please allow access speech and voice")
PermissionsNoticeView(message: "Please allow access speech and voice")
.environmentObject(coordinator)
}
}
Expand All @@ -176,18 +187,19 @@ struct ListenAndRepeatView: View {
}
.onDisappear{
QuizTimer.shared.reset()
synthesizer.stopSpeaking(at: .immediate)
}
}

func handleTapToSpeak(answer: String){
if selectedTab < CONSTANT.SHARED.DATA_LISTEN_AND_REPEAT.count - 1 || !(countWrong + countCorrect == CONSTANT.SHARED.DATA_LISTEN_AND_REPEAT.count){
audioPlayer?.pause()
if !isSpeaking {
isSpeaking = true
if !speechRecognizer.isSpeaking {
speechRecognizer.isSpeaking = true
synthesizer.stopSpeaking(at: .immediate)
speechRecognizer.transcribe()
} else {
isSpeaking = false
speechRecognizer.isSpeaking = false
speechRecognizer.stopTranscribing()
if speechRecognizer.transcript.lowercased().cw_localized == answer.lowercased(){
loadAudio(nameSound: "correct")
Expand Down Expand Up @@ -217,6 +229,37 @@ struct ListenAndRepeatView: View {
speechRecognizer.transcript = ""
}

func handleTapToSpeakOnchange(answer: String){
if selectedTab < CONSTANT.SHARED.DATA_LISTEN_AND_REPEAT.count - 1 || !(countWrong + countCorrect == CONSTANT.SHARED.DATA_LISTEN_AND_REPEAT.count){
audioPlayer?.pause()
speechRecognizer.stopTranscribing()
if speechRecognizer.transcript.lowercased().cw_localized == answer.cw_localized.lowercased(){
loadAudio(nameSound: "correct")
DispatchQueue.main.async {
isCorrect = true
}
countCorrect += 1
}else{
loadAudio(nameSound: "wrong")
if countFail < 2{
titleButon = "Again"
}else{
titleButon = "Understand"
}
DispatchQueue.main.async {
isFail = true
}
}
}else if selectedTab == CONSTANT.SHARED.DATA_LISTEN_AND_REPEAT.count - 1 && (countWrong + countCorrect == CONSTANT.SHARED.DATA_LISTEN_AND_REPEAT.count){
loadAudio(nameSound: "congralutions")
withAnimation {
isShowPopup = true
}
audioPlayer?.pause()
}
speechRecognizer.transcript = ""
}

func speakText(textToSpeak: String) {
let utterance = AVSpeechUtterance(string: textToSpeak)
utterance.voice = AVSpeechSynthesisVoice(language: language)
Expand All @@ -227,7 +270,7 @@ struct ListenAndRepeatView: View {
func resetSpeak(){
speechRecognizer.transcript = ""
speechRecognizer.reset()
isSpeaking = false
speechRecognizer.isSpeaking = false
}

func loadAudio(nameSound: String) {
Expand Down
2 changes: 2 additions & 0 deletions DefaultProject/View/Writing/WritingView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ struct WritingView: View {
VStack{
HStack{
Button {
synthesizer.stopSpeaking(at: .immediate)
coordinator.pop()
} label: {
Image(systemName: "chevron.left")
Expand Down Expand Up @@ -103,6 +104,7 @@ struct WritingView: View {
}
.onDisappear{
QuizTimer.shared.reset()
synthesizer.stopSpeaking(at: .immediate)
}
if isShowPopupCheck{
QuestionResultWritingView(isCorrect: $isCorrect, isShowPopupCheck: $isShowPopupCheck, answer: $answer) {
Expand Down
40 changes: 26 additions & 14 deletions DefaultProject/ViewModel/SpeechRecognizer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,15 @@ class SpeechRecognizer: ObservableObject {
}

@Published var transcript: String = ""
@Published var isSpeaking: Bool = false

private var audioEngine: AVAudioEngine?
private var request: SFSpeechAudioBufferRecognitionRequest?
private var task: SFSpeechRecognitionTask?
private var recognizer: SFSpeechRecognizer? = nil
private var isFinal = false
private var timer: Timer?


init() {
recognizer = SFSpeechRecognizer(locale: Locale(identifier: language))
Expand Down Expand Up @@ -66,6 +70,9 @@ class SpeechRecognizer: ObservableObject {
audioEngine = nil
request = nil
task = nil
withAnimation {
isSpeaking = false
}
}

/**
Expand Down Expand Up @@ -96,21 +103,16 @@ class SpeechRecognizer: ObservableObject {
}

if let result = result {
// self.speak(result.bestTranscription.formattedString)

var bestTranscription: SFTranscription?

for transcription in result.transcriptions {
//find the largest confidence value and confidence > 0.5
if let bestSegment = transcription.segments.max(by: { $0.confidence < $1.confidence }), bestSegment.confidence > 0.5 {
bestTranscription = transcription
}
}

if let bestTranscription = bestTranscription {
self.speak(bestTranscription.formattedString)
}
self.speak(result.bestTranscription.formattedString)
self.isFinal = result.isFinal
}

if self.isFinal {
self.stopTranscribing()
} else if error == nil {
self.restartSpeechTimer()
}

}
} catch {
self.reset()
Expand All @@ -119,6 +121,16 @@ class SpeechRecognizer: ObservableObject {
}
}

func restartSpeechTimer() {
timer?.invalidate()
timer = Timer.scheduledTimer(withTimeInterval: 1.85, repeats: false, block: { (timer) in
// Do whatever needs to be done when the timer expires
print("timeout")

self.stopTranscribing()
})
}

private static func prepareEngine() throws -> (AVAudioEngine, SFSpeechAudioBufferRecognitionRequest) {
let audioEngine = AVAudioEngine()

Expand Down
Loading

0 comments on commit c97dc0b

Please sign in to comment.