Spectrometer Data

Originally I wanted the spectrometer data to be calculated in real time based on every sound that was played. While not impossible, decompressing audio in real time to do this would be inefficient. My next idea was to extract the spectrometer data externally and parse it in to the game. This would probably have been the optimal solution, but when talking to our tutor he advised against it and told us to fake it. And fake it I did.

spect

So what I started with was plotting the spectrum using Audacity’s Plot Spectrum function and approximated it by hand. Since we used 10 bars I had to approximated 10 different values at every point where there is significant change in the spectrum. I had set up the code to linearly interpolate between these spectrum data points based on time. I gave up on this solution pretty quickly when I realized doing even a single audio file (we have maybe 30 audio files with spectrum data in the game)  would take over an hour. Instead I only approximated one spectrum per audio file and then multiplied that spectrum with an amplitude that I would approximate instead. Since I only had to calculate one value every time there was significant change in the spectrum my workload was lowered by a factor of 10.

cryspect

In this picture the you can see the approximated spectrum at the top. On the rest, the left hand value is a time stamp and the right hand value is an approximated amplitude. For each seismic sound that is being played the code checks where in time that sound is, finds between which time stamps that time is and using linear interpolation finds what amplitude it should use. It then multiplies this amplitude with the volume multiplier calculated by the triangulation mentioned in a previous blog post. Each value in the spectrum is then multiplied by that result. Each spectrum is then compared value by value and the output spectrum uses all the highest values. After this a little random noise is added to the spectrum to give it a little more variation.

It was a lot of work. Making a parser would most definitely have been a better choice. If the game is to be worked on further a parser would have to be implemented. The current system makes the game incredibly unscalable.

About Malcolm Yllenius

2015 Programming