Wednesday, August 12, 2009

Equalization Abound!

Alright so I promised a post on equalization, but first I'm going to discuss a bit of theatricals. Many people have heard of equalization but most probably don't have a very good idea of what it really does. You see most speakers don't respond to all frequencies of music/sound ideally and so the equalizer provides a way to amplify/attenuate the frequencies that don't do well so that they then play to our ears ideally. As one might imagine, there's more than one way one can do this. But the method implemented is all about tradeoffs. Audiophiles listen up:
  • Analog
  • Digital - IIR (Infinite Impulse Response)
  • Digital - FIR (Finite Impulse Response)
Now I'm not an analog electronics expert/novice (yet!) but I do know analog is very tricky and nothing works exactly the way you want it to, you will always have non-linear side-effects. Not to mention you need to buy some expensive equipment to get analog filtering stuff or be an expert yourself. If you want cheap and practical (and experts agree, even best performing!) go with digital. But here you have a choice. IIR, much like analog, has some unwanted side effects.
IIR pros:
  • Low latency
  • Little computing power necessary (for where it will run at least)
IIR cons:
  • need very high precision (floats/single precision barely cut it) or things get unstable
  • Tough to design = hard to reconfigure
  • Limitations on how close your filter is to it's ideal response, often pretty big limitations.
  • Possible PITA to code in C (what most system software is) if you use filter banks to offset the problem of high precision.
  • Need upfront good CPU to design the filter/solve coefficients if you can't do it by hand
Lastly we have digital FIR filters.
FIR pros:
  • High (but fixed!) latency, dependent on filter type however (for equalization, the latency is high).
  • Very high accuracy
  • If you can imagine a frequency response, you can do it with this method.
  • The filter works exactly as you designed it to.
FIR cons:
  • Moderate to High Computing power necessary depending on the size and FIR computation algorithm.
  • Need to have a good FFT algorithm to work with reasonably sized filters and this isn't that trivial.

So for my little venture into the audio DSP world (previously I just did alot of image work), I chose the last one as its the easiest to design/reconfigure on the fly and leads to the best results. Additionally, anything newer than a Pentium 2 shouldn't have problems playing music. It's also the kind of filtering I have the most experience with. But as a rather neat detail, let me make this clear:
You've maybe heard of low/high pass filters, shelf filters etc? Well, anything short of adding delay or echo to the signal this one "equalizer" can do (It can actually do that sort of thing with only a few small modifications for which I see no value in ATM). It's really more of a full on general DSP filter offering pretty much all of that functionality in one. The only real limitation that would require some restructuring is filter size but most people would probably be happy with a filter of size 2^16 (don't panic, FFT loves 2^n numbers and goes through it in a blaze in real time).

So yes, I'm happy about the theoretical aspects of "equalizer" and have gotten some kicks out of it. But now lets discuss some prior work and motive. If you're reading this, you might've heard about LADSPA and Steve Harris' plugins. He offers a few plugins, most of which I wasn't interested in except for mbeq (multi-band equalizer). It was useful to me and I thank the man for his work, but it just doesn't work properly in many ways. Its a PITA to configure with the setup of the ladspa sink in pulseaudio. One must put in numbers for which only gain meaning if you read the source (though you kinda get a feel for what positive and negative numbers do for the most part). There's a few problems with Harris' approach to the equalizer:
  • The frequency bands (hz) used/defined there!=frequencies we speak of naturally. They are proportional, however.
  • Each band then represents ~46 frequency bands (varies by sink sample rate) which while not too bad, is not the finest level of control
  • It should'nt be such a hassle to change the frequency band coefficients and instead I will be opting towards a gui so users can see frequency modification in realtime, the only real way things like this are tunable. This bit is actually more against pulseaudio's current implementation with ladspa, not with Harris.

Now why am I doing my equalizer again? Well, the start of it was my logitech X-540 5.1 speaker system which I bought a few months ago. Terrific speaker OMG way too much base. They have one bass dial which allows you to trade between tremble/bass but sound sounds so crappy if its less than half way maximum. It feels empty. Don't get me wrong though, I don't really like too much base like most people seem to enjoy (I don't understand why people feel it so necessary to hear those LFE which shouldn't usually be there anyway). It took some time to configure and some tradeoffs but with MBEQ and the ladspa sink, I got pretty good audio. You see, the Frequencies below 60hz from any speaker are also sent to this subwoofer from the X-540 set = way too much bass for most things. I also live in an apartment and I'm sure my neighbors wouldn't appreciate my subwoofer blasting them at 3 in the morning or some such. The ladspa sink has had a problematic past as of late with PA though and it broke quite a few times which left me hanging out to dry and gave me a difficult time a few days when I needed to listen to music/watch stuff. And finally, I just wanted to process some audio. I had filtered some other songs in the past with audacity, was amazed at the difficulty I had doing it. After enough time/effort, was impressed with the results on some songs that had some static in the background. It had cleaned them up and gotten rid of the parts that were bad while having no loss to the quality of the song, in fact this is one of those times where the quality of the song is improved. Audacious goes out of its way to use some of the above techniques I've mentioned but guard you from making up the specifics of the actual filter used, which I find interesting/weird.

Anywho, I knew right where to implement my equalizer so it'd have maximum effect: pulseaudio. A lowlatency audio framework with I believe a very promising future. On top of making plugins like mine possible (most OS's sound stuff doesn't like floats which are a requirement!), it does some interesting stuff that makes forwarding sound streams between sound cards or networks trivial. Oh yes, its also cross platform. And this isn't your run of the mill winamp equalizer, its system level baby. Every application can easily take advantage (whether it wants to or not, flash) of it if you forward the stream using paman or somesuch or set it as your default sink (as I do). Up next I'll explain how to set up your own equalizer until my packages make it into the Ubuntu repos. Opensuse Factory users are lucky enough to already have packages availble. But you'll still have to wait for the next post to find out where to dl :-)


  1. This sounds very exciting! I cant wait to see your work come to Fedora! I used to own a DBX DriveRack 480. It would be very nice to have all that built into my machine complete with automatic room balancing. from what your talking about above, it would be cool if the software were able to add a frequency control in each specific area of concern to more efficiently (cheaper on cpu) balance the room.(kinda like dragging set points on a gamma curve in the graphics world.

    In anycase i look forward to your contribution!!!

  2. I almost forgot - it would be nice to equalize each output individually - each channel has a different curve when balancing the room. also each set of speakers will require different equalizations settings.