3 Replies
      Latest reply on Nov 21, 2016 1:15 PM by hotpaw2
      jogga Level 1 Level 1 (0 points)

        Hi all,


        Trying to migrate my code to Swift 3 but I can't seem to solve this problem. Need help!


        I've got this audio buffer and a pointer to it's channel data... like this


        var buffer: AVAudioPCMBuffer
        var bufPtrs = [UnsafeMutablePointer<Float>]()
        for index in 0..<outputChannelCount {
             let channelPtr = buffer.floatChannelData?[index]


        Now I need to hand in this pointer to a C library function that does some resampling.


        This function wants an UInt8 pointer to an array of UInt8 elements, like this...


        func swr_convert(_ s: OpaquePointer!, _ out: UnsafeMutablePointer<UnsafeMutablePointer<UInt8>?>!, _ out_count: Int32, _ in: UnsafeMutablePointer<UnsafePointer<UInt8>?>!, _ in_count: Int32) -> Int32


        Before Swift 3 I just used this code successfully.


        numSamplesDecoded = swr_convert(audioPlayer.swrContext!,


        But that doesn't work in Swift 3.


        How do I reference this chunk of memory in Swift 3 so that the C library function will accept it?


        I really don't understand the explanation in the documentation for Swift 3.


        Please help / Jörgen

        • Re: Swift 3 UnsafeMutablePointer<Float> migration problem
          jogga Level 1 Level 1 (0 points)

          I came up with this solution. Don't know if it's safe or has any drawbacks compared to my Swift 2 solution...


          let outBuffers = unsafeBitCast(UnsafeMutablePointer(mutating: bufPtrs), to: UnsafeMutablePointer<UnsafeMutablePointer<UInt8>?>.self)
          let inBuffers = unsafeBitCast(frame?.pointee.extended_data, to: UnsafeMutablePointer<UnsafePointer<UInt8>?>.self)
          numSamplesDecoded = swr_convert(audioPlayer.swrContext!,


          Any comments are welcome!