-
Notifications
You must be signed in to change notification settings - Fork 85
/
ContentView.swift
186 lines (166 loc) · 6.55 KB
/
ContentView.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
//
// Copyright 2018-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 SwiftUI
import ios_voice_processor
import Rhino
struct SheetView: View {
@Binding var contextInfo: String
var body: some View {
ScrollView {
Text(self.contextInfo)
.padding()
.font(.system(size: 14))
}
}
}
struct ContentView: View {
let language: String = ProcessInfo.processInfo.environment["LANGUAGE"]!
let context: String = ProcessInfo.processInfo.environment["CONTEXT"]!
@State var rhinoManager: RhinoManager!
@State var buttonLabel = "START"
@State var result: String = ""
@State var errorMessage: String = ""
@State var contextInfo: String = ""
@State var showInfo: Bool = false
let ACCESS_KEY = "${YOUR_ACCESS_KEY_HERE}" // Obtained from Picovoice Console (https://console.picovoice.ai)
func initRhino() {
let contextPath = Bundle.main.url(
forResource: "\(context)_ios",
withExtension: "rhn",
subdirectory: "contexts")!.path
let modelPath = (language == "en") ? nil :
Bundle.main.url(
forResource: "rhino_params_\(language)",
withExtension: "pv",
subdirectory: "models")!.path
do {
self.rhinoManager = try RhinoManager(
accessKey: self.ACCESS_KEY,
contextPath: contextPath,
modelPath: modelPath,
onInferenceCallback: { x in
DispatchQueue.main.async {
result = "{\n"
self.result += " \"isUnderstood\" : \"" +
x.isUnderstood.description + "\",\n"
if x.isUnderstood {
self.result += " \"intent : \"" + x.intent + "\",\n"
if !x.slots.isEmpty {
result += " \"slots\" : {\n"
for (k, v) in x.slots {
self.result += " \"" + k + "\" : \"" + v + "\",\n"
}
result += " }\n"
}
}
result += "}\n"
self.buttonLabel = "START"
}
},
processErrorCallback: { error in
DispatchQueue.main.async {
errorMessage = "\(error)"
}
})
self.contextInfo = self.rhinoManager.contextInfo
} catch let error as RhinoInvalidArgumentError {
errorMessage = "\(error.localizedDescription)"
} catch is RhinoActivationError {
errorMessage = "ACCESS_KEY activation error"
} catch is RhinoActivationRefusedError {
errorMessage = "ACCESS_KEY activation refused"
} catch is RhinoActivationLimitError {
errorMessage = "ACCESS_KEY reached its limit"
} catch is RhinoActivationThrottledError {
errorMessage = "ACCESS_KEY is throttled"
} catch {
errorMessage = "\(error)"
}
}
func startListening() {
self.result = ""
if self.rhinoManager == nil {
initRhino()
}
do {
if self.rhinoManager != nil {
try self.rhinoManager.process()
self.buttonLabel = " ... "
}
} catch {
errorMessage = "\(error)"
}
}
var body: some View {
NavigationView {
VStack {
Spacer()
Spacer()
Text("\(result)")
.foregroundColor(Color.black)
.padding()
Text(errorMessage)
.padding()
.background(Color.red)
.foregroundColor(Color.white)
.frame(minWidth: 0, maxWidth: UIScreen.main.bounds.width - 50)
.font(.body)
.opacity(errorMessage.isEmpty ? 0 : 1)
.cornerRadius(.infinity)
Spacer()
Button {
if self.buttonLabel == "START" {
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.startListening()
}
}
return
}
startListening()
} else {
self.buttonLabel = "START"
}
} label: {
Text("\(buttonLabel)")
.padding()
.background(errorMessage.isEmpty ? Color.blue: Color.gray)
.foregroundColor(Color.white)
.font(.largeTitle)
}.disabled(!errorMessage.isEmpty)
}
.padding()
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
.background(Color.white)
.navigationBarItems(trailing: Button("Context Info") {
if self.rhinoManager == nil {
initRhino()
}
if self.rhinoManager != nil {
self.showInfo = true
}
})
}
.sheet(isPresented: self.$showInfo) {
SheetView(contextInfo: self.$contextInfo)
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}