繁体   English   中英

在 AVAudioPCMBuffer 初始化程序中为 frameCapacity 设置什么值?

[英]What value to put for frameCapacity in AVAudioPCMBuffer initializer?

我正在使用AVAudioEngineAVAudioPlayerNodeAVAudioPCMBuffer制作节拍器。 缓冲区是这样创建的:

/// URL of the sound file
let soundURL = Bundle.main.url(forResource: <filename>, withExtension: "wav")!
/// Create audio file
let audioFile = try! AVAudioFile(forReading: soundURL)
let audioFormat = audioFile.processingFormat
    
/// Create the buffer - what value to put for frameCapacity?
if let buffer = AVAudioPCMBuffer(pcmFormat: audioFormat, frameCapacity: ???) {
    buffer.frameLength = audioFrameCount
    try? audioFile.read(into: buffer)
        
    return buffer
}

我在 AVAudioPCMBuffer 初始化程序中为 frameCapacity 设置了什么值?

文档说 frameCapacity 应该是“PCM 样本帧中缓冲区的容量”。 这意味着什么? 这是 static 值还是从音频文件中获取?

frameCapacityAVAudioPCMBuffer可以容纳的最大帧数。 您不必使用所有框架。 AVAudioPCMBuffer的消费者应该只参考frameLength帧,并且frameLength <= frameCapacity 如果您正在处理N帧块中的音频,并且无论出于何种原因,您会得到一个简短的阅读,那么与长度不同的容量可能会很有用:

let buffer = AVAudioPCMBuffer(pcmFormat: audioFormat, frameCapacity: N)

while readChunk(chunk) {
    let frameLength = min(buffer.frameCapacity, chunk.lengthInFrames)
    // copy frameLength frames into buffer.floatChannelData[0] or something
    buffer.frameLength = chunkLength // could be less than N
}

但是,如果您只想在缓冲区中存储audioFrameCount帧(文件的长度?),则将frameCapacity设置为:

let buffer = AVAudioPCMBuffer(pcmFormat: audioFormat, frameCapacity: audioFrameCount)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM