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

前端 未结 2 1164
粉色の甜心
粉色の甜心 2021-01-01 05:00

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

2条回答
  •  佛祖请我去吃肉
    2021-01-01 05:12

    It depends how ' real-time ' you need it

    if you need it very crisp, go down right at the bottom level and use audio units. that means setting up an INPUT callback. remember, when this fires you need to allocate your own buffers and then request the audio from the microphone.

    ie don't get fooled by the presence of a buffer pointer in the parameters... it is only there because Apple are using the same function declaration for the input and render callbacks.

    here is a paste out of one of my projects:

    OSStatus dataArrivedFromMic(
                        void                        * inRefCon, 
                        AudioUnitRenderActionFlags  * ioActionFlags, 
                        const AudioTimeStamp        * inTimeStamp, 
                        UInt32                      inBusNumber, 
                        UInt32                      inNumberFrames, 
                        AudioBufferList             * dummy_notused )
    {    
        OSStatus status;
    
        RemoteIOAudioUnit* unitClass = (RemoteIOAudioUnit *)inRefCon;
    
        AudioComponentInstance myUnit = unitClass.myAudioUnit;
    
        AudioBufferList ioData;
        {
            int kNumChannels = 1; // one channel...
    
            enum {
                kMono = 1,
                kStereo = 2
            };
    
            ioData.mNumberBuffers = kNumChannels;
    
            for (int i = 0; i < kNumChannels; i++) 
            {
                int bytesNeeded = inNumberFrames * sizeof( Float32 );
    
                ioData.mBuffers[i].mNumberChannels = kMono;
                ioData.mBuffers[i].mDataByteSize = bytesNeeded;
                ioData.mBuffers[i].mData = malloc( bytesNeeded );
            }
        }
    
        // actually GET the data that arrived
        status = AudioUnitRender( (void *)myUnit, 
                                 ioActionFlags, 
                                 inTimeStamp, 
                                 inBusNumber, 
                                 inNumberFrames, 
                                 & ioData );
    
    
        // take MONO from mic
        const int channel = 0;
        Float32 * outBuffer = (Float32 *) ioData.mBuffers[channel].mData;
    
        // get a handle to our game object
        static KPRing* kpRing = nil;
        if ( ! kpRing )
        {
            //AppDelegate *  appDelegate = [UIApplication sharedApplication].delegate;
    
            kpRing = [Game singleton].kpRing;
    
            assert( kpRing );
        }
    
        // ... and send it the data we just got from the mic
        [ kpRing floatsArrivedFromMic: outBuffer
                                count: inNumberFrames ];
    
        return status;
    }
    

提交回复
热议问题