AVAudioPCMBuffer as Base64

288 views Asked by At

How can the data from an AVAudioPCMBuffer be obtained as a Base64 encoded string, with Swift?

I'm asking as I'd like to send data from the microphone input on an iOS device to a WebSocket.

1

There are 1 answers

1
Shawn Frank On BEST ANSWER

Maybe you can convert the AVAudioPCMBuffer to Data and from Data to a base64 string.

private func encodeBuffer(_ buffer: AVAudioPCMBuffer) {
    
    let audioBuffer = buffer.audioBufferList.pointee.mBuffers
    
    if let audioDataReference = audioBuffer.mData
    {
        let bufferData = Data(bytes: audioDataReference,
                              count: Int(audioBuffer.mDataByteSize))
        
        let encodedString = bufferData.base64EncodedString()
        
        // do what you need with the base 64 string
    }
}