title: Build a simple Equalizer
body: <p>I would like to make a 5-band audio equalizer (60Hz, 230Hz, 910Hz, 4kHz, 14kHz) using <code>AVAudioEngine</code>. I would like to have the user input gain per band through a vertical slider and accordingly adjust the audio that is playing. I tried using <code>AVAudioUnitEQ</code> to do this, but I hear no difference when playing the audio. I tried to hardcode in values to specify a gain at each frequency, but it still does not work. Here is the code I have:</p>
  
  <pre><code>var audioEngine: AVAudioEngine = AVAudioEngine()
  var equalizer: AVAudioUnitEQ!
  var audioPlayerNode: AVAudioPlayerNode = AVAudioPlayerNode()
  var audioFile: AVAudioFile!
  
  // in viewDidLoad():
  equalizer = AVAudioUnitEQ(numberOfBands: 5)
  audioEngine.attach(audioPlayerNode)
  audioEngine.attach(equalizer)
  let bands = equalizer.bands
  let freqs = [60, 230, 910, 4000, 14000]
  audioEngine.connect(audioPlayerNode, to: equalizer, format: nil)
  audioEngine.connect(equalizer, to: audioEngine.outputNode, format: nil)
  for i in 0...(bands.count - 1) {
      bands[i].frequency = Float(freqs[i])
  }
  
  bands[0].gain = -10.0
  bands[0].filterType = .lowShelf
  bands[1].gain = -10.0
  bands[1].filterType = .lowShelf
  bands[2].gain = -10.0
  bands[2].filterType = .lowShelf
  bands[3].gain = 10.0
  bands[3].filterType = .highShelf
  bands[4].gain = 10.0
  bands[4].filterType = .highShelf
  
  do {
      if let filepath = Bundle.main.path(forResource: "song", ofType: "mp3") {
          let filepathURL = NSURL.fileURL(withPath: filepath)
          audioFile = try AVAudioFile(forReading: filepathURL)
          audioEngine.prepare()
          try audioEngine.start()
          audioPlayerNode.scheduleFile(audioFile, at: nil, completionHandler: nil)
          audioPlayerNode.play()
      }
  } catch _ {}
  </code></pre>
  
  <p>Since the low frequencies have a gain of -10 and the high frequencies have a gain of 10, there should be a very noticeable difference when playing any media. However, when the media starts playing, it sounds the same as if played without any equalizer attached. </p>
  
  <p>I'm not sure why this is happening, but I tried several different things to debug. I thought that it might be the order of the functions so I tried switching it so that audioEngine.connect is called after adjusting all of the bands, but that did not make a difference either.</p>
  
  <p>I tried this same code with using an <code>AVAudioUnitTimePitch</code>, and it worked perfectly, so I am dumbfounded as to why it does not work with <code>AVAudioUnitEQ</code>. </p>
  
  <p>I do not want to use any third-party libraries or cocoa pods for this project, I would like to do it using <code>AVFoundation</code> alone.</p>
  
  <p>Any help would be greatly appreciated!</p>
  
  <p>Thanks in advance.</p>