I'm capturing audio with AVCaptureSession. In my callback function for processing the data captured, I put the stream in the Data structure (byte buffer). It appears Data is UInt8 (makes sense for a byte buffer), but I believe the stream data is UInt32.
I'm not sure which of the following I should be doing, but I can't get any of them to work. Do I:
- convert Data to be UInt32 instead of UInt8?
- When reading from data, take 4 bytes to make a UInt32?
- Change my capture session to UInt8?
- Give up on Data structure and make my own?
My callback function is:
func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {
var audioBufferList = AudioBufferList()
var data = Data()
var blockBuffer: CMBlockBuffer?
// Put the sample buffer in to a list of audio buffers (audioBufferList)
CMSampleBufferGetAudioBufferListWithRetainedBlockBuffer(sampleBuffer, bufferListSizeNeededOut: nil, bufferListOut: &audioBufferList, bufferListSize: MemoryLayout<AudioBufferList>.size, blockBufferAllocator: nil, blockBufferMemoryAllocator: nil, flags: 0, blockBufferOut: &blockBuffer)
// Extract the BufferList in to an array of buffers
let buffers = UnsafeBufferPointer<AudioBuffer>(start: &audioBufferList.mBuffers, count: Int(audioBufferList.mNumberBuffers))
// for each buffer, extract the frame. There should only be one buffer as we are recording in mono!
for audioBuffer in buffers {
assert(audioBuffer.mNumberChannels == 1) // it should always be 1 for mono channel
let frame = audioBuffer.mData?.assumingMemoryBound(to: UInt8.self)
data.append(frame!, count: Int(audioBuffer.mDataByteSize) / 8)
}
// limit how much of the sample we pass through.
viewDelegate?.gotSoundData(data.prefix(MAX_POINTS))
}
All the gotSoundData goes from the view to a number of subviews for processing
func addSamples(samples: Data) {
//if (isHidden) { return }
samples.forEach { sample in
[...process each byte...]
}
}
I can see that Data.append has the definition:
mutating func append(_ bytes: UnsafePointer<UInt8>, count: Int)