Example of how to use computeSpectrum with Wowza Media Server

computeSpectrum is an ActionScript public method within the SoundMixer class that takes a snapshot of the current sound wave and places it into a specified ByteArray object. This example of how to use computeSpectrum with a Wowza Streaming Engine media server requires that the FastPlayVideoStreaming example be installed. After this example is installed, edit the file [install-dir]/conf/fastplay/Application.xml and change the Client/Access/StreamAudioSampleAccess to *.

The basic ActionScript 3.0 code looks like this:

var nc:NetConnection;
var ns:NetStream;
var serverUrl:String = "rtmp://localhost/fastplay";

function ncOnStatus(infoObject:NetStatusEvent)
{
	trace("ncOnStatus: "+infoObject.info.code);
	if (infoObject.info.code == "NetConnection.Connect.Success")
	{
		doPlay();
	}
}

function doConnect()
{
	nc = new NetConnection();
	nc.addEventListener(NetStatusEvent.NET_STATUS, ncOnStatus);
	var ncClientObj:Object = new Object();
	nc.client = ncClientObj;


	trace("connect: "+serverUrl);
	nc.connect(serverUrl);
}

function nsOnStatus(infoObject:NetStatusEvent)
{
	trace("nsOnStatus: "+infoObject.info.code);
	if (infoObject.info.code == "NetStream.Play.Start")
	{
		setInterval(doCompute, 500);
	}
}

function doPlay()
{
	ns = new NetStream(nc);
	ns.addEventListener(NetStatusEvent.NET_STATUS, nsOnStatus);
	var nsClientObj:Object = new Object();
	ns.client = nsClientObj;

	nsClientObj.onPlayStatus = function(infoObject:Object)
	{
		trace("onPlayStatus: "+infoObject.info.code);
	}

	ns.bufferTime = 3;

	ns.play("sample");
}

function doCompute()
{
	trace("doCompute: ");
	var spectrum : ByteArray = new ByteArray();
	SoundMixer.computeSpectrum(spectrum, true);

	var spectrumStr:String = "";
	var index:Number = 0;

	var channelOffset:Number = 0;
	for(var i=0;i<2;i++)
	{
		if (i == 0)
			spectrumStr += "Left\n";
";
		else
			spectrumStr += "Right\n";
";

		for(var j=0;j<16;j++)
		{
			var line:String = "";
			for(var k=0;k<16;k++)
			{
				if (line.length > 0)
					line += " ";

				var currVal:Number = spectrum.readFloat();
				line += ""+currVal;

				index++;
			}

			spectrumStr += line + "\n";
";
		}
	}

	trace(spectrumStr);
}

doConnect();
Link to a working example:

ComputeSpectrum.zip
Originally Published: 10-01-2010.
 

If you're having problems or want to discuss this article, post in our forum.