How to access multiple buffers in UnsafePointer<AudioBufferList> (non-mutable)

£可爱£侵袭症+ 提交于 2020-03-25 18:23:54

问题


I've been trying to use the new AVAudioSinkNode in Core Audio. It passes audioBufferList which is of type UnsafePointer<AudioBufferList> to the closure handling the audio.

The implementation of AudioBufferList in Swift is such that you cannot access multiple buffers, if there are more than one. Apple provided a wrapper for the mutable version, UnsafeMutablePointer<AudioBufferList>, which is called UnsafeMutableAudioBufferListPointer and allows access to multiple buffers.

But I cannot find a relevant wrapper that I could use with the non-mutable UnsafePointer<AudioBufferList>....

let sinkNode = AVAudioSinkNode() { (timestamp, frames, audioBufferList) -> OSStatus in
    // audioBufferList is UnsafePointer<AudioBufferList>
    // how to access multiple buffers in audioBufferList?
}

Thanks in advance!


回答1:


To access each AudioBuffer in an AudioBufferList in Swift, you may choose two ways:

  • Do some pointer operations yourself and calculate each address of AudioBuffer.
  • Use UnsafeMutableAudioBufferListPointer forcefully.

If you could find a right sample code before UnsafeMutableAudioBufferListPointer was introduced in Swift, the first way might be your option, but as for now, it's hard to find one.


To use UnsafeMutableAudioBufferListPointer forcefully, you may need to convert UnsafePointer<AudioBufferList> to UnsafeMutablePointer<AudioBufferList>, which is not too difficult:

            let audioBufferListPtr = UnsafeMutableAudioBufferListPointer(UnsafeMutablePointer(mutating: audioBufferList))

I believe you know how to access buffers once you get UnsafeMutableAudioBufferListPointer.

Caution: Even if type conversion from UnsafePointer<AudioBufferList> to UnsafeMutablePointer<AudioBufferList> is easily made, the actual region passed through audioBufferList is not mutable. You may need extra care not to mutate such region.



来源:https://stackoverflow.com/questions/60801491/how-to-access-multiple-buffers-in-unsafepointeraudiobufferlist-non-mutable

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!