Sound class extended to draw a Sound wave.

Professeur Pirson Nicolas Pirson :
Go to my website : tatactic.be [FR] (Website about hepatitis)
nicolas.pirson.me [FR - EN] (current website)
business card - blog [FR - EN] (just for fun)
Nicolas Pirson CV page [FR] on tatactic.be
Back to AS3 - FLASH index ... [EN]

Sound class extended and included to a drawWave class... For informations about this file and your flash player, right click on the file above (.swf)
With a framerate of 1 frame/second on the TimeLine. Back To index...

File and source of main application here under.

With a few lines of code, you can include a Sound in the library or load an external sound file, then draw and stop draw the sound wave.

The graphic part is easily resizable and customizable...
Colors of the containers, line tickness, colors of the containers, colors of the wave channels (left or right...) AS3 test V 01.0.0

//Nothing on stage exept this code on frame 1 :

IMPORTS :

// import flash native classes if loading external Sound(optionnal)


import flash.utils.*;
import flash.text.TextFormat;
import flash.net.URLRequest;
import flash.events.Event;
import flash.events.ProgressEvent;
import flash.events.TimerEvent;

// ----------------------------------------------------------- //

// import classes to draw Sound
import tatactic.media.SoundGraph_Class;
import tatactic.media.sounds.ObjectSound_Class;

// ----------------------------------------------------------- //

// import optionnal classes for display Objects
import tatactic.display.CustomSPRITE;
// ----------------------------------------------------------- //

// import ContextMenuChangeClass
import tatactic.system.FlashPlayer_Class;
// ----------------------------------------------------------- //



// Change the default ContextMenu of flash player
var player:FlashPlayer_Class = new FlashPlayer_Class();
player.playerToMenu(this,true,false,true,false,false,false,true);
// ----------------------------------------------------------- //


// ----------------------------------------------------------- //
// variables declarations to create the Sound Object;
var soundAttached:ObjectSound_Class;
var testSound:ObjectSound_Class;

// ----------------------------------------------------------- //


// ----------------------------------------------------------- //
// variables declarations to draw Sound
var myFirstGraph:SoundGraph_Class;
var displayWidth:Number = 450;
var displayHeight:Number = 150;
var middleX:uint = Math.floor(stage.stageWidth/2 - displayWidth/2);
var middleY:uint = Math.floor(stage.stageHeight/2 - displayHeight/2);

// ----------------------------------------------------------- //

// ----------------------------------------------------------- //
// OPTIONNAL (in case of loading an external mp3) Preloader
var soundLoader:CustomSPRITE;
var percent:Number;
var preloaderText:TextField;
var fpsInfosText:TextField;
var preloaderTformat:TextFormat;
var mp3_fileName:String;
var mp3_path:String;
var mp3_toLoad:String;
var mp3_URLRequest:URLRequest;
var size_Ko_String:String;
var totalBytes:Number;
var displayText:String;
var onSondLoadedEvent:Event;
const SOUND_LOADED:String = new String("sound_loaded");

// ----------------------------------------------------------- //
var cadre_spr:CustomSPRITE;
cadre_spr = new CustomSPRITE();
this.addChild(cadre_spr);
CustomSPRITE(cadre_spr).drawRectangle(stage.stageWidth-10,stage.stageHeight-10,5,5,0x222222);
// ----------------------------------------------------------- //
// OPTIONNAL (in case of loading an external mp3)
soundLoader = CustomSPRITE(this.addChild(new CustomSPRITE()));
preloaderText = TextField(soundLoader.addChild(new TextField()));
fpsInfosText = TextField(this.addChild(new TextField()));
preloaderTformat = new TextFormat(null,14,0xCCCCCC,true,null,null,null,null,"left",null,null,null,null);
preloaderText.x = -2;
preloaderText.y = 12;
preloaderText.autoSize = "left";

fpsInfosText.x = 20;
fpsInfosText.y = 20;
fpsInfosText.autoSize = "left";
fpsInfosText.text = ".FLA Framerate = " + stage.frameRate + " (retrieved by : stage.frameRate)";
fpsInfosText.setTextFormat(preloaderTformat);

soundLoader.drawRectangle(1,10,0,0,0x857885,0xcccccc,0x000000,1,1);
soundLoader.place(50,50);

mp3_fileName = new String("quatreBoulesDeCuirPublic.mp3");
mp3_path = new String("medias/music/mp3/");
mp3_toLoad = new String(mp3_path + mp3_fileName);
mp3_URLRequest = new URLRequest(mp3_toLoad);


// NEW EVENT CONSTRUCTOR SOUND_LOADED
onSondLoadedEvent = new Event(SOUND_LOADED,false,false);
// ----------------------------------------------------------- //
function onSoundLoaded(e:Event):void{
this.removeChild(soundLoader);
// exec when sound is loaded...
getSOUNDInfos(testSound);
fpsInfosText.appendText ("\ntotal file size = " + size_Ko_String + " Ko." + " (" + totalBytes + " Ko)");
fpsInfosText.setTextFormat(preloaderTformat);
trace(e.target + " has dispatched : " + e);
trace(" *** SOUND IS LOADED ***");
if(e.target.hasEventListener(SOUND_LOADED)){
trace("\n________________________________________________\n")
trace(e.target.hasEventListener(SOUND_LOADED))
this.removeEventListener(SOUND_LOADED,onSoundLoaded,false);
trace(e.target.hasEventListener(SOUND_LOADED))
trace("SOUND_LOADED,onSoundLoaded,false REMOVED")
trace("\n________________________________________________\n")
}
startApplication();
// sound is loaded, now play and draw the sound...
}

function getURLRequestInfos(uri:URLRequest):URLRequest{
// get loaded sound size information
trace("\n// ------ getURLRequestInfos(uri:URLRequest):URLRequest Method called to display file infos ------ //\n");
trace(" -> url = " + uri.url)
trace(" -> contentType = " + uri.contentType)
trace(" -> data = " + uri.data)
trace(" -> digest = " + uri.digest)
trace(" -> method = " + uri.method)
trace(" -> getURLRequestInfos(uri:URLRequest) will return :")
trace(" -> " + uri + " (url = \"" + uri.url + "\")")
trace("\n// ------ getURLRequestInfos(uri:URLRequest):URLRequest END display file infos ------ //\n");
return uri;
}

function getSOUNDInfos(snd:ObjectSound_Class):void{
// get loaded sound size information
trace("\n// ------ getSOUNDInfos(snd:ObjectSound_Class):void Method called to display Sound infos ------ //\n");
size_Ko_String = new String(Math.round(Math.round(snd.bytesTotal)/1024)/1000);
size_Ko_String = size_Ko_String.replace(".",",");
totalBytes = snd.bytesTotal;
trace(" -> total file size = " + size_Ko_String + " Ko." + " (" + snd.bytesTotal + " Ko)");
trace("\n// ------ getSOUNDInfos(snd:ObjectSound_Class):void END display file infos ------ //\n");

}

function mp3_hasLoaded(e:Event):void{
// MP3 LOADED
getURLRequestInfos(mp3_URLRequest);
getSOUNDInfos(ObjectSound_Class(e.target));
}

function mp3_isLoading(e:ProgressEvent):void{
// MP3 LOADING
trace(ObjectSound_Class(e.target).bytesLoaded + " / " + ObjectSound_Class(e.target).bytesTotal)
percent = (e.target.bytesLoaded/e.target.bytesTotal) * 100;
displayText = new String("MP3 LOAD PROGRESS = " + Math.round(percent) + "%");
preloaderText.text = displayText;
preloaderText.setTextFormat(preloaderTformat);
soundLoader.graphics.clear();
soundLoader.drawRectangle(percent*2,10,0,0,0x857885,0xcccccc,0x000000,1,1);
if(percent == 100){
if(e.target.hasEventListener(ProgressEvent.PROGRESS)){
trace("\n________________________________________________\n")
trace(e.target.hasEventListener(ProgressEvent.PROGRESS))

testSound.removeEventListener(ProgressEvent.PROGRESS,mp3_isLoading,false);
trace(e.target.hasEventListener(ProgressEvent.PROGRESS))
trace("ProgressEvent.PROGRESS,mp3_isLoading,false REMOVED")
trace("\n________________________________________________\n")

}
dispatchEvent(onSondLoadedEvent);
}
}


// ----------------------------------------------------------- //
testSound = new ObjectSound_Class();
testSound.load(mp3_URLRequest);
this.addEventListener(SOUND_LOADED,onSoundLoaded,false,0,false);
// ADD LISTENER FOR THE EVENT SOUND_LOADED
testSound.addEventListener(ProgressEvent.PROGRESS,mp3_isLoading,false,0,false);
// ----------------------------------------------------------- //
function startApplication():void{
//getURLRequestInfos(mp3_URLRequest)
//testSound.start(0,0,null)
createGraphObject();
}

function createGraphObject():void{
myFirstGraph = new SoundGraph_Class(this,
testSound,
0,
0,
displayWidth,
displayHeight
)
// this calls the constructor for sound graph.
myFirstGraph.setbackgroundColors(0x2a3d2a,0x262A22,0.8,0.7);
// Customize a little the display:
// For more options to customize the look :
// Just edit tatactic/settings/DefaultsSettings_Class.as
myFirstGraph.displayWaveCoef(1.5,1.5);
// this coef multiply the wave height in the graph
// so here sometimes out of display limits BUT MASKED IF OVERSIZED!
// METHODS to CALL BEFORE to PLAY or LAUNCH here ABOVE !!!
myFirstGraph.playSoundObject(10000,0,null);
// 10 seconds of applauses skipped :)
// initialize and play the Sound (ComputeSpectrum, sets channel for Sound at startPlay...)
myFirstGraph.startDrawSound();
// if startDrawSound() is not called, Sound will NOT be drawed
myFirstGraph.place(middleX, middleY);
// here above : place the graph Object where you want.
// END MAIN APPLICATION.
// ----------------------------------------------------------- //
}
// ----------------------------------------------------------- //
// END frame 1 .fla



XHTML 1.0 STRICT. Compatible W3C     CSS 2.0  Compatible W3C