title: Can't reverse AVAsset audio properly. The only result is white noise
body: <p>I'm trying to reverse an <code>AVAsset</code> audio and save it to a file. To make things clear, I've made simple application with the issue <a href="https://github.com/ksenia-lyagusha/AudioReverse.git" rel="nofollow noreferrer">https://github.com/ksenia-lyagusha/AudioReverse.git</a></p>
  
  <p>The application takes <code>mp4</code> video file from bundle, exports it to Temporary folder in the sandbox as single <code>m4a</code> file, then tries to read it from there, reverse and save result file back.
  Temporary <code>m4a</code> file is OK.</p>
  
  <p>The only result of my reverse part is Audio file in the <code>Sandbox</code> with white noise. </p>
  
  <p>There is the part of code below, that is in charge of reversing <code>AVAsset</code>. It is based on related questions </p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/6593118/how-to-reverse-an-audio-file">How to reverse an audio file?</a></li>
  <li><a href="https://stackoverflow.com/questions/6707998/ios-audio-manipulation-play-local-caf-file-backwards">iOS audio manipulation - play local .caf file backwards</a> </li>
  </ul>
  
  <p>However, it doesn't work for me.</p>
  
  <pre><code>OSStatus theErr = noErr;
  UInt64 fileDataSize = 0;
  AudioFileID inputAudioFile;
  AudioStreamBasicDescription theFileFormat;
  UInt32 thePropertySize = sizeof(theFileFormat);
  
  theErr = AudioFileOpenURL((__bridge CFURLRef)[NSURL URLWithString:inputPath], kAudioFileReadPermission, 0, &amp;inputAudioFile);
  
  thePropertySize = sizeof(fileDataSize);
  theErr = AudioFileGetProperty(inputAudioFile, kAudioFilePropertyAudioDataByteCount, &amp;thePropertySize, &amp;fileDataSize);
  
  UInt32 ps = sizeof(AudioStreamBasicDescription) ;
  AudioFileGetProperty(inputAudioFile, kAudioFilePropertyDataFormat, &amp;ps, &amp;theFileFormat);
  
  UInt64 dataSize = fileDataSize;
  void *theData = malloc(dataSize);
  
  // set up output file
  AudioFileID outputAudioFile;
  
  AudioStreamBasicDescription myPCMFormat;
  
  myPCMFormat.mSampleRate       = 44100;
  myPCMFormat.mFormatID         = kAudioFormatLinearPCM;
  // kAudioFormatFlagsCanonical is deprecated
  myPCMFormat.mFormatFlags      = kAudioFormatFlagIsFloat | kAudioFormatFlagIsNonInterleaved;
  myPCMFormat.mChannelsPerFrame = 1;
  myPCMFormat.mFramesPerPacket  = 1;
  myPCMFormat.mBitsPerChannel   = 32;
  myPCMFormat.mBytesPerPacket   = (myPCMFormat.mBitsPerChannel / 8) * myPCMFormat.mChannelsPerFrame;
  myPCMFormat.mBytesPerFrame    = myPCMFormat.mBytesPerPacket;
  
  
  
  NSString *exportPath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"ReverseAudio.caf"];
  NSURL *outputURL = [NSURL fileURLWithPath:exportPath];
  
  theErr = AudioFileCreateWithURL((__bridge CFURLRef)outputURL,
                         kAudioFileCAFType,
                         &amp;myPCMFormat,
                         kAudioFileFlags_EraseFile,
                         &amp;outputAudioFile);
  
  //Read data into buffer
  //if readPoint  = dataSize, then bytesToRead = 0 in while loop and
  //it is endless
  SInt64 readPoint  = dataSize-1;
  UInt64 writePoint = 0;
  
  while(readPoint &gt; 0)
  {
      UInt32 bytesToRead = 2;
      AudioFileReadBytes(inputAudioFile, false, readPoint, &amp;bytesToRead, theData);
      // bytesToRead is now the amount of data actually read
  
      UInt32 bytesToWrite = bytesToRead;
      AudioFileWriteBytes(outputAudioFile, false, writePoint, &amp;bytesToWrite, theData);
      // bytesToWrite is now the amount of data actually written
  
      writePoint += bytesToWrite;
      readPoint -= bytesToRead;
  }
  
  free(theData);
  AudioFileClose(inputAudioFile);
  AudioFileClose(outputAudioFile);
  </code></pre>
  
  <p>If I change file type in <code>AudioFileCreateWithURL</code> from <code>kAudioFileCAFType</code> to another the result file is not created in the <code>Sandbox</code> at all.</p>
  
  <p>Thanks for any help.</p>