如何使用AudioConverterConvertBuffer在PCM和IMA4之间转换?

时间:2018-09-07 16:17:02

标签: swift pcm audio-converter ima4

我正在用16位线性PCM录制,并想将其实时转换为IMA4。我需要两种格式:用于某些脱机处理的PCM和用于本地存储的IMA4。我希望使用AudioConverterConvertBuffer,因为我使用的两种格式都指定了相同的采样率。但是,当我以PCM作为输入缓冲区调用AudioConverterConvertBuffer时,我收到-50作为OSStatus(这似乎不映射到AudioConverter API返回的任何状态)。

我已经在Swift中编写了此代码,因此我可能会将Data对象的翻译搞乱了,成为不安全的字节,但这是我正在运行的代码:

var inputFormat = AudioStreamBasicDescription()  
inputFormat.mSampleRate = 16000.0  
inputFormat.mFormatID = kAudioFormatLinearPCM  
inputFormat.mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked  
inputFormat.mBytesPerPacket = 2  
inputFormat.mFramesPerPacket = 1  
inputFormat.mBytesPerFrame = 2  
inputFormat.mChannelsPerFrame = 1  
inputFormat.mBitsPerChannel = 16  
inputFormat.mReserved = 0  

var outputFormat = AudioStreamBasicDescription()  
outputFormat.mSampleRate = 16000.0  
outputFormat.mFormatID = kAudioFormatAppleIMA4  
outputFormat.mFormatFlags = 0  
outputFormat.mBytesPerPacket = 34  
outputFormat.mFramesPerPacket = 64  
outputFormat.mBytesPerFrame = 0  
outputFormat.mChannelsPerFrame = 1  
outputFormat.mBitsPerChannel = 0  
outputFormat.mReserved = 0  

var audioConverter: AudioConverterRef?  
let inData = Data() // This contains 16000 bytes of 16-bit Linear PCM audio data  
var outData = Data(count: 32 * 1024) // Should be more than enough space  
var outByteCount = UInt32(outData.count)  

let result = AudioConverterNew(&inputFormat, &outputFormat, &audioConverter)  // result is noErr here  

let obc = inData.withUnsafeBytes { (inU8ptr: UnsafePointer) -> UInt32 in  
     outData.withUnsafeMutableBytes { (outU8ptr: UnsafeMutablePointer) -> UInt32 in  
          let result = AudioConverterConvertBuffer(audioConverter!,  
                                                   UInt32(inData.count), inU8ptr,  
                                                   &outByteCount, outU8ptr)  
          if result != noErr {  
               return UInt32(0)  
          }  

          return outByteCount  
     }  
}  

print("outputBytes after conversion = \(obc)"  

是否有人对此有帮助?我什至在正确的轨道上吗?

谢谢

塞恩

0 个答案:

没有答案