Audio Visualizer - ActionScript to JavaScript

100 Views Asked by At

I once found such a very cool Audio Visualizer in ShockWave. And I'd like to rewrite it into plain JavaScript with or without using JQuery. Some parts of the code I know how to change but could someone help me with this fragment? Then I will know how to do with other passages.

package fl.motion.easing
{
   public class Quadratic
   {
       
      
      public function Quadratic()
      {
         super();
      }
      
      public static function easeIn(param1:Number, param2:Number, param3:Number, param4:Number) : Number
      {
         return param3 * (param1 = param1 / param4) * param1 + param2;
      }
      
      public static function easeOut(param1:Number, param2:Number, param3:Number, param4:Number) : Number
      {
         return -param3 * (param1 = param1 / param4) * (param1 - 2) + param2;
      }
      
      public static function easeInOut(param1:Number, param2:Number, param3:Number, param4:Number) : Number
      {
         if((param1 = param1 / (param4 / 2)) < 1)
         {
            return param3 / 2 * param1 * param1 + param2;
         }
         return -param3 / 2 * (--param1 * (param1 - 2) - 1) + param2;
      }
   }
}
2

There are 2 best solutions below

0
BGPHiJACK On

You cannot convert ActionScript to JavaScript in the case at all, two different languages however the pseudo can be passed along. In the case of JavaScript you may want to look down this route and see what you can do. Reference from

https://developer.mozilla.org/en-US/docs/Web/API/ScriptProcessorNode/onaudioprocess

var audioCtx = new AudioContext();
var scriptNode = audioCtx.createScriptProcessor(4096, 1, 1);
scriptNode.onaudioprocess = function() { ...};

This is only part of the answer unfortunately. You will actually find that you will need to create events like the one you posted that'll run when certain events take place (up to how you design it).

Javascript is part of it but not all of it, Check out a cool code-pen I found! https://codepen.io/Wpitallo/pen/oNXjPBr

0
VC.One On

There is no public or static function in Javascript. No need to declare data types (eg: :Number).

If this was my problem, I would just call the functions directly when needed.

In AS3 I assume it's used like this:

var myQuad :Quadratic = new Quadratic; //import class

//# then later on, using class...
some_Result_Num = myQuad.easeInOut( a, b, c, d);

In JS you can call the function (no class or imports)

some_Result_Num = Quadratic_easeInOut( a, b, c, d);

Where Quadratic_easeInOut looks like this in your JS code:

function Quadratic_easeInOut ( param1, param2, param3, param4)
{
    if( ( param1 / (param4 / 2) ) < 1)
    { return ( param3 / 2 * param1 * param1 + param2 ); }
    
    return ( -param3 / 2 * (--param1 * (param1 - 2) - 1) + param2 );
}