如何在 Swift 中用 Float 填充 AudioBuffer

how to fill AudioBuffer with Float in Swift

我制作了一个使用 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 和上面的函数从麦克风获取缓冲区数据。

未来的想法是通过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;

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

铸造像:

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

失败..

看来我找到了如何将 UnsafeMurtablePointer 转换为 UnsafeMutablePointer 的方法:

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