swift3audiotoolboxaudioqueueservices

Convert code of "AudioStreamPacketDescription" into swift3.2


I'm converting Audio Queues Service code into swift3.2 but i got stuck here.i don't know how to write this line of code in swift updated version.

I want to convert below code into swift 3.2

player.packetDescs = UnsafeMutablePointer<AudioStreamPacketDescription>(malloc(sizeof(AudioStreamPacketDescription) * Int(player.numPacketsToRead)))

where player object is :

 class Player {
        var playbackFile: AudioFileID? = nil
        var packetPosition: Int64 = 0
        var numPacketsToRead: UInt32 = 0
        var packetDescs: UnsafeMutablePointer<AudioStreamPacketDescription>? = nil
        var isDone = false
    }

I tried this :

let j = MemoryLayout.size(ofValue: AudioStreamPacketDescription.self) * Int(player.numPacketsToRead)                
player.packetDescs = UnsafeMutablePointer<AudioStreamPacketDescription>(malloc(j))

But this give me error :

Cannot invoke initializer for type 'UnsafeMutablePointer' with an argument list of type '(UnsafeMutableRawPointer!)'


Solution

  • ok Try this

    let sizeTmp = MemoryLayout.size(ofValue: AudioStreamPacketDescription.self) * Int(player.numPacketsToRead)
    let tmpPointer = UnsafeMutablePointer<AudioStreamPacketDescription>.allocate(capacity: sizeTmp)
    player.packetDescs = tmpPointer