CoreAudio iPhone Creating a Graphic Equalizer
CoreAudio is really badly document. On my blog I've been trying to get some good documentation out there to help aspiring audio developers. In this blog entry I'll show you how to set up an N-Band graphic equalizer using CoreAudio.
I'm not going to go into setting up an AUGraph here. For that look at my other tutorials. In this page I'm just going to explain how to use the NBandEqualizer module.
First do the standard setup for your module i.e. create the module, add it to the graph, set up connections to other nodes and then start the graph.
// Create the N band Equalizer node AudioComponentDescription auNBandEQUnitDescription; auNBandEQUnitDescription.componentType = kAudioUnitType_Effect ; auNBandEQUnitDescription.componentSubType = kAudioUnitSubType_NBandEQ; auNBandEQUnitDescription.componentManufacturer=kAudioUnitManufacturer_Apple; // Add the node to the graph AUGraphAddNode(_audioGraph, &auNBandEQUnitDescription, &_nBandEQNode); // Once the graph has been opened get an instance of the equalizer AUGraphNodeInfo (_audioGraph, _nBandEQNode, NULL, &_nBandEQUnit); // Next you would want to set up connections to the node...
At this point you're ready to start configuring the Equalizer. To do this you setup the equalizer frequency bands.
// Frequency bands NSArray *eqFrequencies = @[ @32, @250, @500, @1000, @2000, @16000 ]; // By default the equalizer isn't enabled! You need to set bypass // to zero so the equalizer actually does something NSArray *eqBypass = @[@0, @0, @0, @0, @0, @0]; UInt32 noBands = [eqFrequenices count]; // Set the number of bands first AudioUnitSetProperty(equalizerUnit, kAUNBandEQProperty_NumberOfBands, kAudioUnitScope_Global, 0, &noBands, sizeof(noBands)); // Set the frequencies for (NSUInteger i=0; i<noBands.count; i++) { AudioUnitSetParameter(equalizerUnit, kAUNBandEQParam_Frequency+i, kAudioUnitScope_Global, 0, (AudioUnitParameterValue)[[eqFrequencies objectAtIndex:i] floatValue], 0); } // Set the bypass for (NSUInteger i=0; i<bands.count; i++) { AudioUnitSetParameter(equalizerUnit, kAUNBandEQParam_BypassBand+i, kAudioUnitScope_Global, 0, (AudioUnitParameterValue)[[eqBypass objectAtIndex:i] intValue], 0); }
That's pretty much it! Now the only thing left to do is set the gain on a particular band. Remember, the gain can vary between -96dB and 24dB.
// To set a parameter for a band you need to add the band number to the revelant enum for that parameter AudioUnitParameterID parameterID = kAUNBandEQParam_Gain + bandPosition; AudioUnitSetParameter(equalizerUnit, parameterID, kAudioUnitScope_Global, 0, [set gain here!], 0);
This is a pretty quick introduction. If you have any questions let me know below.
Add new comment