Sound Spectrum Analysis

This spectrum sample (in the sample/glut directry in the source distribution) uses several C libraries through SML# interoperability with C to construct a non-trivial application.

It reads 16 bit raw stereo sound stream data using C and POSIX library functions, it then uses GNU Scientific Library to compute sound spectrum, and it finally graphically display the result using Open GL library.

Here is a screen shot.

a screen shot.

For those interested, the following is an outline of the code.

  1. It reads raw sound streem as signed 16-bit data stream from stdin using fdopen(3) and fread(3).
  2. Its SML# code converts the input 16 bit stream data into floating point data and stores into real arrays, and perform windowing.
  3. It calls FFT function in GSL library to perform fourier transformation.
  4. Its SML# code calculates the power spectrum from the result of the fourier transformation, and then converts them to decibel scale.
  5. Its SML# code prepares histgram data from the power spectrum and then graphically displays the result by calling OpenGL and GLUT.
  6. Repeat the above 1-5 steps.

As you can see, it successfully performs realtime animation despite the fact that functional code performs lots of floating point data manipulation on its virtual machine. We should also note that this entire program is a plain user-level SML# code, without using any special stubs or ''data conversion libraries'' etc.

How to run the sample.

This program require GNU Scientific Library installed.

It also uses experimental CConfig library. To run the program, consult the example of Graphical Animation Using Open GL and set the necessary environment variables.


env CPPFLAGS='-I/usr/X11R6/include -I/usr/local/include' \
    LDFLAGS='-L/usr/X11R6/lib -L/usr/local/lib' smlsharp load.sml

Mac OS X:

env CPPFLAGS=-I/sw/include LDFLAGS=-L/sw/lib \
    LDD='otool -L' smlsharp load.sml


env CPPFLAGS=-I/usr/X11R6/include LDFLAGS=-L/usr/X11R6/lib \
    LIBNAME_SUFFIX=.dll LDD=cygcheck ARCSEP='\' smlsharp load.sml

You also need to prepare sound data stream in signed 16bit stereo raw format. If you do not have any sound stream tool at hand, then try the spectrum sample with the sine wave generator by compiling sine.c file.

Last modified:2007/07/06 03:12:01
References:[Seamless Interoperability with C] [Program Examples]