title: Decoding AAC ADTS to PCM (weird sound)
body: <p>I'm trying to decode AAC to PCM and everything seems to be working okay. The problem is that I can hear an strange sound in my audio file (Like a snake) :)</p>
  
  <p>The audio file is this one: <a href="https://www.dropbox.com/s/2a6c1m38w1cu0km/myaudiofileWrite.m4a?dl=0" rel="nofollow">https://www.dropbox.com/s/2a6c1m38w1cu0km/myaudiofileWrite.m4a?dl=0</a></p>
  
  <p>my method to decode AAC ADTS to PCM is this:</p>
  
  <pre><code>-(void) audioFileReaderWithData: (NSData *) audioData {
  
      AudioFileID         refAudioFileID;
      ExtAudioFileRef     inputFileID;
  
  
      OSStatus result = AudioFileOpenWithCallbacks((__bridge void * _Nonnull)(audioData), readProc, NULL, getSizeProc, NULL, kAudioFileAAC_ADTSType, &amp;refAudioFileID);
      if(result != noErr){
          NSLog(@"problem in theAudioFileReaderWithData function: result code %i \n", result);
      }
  
      //kAudioFilePermissionsError
      result = ExtAudioFileWrapAudioFileID(refAudioFileID, false, &amp;inputFileID);
      if (result != noErr){
          NSLog(@"problem in theAudioFileReaderWithData function Wraping the audio FileID: result code %i \n", result);
      }
  
      // Client Audio Format Description
      AudioStreamBasicDescription clientFormat =[self getAudioDescription];
  
  
      //Output Audio Format Description
      AudioStreamBasicDescription outputFormat =[self getAACAudioDescription];
  
      UInt32 codec = kAppleHardwareAudioCodecManufacturer;
      int size = sizeof(codec);
      result = ExtAudioFileSetProperty(outputFileID,
                                       kExtAudioFileProperty_CodecManufacturer,
                                       size,
                                       &amp;codec);
  
      if(result) printf("ExtAudioFileSetProperty %d \n", (int)result);
  
      // create the outputFile that we're writing to here....
      UInt32 outputFormatSize = sizeof(outputFormat);
      result = 0;
      result = AudioFormatGetProperty(kAudioFormatProperty_FormatInfo, 0, NULL, &amp;outputFormatSize, &amp;outputFormat);
      if(result != noErr)
          NSLog(@"could not set the output format with status code %i \n",result);
  
  
  
  
      size = sizeof(clientFormat);
      result = 0;
      result = ExtAudioFileSetProperty(inputFileID, kExtAudioFileProperty_ClientDataFormat, size, &amp;clientFormat);
  
      if(result != noErr)
          NSLog(@"error on ExtAudioFileSetProperty for input File with result code %i \n", result);
  
      size = sizeof(clientFormat);
      result = 0;
      result = ExtAudioFileSetProperty(outputFileID, kExtAudioFileProperty_ClientDataFormat, size, &amp;clientFormat);
      if(result != noErr)
          NSLog(@"error on ExtAudioFileSetProperty for output File with result code %i \n", result);
  
      int totalFrames = 0;
      UInt32 outputFilePacketPosition = 0; //in bytes
      UInt32 encodedBytes = 0;
  
  
      while (1) {
          UInt32 bufferByteSize       = 22050 * 4 * 2;
          char srcBuffer[bufferByteSize];
          UInt32 numFrames            = (bufferByteSize/clientFormat.mBytesPerFrame);
  
          AudioBufferList fillBufList;
          fillBufList.mNumberBuffers  = 1;
          fillBufList.mBuffers[0].mNumberChannels     = clientFormat.mChannelsPerFrame;
          fillBufList.mBuffers[0].mDataByteSize       = bufferByteSize;
          fillBufList.mBuffers[0].mData               = srcBuffer;
          result = 0;
          result = ExtAudioFileRead(inputFileID, &amp;numFrames, &amp;fillBufList);
  
          if (result != noErr) {
              NSLog(@"Error on ExtAudioFileRead with result code %i \n", result);
              totalFrames = 0;
              break;
          }
          if (!numFrames)
              break;
  
          totalFrames = totalFrames + numFrames;
  
          result = 0;
          result = ExtAudioFileWrite(outputFileID,
                                     numFrames,
                                     &amp;fillBufList);
  
          if(result!= noErr){
              NSLog(@"ExtAudioFileWrite failed with code %i \n", result);
          }
  
          encodedBytes += numFrames  * clientFormat.mBytesPerFrame;
  
      }
  
      //Clean up
  
      ExtAudioFileDispose(inputFileID);
      //ExtAudioFileDispose(outputFileID); I'm calling this in other method
      AudioFileClose(refAudioFileID);
  
  }
  
  static OSStatus readProc(void* clientData,
                           SInt64 position,
                           UInt32 requestCount,
                           void* buffer,
                           UInt32* actualCount)
  {
  
      NSData *inAudioData = (__bridge NSData *) clientData;
  
      size_t dataSize = inAudioData.length;
      size_t bytesToRead = 0;
  
      if(position &lt; dataSize) {
          size_t bytesAvailable = dataSize - position;
          bytesToRead = requestCount &lt;= bytesAvailable ? requestCount : bytesAvailable;
  
          [inAudioData getBytes: buffer range:NSMakeRange(position, bytesToRead)];
      } else {
          NSLog(@"data was not read \n");
          bytesToRead = 0;
      }
  
      if(actualCount)
          *actualCount = bytesToRead;
  
      return noErr;
  }
  
  static SInt64 getSizeProc(void* clientData) {
      NSData *inAudioData = (__bridge NSData *)(clientData);
      size_t dataSize = inAudioData.length;
      return dataSize;
  }
  </code></pre>
  
  <p>formats:</p>
  
  <pre><code>- (AudioStreamBasicDescription)getAudioDescription {
      AudioStreamBasicDescription audioDescription = {0};
      audioDescription.mFormatID          = kAudioFormatLinearPCM;
      audioDescription.mFormatFlags       = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked | kAudioFormatFlagsNativeEndian;
      audioDescription.mChannelsPerFrame  = 1;
      audioDescription.mBytesPerPacket    = sizeof(SInt16)*audioDescription.mChannelsPerFrame;
      audioDescription.mFramesPerPacket   = 1;
      audioDescription.mBytesPerFrame     = sizeof(SInt16)*audioDescription.mChannelsPerFrame;
      audioDescription.mBitsPerChannel    = 8 * sizeof(SInt16);
      audioDescription.mSampleRate        = SAMPLE_RATE_AUDIO;
      return audioDescription;
  }
  
  - (AudioStreamBasicDescription)getAACAudioDescription {
      AudioStreamBasicDescription audioFormat;
      audioFormat.mSampleRate = 44100;
      audioFormat.mFormatID = kAudioFormatMPEG4AAC;
      audioFormat.mFormatFlags = kMPEG4Object_AAC_LC;
      audioFormat.mBytesPerPacket= 0;
      audioFormat.mFramesPerPacket= 1024;
      audioFormat.mBytesPerFrame= 0;
      audioFormat.mChannelsPerFrame= 1;
      audioFormat.mBitsPerChannel= 0;
      audioFormat.mReserved= 0;
      return audioFormat;
  }
  </code></pre>
  
  <p>Do you know what could be happening? </p>