How to set up a framework using AudioUnit.framework on iOS

I have an audio application that I need to record microphones for encoding to mp3 using ffmpeg

First set up the audio:

  / **  
      * We need to specifie our format on which we want to work.
      * We use Linear PCM cause its uncompressed and we work on raw data.
      * for more informations check.
      * 
      * We want 16 bits, 2 bytes (short bytes) per packet / frames at 8khz 
      * /
     AudioStreamBasicDescription audioFormat;
     audioFormat.mSampleRate = SAMPLE_RATE;
     audioFormat.mFormatID = kAudioFormatLinearPCM;
     audioFormat.mFormatFlags = kAudioFormatFlagIsPacked |  kAudioFormatFlagIsSignedInteger;
     audioFormat.mFramesPerPacket = 1;
     audioFormat.mChannelsPerFrame = 1;
     audioFormat.mBitsPerChannel = audioFormat.mChannelsPerFrame * sizeof (SInt16) * 8;
     audioFormat.mBytesPerPacket = audioFormat.mChannelsPerFrame * sizeof (SInt16);
     audioFormat.mBytesPerFrame = audioFormat.mChannelsPerFrame * sizeof (SInt16);

Record Callback:

  static OSStatus recordingCallback (void * inRefCon, 
                                   AudioUnitRenderActionFlags * ioActionFlags, 
                                   const AudioTimeStamp * inTimeStamp, 
                                   UInt32 inBusNumber, 
                                   UInt32 inNumberFrames, 
                                   AudioBufferList * ioData) 
 {
     NSLog (@ "Log record:% lu", inBusNumber);
     NSLog (@ "Log record:% lu", inNumberFrames);
     NSLog (@ "Log record:% lu", (UInt32) inTimeStamp);

     // the data gets rendered here
     AudioBuffer buffer;

     // a variable where we check the status
     OSStatus status;

     / **
      This is the reference to the object who owns the callback.
      * /
     AudioProcessor * audioProcessor = (__bridge AudioProcessor *) inRefCon;

     / **
      on this point we define the number of channels, which is mono
      for the iphone.  the number of frames is usally 512 or 1024.
      * /
     buffer.mDataByteSize = inNumberFrames * sizeof (SInt16);  // sample size
     buffer.mNumberChannels = 1;  // one channel

     buffer.mData = malloc (inNumberFrames * sizeof (SInt16));  // buffer size

     // we put our buffer into a bufferlist array for rendering
     AudioBufferList bufferList;
     bufferList.mNumberBuffers = 1;
     bufferList.mBuffers [0] = buffer;

     // render input and check for error
     status = AudioUnitRender ([audioProcessor audioUnit], ioActionFlags, inTimeStamp, inBusNumber, inNumberFrames, & bufferList);
     [audioProcessor hasError: status: __ FILE __: __ LINE__];

     // process the bufferlist in the audio processor
     [audioProcessor processBuffer: & bufferList];

     // clean up the buffer
     free (bufferList.mBuffers [0] .mData);


     // NSLog (@ "RECORD");
     return noErr;
 }

With data:

inBusNumber = 1

inNumberFrames = 1024

inTimeStamp = 80444304 // All the time the same inTimeStamp, this is strange

However, the format I need to encode mp3 is 1152. How do I configure it?

If I do buffering, this implies a delay, but I would like to avoid this because it is a real-time application. If I use this configuration, in each buffer I get counts with a bang, 1152 - 1024 = 128 bad samples. All samples are SInt16.

+4
source share
1 answer

You can configure the number of frames per fragment that AudioUnit will use with the kAudioUnitProperty_MaximumFramesPerSlice property. However, I believe that the best solution in your case is to buffer the incoming sound into the ring buffer, and then the signal from your encoder, which is available for audio. Since you are transcoding to MP3, I'm not sure what it means in this case in real time.

+2
source

Source: https://habr.com/ru/post/1440511/


All Articles