ioswavswift4.2voice-recording

Can't encode an Audio file to Base64?


Objective: Dialog Flow Voice Bot Api

I need to send a wav file to the Dialog Flow Api and the format and settings were pre-defined.

audioFilename = getDocumentsDirectory().appendingPathComponent("input.wav")

let settings: [String: Any] = [
    AVFormatIDKey: Int(kAudioFormatLinearPCM),
    AVSampleRateKey: 16000,
    AVNumberOfChannelsKey: 2,
    AVLinearPCMBitDepthKey: 16,
    AVLinearPCMIsBigEndianKey: false,
    AVEncoderAudioQualityKey: AVAudioQuality.max.rawValue
]

do {
    audioRecorder = try AVAudioRecorder(url: audioFilename!, settings: settings)
    audioRecorder.isMeteringEnabled = true
    audioRecorder.prepareToRecord()
    audioRecorder.delegate = self
    audioRecorder.record()
    recordButton.setTitle("Tap to Stop", for: .normal)
} catch {
    print(error.localizedDescription)
    finishRecording(success: false)
    }
}
let outputFile = try Data.init(contentsOf: fileUrl)
let base64String = outputFile.base64EncodedString(options: NSData.Base64EncodingOptions.init(rawValue: 0))
print(base64String)

So whenever I try to decode that encoded string, using an online converter, it displays some corrupted bytes Decoded output

Thoughts??


Solution

  • So I've found the answer to the question. The reason my byte array wasn't able to maintain correct headers was because of the following key that I omitted in the settings variable

    AVAudioFileTypeKey: kAudioFileWAVEType
    
        let settings: [String: Any] = [
          AVSampleRateKey: 16000,
          AVNumberOfChannelsKey: 1,
          AVAudioFileTypeKey: kAudioFileWAVEType,  //MANDATORY
          AVFormatIDKey: kAudioFormatLinearPCM,
          AVLinearPCMIsBigEndianKey: false,
          AVLinearPCMIsNonInterleaved: true,
          AVEncoderAudioQualityKey: AVAudioQuality.high.rawValue
        ]
    

    It was given in the docs that if you won't provide the settings i.e.

    audioRecorder = try AVAudioRecorder(url: audioFilename!, settings: [:] /*empty settings*/)

    then

    āŒ AVAudio recorder will automatically prepare the file from the Format defined in the file. āŒ

    But turns out, that didn't help either šŸ˜«

    So whilst I was playing with the settings, I found this very important key AVAudioFileTypeKey, which helped in maintaining the correct headers and thus a valid .wav file šŸ˜Ž

    This is how a wav file with Valid headers look like A valid file