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.

  1. // Create the N band Equalizer node
  2. AudioComponentDescription auNBandEQUnitDescription;
  3. auNBandEQUnitDescription.componentType = kAudioUnitType_Effect ;
  4. auNBandEQUnitDescription.componentSubType = kAudioUnitSubType_NBandEQ;
  5. auNBandEQUnitDescription.componentManufacturer=kAudioUnitManufacturer_Apple;
  6.  
  7. // Add the node to the graph
  8. AUGraphAddNode(_audioGraph,
  9. &auNBandEQUnitDescription,
  10. &_nBandEQNode);
  11.  
  12. // Once the graph has been opened get an instance of the equalizer
  13. AUGraphNodeInfo (_audioGraph,
  14. _nBandEQNode,
  15. NULL,
  16. &_nBandEQUnit);
  17.  
  18. // 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.

  1. // Frequency bands
  2. NSArray *eqFrequencies = @[ @32, @250, @500, @1000, @2000, @16000 ];
  3.  
  4. // By default the equalizer isn't enabled! You need to set bypass
  5. // to zero so the equalizer actually does something
  6. NSArray *eqBypass = @[@0, @0, @0, @0, @0, @0];
  7.  
  8. UInt32 noBands = [eqFrequenices count];
  9. // Set the number of bands first
  10. AudioUnitSetProperty(equalizerUnit,
  11. kAUNBandEQProperty_NumberOfBands,
  12. kAudioUnitScope_Global,
  13. 0,
  14. &noBands,
  15. sizeof(noBands));
  16. // Set the frequencies
  17. for (NSUInteger i=0; i<noBands.count; i++) {
  18. AudioUnitSetParameter(equalizerUnit,
  19. kAUNBandEQParam_Frequency+i,
  20. kAudioUnitScope_Global,
  21. 0,
  22. (AudioUnitParameterValue)[[eqFrequencies objectAtIndex:i] floatValue],
  23. 0);
  24. }
  25. // Set the bypass
  26. for (NSUInteger i=0; i<bands.count; i++) {
  27. AudioUnitSetParameter(equalizerUnit,
  28. kAUNBandEQParam_BypassBand+i,
  29. kAudioUnitScope_Global,
  30. 0,
  31. (AudioUnitParameterValue)[[eqBypass objectAtIndex:i] intValue],
  32. 0);
  33. }

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.

  1. // To set a parameter for a band you need to add the band number to the revelant enum for that parameter
  2. AudioUnitParameterID parameterID = kAUNBandEQParam_Gain + bandPosition;
  3. AudioUnitSetParameter(equalizerUnit,
  4. parameterID,
  5. kAudioUnitScope_Global,
  6. 0,
  7. [set gain here!],
  8. 0);

This is a pretty quick introduction. If you have any questions let me know below.

Tweet: 

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <c>, <cpp>, <drupal5>, <drupal6>, <java>, <javascript>, <php>, <python>, <ruby>. The supported tag styles are: <foo>, [foo].
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.