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.
AVAudioRecorder
in .wav
format using
following settingsaudioFilename = 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)
}
}
Base64
audio formatlet 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
Thoughts??
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 š