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.
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?)’”
Here’s the code as I’ve modified it:
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?