Skip to content

Commit

Permalink
Fix compilation errors for Swift 4.1
Browse files Browse the repository at this point in the history
  • Loading branch information
Bruno Berisso committed May 18, 2018
1 parent a427d62 commit 321f352
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
27 changes: 18 additions & 9 deletions TLSphinx/Decoder.swift
Expand Up @@ -46,9 +46,9 @@ fileprivate extension AVAudioPCMBuffer {
public enum DecodeErrors : Error {
case CantReadSpeachFile(String)
case CantSetAudioSession(NSError)
case NoAudioInputAvailable
case CantStartAudioEngine(NSError)
case CantAddWordsWhileDecodeingSpeech
case CantConvertAudioFormat
}


Expand Down Expand Up @@ -177,22 +177,31 @@ public final class Decoder {

engine = AVAudioEngine()

guard let input = engine.inputNode else {
throw DecodeErrors.NoAudioInputAvailable
}

let input = engine.inputNode
let mixer = AVAudioMixerNode()
engine.attach(mixer)
engine.connect(input, to: mixer, format: input.outputFormat(forBus: 0))

let formatIn = AVAudioFormat(commonFormat: .pcmFormatFloat32, sampleRate: 16000, channels: 1, interleaved: false)
let formatOut = AVAudioFormat(commonFormat: .pcmFormatInt16, sampleRate: 16000, channels: 1, interleaved: false)
let bufferMapper = AVAudioConverter(from: formatIn, to: formatOut)
// 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

let sphinxBuffer = AVAudioPCMBuffer(pcmFormat: formatOut, frameCapacity: buffer.frameCapacity)
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)")
Expand Down
2 changes: 1 addition & 1 deletion TLSphinxTests/LiveDecode.swift
Expand Up @@ -40,7 +40,7 @@ class LiveDecode: XCTestCase {
return
}

try! decoder.startDecodingSpeech { print("Utterance: \($0)") }
try! decoder.startDecodingSpeech { print("Utterance: \(String(describing: $0))") }

let theExpectation = expectation(description: "")

Expand Down

0 comments on commit 321f352

Please sign in to comment.