I would like to load mp3 and wav audio files as arrays of floats or doubles, similar to the io.wavfile.read function in scipy. I can do this with microphone data or playing
Above answers didn't work for me, I'm using Swift5, found this extensions that worked for me here: https://gist.github.com/jtodaone/f2fa59c19794811dbe989dff65a772bc
Also here is how i use the code on Playground
import UIKit
import AVFoundation
let filePath: String = Bundle.main.path(forResource: "nameOfFile", ofType: "wav")!
print("\(filePath)")
let fileURL: NSURL = NSURL(fileURLWithPath: filePath)
let audioFile = try AVAudioFile(forReading: fileURL as URL)
let audioFormat = audioFile.processingFormat
let audioFrameCount = UInt32(audioFile.length)
let audioFileBuffer = AVAudioPCMBuffer(pcmFormat: audioFormat, frameCapacity: audioFrameCount)
try audioFile.read(into: audioFileBuffer!)
extension AudioBuffer {
func array() -> [Float] {
return Array(UnsafeBufferPointer(self))
}
}
extension AVAudioPCMBuffer {
func array() -> [Float] {
return self.audioBufferList.pointee.mBuffers.array()
}
}
extension Array where Element: FloatingPoint {
mutating func buffer() -> AudioBuffer {
return AudioBuffer(mNumberChannels: 1, mDataByteSize: UInt32(self.count * MemoryLayout.size), mData: &self)
}
}
let array = audioFileBuffer?.array()
print(array?.count) //Optional(2705408)