5
votes

I'm writing an iOS app that takes input from the microphone, runs it through a high-pass filter audio unit, and plays it back through the speakers. I've been able to do this successfully by using the AUGraph API. In it, I put two nodes: a Remote I/O unit, and an effect audio unit (kAudioUnitType_Effect, kAudioUnitSubType_HighPassFilter), and connected the io unit's input element's output scope to the effect unit's input, and the effect node's output to the io unit's output element's input scope. But now I have the need to do some analysis based on the processed audio samples, so I need direct access to the buffer. This means (and please correct me if I'm wrong) I can no longer use AUGraphConnectNodeInput to make the connection between the effect node's output and the io unit's output element, and have to attach an render callback function for the io unit's output element, so that I can access the buffer whenever the speakers need new samples. I've done so, but I get a -50 error when I call the AudioUnitRender function in the render callback. I believe I have a case of ASBDs mismatch between the two audio units, since I'm not doing anything about that in the render callback (and the AUGraph took care of it before). Here's the code:

AudioController.h:

@interface AudioController : NSObject
{
    AUGraph mGraph;
    AudioUnit mEffects;
    AudioUnit ioUnit;
}

@property (readonly, nonatomic) AudioUnit mEffects;
@property (readonly, nonatomic) AudioUnit ioUnit;

-(void)initializeAUGraph;
-(void)startAUGraph;
-(void)stopAUGraph;

@end

AudioController.mm:

@implementation AudioController

…

static OSStatus renderInput(void *inRefCon, AudioUnitRenderActionFlags *ioActionFlags, const AudioTimeStamp *inTimeStamp, UInt32 inBusNumber, UInt32 inNumberFrames, AudioBufferList *ioData)
{
    AudioController *THIS = (__bridge AudioController*)inRefCon;

    AudioBuffer buffer;

    AudioStreamBasicDescription fxOutputASBD;
    UInt32 fxOutputASBDSize = sizeof(fxOutputASBD);
    AudioUnitGetProperty([THIS mEffects], kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, 0, &fxOutputASBD, &fxOutputASBDSize);

    buffer.mDataByteSize = inNumberFrames * fxOutputASBD.mBytesPerFrame;
    buffer.mNumberChannels = fxOutputASBD.mChannelsPerFrame;
    buffer.mData = malloc(buffer.mDataByteSize);

    AudioBufferList bufferList;
    bufferList.mNumberBuffers = 1;
    bufferList.mBuffers[0] = buffer;

    //TODO prender ARM y solucionar problema de memoria

    OSStatus result = AudioUnitRender([THIS mEffects], ioActionFlags, inTimeStamp, inBusNumber, inNumberFrames, &bufferList);
    [THIS hasError:result:__FILE__:__LINE__];

    memcpy(ioData, buffer.mData, buffer.mDataByteSize);

    return noErr;
}


- (void)initializeAUGraph
{
    OSStatus result = noErr;

    // create a new AUGraph
    result = NewAUGraph(&mGraph);

    AUNode outputNode;
    AUNode effectsNode;

    AudioComponentDescription effects_desc;
    effects_desc.componentType = kAudioUnitType_Effect;
    effects_desc.componentSubType = kAudioUnitSubType_LowPassFilter;
    effects_desc.componentFlags = 0;
    effects_desc.componentFlagsMask = 0;
    effects_desc.componentManufacturer = kAudioUnitManufacturer_Apple;

    AudioComponentDescription output_desc;
    output_desc.componentType = kAudioUnitType_Output;
    output_desc.componentSubType = kAudioUnitSubType_RemoteIO;
    output_desc.componentFlags = 0;
    output_desc.componentFlagsMask = 0;
    output_desc.componentManufacturer = kAudioUnitManufacturer_Apple;

    // Add nodes to the graph to hold the AudioUnits
    result = AUGraphAddNode(mGraph, &output_desc, &outputNode);
    [self hasError:result:__FILE__:__LINE__];
    result = AUGraphAddNode(mGraph, &effects_desc, &effectsNode );
    [self hasError:result:__FILE__:__LINE__];

    // Connect the effect node's output to the output node's input
    // This is no longer the case, as I need to access the buffer
    // result = AUGraphConnectNodeInput(mGraph, effectsNode, 0, outputNode, 0);
    [self hasError:result:__FILE__:__LINE__];

    // Connect the output node's input scope's output to the effectsNode input
    result = AUGraphConnectNodeInput(mGraph, outputNode, 1, effectsNode, 0);
    [self hasError:result:__FILE__:__LINE__];

    // open the graph AudioUnits
    result = AUGraphOpen(mGraph);
    [self hasError:result:__FILE__:__LINE__];

    // Get a link to the effect AU
    result = AUGraphNodeInfo(mGraph, effectsNode, NULL, &mEffects);
    [self hasError:result:__FILE__:__LINE__];

    // Same for io unit
    result = AUGraphNodeInfo(mGraph, outputNode, NULL, &ioUnit);
    [self hasError:result:__FILE__:__LINE__];

    // Enable input on io unit
    UInt32 flag = 1;
    result = AudioUnitSetProperty(ioUnit, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Input, 1, &flag, sizeof(flag));
    [self hasError:result:__FILE__:__LINE__];

    // Setup render callback struct
    AURenderCallbackStruct renderCallbackStruct;
    renderCallbackStruct.inputProc = &renderInput;
    renderCallbackStruct.inputProcRefCon = (__bridge void*)self;

    // Set a callback for the specified node's specified input
    result = AUGraphSetNodeInputCallback(mGraph, outputNode, 0, &renderCallbackStruct);
    [self hasError:result:__FILE__:__LINE__];

    // Get fx unit's input current stream format...
    AudioStreamBasicDescription fxInputASBD;
    UInt32 sizeOfASBD = sizeof(AudioStreamBasicDescription);

    result = AudioUnitGetProperty(mEffects, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, 0, &fxInputASBD, &sizeOfASBD);
    [self hasError:result:__FILE__:__LINE__];

    // ...and set it on the io unit's input scope's output 
    result = AudioUnitSetProperty(ioUnit,
                                  kAudioUnitProperty_StreamFormat,
                                  kAudioUnitScope_Output,
                                  1,
                                  &fxInputASBD,
                                  sizeof(fxInputASBD));
    [self hasError:result:__FILE__:__LINE__];

    // Set fx unit's output sample rate, just in case
    Float64 sampleRate = 44100.0;

    result = AudioUnitSetProperty(mEffects,
                                  kAudioUnitProperty_SampleRate,
                                  kAudioUnitScope_Output,
                                  0,
                                  &sampleRate,
                                  sizeof(sampleRate));
    [self hasError:result:__FILE__:__LINE__];

    // Once everything is set up call initialize to validate connections
    result = AUGraphInitialize(mGraph);
    [self hasError:result:__FILE__:__LINE__];
}

@end

As I said before, I'm getting a -50 error on the AudioUnitRender call, and I'm finding little to no documentation at all about it.

Any help will be much appreciated.

Thanks to Tim Bolstad (http://timbolstad.com/2010/03/14/core-audio-getting-started/) for providing an excellent starting point tutorial.

3

3 Answers

0
votes

There are simpler working examples of using RemoteIO for just playing buffers of audio. Perhaps start with one of those first rather than a graph.

0
votes

Check to make sure that you are actually making all of the necessary connections. It appears that you are initializing most everything necessary, but if you simply want to passthrough audio you don't need the render callback function.

Now, if you want to do the filter, you may need one, but even so, make sure that you are actually connecting the components together properly.

Here's a snippet from an app I'm working on:

AUGraphConnectNodeInput(graph, outputNode, kInputBus, mixerNode, kInputBus);
AUGraphConnectNodeInput(graph, mixerNode, kOutputBus, outputNode, kOutputBus);

This connects the input from the RemoteIO unit to a Multichannel Mixer unit, then connects the output from the mixer to the RemoteIO's output to the speaker.

0
votes

It looks to me like you're passing in the wrong audio unit to AudioUnitRender. I think you need to pass in ioUnit instead of mEffects. In any case, double check all of the parameters you're passing in to AudioUnitRender. When I see -50 returned it's because I botched one of them.