-
Notifications
You must be signed in to change notification settings - Fork 17
/
AVSpeechSynthesizer+LambdaKit.swift
161 lines (140 loc) · 6.99 KB
/
AVSpeechSynthesizer+LambdaKit.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
//
// AVSpeechSynthesizer+LambdaKit.swift
// Created by Matias Pequeno on 9/23/15.
//
// Copyright (c) 2015 Lyft (http://lyft.com)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
import Foundation
import AVFoundation
public typealias LKDidStartSpeechUtterance = (AVSpeechSynthesizer, AVSpeechUtterance) -> Void
public typealias LKDidFinishSpeechUtterance = (AVSpeechSynthesizer, AVSpeechUtterance) -> Void
public typealias LKDidPauseSpeechUtterance = (AVSpeechSynthesizer, AVSpeechUtterance) -> Void
public typealias LKDidContinueSpeechUtterance = (AVSpeechSynthesizer, AVSpeechUtterance) -> Void
public typealias LKDidCancelSpeechUtterance = (AVSpeechSynthesizer, AVSpeechUtterance) -> Void
public typealias LKWillSpeakRangeOfSpeechString = (AVSpeechSynthesizer, NSRange, AVSpeechUtterance) -> Void
// A global var to produce a unique address for the assoc object handle
private var associatedEventHandle: UInt8 = 0
/// AVSpeechSynthesizer with closure callback(s).
///
/// Example:
///
/// ```swift
/// let player = try? AVAudioPlayer(contentsOfURL: soundURL)
/// player?.play { player, success in
/// // deactivate audio session
/// }
/// ```
extension AVSpeechSynthesizer: AVSpeechSynthesizerDelegate {
private var closuresWrapper: ClosuresWrapper {
get {
if let wrapper = objc_getAssociatedObject(self, &associatedEventHandle) as? ClosuresWrapper {
return wrapper
}
let closuresWrapper = ClosuresWrapper()
self.closuresWrapper = closuresWrapper
return closuresWrapper
}
set {
self.delegate = self
objc_setAssociatedObject(self, &associatedEventHandle, newValue,
objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
}
}
/// The closure that fires when the synthesizer has begun speaking an utterance.
public var didStartSpeechUtterance: LKDidStartSpeechUtterance? {
set { self.closuresWrapper.didStartSpeechUtterance = newValue }
get { return self.closuresWrapper.didStartSpeechUtterance }
}
/// The closure that fires when the synthesizer has finished speaking an utterance.
public var didFinishSpeechUtterance: LKDidFinishSpeechUtterance? {
set { self.closuresWrapper.didFinishSpeechUtterance = newValue }
get { return self.closuresWrapper.didFinishSpeechUtterance }
}
/// The closure that fires when the synthesizer has paused while speaking an utterance.
public var didPauseUtterance: LKDidPauseSpeechUtterance? {
set { self.closuresWrapper.didPauseSpeechUtterance = newValue }
get { return self.closuresWrapper.didPauseSpeechUtterance }
}
/// The closure that fires when the synthesizer has resumed speaking an utterance after being paused.
public var didContinueSpeechUtterance: LKDidContinueSpeechUtterance? {
set { self.closuresWrapper.didContinueSpeechUtterance = newValue }
get { return self.closuresWrapper.didContinueSpeechUtterance }
}
/// The closure that fires when the synthesizer has canceled speaking an utterance.
public var didCancelSpeechUtterance: LKDidCancelSpeechUtterance? {
set { self.closuresWrapper.didCancelSpeechUtterance = newValue }
get { return self.closuresWrapper.didCancelSpeechUtterance }
}
/// The closure that fires when the synthesizer is about to speak a portion of an utterance’s text.
public var willSpeakRangeOfSpeechString: LKWillSpeakRangeOfSpeechString? {
set { self.closuresWrapper.willSpeakRangeOfSpeechString = newValue }
get { return self.closuresWrapper.willSpeakRangeOfSpeechString }
}
/// Enqueues an utterance to be spoken.
///
/// - parameter utterance: An AVSpeechUtterance object containing text to be spoken.
/// - parameter closure: Closure to be called when speech finishes speaking. This won't be called if the
/// synthesizer is paused or canceled.
public func speakUtterance(_ utterance: AVSpeechUtterance,
didFinishUtterance closure: @escaping LKDidFinishSpeechUtterance)
{
self.didFinishSpeechUtterance = closure
self.speak(utterance)
}
// MARK: AVSpeechSynthesizerDelegate implementation
public func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer,
didStart utterance: AVSpeechUtterance)
{
self.closuresWrapper.didStartSpeechUtterance?(synthesizer, utterance)
}
public func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer,
didFinish utterance: AVSpeechUtterance)
{
self.closuresWrapper.didFinishSpeechUtterance?(synthesizer, utterance)
}
public func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer,
didPause utterance: AVSpeechUtterance)
{
self.closuresWrapper.didPauseSpeechUtterance?(synthesizer, utterance)
}
public func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer,
didContinue utterance: AVSpeechUtterance)
{
self.closuresWrapper.didContinueSpeechUtterance?(synthesizer, utterance)
}
public func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer,
didCancel utterance: AVSpeechUtterance)
{
self.closuresWrapper.didCancelSpeechUtterance?(synthesizer, utterance)
}
public func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer,
willSpeakRangeOfSpeechString characterRange: NSRange, utterance: AVSpeechUtterance)
{
self.closuresWrapper.willSpeakRangeOfSpeechString?(synthesizer, characterRange, utterance)
}
}
fileprivate final class ClosuresWrapper {
fileprivate var didStartSpeechUtterance: LKDidStartSpeechUtterance?
fileprivate var didFinishSpeechUtterance: LKDidFinishSpeechUtterance?
fileprivate var didPauseSpeechUtterance: LKDidPauseSpeechUtterance?
fileprivate var didContinueSpeechUtterance: LKDidContinueSpeechUtterance?
fileprivate var didCancelSpeechUtterance: LKDidCancelSpeechUtterance?
fileprivate var willSpeakRangeOfSpeechString: LKWillSpeakRangeOfSpeechString?
}