Trying to access the first instance of an array in Actionscript 3

109 views Asked by At

I am trying to do deat detection in actionscript 3. My idea is to create an array of dots (MovieClips) on the x axis which represents the frequency spectrum, SoundMixer.computeSpectrum(bytes, true, 0); is set to true. How do I access the first dot instance of my array. I then want to check it's highest value on each current frame and and measure it against the last value. I think I need to set a threshold and when the value is within the threshold call that a beat....I'm lost, can anybody point me in the right direction..

Thanks in advance.

var snd: Sound = new Sound();
var req: URLRequest = new URLRequest("mySong.mp3");
snd.load(req);

var channel: SoundChannel;
channel = snd.play();
addEventListener(Event.ENTER_FRAME, onEnterFrame);
snd.addEventListener(Event.SOUND_COMPLETE, onPlaybackComplete);

const CHANNEL_LENGTH: int = 256;
const BUFFER_LENGTH: int = 512;
var dot:Dot;

dot.cacheAsBitmap = true;
var myArray:Array = new Array();
var bytes:ByteArray = new ByteArray();

function onEnterFrame(event: Event): void 
{

  SoundMixer.computeSpectrum(bytes, true, 0);


   for (var i:int = 0; i < CHANNEL_LENGTH; i+=8) // channel_length = 256
   {


     var sampleValue:Number = bytes.readFloat();
     dot = new Dot();
     dot.x = i * 2;
     dot.y = sampleValue * 250; //50 + (i * 30)
     addChild(dot);
     myArray.push(dot);
   }

}

1

There are 1 answers

4
Jacky Lau On BEST ANSWER

I am not sure what excetly you are going to do.

But if you want to do a sound spectrum visualizer, I think your direction is right.

I follow what you do and get result like this: (http://www.imageupload.co.uk/5M3n) Those dots will dance with the music

just move dot.cacheAsBitmap = true; after dot = new Dot(); or you can remove it.

and in Dot class, don't forget to dispose itself after some time.

But actually I dont need to use myArray at all.

Here is my code:

import flash.events.Event;

var snd: Sound = new Sound();
var req: URLRequest = new URLRequest("mySong.mp3");
snd.load(req);

var channel: SoundChannel;
channel = snd.play();
addEventListener(Event.ENTER_FRAME, onEnterFrame);
snd.addEventListener(Event.SOUND_COMPLETE, onPlaybackComplete);

const CHANNEL_LENGTH: int = 256;
const BUFFER_LENGTH: int = 512;
var dot:Dot;

var myArray:Array;
var bytes:ByteArray = new ByteArray();

function onEnterFrame(event: Event): void 
{

  SoundMixer.computeSpectrum(bytes, true, 0);
  myArray = [];

   for (var i:int = 0; i < CHANNEL_LENGTH; i+=8) // channel_length = 256
   {


     var sampleValue:Number = bytes.readFloat();
     dot = new Dot();
     dot.cacheAsBitmap = true;
     dot.x = i * 2;
     dot.y = sampleValue * stage.StageHeight;
     addChild(dot);
     myArray.push(dot);
   }
   var firstElement:Dot = myArray.length>0?myArray[0]:null;
   if(firstElement)
   {
      handleWithFirstElement(firstElement);
   }
}

function onPlaybackComplete(e:Event):void
{
    removeEventListener(Event.ENTER_FRAME, onEnterFrame);
}

function handleWithFirstElement(ele:Dot):void
{
   //your code
}

And in Dot class:

flash.utils.setTimeout(this.parent.removeChild, 100, this);
// Run this just after added on Stage