FMOD

BlitzBasic, by design, has very limited low-level capabilities. The ability to analyze a sound stream is completely left out, and for good reason: it is much too complicated for beginners. Therefore, we had to find another way to accomplish our desired task. Enter FMOD.

FMOD is a very powerful multi-platform music and sound effect system that allows for the playback of many sound formats. It also has support for playback methods such as streaming, as well as recording sound from external devices.

Our main focus was spectrum analysis, which is covered in this document. The nature of our project also required a minimal amount of delay when the sound file was opened and played.

The first thing to do in order to get FMOD to work with Blitz3D is to get the relevant .DECLS files and place them in the Blitz3D\userlibs directory. This allows Blitz3D to interface with fmod.dll. We have included these files in the source code download. The second thing, obviously, is the fmod.dll file itself, which should reside in the same directory as the source code or executable. It can reside in the Windows\System32 directory as well, but that should be considered bad programming style in general. Furthermore, a .DECLS file is included to interface with the kernel32.dll Windows component, which allows Blitz3D to translate FMOD's spectrum analyzer data into a format usable by Blitz.

It would probably be a good idea for a coder to look through the .DECLS files to see what functions are available and what types of arguments can be passed to those functions.

FMOD must first be initialized by calling the FSOUND_Init function with the proper arguments. Once this is done, sounds can be loaded (using FSOUND_Sample_Load) and played (using FSOUND_PlaySound).

Normally, FMOD stops the sound when it is done playing. There are options to loop sound. However, we wanted the music to go from track to track, much like a continuously-playing CD. So, we use the FSOUND_Sample_GetLength and FSOUND_GetCurrentPosition to find out when the track is 99.9% finished playing, at which point we load and play a new track.

The main FMOD functionality we used was the FSOUND_DSP_GetSpectrum function. This returns a 512-band spectrum analyzer snapshot every time it is called. For more details on the spectrum analyzer, see this document. The fourth band was selected, as this corresponded to the beat of the music. When the level bass response exceeded 45%, the environment's red, green, and blue values were shifted by random increments.

While streaming the audio files from the hard drive would have been much more efficient than reading the entire song into memory, we have found that the support for streams in Blitz3D is somewhat unreliable. Furthermore, this leads to a rather large memory footprint, as entire WAV files are loaded into memory. We chose to use WAV files because other compressed formats would have to be decompressed as they are read in to memory which creates a large, extremely noticeable pause in gameplay. The lack of compression in WAV files leads to a much smaller delay, which is barely noticeable.

There are many more FMOD functions used than what has been described. To see what these functions are, please view the Sound.bb source code file.