我正在尝试使用Audiokit及其AKFFTap来获取音频文件的快速傅立叶变换数据。
我设法在实时处理中获取它们,但是当我在脱机渲染模式下执行时,生成的数据是0。
所以我想知道是否有可能在离线渲染模式下获得它?
下面是我使用的代码:
class OfflineProcessingClass {
var tracker: AKFrequencyTracker!
var fftTap: AKFFTTap!
// ....
private func process(audioFile: AKAudioFile) throws {
// Make connection
let player = try AKAudioPlayer(file: audioFile)
tracker = AKFrequencyTracker(player)
fftTap = AKFFTTap(tracker)
AudioKit.output = tracker
// Setup offline rendering mode
let timeIntervalInSeconds: TimeInterval = 0.1
let sampleInterval = Int(floor(timeIntervalInSeconds * audioFile.sampleRate))
try AudioKit.engine.enableManualRenderingMode(
.offline,
format: audioFile.fileFormat,
maximumFrameCount: AVAudioFrameCount(sampleInterval)
)
// Setup buffer
let buffer = AVAudioPCMBuffer(
pcmFormat: AudioKit.engine.manualRenderingFormat,
frameCapacity: AudioKit.engine.manualRenderingMaximumFrameCount
)
// Start processing
try AudioKit.start()
player.start()
// Read file offline
while AudioKit.engine.manualRenderingSampleTime < audioFile.length {
let frameCount = audioFile.length - manualRenderingSampleTime
let framesToRender = min(AVAudioFrameCount(frameCount), buffer.frameCapacity)
try! AudioKit.engine.renderOffline(framesToRender, to: buffer)
// track is good
print("\(tracker.amplitude) dB - \(tracker!.frequency) Hz")
// Array of 0
print(fftTap.fftData) /////////////// <====== Error is here
}
// End processing
player.stop()
AudioKit.engine.stop()
}
}你在这段代码中看到错误了吗?
发布于 2021-09-28 15:08:53
这是因为BaseTap中的handleTapBlock在主队列上执行异步调度。这意味着,由于您占用了for循环中的主队列,BaseTap将永远没有机会获得任何回调。您需要放弃主队列才能正常工作。
https://stackoverflow.com/questions/61396664
复制相似问题