chrisballinger / Opus-iOS

iOS build scripts for libopus
MIT License
221 stars 98 forks source link

Decode fails with -4 OPUS_INVALID_PACKET #17

Open tulssinep opened 6 years ago

tulssinep commented 6 years ago

Using the 2 second file from https://people.xiph.org/~giles/2012/opus/ and code attached below, all I get is -4. Am I doing something wrong here? I suppose that it might be due to the offset for header data and all that but how can I remove that from my data?

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

            var errorDeCr: Int32 = 0
            let dd = opus_decoder_create(48000, 1, &errorDeCr)
            print(errorDeCr)

            var sampleBuffer = [opus_int16](repeating: 0, count: data.length)
            print(opus_int32(bytes.count)) //6270

            let w = opus_decode(dd!, bytes, opus_int32(bytes.count), &sampleBuffer, opus_int32(5760), opus_int32(0))

            print(sampleBuffer)   // 0 every time
            print(w)    //-4 every time
            //-4 = OPUS_INVALID_PACKET

            print(errorDeCr)

        }