Search code examples
audiocore-audiosampling

Simplest way to capture raw audio from audio input for real time processing on a mac


What is the simplest way to capture audio from the built in audio input and be able to read the raw sampled values (as in a .wav) in real time as they come in when requested, like reading from a socket.

Hopefully code that uses one of Apple's frameworks (Audio Queues). Documentation is not very clear, and what I need is very basic.


Solution

  • Try the AudioQueue Framework for this. You mainly have to perform 3 steps:

    1. setup an audio format how to sample the incoming analog audio
    2. start a new recording AudioQueue with AudioQueueNewInput()
    3. Register a callback routine which handles the incoming audio data packages

    In step 3 you have a chance to analyze the incoming audio data with AudioQueueGetProperty()

    It's roughly like this:

    static void HandleAudioCallback (void                               *aqData,
                                     AudioQueueRef                      inAQ,
                                     AudioQueueBufferRef                inBuffer, 
                                     const AudioTimeStamp               *inStartTime, 
                                     UInt32                             inNumPackets, 
                                     const AudioStreamPacketDescription *inPacketDesc) {
        // Here you examine your audio data
    }
    
    static void StartRecording() {
        // now let's start the recording
        AudioQueueNewInput (&aqData.mDataFormat,  // The sampling format how to record
                            HandleAudioCallback,  // Your callback routine
                            &aqData,              // e.g. AudioStreamBasicDescription
                            NULL,
                            kCFRunLoopCommonModes, 
                            0, 
                            &aqData.mQueue);      // Your fresh created AudioQueue
        AudioQueueStart(aqData.mQueue,
                        NULL);
    }
    

    I suggest the Apple AudioQueue Services Programming Guide for detailled information about how to start and stop the AudioQueue and how to setup correctly all ther required objects.

    You may also have a closer look into Apple's demo prog SpeakHere. But this is IMHO a bit confusing to start with.