使用 AudioConverter 更改采样率
Posted
技术标签:
【中文标题】使用 AudioConverter 更改采样率【英文标题】:Change Sample rate with AudioConverter 【发布时间】:2020-03-16 19:12:44 【问题描述】:我正在尝试将输入音频从 44.1 kHz 重新采样到 48 kHz。
-
使用AudioToolbox的
AUAudioUnit.inputHandler
将输入的 44.1 kHZ 写入 wav 文件(效果很好)
将 44.1 kHz 转换为 48 kHz 并将转换后的字节写入文件。 https://developer.apple.com/documentation/audiotoolbox/1503098-audioconverterfillcomplexbuffer
问题出在第三步。写入文件后,声音非常嘈杂。 这是我的代码:
// convert to 48kHz
var audioConverterRef: AudioConverterRef?
CheckError(AudioConverterNew(&self.hardwareFormat,
&self.convertingFormat,
&audioConverterRef), "AudioConverterNew failed")
let outputBufferSize = inNumBytes
let outputBuffer = UnsafeMutablePointer<Int16>.allocate(capacity: MemoryLayout<Int16>.size * Int(outputBufferSize))
let convertedData = AudioBufferList.allocate(maximumBuffers: 1)
convertedData[0].mNumberChannels = self.hardwareFormat.mChannelsPerFrame
convertedData[0].mDataByteSize = outputBufferSize
convertedData[0].mData = UnsafeMutableRawPointer(outputBuffer)
var ioOutputDataPackets = UInt32(inNumPackets)
CheckError(AudioConverterFillComplexBuffer(audioConverterRef!,
self.coverterCallback,
&bufferList,
&ioOutputDataPackets,
convertedData.unsafeMutablePointer,
nil), "AudioConverterFillComplexBuffer error")
let convertedmData = convertedData[0].mData!
let convertedmDataByteSize = convertedData[0].mDataByteSize
// Write converted packets to file -> audio_unit_int16_48.wav
CheckError(AudioFileWritePackets(self.outputFile48000!,
false,
convertedmDataByteSize,
nil,
recordPacket,
&ioOutputDataPackets,
convertedmData), "AudioFileWritePackets error")
转换回调体在这里:
let buffers = UnsafeMutableBufferPointer<AudioBuffer>(start: &bufferList.mBuffers, count: Int(bufferList.mNumberBuffers))
let dataPtr = UnsafeMutableAudioBufferListPointer(ioData)
dataPtr[0].mNumberChannels = 1
dataPtr[0].mData = buffers[0].mData
dataPtr[0].mDataByteSize = buffers[0].mDataByteSize
ioDataPacketCount.pointee = buffers[0].mDataByteSize / UInt32(MemoryLayout<Int16>.size)
示例项目在这里:https://drive.google.com/file/d/1GvCJ5hEqf7PsBANwUpVTRE1L7S_zQxnL/view?usp=sharing
【问题讨论】:
"将 44.1 kHz 转换为 48 kHz" 我认为问题在于您没有转换它。你只是说采样率是 48kHz,但它是 44.1kHz——所以你会得到蹩脚的声音。重采样是一项复杂的业务,我的印象是您根本没有这样做。当然,我可能完全错了!但这是我的总体印象。如果有的话,请带上一粒盐。 AudioConverterFillComplexBuffer 是否应该完成这项工作? 【参考方案1】:如果您的链的一部分仍然是 AVAudioEngine,那么 Apple 提供了 offline processing of AVAudioFiles 的示例代码。
这是一个包含 sampleRate 更改的修改版本:
import Cocoa
import AVFoundation
import PlaygroundSupport
let outputSampleRate = 48_000.0
let outputAudioFormat = AVAudioFormat(standardFormatWithSampleRate: outputSampleRate, channels: 2)!
// file needs to be in ~/Documents/Shared Playground Data
let localURL = playgroundSharedDataDirectory.appendingPathComponent("inputFile_44.aiff")
let outputURL = playgroundSharedDataDirectory.appendingPathComponent("outputFile_48.aiff")
let sourceFile: AVAudioFile
let format: AVAudioFormat
do
sourceFile = try AVAudioFile(forReading: localURL)
format = sourceFile.processingFormat
catch
fatalError("Unable to load the source audio file: \(error.localizedDescription).")
let sourceSettings = sourceFile.fileFormat.settings
var outputSettings = sourceSettings
outputSettings[AVSampleRateKey] = outputSampleRate
let engine = AVAudioEngine()
let player = AVAudioPlayerNode()
engine.attach(player)
// Connect the nodes.
engine.connect(player, to: engine.mainMixerNode, format: format)
// Schedule the source file.
player.scheduleFile(sourceFile, at: nil)
do
// The maximum number of frames the engine renders in any single render call.
let maxFrames: AVAudioFrameCount = 4096
try engine.enableManualRenderingMode(.offline, format: outputAudioFormat,
maximumFrameCount: maxFrames)
catch
fatalError("Enabling manual rendering mode failed: \(error).")
do
try engine.start()
player.play()
catch
fatalError("Unable to start audio engine: \(error).")
let buffer = AVAudioPCMBuffer(pcmFormat: engine.manualRenderingFormat, frameCapacity: engine.manualRenderingMaximumFrameCount)!
var outputFile: AVAudioFile?
do
outputFile = try AVAudioFile(forWriting: outputURL, settings: outputSettings)
catch
fatalError("Unable to open output audio file: \(error).")
let outputLengthD = Double(sourceFile.length) * outputSampleRate / sourceFile.fileFormat.sampleRate
let outputLength = Int64(ceil(outputLengthD)) // no sample left behind
while engine.manualRenderingSampleTime < outputLength
do
let frameCount = outputLength - engine.manualRenderingSampleTime
let framesToRender = min(AVAudioFrameCount(frameCount), buffer.frameCapacity)
let status = try engine.renderOffline(framesToRender, to: buffer)
switch status
case .success:
// The data rendered successfully. Write it to the output file.
try outputFile?.write(from: buffer)
case .insufficientDataFromInputNode:
// Applicable only when using the input node as one of the sources.
break
case .cannotDoInCurrentContext:
// The engine couldn't render in the current render call.
// Retry in the next iteration.
break
case .error:
// An error occurred while rendering the audio.
fatalError("The manual rendering failed.")
catch
fatalError("The manual rendering failed: \(error).")
// Stop the player node and engine.
player.stop()
engine.stop()
outputFile = nil // AVAudioFile won't close until it goes out of scope, so we set output file back to nil here
【讨论】:
以上是关于使用 AudioConverter 更改采样率的主要内容,如果未能解决你的问题,请参考以下文章