objective-cswiftaudioqueueservicesezaudio

how to fill AudioBuffer with Float in Swift


I making an app that uses EZAudio library, and it have to get data from the microphone and store it in variable (i already have done this) :

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

As you can see, i`m using EZMicrophoneDelegate, and func above, to get buffer data from microphone.

The future idea is to send the values by WebSocket and play immediately one by one.

I want playback that array, using EZOuput and EZOuputDataSource`s method:

    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
    }

But the type of "mData" is UnsafeMutablePointer, and I don`t know how to fill it, I saw some examples in objective-C like this, exactly this line:

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

and I can`t understand how I have to store my Float type from array to mData with Void type, in swift?

Casting like:

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

fails..


Solution

  • Looks like i found how to cast an UnsafeMurtablePointer to UnsafeMutablePointer :

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