Swift 3:使用AVCaptureAudioDataOutput分析音频输入 [英] Swift 3: Using AVCaptureAudioDataOutput to analyze audio input

查看:175
本文介绍了Swift 3:使用AVCaptureAudioDataOutput分析音频输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用AVCaptureAudioDataOutput分析音频输入,如此处所述.我自己无法弄清楚这些内容,因此我正在复制示例,但遇到了困难.

I’m trying to use AVCaptureAudioDataOutput to analyze audio input, as described here . This is not stuff I could figure out on my own, so I’m copying the example, but I’m having difficulty.

Swift 3中的Xcode提示我进行一些更改.分配samples的行出现编译错误. Xcode说:无法为类型为'(UnsafeMutableRawPointer?)的参数列表的类型为'UnsafeMutablePointer< __>的初始化程序'"

Xcode in Swift 3 has prompted me to make a couple of changes. I’m getting a compile error with the line assigning samples. Xcode says, "Cannot invoke initializer for type ‘UnsafeMutablePointer<_> with an argument list of type ‘(UnsafeMutableRawPointer?)’"

这是我修改后的代码:

func captureOutput(_ captureOutput: AVCaptureOutput!,
                    didOutputSampleBuffer sampleBuffer: CMSampleBuffer!,
                   from connection: AVCaptureConnection!){
    var buffer: CMBlockBuffer? = nil
    var audioBufferList = AudioBufferList(mNumberBuffers: 1,
                                          mBuffers: AudioBuffer(mNumberChannels: 1, mDataByteSize: 0, mData: nil))
    CMSampleBufferGetAudioBufferListWithRetainedBlockBuffer(
        sampleBuffer,
        nil,
        &audioBufferList,
        MemoryLayout<AudioBufferList>.size,     // changed for Swift 3
        nil,
        nil,
        UInt32(kCMSampleBufferFlag_AudioBufferList_Assure16ByteAlignment),
        &buffer
    )
    let abl = UnsafeMutableAudioBufferListPointer(&audioBufferList)
    var sum:Int64 = 0
    var count:Int = 0
    var bufs:Int = 0
    for buf in abl {
        let samples = UnsafeMutableBufferPointer<Int16>(start: UnsafeMutablePointer(buf.mData),  // Error here
                                                        count: Int(buf.mDataByteSize)/sizeof(Int16))
        for sample in samples {
            let s = Int64(sample)
            sum = (sum + s*s)
            count += 1
        }
        bufs += 1
    }
    print( "found \(count) samples in \(bufs) buffers, sum is \(sum)" )
}

有人可以告诉我如何解决此代码吗?

Can anyone tell me how to fix this code?

推荐答案

答案是我需要将buf.mData包装在OpaquePointer中.即在对UnsafeMutableBufferPointer<Int16>(OpaquePointer(buff.mData))的调用中,更改

The answer is that I need to wrap buf.mData in an OpaquePointer. i.e., in the call to UnsafeMutableBufferPointer<Int16>(OpaquePointer(buff.mData)), change

start: UnsafeMutablePointer(buff.mData)

start: UnsafeMutablePointer(OpaquePointer(buff.mData))

这是完整的代码,已为Swift 3更新:

Here is the complete code, updated for Swift 3:

    func captureOutput(_ captureOutput: AVCaptureOutput!,
                   didOutputSampleBuffer sampleBuffer: CMSampleBuffer!,
                   from connection: AVCaptureConnection!){
    var buffer: CMBlockBuffer? = nil
    var audioBufferList = AudioBufferList(mNumberBuffers: 1,
                                          mBuffers: AudioBuffer(mNumberChannels: 1, mDataByteSize: 0, mData: nil))
    CMSampleBufferGetAudioBufferListWithRetainedBlockBuffer(
        sampleBuffer,
        nil,
        &audioBufferList,
        MemoryLayout<AudioBufferList>.size,
        nil,
        nil,
        UInt32(kCMSampleBufferFlag_AudioBufferList_Assure16ByteAlignment),
        &buffer
    )
    let abl = UnsafeMutableAudioBufferListPointer(&audioBufferList)
    var sum:Int64 = 0
    var count:Int = 0
    var bufs:Int = 0
    for buff in abl {
        let samples = UnsafeMutableBufferPointer<Int16>(start: UnsafeMutablePointer(OpaquePointer(buff.mData)),
                                                        count: Int(buff.mDataByteSize)/MemoryLayout<Int16>.size)
        for sample in samples {
            let s = Int64(sample)
            sum = (sum + s*s)
            count += 1
        }
        bufs += 1
    }
    print( "found \(count) samples in \(bufs) buffers, RMS is \(sqrt(Float(sum)/Float(count)))" )
}

这满足了编译器的要求,并且似乎生成了合理的数字.

This satisfies the compiler, and it seems to generate reasonable numbers.

这篇关于Swift 3:使用AVCaptureAudioDataOutput分析音频输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆