Any imported item is accessible in the Library
(ctrl+L or find under Window
in top menu bar)..
In the Library
just right-click the current name of your audio item (will be Type: Sound
)
and choose Properties. In there you should see Linkage section so tick Export For ActionScript.
In the now available Class box you can now put your own preferred instance name (no_spaces) and leave Base Class as flash.media.Sound (should be that way)
//assuming you have.. my_Audio ..as Linkage Class name
var mySound:Sound = new my_Audio();
var myChannel:SoundChannel = new SoundChannel();
myChannel = mySound.play();
addEventListener(Event.ENTER_FRAME, show_Amplitude);
function show_Amplitude(evt:Event)
{
// where 200 is your own number for the maximum width or height of amplitude bars
mc_ampLeft.width = myChannel.leftPeak * 200;
mc_ampRight.width = myChannel.rightPeak * 200;
}
Alternative solution: Get amplitude via computeSpectrum
For whatever situations where the above solution is not applicable, then the alternative would be to just use ComputeSpectrum
(which works globally on all audio since its tied to the SoundMixer
not just specific sound Object). This is an example as starting point (tweak this or research a better formula)
var n_RMS :Number = 0;
var n_FFT :Number = 0;
var max_AMP :Number = 200; // max width or height of bar at full volume
var FFT_bytes:ByteArray = new ByteArray;
addEventListener(Event.ENTER_FRAME, compute_Amplitude);
function compute_Amplitude(evt:Event)
{
SoundMixer.computeSpectrum( FFT_bytes, false, 0 );
for (var i:int = 0; i < 256; i++) //GETS LEFT CHANNEL FFT
{
n_FFT = FFT_bytes.readFloat();
n_RMS = 0.8 * Math.sqrt( Math.abs(n_FFT) ) / 0.434294481904;
}
mc_ampLeft.width = (n_RMS /2) * max_AMP; //update LEFT bar
for (var j:int = 0; j < 256; j++) //GETS RIGHT CHANNEL FFT
{
n_FFT = FFT_bytes.readFloat();
n_RMS = 0.8 * Math.sqrt( Math.abs(n_FFT) ) / 0.434294481904;
}
mc_ampRight.width = (n_RMS /2) * max_AMP; //update RIGHT bar
}