如何从 Swift 中的输入音频文件生成相位反转音频文件?
Posted
技术标签:
【中文标题】如何从 Swift 中的输入音频文件生成相位反转音频文件?【英文标题】:How to Generate Phase Inverse audio file from input audio file in Swift? 【发布时间】:2019-01-22 12:12:27 【问题描述】:我正在通过音频引擎录制声音并创建一个文件名my_file.caf
并尝试创建另一个文件,使其相位相反,我可以取消它的单声道声音。
但是当我进行一些运算和计算时,它会反转其正弦波,但也会反转声音。
do
let inFile: AVAudioFile = try AVAudioFile(forReading: URLFor(filename: "my_file.caf")!)
let format: AVAudioFormat = inFile.processingFormat
let frameCount: AVAudioFrameCount = UInt32(inFile.length)
let outSettings = [AVNumberOfChannelsKey: format.channelCount,
AVSampleRateKey: format.sampleRate,
AVLinearPCMBitDepthKey: 16,
AVFormatIDKey: kAudioFormatMPEG4AAC] as [String : Any]
let outFile: AVAudioFile = try AVAudioFile(forWriting: URLFor(filename: "my_file1.caf")!, settings: outSettings)
let forwardBuffer: AVAudioPCMBuffer = AVAudioPCMBuffer(pcmFormat: format, frameCapacity: frameCount)!
let reverseBuffer: AVAudioPCMBuffer = AVAudioPCMBuffer(pcmFormat: format, frameCapacity: frameCount)!
try inFile.read(into: forwardBuffer)
let frameLength = forwardBuffer.frameLength
reverseBuffer.frameLength = frameLength
let audiostride = forwardBuffer.stride
for channelIdx in 0..<forwardBuffer.format.channelCount
let forwardChannelData = forwardBuffer.floatChannelData?.advanced(by: Int(channelIdx)).pointee
let reverseChannelData = reverseBuffer.floatChannelData?.advanced(by: Int(channelIdx)).pointee
var reverseIdx: Int = 0
for idx in stride(from: frameLength, to: 0, by: -1)
memcpy(reverseChannelData?.advanced(by: reverseIdx * audioStride), forwardChannelData?.advanced(by: Int(idx) * audioStride), MemoryLayout<Float>.size)
reverseIdx += 1
try outFile.write(from: reverseBuffer)
catch let error
print(error.localizedDescription)
我已经尝试了所有可能的方法,但我被卡住了,有人可以帮忙吗?
【问题讨论】:
【参考方案1】:我的幼稚方法是:减去平均值(偏差),乘以 -1,再加上平均值。
【讨论】:
为什么不直接乘以 -1 ?? 是的,可以乘以-1。以上是关于如何从 Swift 中的输入音频文件生成相位反转音频文件?的主要内容,如果未能解决你的问题,请参考以下文章