A lightweight swift wrapper for Codec 2
Codec 2 is a low bitrate speech codec with compression modes ranging from 700 bit/s to 3200 bit/s. It is useful for voice compression in bandwidth constrained environments. The codec accepts 16 bit 8Khz PCM audio as input.
This framework is based on a fork of the Codec 2 source code. The original source can be found on SVN here and on GitHub here.
- Add this line to your
Cartfile
:github "Beartooth/codec2-ios"
- Use the typical carthage workflow to add
Codec2.framework
to your project.
Instantiating a codec using 1.2kbps compression:
import Codec2
let codec = Codec2(mode: ._1200)!
let nBitsPerFrame = codec.bitsPerEncFrame
let nBytesPerFrame = codec.bytesPerEncFrame
let nSamplesPerFrame = codec.samplesPerFrame
Encoding and Decoding:
import Codec2
let codec = Codec2(mode: ._1200)!
func encodeVoiceFrame(_ toEnc: inout [Int16]) -> [UInt8]? {
guard toEnc.count == codec.samplesPerFrame else { return nil }
return codec.encode(speech: toEnc)
}
func decodeVoiceFrame(_ toDec: inout [UInt8]) -> [Int16]? {
guard toDec.count == codec.bytesPerEncFrame else { return nil }
return codec.decode(frame: toDec)
}
- Clone the repository:
git clone --recursive https://github.com/Beartooth/codec2-ios.git
- Build the
codec2-ios
scheme from XCode or usingxcodebuild
David Rowe originally developed the codec, along with support from other researchers.
Jeff Jones developed the Swift port and cross compilation build script.