- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要将用 2 个音频 channel 录制的 .wav 文件转换为只有 1 个 channel 的 .wav 文件,并将位深度从 32 减少到 16。我一直在尝试使用 AVAudioConverter.convertToBuffer
但是,转换会引发错误: Error Domain=NSOSStatusErrorDomain Code=-50 "(null)"
基本上,唯一真正需要改变的是将音频缩减为单个 channel 和位深度。我从不同的工具获取这些文件,因此我不能仅仅更改文件的记录方式。
我不太擅长处理音频,而且我有点困惑。我正在编写的代码如下 - 有什么我遗漏的吗?
let inAudioFileURL:NSURL = <url_to_wav_file>
var inAudioFile:AVAudioFile?
do
{
inAudioFile = try AVAudioFile(forReading: inAudioFileURL)
}
catch let error
{
print ("error: \(error)")
}
let inAudioFormat:AVAudioFormat = inAudioFile!.processingFormat
let inFrameCount:UInt32 = UInt32(inAudioFile!.length)
let inAudioBuffer:AVAudioPCMBuffer = AVAudioPCMBuffer(PCMFormat: inAudioFormat, frameCapacity: inFrameCount)
do
{
try inAudioFile!.readIntoBuffer(inAudioBuffer)
}
catch let error
{
print ("readError: \(error)")
}
let startFormat:AVAudioFormat = AVAudioFormat.init(settings: inAudioFile!.processingFormat.settings)
print ("startFormat: \(startFormat.settings)")
var endFormatSettings = startFormat.settings
endFormatSettings[AVLinearPCMBitDepthKey] = 16
endFormatSettings[AVNumberOfChannelsKey] = 1
endFormatSettings[AVEncoderAudioQualityKey] = AVAudioQuality.Medium.rawValue
print ("endFormatSettings: \(endFormatSettings)")
let endFormat:AVAudioFormat = AVAudioFormat.init(settings: endFormatSettings)
let outBuffer = AVAudioPCMBuffer(PCMFormat: endFormat, frameCapacity: inFrameCount)
let avConverter:AVAudioConverter = AVAudioConverter.init(fromFormat: startFormat, toFormat: endFormat)
do
{
try avConverter.convertToBuffer(outBuffer, fromBuffer: inAudioBuffer)
}
catch let error
{
print ("avconverterError: \(error)")
}
至于输出:
startFormat:
["AVSampleRateKey": 16000,
"AVLinearPCMBitDepthKey": 32,
"AVLinearPCMIsFloatKey": 1,
"AVNumberOfChannelsKey": 2,
"AVFormatIDKey": 1819304813,
"AVLinearPCMIsNonInterleaved": 0,
"AVLinearPCMIsBigEndianKey": 0]
endFormatSettings:
["AVSampleRateKey": 16000,
"AVLinearPCMBitDepthKey": 16,
"AVLinearPCMIsFloatKey": 1,
"AVNumberOfChannelsKey": 1,
"AVFormatIDKey": 1819304813,
"AVLinearPCMIsNonInterleaved": 0,
"AVLinearPCMIsBigEndianKey": 0,
"AVEncoderQualityKey": 64]
avconverterError: Error Domain=NSOSStatusErrorDomain Code=-50 "(null)"
最佳答案
我不是 100% 确定为什么会出现这种情况,但我找到了一个适合我的解决方案,所以这就是我对这个问题的理解。我通过尝试使用备用 convert(to:error:withInputFrom:)
方法找到了此解决方案。使用这个给了我一个不同的错误:
`ERROR: AVAudioConverter.mm:526: FillComplexProc: required condition is false: [impl->_inputBufferReceived.format isEqual: impl->_inputFormat]`
问题是在我设置 AVAudioConverter
的行中引起的:
let avConverter:AVAudioConverter = AVAudioConverter.init(fromFormat: startFormat, toFormat: endFormat)
音频转换器似乎想要使用输入缓冲区正在使用的相同 AVAudioFormat
,而不是使用基于原始设置的副本。当我将 startFormat
替换为 inAudioFormat
后,convert(to:error:withInputFrom:)
错误就被消除了,一切都按预期进行。然后,我能够返回使用更简单的 convert(to:fromBuffer:)
方法,并且我正在处理的原始错误也消失了。
回顾一下,设置转换器的行现在看起来像:
let avConverter:AVAudioConverter = AVAudioConverter.init(fromFormat: inAudioFormat, toFormat: endFormat)
至于缺乏如何使用 AVAudioConverter
的文档,我不知道为什么 API 引用几乎没有。相反,在 Xcode 中,按住 CMD 键单击代码中的 AVAudioConverter
以转到其头文件。那里有很多评论和信息。不是完整的示例代码或任何东西,但它至少是一些东西。
关于ios - AVAudioConverter.convertToBuffer 抛出错误代码 -50,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39498793/
我需要将用 2 个音频 channel 录制的 .wav 文件转换为只有 1 个 channel 的 .wav 文件,并将位深度从 32 减少到 16。我一直在尝试使用 AVAudioConverte
我是一名优秀的程序员,十分优秀!