Wednesday, December 5

Code snippet: Simple sound visualisation


While United Visual Artists produce ambitious, mammoth installations for artists like the Chemical Brothers (shown above), you can respond to music and sound with some simple Flash code.

With the advent of version 9 of the software, Adobe has introduced a spectrograph tool, capable of reading and responding to the various EQ changes in a song. This enables rich feedback - scripts for example could show deep purple for base heavy techno, and a light pink line for a high-pitched opera sonata. Programmers have responded with an array of experiments, sound toys, and visualisers like the ones from this competition on The Flash Blog.

The only problem is that live sound, either from a microphone or the input on a computer, doesn't have this ability. Programmers are restricted to responding to basic volume changes, which although simple, can still be compelling. From a dozen lines of code....

// construct mic object
import flash.events.ActivityEvent;
import flash.events.StatusEvent;
import flash.media.Microphone;
var mic:Microphone = Microphone.getMicrophone();

// setup mic parameters
mic.gain = 60;
mic.rate = 11;
mic.setUseEchoSuppression(true);
mic.setLoopBack(true);
mic.setSilenceLevel(0, 10000);

// respond to mic volume
function showVolume(e:Event):void {
ring.scaleX=mic.activityLevel/100;
ring.scaleY=mic.activityLevel/100;
}

// run this every frame
addEventListener(Event.ENTER_FRAME, showVolume);


....To more complex examples using extensions such as open-source 3d project Papervision:

No comments: