Quantifying Flash Audio Visualizations

In my last post I wrote about The Math Behind Flash’s FFT Results and discussed the need to transform the default linear values returned by computeSpectrum into logarithmically spaced values. In this post I’ll be discussing the Math behind my own FrequencyAnalyzer class (available below) which does just that.

The goal I had when creating this class was to finally quantify the values used to create visualizations. That is, to know the exact center frequency and bandwidth of the frequency bands displayed. This is extremely important when creating “EQ” frequency visualizations and still very useful for creating experimental visualizations, but the information just hasn’t been available in Flash – until now.

At the core of FrequencyAnalyzer is the ability to take a frequency value and determine the closest index from the computeSpectrum results to correlate with it. This allows it to retrieve the closest known amplitude for any given frequency. It sounds kind of complicated, but it’s actually just the inverse of the frequency to index calculation that I discussed in my previous post.

frequency = i/1024 * 44100;
i = frequency/44100 * 1024;

This makes everything else possible, but I need to know more than a single frequency’s amplitude. What I really need to know is the amplitude (or spectral density) of an entire range of frequencies. Furthermore this range needs to be defined logarithmically based on the center frequency given. Luckily, I have the math for that as well.

Since we all perceive audio in a logarithmic scale of base 2, I know that an octave below any given frequency is frequency/2 and an octave above it is frequency*2. Octaves are a great way to determine the frequency range because they are inherently logarithmic, and in fact this is what most real-world frequency displays show (1, 1/2, or 1/3 octave bands). Knowing the defined octave bandwidth, FrequencyAnalyzer finds the index for the min and max frequencies of each frequency band and performs an aggregate calculation for the amplitude value. Simply pass in an array of center frequencies (some common ones are provided as constants) and the bandwidth (in octaves) and the rest is done for you!

var amplitudes:Vector.<Number> = FrequencyAnalyzer.computeFrequencies([250, 400, 600, 800], 1); // 4-band, 1 octave

 
It’s important to note that it doesn’t calculate the average value for frequency bands. There’s a special aggregate calculation needed which I’m still testing, but for now using the peak values seems to work quite well. Another item of note is the optional stretchFactor parameter.

As discussed in my previous post, the sample rate (affected by stretchFactor) determines the highest frequency which is measured (11,025 Hz by default). For each increment of stretchFactor you will decrease the top measurable frequency by half, but you also boost the fidelity of the lower frequency ranges. This is important if you require a larger data set (~ 30+ frequency bands) and aren’t interested in higher frequencies (above 5512.5 Hz for stretchFactor = 1). It’s even possible to run computeSpectrum at both sample rates or determine the best stretchFactor automatically, but I’ve had enough math for today. I’ll leave the rest up to you.

The Flash plugin is required to view this object.

 


Download: FrequencyAnalyzer.zip (2.37 MB)

9 Responses to “Quantifying Flash Audio Visualizations”

  1. doug says:

    much much better already thanks Ben!

  2. doug says:

    noticed – bottom two and top six bands in 31 band eq are duplicates

  3. Joshua Davis says:

    nice… and I love the colors.

    :)

  4. Ben says:

    Doug – yeah, repeating bands are due to either fidelity or the cutoff frequency. The top six bands in the standard 31 band are above 5512.5 Hz (the cut-off frequency when stretchFactor = 1), so that’s a bummer.

    Joshua – Thanks. The colors are courtesy of your awesome HYPE examples of course, but some of the code’s mine I swear! :)

  5. thomas says:

    am I the only one who cant see this working? it plays the song, but the visual bars will not show up or get stuck :( and I cant open the fla files at all in flash. I’m on a mac if that makes any difference.