This program demonstrates how to retrieve and graph a set of samples of sound data. To see the program run, left-click the box above.

The code begins with three lines to output the click instructions. The next three lines create a Sound object, load an mp3 file, and set a callback for when the sound finishes playing. Then we create a ByteArray to store the sound data and set the StartSound() function as a callback for left-clicks.

The rest of the code consists of functions. The StartSound() function begins playing the sound and sets the callback function StartFrame() to get called for every frame of the animation. The SoundFinished() function stops StartFrame() from receiving messages after the sound is finished playing.

The function StartFrame() retrieves the sound data and graphs it. It begins with a call to the static function computeSpectrum() that returns 512 sound samples, 256 for each channel, in the ByteArrayqSignal. Next, it clears the old graphics and sets the line style to a green line of width 1 pixel, and calls DrawSignal() twice, once for each channel.

Finally, the DrawSignal() function graphs each of the 256 sound samples. The graph is centered at the y-value dCenterY and the amplitude is scaled by 50 pixels. The scaling means that the samples, which range between -1 and 1, are scaled to the range -50 to 50. We begin each graph with a call to moveTo() so that no line is written from the previous position.