我使用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]);
}];
现在我想获取该文件的样本并删除文件开头和结尾的静默部分。我' m使用缓冲区来读取文件:
AVAudioFrameCount kBufferFrameCapacity = 128 * 1024L;
AVAudioPCMBuffer *readBuffer = [[AVAudioPCMBuffer alloc]initWithPCMFormat:audioFile.processingFormat frameCapacity:kBufferFrameCapacity];
[audioFile readIntoBuffer:readBuffer error: &error]
然后我试图访问audioFile样本的浮点值:
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。我究竟做错了什么?我是以正确的方式采集audioFile的样本还是我在这里完全错了?