-
Notifications
You must be signed in to change notification settings - Fork 499
/
ViewController.swift
192 lines (165 loc) · 7 KB
/
ViewController.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
187
188
189
190
191
192
//
// 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 UIKit
import ios_voice_processor
import Porcupine
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
@IBOutlet weak var wakeWordPicker: UIPickerView!
@IBOutlet weak var startButton: UIButton!
@IBOutlet weak var errorPanel: UITextView!
let accessKey = "${YOUR_ACCESS_KEY_HERE}" // Obtained from Picovoice Console (https://console.picovoice.ai)
let language: String = ProcessInfo.processInfo.environment["LANGUAGE"]!
var wakeWordKeys = [String]()
var wakeWord = ""
var porcupineManager: PorcupineManager!
var isListening = false
override func viewDidLoad() {
super.viewDidLoad()
if language == "en" {
for w in Porcupine.BuiltInKeyword.allCases {
wakeWordKeys.append(w.rawValue)
}
} else {
let bundle = Bundle(for: type(of: self))
let keywordUrls: [URL] = bundle.urls(forResourcesWithExtension: "ppn", subdirectory: "keywords")!
for k in keywordUrls {
let keyword = k.lastPathComponent
.replacingOccurrences(of: "_ios.ppn", with: "")
.replacingOccurrences(of: "_", with: " ")
wakeWordKeys.append(keyword)
}
}
wakeWord = wakeWordKeys[0]
wakeWordPicker.delegate = self
wakeWordPicker.dataSource = self
let viewSize = view.frame.size
let startButtonSize = CGSize(width: 120, height: 120)
let errorLabelSize = CGSize(width: (viewSize.width - 40), height: 120)
startButton.frame.size = startButtonSize
startButton.frame.origin =
CGPoint(x: (viewSize.width - startButtonSize.width) / 2, y: viewSize.height - (startButtonSize.height + 40))
startButton.layer.cornerRadius = 0.5 * startButton.bounds.size.width
startButton.clipsToBounds = true
wakeWordPicker.frame.origin = CGPoint(x: 0, y: 0)
wakeWordPicker.frame.size = CGSize(
width: viewSize.width,
height: viewSize.height - (startButtonSize.height + errorLabelSize.height + 40)
)
errorPanel.layer.cornerRadius = 10
errorPanel.frame.origin = CGPoint(
x: ((viewSize.width - errorLabelSize.width) / 2),
y: viewSize.height - (startButtonSize.height + errorLabelSize.height + 60)
)
errorPanel.frame.size = errorLabelSize
errorPanel.isHidden = true
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return wakeWordKeys.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return wakeWordKeys[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
wakeWord = wakeWordKeys[row]
}
func showErrorAlert(_ message: String) {
errorPanel.text = message
errorPanel.isHidden = false
wakeWordPicker.isUserInteractionEnabled = false
startButton.isEnabled = false
}
@IBAction func toggleStartButton(_ sender: UIButton) {
if !isListening {
startListening()
} else {
stopListening()
}
}
func startListening() {
guard VoiceProcessor.hasRecordAudioPermission else {
VoiceProcessor.requestRecordAudioPermission { isGranted in
guard isGranted else {
DispatchQueue.main.async {
self.showErrorAlert("Demo requires microphone permission")
}
return
}
DispatchQueue.main.async {
self.startListening()
}
}
return
}
let originalColor = self.view.backgroundColor
let keywordCallback: ((Int32) -> Void) = { _ in
self.view.backgroundColor = UIColor.orange
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
self.view.backgroundColor = originalColor
}
}
let errorCallback: ((Error) -> Void) = {error in
self.showErrorAlert("\(error)")
}
do {
if language == "en" {
porcupineManager = try PorcupineManager(
accessKey: accessKey,
keyword: Porcupine.BuiltInKeyword.init(rawValue: wakeWord)!,
onDetection: keywordCallback,
errorCallback: errorCallback)
} else {
let bundle = Bundle(for: type(of: self))
let keywordUrl: URL = bundle.url(
forResource: "\(wakeWord.lowercased())_ios",
withExtension: "ppn",
subdirectory: "keywords")!
let modelUrl: URL = bundle.url(
forResource: "porcupine_params_\(language)",
withExtension: "pv",
subdirectory: "models")!
porcupineManager = try PorcupineManager(
accessKey: accessKey,
keywordPath: keywordUrl.path,
modelPath: modelUrl.path,
onDetection: keywordCallback,
errorCallback: errorCallback)
}
try porcupineManager.start()
wakeWordPicker.isUserInteractionEnabled = false
isListening = true
startButton.setTitle("STOP", for: UIControl.State.normal)
} catch let error as PorcupineInvalidArgumentError {
showErrorAlert("\(error.localizedDescription)")
} catch is PorcupineActivationError {
showErrorAlert("AccessKey activation error")
} catch is PorcupineActivationRefusedError {
showErrorAlert("AccessKey activation refused")
} catch is PorcupineActivationLimitError {
showErrorAlert("AccessKey reached its limit")
} catch is PorcupineActivationThrottledError {
showErrorAlert("AccessKey is throttled")
} catch {
showErrorAlert("\(error)")
}
}
func stopListening() {
do {
try porcupineManager.stop()
} catch {
showErrorAlert("\(error)")
return
}
wakeWordPicker.isUserInteractionEnabled = true
isListening = false
startButton.setTitle("START", for: UIControl.State.normal)
}
}