Advertisement
Guest User

Untitled

a guest
Oct 9th, 2015
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.14 KB | None | 0 0
  1. if let audioCaptureDevice : AVCaptureDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeAudio){
  2.  
  3.  
  4.  
  5. try audioCaptureDevice.lockForConfiguration()
  6.  
  7. let audioInput = try AVCaptureDeviceInput(device: audioCaptureDevice)
  8. audioCaptureDevice.unlockForConfiguration()
  9.  
  10. if(captureSession.canAddInput(audioInput)){
  11. captureSession.addInput(audioInput)
  12. print("added input")
  13. }
  14.  
  15.  
  16. let audioOutput = AVCaptureAudioDataOutput()
  17.  
  18. audioOutput.setSampleBufferDelegate(self, queue: GlobalUserInitiatedQueue)
  19.  
  20. if(captureSession.canAddOutput(audioOutput)){
  21. captureSession.addOutput(audioOutput)
  22. print("added output")
  23. }
  24.  
  25.  
  26. //supposed to start session not on UI queue coz it takes a while
  27. dispatch_async(GlobalUserInitiatedQueue) {
  28. print("starting captureSession")
  29. self.captureSession.startRunning()
  30. }
  31. }
  32.  
  33. func captureOutput(captureOutput: AVCaptureOutput!, let didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, fromConnection connection: AVCaptureConnection!) {
  34.  
  35. // Needs to be initialized somehow, even if we take only the address
  36. var audioBufferList = AudioBufferList(mNumberBuffers: 1,
  37. mBuffers: AudioBuffer(mNumberChannels: 1, mDataByteSize: 0, mData: nil))
  38.  
  39.  
  40.  
  41. CMSampleBufferGetAudioBufferListWithRetainedBlockBuffer(
  42. sampleBuffer,
  43. nil,
  44. &audioBufferList,
  45. sizeof(audioBufferList.dynamicType),
  46. nil,
  47. nil,
  48. UInt32(kCMSampleBufferFlag_AudioBufferList_Assure16ByteAlignment),
  49. &buffer
  50. )
  51.  
  52.  
  53.  
  54. // Create UnsafeBufferPointer from the variable length array starting at audioBufferList.mBuffers
  55.  
  56.  
  57.  
  58. let abl = UnsafeMutableAudioBufferListPointer(&audioBufferList)
  59.  
  60. for buffer in abl{
  61. let samples = UnsafeMutableBufferPointer<Int16>(start: UnsafeMutablePointer(buffer.mData),
  62. count: Int(buffer.mDataByteSize)/sizeof(Int16))
  63.  
  64.  
  65. var sum:Int = 0
  66. for sample in samples {
  67. sum = sum + Int(sample*sample)
  68.  
  69. }
  70.  
  71.  
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement