2017-01-24 11 views
0

AVCaptureAudioDataOutputを使用してオーディオ入力を分析しようとしています(here)。これは私が自力で把握できるものではないので、例をコピーしていますが、難しいです。Swift 3:AVCaptureAudioDataOutputを使用してオーディオ入力を分析する

Swift 3のXcodeは私にいくつかの変更を促しました。 samplesを割り当てる行でコンパイルエラーが発生します。 Xcodeは「「タイプの引数リストでUnsafeMutablePointer < _> 『タイプのための初期化子を呼び出すことはできません』(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)") 
} 

ことができます誰もこのコードを修正する方法を教えてください。

答えて

0

答えはOpaquePointerbuf.mDataをラップする必要があるということです。すなわち、UnsafeMutableBufferPointer<Int16>(OpaquePointer(buff.mData))への呼び出しでは、ここで

start: UnsafeMutablePointer(OpaquePointer(buff.mData)) 

start: UnsafeMutablePointer(buff.mData) 

を変更スウィフト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)))") 
} 

これは、コンパイラを満たし、そしてそれはそう合理的な数字を生成する。

関連する問題