I have successfully added https://github.com/chrisballinger/Opus-iOS to my project and I am able to call the functions declared in its header.
I want to convert from OPUS to AAC so my first step would be to decode my opus file. However, it keeps throwing an error code.
The file I am using is the 2-second file from https://people.xiph.org/~giles/2012/opus/.
This is my code
let url = Bundle.main.url(forResource: "detodos", withExtension: "opus")
print(url) //prints path correctly
var bytes = [UInt8]()
if let data = NSData(contentsOf: url!) {
var buffer = [UInt8](repeating: 0, count: data.length)
data.getBytes(&buffer, length: data.length)
bytes = buffer
let d = opus_decoder_create(48000, 1, nil)
var sampleBuffer = [opus_int16](repeating: 0, count: data.length)
print(opus_int32(bytes.count)) //6270
let w = opus_decode(d!, bytes, opus_int32(bytes.count), &sampleBuffer, opus_int32(5760), opus_int32(1))
print(sampleBuffer) // [0,0,...] every time
print(w) //-4 every time
//-4 = OPUS_INVALID_PACKET
}
I would've guessed that in this very minimal implementation nothing should go wrong but apparently it does. Printing my bytes
object returns tons of different numbers so I know for a fact it doesn't stay at all-0's.
I realized that it might be due to the method expecting pure "audio data" but the file also contains a header etc. How can I strip this off?