-
Notifications
You must be signed in to change notification settings - Fork 58
/
Decoder.swift
267 lines (207 loc) · 8.1 KB
/
Decoder.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
//
// Decoder.swift
// TLSphinx
//
// Created by Bruno Berisso on 5/29/15.
// Copyright (c) 2015 Bruno Berisso. All rights reserved.
//
import Foundation
import AVFoundation
import Sphinx
fileprivate enum SpeechStateEnum : CustomStringConvertible {
case silence
case speech
case utterance
var description: String {
get {
switch(self) {
case .silence:
return "Silence"
case .speech:
return "Speech"
case .utterance:
return "Utterance"
}
}
}
}
fileprivate extension AVAudioPCMBuffer {
func toData() -> Data {
let channels = UnsafeBufferPointer(start: int16ChannelData, count: 1)
let ch0Data = Data(bytes: UnsafeMutablePointer<int16>(channels[0]),
count: Int(frameCapacity * format.streamDescription.pointee.mBytesPerFrame))
return ch0Data
}
}
public enum DecodeErrors : Error {
case CantReadSpeachFile(String)
case CantSetAudioSession(NSError)
case CantStartAudioEngine(NSError)
case CantAddWordsWhileDecodeingSpeech
case CantConvertAudioFormat
}
public final class Decoder {
fileprivate var psDecoder: OpaquePointer?
fileprivate var engine: AVAudioEngine!
fileprivate var speechState: SpeechStateEnum
public init?(config: Config) {
speechState = .silence
psDecoder = config.cmdLnConf.flatMap(ps_init)
if psDecoder == nil {
return nil
}
}
deinit {
let refCount = ps_free(psDecoder)
assert(refCount == 0, "Can't free decoder because it's shared among instances")
}
@discardableResult fileprivate func process_raw(_ data: Data) -> CInt {
let dataLenght = data.count / 2
let numberOfFrames = data.withUnsafeBytes { (bytes : UnsafePointer<Int16>) -> Int32 in
ps_process_raw(psDecoder, bytes, dataLenght, SFalse32, SFalse32)
}
let hasSpeech = in_speech()
switch (speechState) {
case .silence where hasSpeech:
speechState = .speech
case .speech where !hasSpeech:
speechState = .utterance
case .utterance where !hasSpeech:
speechState = .silence
default:
break
}
return numberOfFrames
}
fileprivate func in_speech() -> Bool {
return ps_get_in_speech(psDecoder) == STrue
}
@discardableResult fileprivate func start_utt() -> Bool {
return ps_start_utt(psDecoder) == 0
}
@discardableResult fileprivate func end_utt() -> Bool {
return ps_end_utt(psDecoder) == 0
}
fileprivate func get_hyp() -> Hypothesis? {
var score: int32 = 0
guard let string = ps_get_hyp(psDecoder, &score) else {
return nil
}
if let text = String(validatingUTF8: string) {
return Hypothesis(text: text, score: Int(score))
} else {
return nil
}
}
fileprivate func hypotesisForSpeech (inFile fileHandle: FileHandle) -> Hypothesis? {
start_utt()
let hypothesis = fileHandle.reduceChunks(2048, initial: nil, reducer: {
(data: Data, partialHyp: Hypothesis?) -> Hypothesis? in
process_raw(data)
var resultantHyp = partialHyp
if speechState == .utterance {
end_utt()
resultantHyp = partialHyp + get_hyp()
start_utt()
}
return resultantHyp
})
end_utt()
//Process any pending speech
if speechState == .speech {
return hypothesis + get_hyp()
} else {
return hypothesis
}
}
public func decodeSpeech (atPath filePath: String, complete: @escaping (Hypothesis?) -> ()) throws {
guard let fileHandle = FileHandle(forReadingAtPath: filePath) else {
throw DecodeErrors.CantReadSpeachFile(filePath)
}
DispatchQueue.global().async {
let hypothesis = self.hypotesisForSpeech(inFile:fileHandle)
fileHandle.closeFile()
DispatchQueue.main.async {
complete(hypothesis)
}
}
}
public func startDecodingSpeech (_ utteranceComplete: @escaping (Hypothesis?) -> ()) throws {
do {
if #available(iOS 10.0, *) {
try AVAudioSession.sharedInstance().setCategory(.playAndRecord, mode: .voiceChat, options: [])
} else {
try AVAudioSession.sharedInstance().setCategory(.playAndRecord)
}
} catch let error as NSError {
print("Error setting the shared AVAudioSession: \(error)")
throw DecodeErrors.CantSetAudioSession(error)
}
engine = AVAudioEngine()
let input = engine.inputNode
let mixer = AVAudioMixerNode()
engine.attach(mixer)
engine.connect(input, to: mixer, format: input.outputFormat(forBus: 0))
// We forceunwrap this because the docs for AVAudioFormat specify that this constructor return nil when the channels
// are grater than 2.
let formatIn = AVAudioFormat(commonFormat: .pcmFormatFloat32, sampleRate: 16000, channels: 1, interleaved: false)!
let formatOut = AVAudioFormat(commonFormat: .pcmFormatInt16, sampleRate: 16000, channels: 1, interleaved: false)!
guard let bufferMapper = AVAudioConverter(from: formatIn, to: formatOut) else {
// Returns nil if the format conversion is not possible.
throw DecodeErrors.CantConvertAudioFormat
}
mixer.installTap(onBus: 0, bufferSize: 2048, format: formatIn, block: {
[unowned self] (buffer: AVAudioPCMBuffer!, time: AVAudioTime!) in
guard let sphinxBuffer = AVAudioPCMBuffer(pcmFormat: formatOut, frameCapacity: buffer.frameCapacity) else {
// Returns nil in the following cases:
// - if the format has zero bytes per frame (format.streamDescription->mBytesPerFrame == 0)
// - if the buffer byte capacity (frameCapacity * format.streamDescription->mBytesPerFrame)
// cannot be represented by an uint32_t
print("Can't create PCM buffer")
return
}
// This is needed because the 'frameLenght' default value is 0 (since iOS 10) and cause the 'convert' call
// to faile with an error (Error Domain=NSOSStatusErrorDomain Code=-50 "(null)")
// More here: http://stackoverflow.com/questions/39714244/avaudioconverter-is-broken-in-ios-10
sphinxBuffer.frameLength = sphinxBuffer.frameCapacity
do {
try bufferMapper.convert(to: sphinxBuffer, from: buffer)
} catch(let error as NSError) {
print(error)
return
}
let audioData = sphinxBuffer.toData()
self.process_raw(audioData)
print("Process: \(buffer.frameLength) frames - \(audioData.count) bytes - sample time: \(time.sampleTime)")
if self.speechState == .utterance {
self.end_utt()
let hypothesis = self.get_hyp()
DispatchQueue.main.async {
utteranceComplete(hypothesis)
}
self.start_utt()
}
})
start_utt()
do {
try engine.start()
} catch let error as NSError {
end_utt()
print("Can't start AVAudioEngine: \(error)")
throw DecodeErrors.CantStartAudioEngine(error)
}
}
public func stopDecodingSpeech () {
engine.stop()
engine = nil
}
public func add(words:Array<(word: String, phones: String)>) throws {
guard engine == nil || !engine.isRunning else {
throw DecodeErrors.CantAddWordsWhileDecodeingSpeech
}
for (word,phones) in words {
let update = words.last?.word == word ? STrue32 : SFalse32
ps_add_word(psDecoder, word, phones, update)
}
}
}