AVAudioRecorder generates strange Wav file(wrong header)

How can I get the only the PCM data from AVAudioRecorder file? these are the settings I use to record the file:

        let settings : [String : Any] = [             AVFormatIDKey: Int(kAudioFormatLinearPCM),             AVSampleRateKey: Int(stethoscopeSampleRateDefault),             AVNumberOfChannelsKey: 1,             AVEncoderAudioQualityKey: AVAudioQuality.medium.rawValue,         ] 

the outcome of this is strange wav file with strange header. How can I extract only the PCM data out of it?

Asked on August 31, 2020 in iOS,   Swift.
Add Comment
1 Answer(s)

The actual sound data in a wav file is in the "data" subchunk of that file – this format description might help you visualize the structure you’ll have to navigate. But maybe what’s tripping you up is that Apple includes an extra subchunk called "fllr" which precedes the sound data, so you have to seek past that too. Fortunately every subchunk is given an id and size, so finding the data subchunk is still relatively straightforward.

  1. Open the file using FileHandle
  2. Seek to byte 12, which gets you past the header and puts you at the beginning of the first subchunk (should be fmt).
  3. Read 4 bytes and convert to a string, then read 4 more bytes and convert to an integer. The string is the subchunk name, and the integer is the size of that subchunk. If the string is not "data" then seek forward "size" number of bytes and repeat step 3.
  4. Read the rest of the file – this is your PCM data.
Answered on August 31, 2020.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.