This method handles the callback from installTap on the input node from an AVAudioEngine. I have confirmed that I'm getting mono float32/48000hz buffer data, and I'd like to convert it to mono int16/16000hz.
var converter: AVAudioConverter? = nil
var convertBuffer: AVAudioPCMBuffer? = nil
let targetFormat = AVAudioFormat(commonFormat: AVAudioCommonFormat.pcmFormatInt16, sampleRate: 16000, channels: 1, interleaved: false)
func recordCallback(buffer: AVAudioPCMBuffer, time: AVAudioTime) {
if converter == nil {
convertBuffer = AVAudioPCMBuffer(pcmFormat: targetFormat, frameCapacity: buffer.frameCapacity)
convertBuffer?.frameLength = convertBuffer!.frameCapacity
converter = AVAudioConverter(from: buffer.format, to: convertBuffer!.format)
converter?.sampleRateConverterAlgorithm = AVSampleRateConverterAlgorithm_Normal
converter?.sampleRateConverterQuality = .max
}
guard let convertBuffer = convertBuffer else { return }
log.info("Converter: \(self.converter!)")
log.info("Converter buffer: \(self.convertBuffer!)")
log.info("Converter buffer format: \(self.convertBuffer!.format)")
log.info("Source buffer: \(buffer)")
log.info("Source buffer format: \(buffer.format)")
do {
try converter!.convert(to: convertBuffer, from: buffer)
} catch let error {
log.error("Conversion error: \(error)")
observer?.onError(EngineRecordTaskError.ConversionError(error: error))
return
}
…
}
This gives me the helpful error:
Conversion error: Error Domain=NSOSStatusErrorDomain Code=-50 "(null)"
If I use the alternative method .convert(to:error:withInputFrom:), it does not populate the NSError, and does not fill the target buffer - fails silently.
Logged info:
Converter: <AVAudioConverter: 0x17001cd40>
Converter buffer: <AVAudioPCMBuffer@0x17021e310: 9600/9600 bytes>
Converter buffer format: <AVAudioFormat 0x17448ba90: 1 ch, 16000 Hz, Int16>
Source buffer: <AVAudioPCMBuffer@0x17021e1b0: 19200/19200 bytes>
Source buffer format: <AVAudioFormat 0x174480910: 1 ch, 48000 Hz, Float32>
Conversion error: Error Domain=NSOSStatusErrorDomain Code=-50 "(null)"
Additional info from debugger:
(lldb) po buffer.frameCapacity
4800
(lldb) po convertBuffer.frameCapacity
4800
(lldb) po buffer.frameLength
4800
(lldb) po convertBuffer.frameLength
4800
Here's the alternative code for conversion I tried:
var conversionError: NSError? = nil
converter!.convert(to: convertBuffer, error: &conversionError, withInputFrom: { (_, _) in
return buffer
})
if let conversionError = conversionError {
log.error("Conversion error: \(conversionError)")
observer?.onError(EngineRecordTaskError.ConversionError(error: conversionError))
return
}
From the discussion of convert
in the header file:
The output buffer's frameCapacity should be at least at large as the inputBuffer's frameLength. If the conversion involves a codec or sample rate conversion, you instead must use convertToBuffer:error:withInputFromBlock:.
You're converting from 48kHz to 16kHz, which counts as a rate conversion, so you have to use convertToBuffer
:
let inputBlock: AVAudioConverterInputBlock = { inNumPackets, outStatus in
outStatus.pointee = AVAudioConverterInputStatus.haveData
return buffer
}
var error: NSError? = nil
let status: AVAudioConverterOutputStatus = converter!.convert(to: convertBuffer, error: &error, withInputFrom: inputBlock)
// TODO: check status
p.s. it must be really expensive to create new NSError
codes'n'strings over at apple - their budget just couldn't cover a "rate conversion requires convertToBuffer" message.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With