2017-07-12 70 views
0

我需要將CMSampleBuffer轉換爲Data格式。我正在使用一個第三方框架進行音頻相關任務。該框架爲我提供了CMSampleBuffer對象中的流媒體(即實時音頻)音頻。如何將Swift中的CMSampleBuffer轉換爲數據?

像這樣:

func didAudioStreaming(audioSample: CMSampleBuffer!) { 
    //Here I need to conver this to Data format. 
    //Because I am using GRPC framework for Audio Recognization, 
} 

請提供給我的步驟來轉換CMSampleBufferData

FYI

let formatDesc:CMFormatDescription? = CMSampleBufferGetFormatDescription(audioSample) 

    <CMAudioFormatDescription 0x17010d890 [0x1b453ebb8]> { 
    mediaType:'soun' 
    mediaSubType:'lpcm' 
    mediaSpecific: { 
     ASBD: { 
      mSampleRate: 16000.000000 
      mFormatID: 'lpcm' 
      mFormatFlags: 0xc 
      mBytesPerPacket: 2 
      mFramesPerPacket: 1 
      mBytesPerFrame: 2 
      mChannelsPerFrame: 1 
      mBitsPerChannel: 16  } 
     cookie: {(null)} 
     ACL: {(null)} 
     FormatList Array: {(null)} 
    } 
    extensions: {(null)} 
} 
+0

請問你是怎麼在16000的採樣率記錄?我已經打電話給'''session.setPreferredSampleRate''',但我不斷收到採樣率44100 –

回答

2

嘗試下面的代碼CMSampleBuffer轉換爲NSData的。

let imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) 
CVPixelBufferLockBaseAddress(imageBuffer!, CVPixelBufferLockFlags(rawValue: 0)) 
let bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer!) 
let height = CVPixelBufferGetHeight(imageBuffer!) 
let src_buff = CVPixelBufferGetBaseAddress(imageBuffer!) 
let data = NSData(bytes: src_buff, length: bytesPerRow * height) 
CVPixelBufferUnlockBaseAddress(imageBuffer!, CVPixelBufferLockFlags(rawValue: 0)) 

編輯 -

對於AudioBuffer使用下面的代碼 -

var audioBufferList = AudioBufferList() 
var data = Data() 
var blockBuffer : CMBlockBuffer? 

CMSampleBufferGetAudioBufferListWithRetainedBlockBuffer(sampleBuffer, nil, &audioBufferList, MemoryLayout<AudioBufferList>.size, nil, nil, 0, &blockBuffer) 

let buffers = UnsafeBufferPointer<AudioBuffer>(start: &audioBufferList.mBuffers, count: Int(audioBufferList.mNumberBuffers)) 

for audioBuffer in buffers { 
    let frame = audioBuffer.mData?.assumingMemoryBound(to: UInt8.self) 
    data.append(frame!, count: Int(audioBuffer.mDataByteSize)) 
} 
+0

謝謝Nilesh,但我正在接收音頻樣本而不是視頻或圖像。它適用於我的情況嗎? – Sridhar

+0

@Sridhar我編輯了我的答案,請讓我知道你是否適合你。 – Nilesh

+0

感謝Nileash,我用你更新的答案,我越來越 - 錯誤域= NSOSStatusErrorDomain代碼= 1954115647「(空)」這個錯誤來自AudioPlayer – Sridhar

相關問題