Search code examples
iosobjective-caudiocore-audio

Read encoded frames from audio file with ExtAudioFileSeek and ExtAudioFileRead


This is what I would like to do:

Get audio from the microphone

  • Encode it in AAC, G.711 or G.726
  • Write the encoded frames to a socket.

And this is how I'm trying to get there:

  • I'm getting audio (PCM) from the microphone using TheAmazingAudioEngine and putting it in a buffer;
  • Using TPAACAudioConverter I'm reading audio from my buffer and writing to a temp file (AAC);
  • In the processing thread of TPAACAudioConverter I replaced this:

    OSStatus status = ExtAudioFileWrite(destinationFile, numFrames, &fillBufList);
    

    with this:

    OSStatus status = ExtAudioFileWrite(destinationFile, numFrames, &fillBufList);
    
    UInt32 framesWritten = numFrames;
    totalFramesWritten += framesWritten;
    
    AudioBufferList readData;
    readData.mNumberBuffers = 1;
    
    ExtAudioFileSeek(destinationFile, totalFramesWritten - framesWritten);
    
    OSStatus readStatus = ExtAudioFileRead(destinationFile, &numFrames, &readData);
    
    ExtAudioFileSeek(destinationFile, totalFramesWritten);
    
    NSLog(@"Bytes read=%d", numFrames);
    

    but what I get is 0 numFrames read from file.

Any idea on what I may be doing wrong or any suggestion on alternative paths to achieve what I need?


Solution

  • I abandoned this approach and reused the AQRecorder class from the SpeakHere example by Apple.

    The project is available here https://github.com/robovm/apple-ios-samples/tree/master/SpeakHere.