FerasAS
FerasAS

Reputation: 293

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))

Upvotes: 3

Views: 719

Answers (1)

Gordon Childs
Gordon Childs

Reputation: 36074

You can do this:

let channelData = audioBuffer.floatChannelData![0]
let arr = Array(UnsafeBufferPointer(start:channelData, count: Int(audioFileLength)))

Upvotes: 1

Related Questions