2

I'm trying to convert a recorded audio file into raw PCM data of type [Double]. I found a way to load and convert audio to PCM data of the type [Float32] like this:

    // load audio from path
    let file = try! AVAudioFile(forReading: self.soundFileURL)
    // declare format
    let format = AVAudioFormat(commonFormat: .PCMFormatFloat32, sampleRate: file.fileFormat.sampleRate, channels: 1, interleaved: false)
    // initialize audiobuffer with the length of the audio file
    let buf = AVAudioPCMBuffer(PCMFormat: format, frameCapacity: UInt32(file.length))
    // write to file
    try! file.readIntoBuffer(buf)
    // copy to array
    let floatArray = Array(UnsafeBufferPointer(start: buf.floatChannelData[0], count:Int(buf.frameLength)))

The problem is, I need the data as [Double] and AVAudioPCMBuffer() only knows the .PCMFormatFloat32. Does anybody know a workaround? Thank you.

4

3 に答える 3