66016

How to convert CMSampleBuffer to Data in Swift?

I need to convert CMSampleBuffer to Data format. I am using one Third party framework for audio related task. That framework gives me the streaming (i.e Real Time audio) audio in CMSampleBuffer object.

Like this:

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

Please provide me the steps to convert the CMSampleBuffer to Data.

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)} }

Answer1:

Try below code to convert CMSampleBuffer to 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))

<strong>EDIT-</strong>

For AudioBuffer use below code -

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)) }

Recommend

  • Swift 3 AVAudioEngine set microphone input format
  • BufferCB not being called by SampleGrabber
  • Spring HttpMessageNotReadableException
  • 404 response on simple RESTeasy example with Tomcat
  • File download through ajax
  • How to insert alt tags dynamically in jsp
  • Is it possible to use an http url as your source location of a Source Filter in DirectShow .Net?
  • How to use remove-erase idiom for removing empty vectors in a vector?
  • Jquery UI tool tip close icon
  • $wpdb not working in file of WordPress plugin
  • jQuery .attr() and value
  • Jetty Server not starting: Unable to establish loopback connection
  • Reading JSON from a file using C++ REST SDK (Casablanca)
  • DomPDF {PAGE_NUM} not on first page
  • R - Combining Columns to String Based on Logical Match
  • how to do an event when i swipe from fragment to the other
  • Linq Objects Group By & Sum
  • Deselecting radio buttons while keeping the View Model in synch
  • Javascript simulate pressing enter in input box
  • How reduce the height of an mschart by breaking up the y-axis
  • How to add date and time under each post in guestbook in google app engine
  • SSO with signing and signature validation doesn't work
  • How to show dropdown in excel using jrxml (jasper api)?
  • Importing jscolor library in angular 2
  • Release, debug version and Authorization Google?
  • Websockets service method fails during R startup
  • Alternatives to the OPTIONAL fallback SPARQL pattern?
  • How to get next/previous record number?
  • Apache 2.4 - remove | delete | uninstall
  • Proper way to use connect-multiparty with express.js?
  • Trying to get generic when generic is not available
  • embed rChart in Markdown
  • How to get Windows thread pool to call class member function?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal