forked from Picovoice/porcupine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ViewController.swift
85 lines (69 loc) · 3.25 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
//
// Copyright 2018-2021 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 Porcupine
import SwiftySound
class ViewController: UIViewController, UITextViewDelegate {
@IBOutlet weak var startButton: UIButton!
@IBOutlet weak var textView: UITextView!
@IBOutlet weak var errorPanel: UITextView!
let accessKey = "${YOUR_ACCESS_KEY_HERE}" // Obtained from Picovoice Console (https://console.picovoice.ai)
var wakeWord = Porcupine.BuiltInKeyword.porcupine
var porcupineManager: PorcupineManager!
var isRecording = false
override func viewDidLoad() {
super.viewDidLoad()
startButton.layer.cornerRadius = 0.5 * startButton.bounds.size.width
startButton.clipsToBounds = true
textView.text = "Press the Start button and say the wake word \"Porcupine\". " +
"Try pressing the home button and saying it again."
errorPanel.layer.cornerRadius = 10
errorPanel.isHidden = true
}
@IBAction func toggleStartButton(_ sender: UIButton) {
if !isRecording {
NotificationManager.shared.requestNotificationAuthorization()
do {
Sound.category = .playAndRecord
let keywordCallback: ((Int32) -> Void) = { _ in
NotificationManager.shared.sendNotification()
Sound.play(file: "beep.wav")
}
self.porcupineManager = try PorcupineManager(
accessKey: accessKey,
keyword: wakeWord,
onDetection: keywordCallback)
try porcupineManager.start()
isRecording = true
startButton.setTitle("STOP", for: UIControl.State.normal)
} catch let error as PorcupineInvalidArgumentError {
showErrorAlert(message: "\(error.localizedDescription)\nEnsure your accessKey '\(accessKey)' is valid")
} catch is PorcupineActivationError {
showErrorAlert(message: "AccessKey activation error")
} catch is PorcupineActivationRefusedError {
showErrorAlert(message: "AccessKey activation refused")
} catch is PorcupineActivationLimitError {
showErrorAlert(message: "AccessKey reached its limit")
} catch is PorcupineActivationThrottledError {
showErrorAlert(message: "AccessKey is throttled")
} catch let error {
showErrorAlert(message: "\(error)")
}
} else {
porcupineManager.stop()
isRecording = false
startButton.setTitle("START", for: UIControl.State.normal)
}
}
private func showErrorAlert(message: String) {
errorPanel.text = message
errorPanel.isHidden = false
startButton.isEnabled = false
}
}