Swift - from audioPCMbuffer to array

is it possible to extract the data in a buffer as an array? I have saved an audiofile into an buffer and now would like o extract the data. The code is:

import UIKit import AVFoundation

//importing audio

let audioFileURL = Bundle.main.url(forResource: "Morning", withExtension: "wav")

let audioFile = try AVAudioFile(forReading: audioFileURL!)

//extract information

var audioFileFormat = audioFile.fileFormat

var audioFilePFormat = audioFile.processingFormat

var audioFileLength = audioFile.length

var audioFrameCount = UInt32(audioFile.length)

var audioFileChannels = audioFile.fileFormat.channelCount

var audioFileSamplingRate = audioFile.fileFormat.sampleRate

// insert into buffer

let audioBuffer = AVAudioPCMBuffer(pcmFormat: audioFilePFormat, frameCapacity: AVAudioFrameCount(audioFileLength))

try audioFile.read(into: audioBuffer, frameCount: AVAudioFrameCount(audioFileLength))

Replies

audioBuffer.floatChannelData

OR

audioBuffer.int16ChannelData

OR

audioBuffer.int32ChannelData


Depending on the formate of the audio.

Hi Lucas,


Those give me an output in playgrounds of : "UnsafePointer(0x600000002F20)"


I have fixed it using :


let audioData = Array(UnsafeBufferPointer(start: audioBuffer.floatChannelData?[0], count:Int(audioBuffer.frameLength)))


best


Feras A.