-
Notifications
You must be signed in to change notification settings - Fork 67
/
ViewModel.swift
159 lines (136 loc) · 4.66 KB
/
ViewModel.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
//
// Copyright 2022-2023 Picovoice Inc.
// You may not use this file except in compliance with the license. A copy of the license is located in the "LICENSE"
// file accompanying this source.
// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
// an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License.
//
import AVFoundation
import Foundation
import Cheetah
import ios_voice_processor
enum UIState {
case INIT
case READY
case RECORDING
case FINALIZED
case ERROR
}
class ViewModel: ObservableObject {
private let ACCESS_KEY = "${YOUR_ACCESS_KEY_HERE}" // Obtained from Picovoice Console (https://console.picovoice.ai)
private var cheetah: Cheetah!
private var isListening = false
private var autoScroll = true
@Published var errorMessage = ""
@Published var state = UIState.INIT
@Published var result: String = ""
init() {
initialize()
}
public func initialize() {
state = UIState.INIT
do {
let modelPath = Bundle(for: type(of: self)).path(forResource: "cheetah_params", ofType: "pv")!
try cheetah = Cheetah(
accessKey: ACCESS_KEY,
modelPath: modelPath,
enableAutomaticPunctuation: true)
VoiceProcessor.instance.addFrameListener(VoiceProcessorFrameListener(audioCallback))
VoiceProcessor.instance.addErrorListener(VoiceProcessorErrorListener(errorCallback))
state = UIState.READY
} catch let error as CheetahInvalidArgumentError {
errorMessage = "\(error.localizedDescription)"
} catch is CheetahActivationError {
errorMessage = "ACCESS_KEY activation error"
} catch is CheetahActivationRefusedError {
errorMessage = "ACCESS_KEY activation refused"
} catch is CheetahActivationLimitError {
errorMessage = "ACCESS_KEY reached its limit"
} catch is CheetahActivationThrottledError {
errorMessage = "ACCESS_KEY is throttled"
} catch {
errorMessage = "\(error)"
}
}
public func destroy() {
if isListening {
toggleRecordingOff()
}
cheetah.delete()
}
public func toggleRecording() {
if isListening {
toggleRecordingOff()
} else {
toggleRecordingOn()
}
}
public func toggleRecordingOff() {
do {
try VoiceProcessor.instance.stop()
state = UIState.FINALIZED
isListening = false
} catch {
errorMessage = "\(error.localizedDescription)"
}
}
public func toggleRecordingOn() {
do {
guard VoiceProcessor.hasRecordAudioPermission else {
VoiceProcessor.requestRecordAudioPermission { isGranted in
guard isGranted else {
DispatchQueue.main.async {
self.errorMessage = "Demo requires microphone permission"
}
return
}
DispatchQueue.main.async {
self.toggleRecordingOn()
}
}
return
}
try VoiceProcessor.instance.start(
frameLength: Cheetah.frameLength,
sampleRate: Cheetah.sampleRate
)
result = ""
state = UIState.RECORDING
isListening = true
} catch {
errorMessage = "\(error.localizedDescription)"
}
}
public func shouldAutoScroll() -> Bool {
autoScroll
}
public func setAutoScroll(_ value: Bool) {
autoScroll = value
}
private func audioCallback(frame: [Int16]) {
guard let cheetah = self.cheetah else {
return
}
do {
var (transcription, endpoint) = try cheetah.process(frame)
if endpoint {
transcription += "\(try cheetah.flush()) "
}
if transcription.count > 0 {
DispatchQueue.main.async {
self.result += transcription
}
}
} catch {
DispatchQueue.main.async {
self.errorMessage = "\(error)"
}
}
}
private func errorCallback(error: VoiceProcessorError) {
DispatchQueue.main.async {
self.errorMessage = "\(error)"
}
}
}