nhg*_*rif 5 audio initialization avfoundation sampling swift
我试图弄清楚如何AVFoundation在Swift中调用此函数。我花了很多时间摆弄声明和语法,并且到此为止。编译器通常很高兴,但我还有最后一个难题。
public func captureOutput(
captureOutput: AVCaptureOutput!,
didOutputSampleBuffer sampleBuffer: CMSampleBuffer!,
fromConnection connection: AVCaptureConnection!
) {
let samplesInBuffer = CMSampleBufferGetNumSamples(sampleBuffer)
var audioBufferList: AudioBufferList
var buffer: Unmanaged<CMBlockBuffer>? = nil
CMSampleBufferGetAudioBufferListWithRetainedBlockBuffer(
sampleBuffer,
nil,
&audioBufferList,
UInt(sizeof(audioBufferList.dynamicType)),
nil,
nil,
UInt32(kCMSampleBufferFlag_AudioBufferList_Assure16ByteAlignment),
&buffer
)
// do stuff
}
Run Code Online (Sandbox Code Playgroud)
编译器抱怨第三个和第四个参数:
变量'audioBufferList'的初始化之前获取的地址
和
初始化之前使用的变量“ audioBufferList”
那我应该在这里做什么?
我正在研究这个StackOverflow答案,但它是Objective-C。我正在尝试将其翻译成Swift,但是遇到了这个问题。
还是有更好的方法?我需要一次从缓冲区读取一个样本的数据,所以我基本上是在尝试获取可以迭代的样本数组。
免责声明:我刚刚尝试将代码从通过 AVAssetReader 读取音频样本转换为 Swift,并验证它是否可以编译。我没有测试过它是否真的有效。
// Needs to be initialized somehow, even if we take only the address
var audioBufferList = AudioBufferList(mNumberBuffers: 1,
mBuffers: AudioBuffer(mNumberChannels: 0, mDataByteSize: 0, mData: nil))
var buffer: Unmanaged<CMBlockBuffer>? = nil
CMSampleBufferGetAudioBufferListWithRetainedBlockBuffer(
sampleBuffer,
nil,
&audioBufferList,
UInt(sizeof(audioBufferList.dynamicType)),
nil,
nil,
UInt32(kCMSampleBufferFlag_AudioBufferList_Assure16ByteAlignment),
&buffer
)
// Ensure that the buffer is released automatically.
let buf = buffer!.takeRetainedValue()
// Create UnsafeBufferPointer from the variable length array starting at audioBufferList.mBuffers
let audioBuffers = UnsafeBufferPointer<AudioBuffer>(start: &audioBufferList.mBuffers,
count: Int(audioBufferList.mNumberBuffers))
for audioBuffer in audioBuffers {
// Create UnsafeBufferPointer<Int16> from the buffer data pointer
var samples = UnsafeMutableBufferPointer<Int16>(start: UnsafeMutablePointer(audioBuffer.mData),
count: Int(audioBuffer.mDataByteSize)/sizeof(Int16))
for sample in samples {
// ....
}
}
Run Code Online (Sandbox Code Playgroud)