How to convert that UnsafeMutablePointer<UnsafeMutablePointer<Float>> variable into AudioBufferList?

ぃ、小莉子 提交于 2019-12-24 05:54:45

问题


I have this EZAudio method in my Swift project, to capture audio from the microphone:

func microphone(microphone: EZMicrophone!, hasAudioReceived bufferList: UnsafeMutablePointer<UnsafeMutablePointer<Float>>, withBufferSize bufferSize: UInt32, withNumberOfChannels numberOfChannels: UInt32) {

}

But what I really need is to have that "bufferList" parameter coming in as an AudioBufferList type, in order to send those audio packets through a socket, just like I did in Objective C:

//Objective C pseudocode:
for(int i = 0; i < bufferList.mNumberBuffers; ++i){
   AudioBuffer buffer = bufferList.mBuffers[i];
   audio = ["audio": NSData(bytes: buffer.mData, length: Int(buffer.mDataByteSize))];
   socket.emit("message", audio);
}

How can I convert that UnsafeMutablePointer> variable into AudioBufferList?


回答1:


I believe you would create a AudioBufferList pointer and use the result of the memory function.

let audioBufferList = UnsafePointer<AudioBufferList>(bufferList).memory 



回答2:


I was able to stream audio from the Microphone, into a socket, like this:

func microphone(microphone: EZMicrophone!, hasBufferList bufferList: UnsafeMutablePointer<AudioBufferList>, withBufferSize bufferSize: UInt32, withNumberOfChannels numberOfChannels: UInt32) {
        let blist:AudioBufferList=bufferList[0]
        let buffer:AudioBuffer = blist.mBuffers
        let audio = ["audio": NSData(bytes: buffer.mData, length: Int(buffer.mDataByteSize))];
        socket.emit("message", audio);//this socket comes from Foundation framework
    }

This general AudioStreamDescriptor setup worked for me, you might have to tweak it for your own needs or ommit some parts, like the waveform animation:

func initializeStreaming() {
        var streamDescription:AudioStreamBasicDescription=AudioStreamBasicDescription()
        streamDescription.mSampleRate       = 16000.0
        streamDescription.mFormatID         = kAudioFormatLinearPCM
        streamDescription.mFramesPerPacket  = 1
        streamDescription.mChannelsPerFrame = 1
        streamDescription.mBytesPerFrame    = streamDescription.mChannelsPerFrame * 2
        streamDescription.mBytesPerPacket   = streamDescription.mFramesPerPacket * streamDescription.mBytesPerFram
        streamDescription.mBitsPerChannel   = 16
        streamDescription.mFormatFlags      = kAudioFormatFlagIsSignedInteger
        microphone = EZMicrophone(microphoneDelegate: self, withAudioStreamBasicDescription: sstreamDescription, startsImmediately: false)
        waveview?.plotType=EZPlotType.Buffer
        waveview?.shouldFill = false
        waveview?.shouldMirror = false
    }

It was complicated to get this thing running, good luck!



来源:https://stackoverflow.com/questions/37863088/how-to-convert-that-unsafemutablepointerunsafemutablepointerfloat-variable-i

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!