Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if let audioCaptureDevice : AVCaptureDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeAudio){
- try audioCaptureDevice.lockForConfiguration()
- let audioInput = try AVCaptureDeviceInput(device: audioCaptureDevice)
- audioCaptureDevice.unlockForConfiguration()
- if(captureSession.canAddInput(audioInput)){
- captureSession.addInput(audioInput)
- print("added input")
- }
- let audioOutput = AVCaptureAudioDataOutput()
- audioOutput.setSampleBufferDelegate(self, queue: GlobalUserInitiatedQueue)
- if(captureSession.canAddOutput(audioOutput)){
- captureSession.addOutput(audioOutput)
- print("added output")
- }
- //supposed to start session not on UI queue coz it takes a while
- dispatch_async(GlobalUserInitiatedQueue) {
- print("starting captureSession")
- self.captureSession.startRunning()
- }
- }
- func captureOutput(captureOutput: AVCaptureOutput!, let didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, fromConnection connection: AVCaptureConnection!) {
- // Needs to be initialized somehow, even if we take only the address
- var audioBufferList = AudioBufferList(mNumberBuffers: 1,
- mBuffers: AudioBuffer(mNumberChannels: 1, mDataByteSize: 0, mData: nil))
- CMSampleBufferGetAudioBufferListWithRetainedBlockBuffer(
- sampleBuffer,
- nil,
- &audioBufferList,
- sizeof(audioBufferList.dynamicType),
- nil,
- nil,
- UInt32(kCMSampleBufferFlag_AudioBufferList_Assure16ByteAlignment),
- &buffer
- )
- // Create UnsafeBufferPointer from the variable length array starting at audioBufferList.mBuffers
- let abl = UnsafeMutableAudioBufferListPointer(&audioBufferList)
- for buffer in abl{
- let samples = UnsafeMutableBufferPointer<Int16>(start: UnsafeMutablePointer(buffer.mData),
- count: Int(buffer.mDataByteSize)/sizeof(Int16))
- var sum:Int = 0
- for sample in samples {
- sum = sum + Int(sample*sample)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement