-
Notifications
You must be signed in to change notification settings - Fork 58
/
Decoder.swift
219 lines (170 loc) · 6.35 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
//
// 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
private 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"
}
}
}
}
public class Decoder {
private var psDecoder: COpaquePointer
private var recorder: AVAudioRecorder!
private var speechState: SpeechStateEnum
public var bufferSize: Int = 2048
public init?(config: Config) {
speechState = .Silence
if config.cmdLnConf != nil{
psDecoder = ps_init(config.cmdLnConf)
if psDecoder == nil {
return nil
}
} else {
psDecoder = nil
return nil
}
}
deinit {
let refCount = ps_free(psDecoder)
assert(refCount == 0, "Can't free decoder, it's shared among instances")
}
private func process_raw(data: NSData) -> CInt {
//Sphinx expect words of 2 bytes but the NSFileHandle read one byte at time so the lenght of the data for sphinx is the half of the real one.
let dataLenght = data.length / 2
let numberOfFrames = ps_process_raw(psDecoder, UnsafePointer(data.bytes), dataLenght, SFalse, SFalse)
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
}
private func in_speech() -> Bool {
return ps_get_in_speech(psDecoder) == 1
}
private func start_utt() -> Bool {
return ps_start_utt(psDecoder) == 0
}
private func end_utt() -> Bool {
return ps_end_utt(psDecoder) == 0
}
private func get_hyp() -> Hypothesis? {
var score: CInt = 0
let string: UnsafePointer<CChar> = ps_get_hyp(psDecoder, &score)
if let text = String.fromCString(string) {
return Hypothesis(text: text, score: Int(score))
} else {
return nil
}
}
private func hypotesisForSpeechAtPath (filePath: String) -> Hypothesis? {
if let fileHandle = NSFileHandle(forReadingAtPath: filePath) {
start_utt()
let hypothesis = fileHandle.reduceChunks(bufferSize, initial: nil, reducer: { (data: NSData, partialHyp: Hypothesis?) -> Hypothesis? in
self.process_raw(data)
var resultantHyp = partialHyp
if self.speechState == .Utterance {
self.end_utt()
resultantHyp = partialHyp + self.get_hyp()
self.start_utt()
}
return resultantHyp
})
end_utt()
fileHandle.closeFile()
//Process any pending speech
if speechState == .Speech {
return hypothesis + get_hyp()
} else {
return hypothesis
}
} else {
return nil
}
}
public func decodeSpeechAtPath (filePath: String, complete: (Hypothesis?) -> ()) {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
let hypothesis = self.hypotesisForSpeechAtPath(filePath)
dispatch_async(dispatch_get_main_queue()) {
complete(hypothesis)
}
}
}
public func startDecodingSpeech (utteranceComplete: (Hypothesis?) -> ()) {
let error: NSErrorPointer = nil
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryRecord)
} catch let error1 as NSError {
error.memory = error1
}
if error != nil {
print("Error setting the shared AVAudioSession: \(error)")
return
}
let tmpFileName = (NSTemporaryDirectory() as NSString).stringByAppendingPathComponent("TLSphinx-\(NSDate.timeIntervalSinceReferenceDate())")
let tmpAudioFile = NSURL(string: tmpFileName)
let settings: [String : AnyObject] = [
AVFormatIDKey: UInt(kAudioFormatLinearPCM),
AVSampleRateKey: 16000.0,
AVNumberOfChannelsKey: 1,
AVLinearPCMBitDepthKey: 16,
AVLinearPCMIsBigEndianKey: false,
AVLinearPCMIsFloatKey: false
]
do {
recorder = try AVAudioRecorder(URL: tmpAudioFile!, settings: settings)
} catch let error1 as NSError {
error.memory = error1
recorder = nil
}
if error != nil {
print("Error setting the audio recorder: \(error)")
return
}
if recorder.record() {
if let audioFileHandle = NSFileHandle(forReadingAtPath: tmpFileName) {
start_utt()
audioFileHandle.readabilityHandler = { (handler: NSFileHandle) -> Void in
let audioData = handler.availableData
if audioData.length > 0 {
self.process_raw(audioData)
if self.speechState == .Utterance {
self.end_utt()
utteranceComplete(self.get_hyp())
self.start_utt()
}
}
}
}
}
}
public func stopDecodingSpeech () {
recorder.stop()
recorder.deleteRecording()
recorder = nil
}
}