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

前端 未结 4 582
遇见更好的自我
遇见更好的自我 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 11:04

    AVAudioFile built-in to iOS (and OS X), is very convenient and will also do format conversions for you:

    import AVFoundation
    // ...
    
    let url = NSBundle.mainBundle().URLForResource("your audio file", withExtension: "wav")
    let file = try! AVAudioFile(forReading: url!)
    let format = AVAudioFormat(commonFormat: .PCMFormatFloat32, sampleRate: file.fileFormat.sampleRate, channels: 1, interleaved: false)
    
    let buf = AVAudioPCMBuffer(PCMFormat: format, frameCapacity: 1024)
    try! file.readIntoBuffer(buf)
    
    // this makes a copy, you might not want that
    let floatArray = Array(UnsafeBufferPointer(start: buf.floatChannelData[0], count:Int(buf.frameLength)))
    
    print("floatArray \(floatArray)\n")
    

    Sadly, for doubles it doesn't seem to be enough to substitute .PCMFormatFloat32 with .PCMFormatFloat64 because AVAudioPCMBuffer doesn't have a float64ChannelData method.

    update because I don't know swift well

    You can avoid copying the array by working with the UnsafeBufferPointer, which is a perfectly good collection type:

    let floatArray = UnsafeBufferPointer(start: buf.floatChannelData[0], count:Int(buf.frameLength))
    

提交回复
热议问题