3

I have recorded an audio file using 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]);
}];

Now I want to take the samples of that file and delete the silent parts at the beginning and at the end of the file. I' m using a buffer to read the file:

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

and then I 'm trying to access the float values of the samples of the 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];
       .... 

If I NSLog sampleLevel always return 0.0. What am I doing wrong? Am I taking the samples of the audioFile with the right way or I am totally wrong here?

Thomas
  • 177
  • 2
  • 9
  • Are you sure that audio file format use float point numbers for samples? – John Tracid Sep 17 '15 at 23:17
  • Sorry for my late reply John but I was out of business for a couple of days. Well my audio file format is: AVAudioFormat 0x7b13ff10: 2 ch, 44100 Hz, Float32, non-inter, so I do have floating point samples. – Thomas Sep 21 '15 at 06:43
  • Could you provide test WAV file? I checked with simple 0.5 seconds sine wave stereo file and it works fine so I think that problem either in file itself (zero samples) or somewhere in your code. – John Tracid Sep 21 '15 at 12:17
  • To tell you the truth I don't know what's exactly going on here. While with NSLog I take 0.0000 values when I'm trying to find my first non zero sample my code works fine: – Thomas Sep 22 '15 at 06:08
  • for (AVAudioChannelCount channelIndex = 0; channelIndex < readBuffer.format.channelCount; ++channelIndex) { float *channelData = readBuffer.floatChannelData[channelIndex]; for (AVAudioFrameCount frameIndex = 0; frameIndex < readBuffer.frameLength; ++frameIndex) { float sampleAbsLevel = fabs(channelData[frameIndex]); if (sampleAbsLevel > 0.0) { samplePosition1 = readPosition + frameIndex; } } – Thomas Sep 22 '15 at 06:14
  • When I play my file from samplePosition1 I have no silence it starts immediately. But now i have another problem. I'm trying to write the contents of my buffer to another buffer from samplePosition1 to samplePosition2(the start and the end of the real recorded sound without silence). I'm using a new buffer: AVAudioPCMBuffer *finalBuffer = [[AVAudioPCMBuffer alloc]initWithPCMFormat:readBuffer.format frameCapacity:(AVAudioFrameCount)(readBuffer.frameLength)]; – Thomas Sep 22 '15 at 06:24
  • And then the same loops as above: for (AVAudioChannelCount channelIndex = 0; channelIndex < finalBuffer.format.channelCount; ++channelIndex) { float *channelData = finalBuffer.floatChannelData[channelIndex]; float *channelData2 = readBuffer.floatChannelData[channelIndex]; for (AVAudioFrameCount frameIndex = 0; frameIndex < (AVAudioFrameCount)(samplePosition2 -samplePosition1); ++frameIndex) { channelData[frameIndex] = channelData2[frameIndex + samplePosition1]; } } – Thomas Sep 22 '15 at 06:27
  • While my samples are ok here (if you NSLog into the second for loop) when I try to schedule and play: [_mixerOutputFilePlayer scheduleBuffer:finalBuffer atTime:nil options:AVAudioPlayerNodeBufferLoops completionHandler:nil]; [_mixerOutputFilePlayer play]; I get an error like AURemoteIO...EXC_ARITHMETIC and my final buffer's length is zero so I don't have valid samples. Why I can't write my samples from one buffer to another with this approach? Any idea on that? Sorry for the difficult to read comments with code. – Thomas Sep 22 '15 at 06:40
  • Better to add this to your question or create new one about buffer copy problem. – John Tracid Sep 22 '15 at 18:53

0 Answers0