如何调用 CMSampleBufferGetAudioBufferListWithRetainedBlockBuffer?

2023-11-22

我正在想办法如何称呼这个AVFoundationSwift 中的函数。我花了很多时间摆弄声明和语法,才走到这一步。编译器大部分都很高兴,但我还有最后一个难题。

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
}

编译器抱怨第三个和第四个参数:

初始化之前获取的变量“audioBufferList”的地址

and

初始化之前使用的变量“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 {
        // ....
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何调用 CMSampleBufferGetAudioBufferListWithRetainedBlockBuffer? 的相关文章

随机推荐