3 Replies
      Latest reply: Nov 21, 2016 1:15 PM by hotpaw2 RSS
      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]
              bufPtrs.append(channelPtr!)
        }
        
        

         

        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!,
                                UnsafeMutablePointer(bufPtrs),
                                frame.memory.nb_samples,
                                UnsafeMutablePointer(frame.memory.extended_data),
                                frame.memory.nb_samples)
        
        

         

        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!,
               outBuffers,
               (frame?.pointee.nb_samples)!,
               inBuffers,
               (frame?.pointee.nb_samples)!)
          

           

          Any comments are welcome!