【发布时间】:2017-03-10 16:09:58
【问题描述】:
我正在尝试对我正在使用 AVAudioEngine 图播放的 mp3 文件的原始 PCM 样本进行一些计算。我每 44100 个样本都有一个闭包,提供一个AVAudioPCMBuffer。它具有UnsafePointer<UnsafeMutablePointer<Float>>? 类型的属性channelData。我没有在 Swift 3 中使用过指针,所以我不清楚如何访问这些 Float 值。
我有以下代码,但有很多问题:
audioPlayerNode.installTap(onBus: 0,
bufferSize: 1024,
format: audioPlayerNode.outputFormat(forBus: 0)) { (pcmBuffer, time) in
let numChans = Int(pcmBuffer.format.channelCount)
let frameLength = pcmBuffer.frameLength
if let chans = pcmBuffer.floatChannelData?.pointee {
for a in 0..<numChans {
let samples = chans[a]// samples is type Float. should be pointer to Floats.
for b in 0..<flength {
print("sample: \(b)") // should be samples[b] but that gives error as "samples" is Float
}
}
}
例如,我如何遍历UnsafeMutablePointer<Floats,它们是N 浮点指针,其中N 是缓冲区中的通道数。我在 Apple Docs on this Class 中找不到有关访问缓冲区样本的讨论。
我认为主要问题是let samples = chans[a]。 Xcode 说chans 是UnsafeMutablePointer<Float> 类型。但这应该是这些指针的 NumChannels 价值。这就是为什么我使用a in 0..<numChans 为其下标。然而,当我这样做时,我只会得到Float。
编辑:
嗯,似乎使用chans.advanced(by: a) 而不是下标固定的东西
【问题讨论】:
-
首先,检查
numChans是否为1
标签: swift audio avfoundation avaudioengine avaudiopcmbuffer