0

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);
   }

}

Joxer
  • 3
  • 4
  • Are you trying to do `myArray[0]`? – Marty Dec 16 '14 at 01:00
  • Hi Marty...I did try that but it didn't seem to work. I will try again. When I do myArray.length, it gives me back either 1 or 0.. This doesn't seem right. Shouldn't myArray.length give me back 256? Thanks for the response.. I'll post back as soon as I get closer. – Joxer Dec 16 '14 at 01:41

1 Answers1

1

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
Jacky Lau
  • 665
  • 5
  • 21
  • Hi, thanks for the response. The reason I am using an array is so as that I can target the first dot(instance). When computeSpectrum is set to true, the first instance should represent the low frequencies. I need to target this instance, measure it's value at its high point and compare that to a previous value. I am hoping that this will constitute a beat. Also,I cannot get the animation to move to the bottom of the stage. Changing the .y value does nothing. @Marty....still lost. Thanks guys – Joxer Dec 16 '14 at 11:56
  • I've edited the answer with introducing an array to store the dots. Dont't forget to clean up the myArray at the beginning of your enterframe event. Also, changing 250 to stage.StageHeight can help your question about moving to the bottom of the stage? Is it the image in your mind? – Jacky Lau Dec 17 '14 at 01:57
  • Hi again. Thank you for the help, it is greatly appreciated. This code is working great. Funny thing is, that the graphics are not moving to the bottom of the page when I change the code to samplevalue * stage.stageHeight. Thanks again for ALL the help. – Joxer Dec 17 '14 at 11:24
  • do you mean dot.y = (1 - sampleValue) * stage.StageHeight; ? – Jacky Lau Dec 18 '14 at 01:47