问题
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