如何转换UnsafeMutablePointer <unsafemutablepointer <float>&gt;变量到AudioBufferList?

时间:2016-06-16 15:12:24

标签: objective-c swift ezaudio audiobuffer audiobufferlist

我在Swift项目中有这个EZAudio方法,用于从麦克风中捕获音频:

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

}

但我真正需要的是拥有&#34; bufferList&#34;作为AudioBufferList类型的参数,以便通过套接字发送这些音频数据包,就像我在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);
}

如何转换UnsafeMutablePointer&gt;变量到AudioBufferList?

2 个答案:

答案 0 :(得分:0)

我相信你会创建一个AudioBufferList指针并使用memory函数的结果。

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

答案 1 :(得分:0)

我能够将麦克风中的音频流式传输到套接字中,如下所示:

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
    }

这个一般的AudioStreamDescriptor设置对我有用,你可能需要根据自己的需要调整它或省略一些部分,比如波形动画:

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
    }

让这件东西运转起来很复杂,祝你好运!