3

我使用 AVAudioEngine 录制了一个音频文件:

[mainMixer installTapOnBus:0 bufferSize:4096 format:[mainMixer outputFormatForBus:0] block:^(AVAudioPCMBuffer *buffer, AVAudioTime *when) {
    NSError *error;
    // as AVAudioPCMBuffer's are delivered this will write sequentially. The buffer's frameLength signifies how much of the buffer is to be written
    // IMPORTANT: The buffer format MUST match the file's processing format which is why outputFormatForBus: was used when creating the AVAudioFile object above
    NSAssert([mixerOutputFile writeFromBuffer:buffer error:&error], @"error writing buffer data to file, %@", [error localizedDescription]);
}];

现在我想获取该文件的样本并删除文件开头和结尾的静音部分。我正在使用缓冲区来读取文件:

AVAudioFrameCount kBufferFrameCapacity = 128 * 1024L;
AVAudioPCMBuffer *readBuffer = [[AVAudioPCMBuffer alloc]initWithPCMFormat:audioFile.processingFormat frameCapacity:kBufferFrameCapacity];
[audioFile readIntoBuffer:readBuffer error: &error]

然后我试图访问音频文件样本的浮点值:

for (AVAudioChannelCount channelIndex = 0; channelIndex < readBuffer.format.channelCount; ++channelIndex)
{
    float *channelData = readBuffer.floatChannelData[channelIndex];

    for (AVAudioFrameCount frameIndex = 0; frameIndex < readBuffer.frameLength; ++frameIndex)
    {            
       float sampleLevel = channelData[frameIndex];
       .... 

如果我 NSLog sampleLevel 总是返回 0.0。我究竟做错了什么?我是以正确的方式获取音频文件的样本还是我完全错了?

4

0 回答 0