How can I generate an array of floats from an audio file in Swift

前端 未结 4 586
遇见更好的自我
遇见更好的自我 2020-12-31 10:35

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

4条回答
  •  遥遥无期
    2020-12-31 10:52

    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)
    

提交回复
热议问题