title: AVAssetReader audio capture, samples missing
body: <p>I've a weird question. I'm capturing some audio samples of a .mov/.wav/.aiff files to playback them.
  I'm using the following codes that captures unchanged PCM samples if available , otherwise convert them to 32 bit float.</p>
  
  <pre><code>NSError *error = nil;
  AVAssetReader *assetReader= [[[AVAssetReader alloc] initWithAsset:self.movieAsset error:&amp;error] autorelease];
  NSArray *audioTracks=[movieAsset tracksWithMediaType:AVMediaTypeAudio];
  AVAssetReaderTrackOutput* audioReaderOutput=nil;
  AVAssetTrack *mainAudioTrack = nil;
  CMTimeRange audioRange;
  if ([audioTracks count]) {
      mainAudioTrack=[audioTracks objectAtIndex:0];
      audioRange = mainAudioTrack.timeRange;
      CMTimeRange readingRange = CMTimeRangeMake(kCMTimeZero,audioRange.duration);
      assetReader.timeRange = readingRange;
      NSArray* formatDesc = mainAudioTrack.formatDescriptions;
      if ([formatDesc count]) {
          CMAudioFormatDescriptionRef item = (CMAudioFormatDescriptionRef)[formatDesc objectAtIndex:0];
          const AudioStreamBasicDescription* pcmAudioDescription = CMAudioFormatDescriptionGetStreamBasicDescription (item);
          NSDictionary * outputSettings;
          memcpy(&amp;audioDescription,pcmAudioDescription,sizeof(AudioStreamBasicDescription));
  
          if (pcmAudioDescription-&gt;mFormatID != kAudioFormatLinearPCM ) {
  
              // Resample
              outputSettings = [NSDictionary dictionaryWithObjectsAndKeys:
                                [NSNumber numberWithInt:kAudioFormatLinearPCM], AVFormatIDKey,
                                [NSNumber numberWithFloat:pcmAudioDescription-&gt;mSampleRate], AVSampleRateKey,
                                [NSNumber numberWithInt:pcmAudioDescription-&gt;mChannelsPerFrame], AVNumberOfChannelsKey,
                                [NSNumber numberWithInt:32], AVLinearPCMBitDepthKey,
                                [NSNumber numberWithBool:YES], AVLinearPCMIsFloatKey,
                                nil];
  
              audioDescription.mFormatID = kAudioFormatLinearPCM;
              audioDescription.mBitsPerChannel = 32;
              audioDescription.mFramesPerPacket = 1;
              audioDescription.mChannelsPerFrame = pcmAudioDescription-&gt;mChannelsPerFrame;
              audioDescription.mBytesPerFrame = audioDescription.mBitsPerChannel / 8 * audioDescription.mChannelsPerFrame;
              audioDescription.mBytesPerPacket = audioDescription.mFramesPerPacket * audioDescription.mBytesPerFrame;
              audioDescription.mFormatFlags = kAudioFormatFlagIsFloat;
  
          } else {
  
              outputSettings = [NSDictionary dictionaryWithObjectsAndKeys:
                                [NSNumber numberWithInt:kAudioFormatLinearPCM], AVFormatIDKey,
                                nil];
  
              audioDescription.mFormatID = kAudioFormatLinearPCM;
          }
  
          audioReaderOutput=[[[AVAssetReaderTrackOutput alloc]
                              initWithTrack:mainAudioTrack
                              outputSettings:outputSettings] autorelease];
          if([assetReader canAddOutput:audioReaderOutput]) [assetReader addOutput:audioReaderOutput];
          else audioReaderOutput = nil;
  
      }
  
  }
  
  if (audioReaderOutput) {
      if([assetReader startReading]==YES){
          CMSampleBufferRef buffer = 0;
          NSTimeInterval duration = 0.0;
  
          while([assetReader status]==AVAssetReaderStatusReading){
              if (audioReaderOutput != nil) {
                  buffer=[audioReaderOutput copyNextSampleBuffer];
                  if (buffer) {
                      CMTime sampleDuration = CMSampleBufferGetDuration (buffer);
                      CMTime currentSampleTime = CMSampleBufferGetOutputPresentationTimeStamp (buffer);
                      MovieSample* sample = [[[MovieSample alloc] init] autorelease];
                      sample.sampleTime = currentSampleTime;
                      sample.sampleBuffer = buffer;
                      sample.sampleDuration = sampleDuration;
                      [self.audioStore addObject:sample];
                      //NSLog(@"Adding sample %lld %lld %f %f",currentSampleTime.value,sampleDuration.value,currentOutputSampleTime.value/(NSTimeInterval)currentOutputSampleTime.timescale,
                           // (currentOutputSampleTime.value+sampleDuration.value)/(NSTimeInterval)currentOutputSampleTime.timescale);
                      NSLog(@"Received PCM buffer with [TIMESTAMP:%.1fms]", CMTimeGetSeconds(currentSampleTime) * 1000);
                      NSLog(@"Buffer contains [SAMPLES:%ld]", CMSampleBufferGetNumSamples(buffer));
                      NSLog(@"Buffer contains [DURATION:%.1fms] worth of audio", CMTimeGetSeconds(sampleDuration) * 1000);
                      duration += CMTimeGetSeconds(CMSampleBufferGetDuration(buffer));
                  }
  
              }
  
          }
          NSLog(@"Total samples duration: %f", duration);
          NSLog(@"Total track reported duration: %f", audioRange.duration.value/(NSTimeInterval)audioRange.duration.timescale);
  
      }
      else {
          DLog(@"could not start Audio reading asset.");
          DLog(@"reader status: %ld", [assetReader status]);
      }
  
  
  }
  </code></pre>
  
  <p>Now the weird thing is that, no matter what file I open, it always miss some samples at the of the file. 
  The following loop <a href="http://www.vvertex.com/loop.wav" rel="nofollow">http://www.vvertex.com/loop.wav</a> has a duration reported as 3.75 either from Audacity and from the audio track duration, mainAudioTrack.timeRange, while the dump of my code that prints the samples length and playing offset is: 3.657120</p>
  
  <p>This is the dump:</p>
  
  <p>2014-03-06 10:48:15.721 FrameDecoder[665:303] Received PCM buffer with [TIMESTAMP:0.0ms]
  2014-03-06 10:48:15.721 FrameDecoder[665:303] Buffer contains [SAMPLES:8192]
  2014-03-06 10:48:15.721 FrameDecoder[665:303] Buffer contains [DURATION:185.8ms] worth of audio
  2014-03-06 10:48:15.721 FrameDecoder[665:303] Received PCM buffer with [TIMESTAMP:185.8ms]
  2014-03-06 10:48:15.721 FrameDecoder[665:303] Buffer contains [SAMPLES:8192]
  2014-03-06 10:48:15.721 FrameDecoder[665:303] Buffer contains [DURATION:185.8ms] worth of audio
  2014-03-06 10:48:15.722 FrameDecoder[665:303] Received PCM buffer with [TIMESTAMP:371.5ms]
  2014-03-06 10:48:15.722 FrameDecoder[665:303] Buffer contains [SAMPLES:8192]
  2014-03-06 10:48:15.722 FrameDecoder[665:303] Buffer contains [DURATION:185.8ms] worth of audio
  2014-03-06 10:48:15.722 FrameDecoder[665:303] Received PCM buffer with [TIMESTAMP:557.3ms]
  2014-03-06 10:48:15.722 FrameDecoder[665:303] Buffer contains [SAMPLES:8192]
  2014-03-06 10:48:15.722 FrameDecoder[665:303] Buffer contains [DURATION:185.8ms] worth of audio
  2014-03-06 10:48:15.723 FrameDecoder[665:303] Received PCM buffer with [TIMESTAMP:743.0ms]
  2014-03-06 10:48:15.723 FrameDecoder[665:303] Buffer contains [SAMPLES:8192]
  2014-03-06 10:48:15.723 FrameDecoder[665:303] Buffer contains [DURATION:185.8ms] worth of audio
  2014-03-06 10:48:15.723 FrameDecoder[665:303] Received PCM buffer with [TIMESTAMP:928.8ms]
  2014-03-06 10:48:15.726 FrameDecoder[665:303] Buffer contains [SAMPLES:8192]
  2014-03-06 10:48:15.726 FrameDecoder[665:303] Buffer contains [DURATION:185.8ms] worth of audio
  2014-03-06 10:48:15.727 FrameDecoder[665:303] Received PCM buffer with [TIMESTAMP:1114.6ms]
  2014-03-06 10:48:15.727 FrameDecoder[665:303] Buffer contains [SAMPLES:8192]
  2014-03-06 10:48:15.727 FrameDecoder[665:303] Buffer contains [DURATION:185.8ms] worth of audio
  2014-03-06 10:48:15.727 FrameDecoder[665:303] Received PCM buffer with [TIMESTAMP:1300.3ms]
  2014-03-06 10:48:15.727 FrameDecoder[665:303] Buffer contains [SAMPLES:8192]
  2014-03-06 10:48:15.727 FrameDecoder[665:303] Buffer contains [DURATION:185.8ms] worth of audio
  2014-03-06 10:48:15.727 FrameDecoder[665:303] Received PCM buffer with [TIMESTAMP:1486.1ms]
  2014-03-06 10:48:15.728 FrameDecoder[665:303] Buffer contains [SAMPLES:8192]
  2014-03-06 10:48:15.728 FrameDecoder[665:303] Buffer contains [DURATION:185.8ms] worth of audio
  2014-03-06 10:48:15.728 FrameDecoder[665:303] Received PCM buffer with [TIMESTAMP:1671.8ms]
  2014-03-06 10:48:15.728 FrameDecoder[665:303] Buffer contains [SAMPLES:8192]
  2014-03-06 10:48:15.728 FrameDecoder[665:303] Buffer contains [DURATION:185.8ms] worth of audio
  2014-03-06 10:48:15.728 FrameDecoder[665:303] Received PCM buffer with [TIMESTAMP:1857.6ms]
  2014-03-06 10:48:15.729 FrameDecoder[665:303] Buffer contains [SAMPLES:8192]
  2014-03-06 10:48:15.729 FrameDecoder[665:303] Buffer contains [DURATION:185.8ms] worth of audio
  2014-03-06 10:48:15.729 FrameDecoder[665:303] Received PCM buffer with [TIMESTAMP:2043.4ms]
  2014-03-06 10:48:15.729 FrameDecoder[665:303] Buffer contains [SAMPLES:8192]
  2014-03-06 10:48:15.729 FrameDecoder[665:303] Buffer contains [DURATION:185.8ms] worth of audio
  2014-03-06 10:48:15.729 FrameDecoder[665:303] Received PCM buffer with [TIMESTAMP:2229.1ms]
  2014-03-06 10:48:15.730 FrameDecoder[665:303] Buffer contains [SAMPLES:8192]
  2014-03-06 10:48:15.730 FrameDecoder[665:303] Buffer contains [DURATION:185.8ms] worth of audio
  2014-03-06 10:48:15.730 FrameDecoder[665:303] Received PCM buffer with [TIMESTAMP:2414.9ms]
  2014-03-06 10:48:15.730 FrameDecoder[665:303] Buffer contains [SAMPLES:8192]
  2014-03-06 10:48:15.730 FrameDecoder[665:303] Buffer contains [DURATION:185.8ms] worth of audio
  2014-03-06 10:48:15.731 FrameDecoder[665:303] Received PCM buffer with [TIMESTAMP:2600.6ms]
  2014-03-06 10:48:15.731 FrameDecoder[665:303] Buffer contains [SAMPLES:8192]
  2014-03-06 10:48:15.731 FrameDecoder[665:303] Buffer contains [DURATION:185.8ms] worth of audio
  2014-03-06 10:48:15.731 FrameDecoder[665:303] Received PCM buffer with [TIMESTAMP:2786.4ms]
  2014-03-06 10:48:15.731 FrameDecoder[665:303] Buffer contains [SAMPLES:8192]
  2014-03-06 10:48:15.731 FrameDecoder[665:303] Buffer contains [DURATION:185.8ms] worth of audio
  2014-03-06 10:48:15.732 FrameDecoder[665:303] Received PCM buffer with [TIMESTAMP:2972.2ms]
  2014-03-06 10:48:15.732 FrameDecoder[665:303] Buffer contains [SAMPLES:8192]
  2014-03-06 10:48:15.732 FrameDecoder[665:303] Buffer contains [DURATION:185.8ms] worth of audio
  2014-03-06 10:48:15.732 FrameDecoder[665:303] Received PCM buffer with [TIMESTAMP:3157.9ms]
  2014-03-06 10:48:15.732 FrameDecoder[665:303] Buffer contains [SAMPLES:8192]
  2014-03-06 10:48:15.733 FrameDecoder[665:303] Buffer contains [DURATION:185.8ms] worth of audio
  2014-03-06 10:48:15.733 FrameDecoder[665:303] Received PCM buffer with [TIMESTAMP:3343.7ms]
  2014-03-06 10:48:15.733 FrameDecoder[665:303] Buffer contains [SAMPLES:8192]
  2014-03-06 10:48:15.733 FrameDecoder[665:303] Buffer contains [DURATION:185.8ms] worth of audio
  2014-03-06 10:48:15.733 FrameDecoder[665:303] Received PCM buffer with [TIMESTAMP:3529.4ms]
  2014-03-06 10:48:15.734 FrameDecoder[665:303] Buffer contains [SAMPLES:5631]
  2014-03-06 10:48:15.734 FrameDecoder[665:303] Buffer contains [DURATION:127.7ms] worth of audio
  2014-03-06 10:48:15.734 FrameDecoder[665:303] Total samples duration: 3.657120
  2014-03-06 10:48:15.734 FrameDecoder[665:303] Total track reported duration: 3.750000</p>
  
  <p>Anyone ever faced a strange issue like this ? I've tried several audio files as well as .mov files with different compressions. Same stuff ! I'm totally stuck on this !</p>
  
  <p>Also when I play the samples through AudioUnits, it seams that what's effectively missing is that from the beginning of the sample....</p>
  
  <p>Thanks !</p>