如何在Swift中使用Float填充AudioBuffer

时间:2016-04-20 11:20:32

标签: objective-c swift audioqueueservices ezaudio

我创建了一个使用EZAudio库的应用程序,它必须从麦克风获取数据并将其存储在变量中(我已经这样做了):

    func microphone(microphone: EZMicrophone!, 
                    hasAudioReceived buffer: UnsafeMutablePointer<UnsafeMutablePointer<Float>>, 
                    withBufferSize bufferSize: UInt32, 
                    withNumberOfChannels numberOfChannels: UInt32) 
   {
       let new = (buffer[0].memory)
       audioStack.append(new)
   }

如您所见,我正在使用EZMicrophoneDelegate和上面的func来从麦克风获取缓冲区数据。

未来的想法是通过WebSocket发送值并立即逐个播放。

我希望使用EZOuput和EZOuputDataSource的方法播放该数组:

    func output(output: EZOutput!, 
                shouldFillAudioBufferList audioBufferList: UnsafeMutablePointer<AudioBufferList>, 
                withNumberOfFrames frames: UInt32, 
                timestamp: UnsafePointer<AudioTimeStamp>) -> OSStatus 
    {
         if audioStack.count > 0 {
         let data = audioBufferList.memory.mBuffers.mData
         // "data" = is pointer to mData, that i want to fill with float
         // and I have do it here

         return noErr
    }

但是“mData”的类型是UnsafeMutablePointer,我不知道如何填充它,我在Objective-C中看到了一些例如this的例子,正是这一行:

Float32 *buffer = (Float32 *)audioBufferList->mBuffers[0].mData;

我无法理解如何在swift中将我的Float类型从数组存储到带有Void类型的mData?

施放像:

(audioBufferList.memory.mBuffers.mData) as Float = audioStack[0]

失败..

1 个答案:

答案 0 :(得分:4)

看起来我找到了如何将UnsafeMurtablePointer转换为UnsafeMutablePointer:

let data = UnsafeMutablePointer<Float>(audioBufferList.memory.mBuffers.mData)