Posts Tagged SoundMixer

actionscript SoundMixer fun

Ever since I grabbed ye olde flex builder for linux, I’ve been get jiggy with some ActionScript.

I did a few things with papervision, but nothing ready to show.

Here are some observations on…

AS3 weird stuff

I’m pretty used to Java these days, so the AS3 class layout was a little weird to me:

package my.package.name {
     import flash.stuff.and.or.junk;
     public class Funzone extends Zone {
          private var x:int;
          public function Funzone() {
          }
          public function fu( n:Number ) : int {
              return 1;
          }
          public function gu() : void {
          }
     }
}

Fer starters, the whole thing is inside a package xyz { } wrapper. If you put something there, the .as file (and it has to have the .as extension of mxmlc will ignore freak), it has to be inside some sorta java-ishy directory nesting and you have to figure out the args to mxmlc >.<

Or you can leave it blank… and keep it in pwd… and compile it directly with mxmlc… yeah… I know…

Look at the weird way the imports go inside the package stanza… ok, maybe not that weird…

The class definition (and if it extends another class) is nice and familiar.

Variable definitions are in the form of: (private|protected|public)? var name:type

Which is why the functions (cleverly marked as such in case you might confuse them for hamburgers) have that quirky colon return type look… Except for the constructor…

It’s a little different, but it looks pretty reasonable… not like ruby/perl/python…

Anyhooo… nuff said? Oh, you can compile it like so: mxmlc Funzone.as , as usual, the filename needs to match the classname and have the “.as” extension…

So, lez…

SoundMixer it up!

One thing people seem to find neat is sound visualization, so I thought it might be something fun to cut my teeth on.
I found a couple of pretty cool posts about using SoundMixer.computeSpectrum and thought… neat!

Those guys go into mechanics of it and there’s really not a whole lot to it. The only real improvement I made was to drive it all of the “Event.ENTER_FRAME” event handler.

inherit the drawSpectrum method

The original class I made to visualize it up was Souvouzou, the method which ultimately does the per fram drawing looks like this:

        public function drawSpectrum( graphics:Graphics, byteArray:ByteArray ) : void {
            graphics.clear();
            graphics.lineStyle( 0, 0x0000FF );
            graphics.beginFill( 0x0000FF );

            var w:int = 2;
            for ( var i:int = 0; i < 512 ; i += w ) {
                var n:Number = ( byteArray.readFloat() * 256 ) + 1;
                graphics.drawRect( i, 256, w, -n );
            }
        }

Which is pretty much copied from the example, but by breaking it out into a separate method, it was pretty easy to override it in Magnozou where I got a little more jiggy with it:

        public class Magnozou extends Souvouzou {
            // ....

        public override function drawSpectrum( graphics:Graphics, byteArray:ByteArray ) : void {
            this.fade();
            this.magnitudes( byteArray );
            graphics.clear();
            this.drawHits( graphics );
            this.drawHats( graphics );
            this.drawMags( graphics );
        }

It’s a little more in the flavor of the olde fadey, jumping hat thing of yesteryear with some nutty laser light show…

Notice the sly override, if you forget it, don’t worry! mxmlc will remind you… 😀

Error: Overriding a function that is not marked for override.

Thanks! I almost left off the handy “override” keyword! What a mess that would have been!!!

???

weird AS3 variable scoping

Another really weird thing about AS3 is that it appears that variable scoping is per function rather than per code block.

Which is weird! It means stuff like this:

        public function noworkie() : void {
            for ( var i:int = 0; i < 512 ; i++ ) {
            }
            for ( var i:int = 0; i < 512 ; i++ ) {
            }
        }

will get a saddy face like:

Warning: Duplicate variable definition.

and you can do weird stuff like this:

        public function noworkie() : void {
            for ( var i:int = 0; i < 512 ; i++ ) {
                var n:int = 12;
            }
            n++;
        }

which is REALLY weird!!! X-D

AS3 is weird

It is weird! But despite it’s few quirks, it’s a pretty usable language. The mxmlc compiler seems really slow…

It also seems really quirky.

Once I get the real directory structure / package / mxmlc straightened out I’ll post it.

BTW, don’t ferget, you can still write in Haxe, have it generate the AS3 and compile that with mxmlc… the big advantage of that is Haxe’s cool dynamic type inferencing, and it can also translate yer code to Javascript too…

I’m just saying… there are alway alternatives

Comments (1)