jumpAround(audio_level); #songsInCode

August 21st, 2009 »

For #songsInCode written and compiled in haXe

if(audio_level > threshold){
    house_of_pain[j].jumpAround(audio_level);
}

You need Flash 10 to view the awesome. It’s a free and fast upgrade.

let it load, press play.
House of Pain - Jump Around (Deadmau5 remix)

Full source

build.hxml

#build file
-cp src
-swf-version 9
-swf-header 500:200:300:FFFFFF
-main JumpAround
-swf deploy/out.swf

JumpAround.hx

import flash.media.Sound;
import flash.media.SoundChannel;
import flash.media.SoundMixer;
import flash.media.SoundTransform;
import flash.media.SoundLoaderContext;
import flash.net.URLRequest;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.utils.ByteArray;
import flash.Lib;
class JumpAround
{
    private var sound:Sound;
    private var transform: SoundTransform;
    private var channel:SoundChannel;
    private var spectrum: ByteArray;
    private var bl: Int;
    private var cl: Int;
    private var play_but: PlayPauseBut;

    private var house_of_pain: Array<Guy>;
    private static inline var house_of_pain_member_count: Int = 3;
    private var crowd: Array<Extra>;
    private static inline var crowd_count: Int = 8;

    private static inline var gravity: Float = 2.4;
    private static inline var threshold: Float = .26;
    private static inline var fft:Bool = true;
    private static inline var stretch:Int = 1;
    private static inline var PATH_TO_MP3: String = "DeadMau5_JumpAround.mp3";


    public function new()
    {
        house_of_pain = new Array<Guy>();
        crowd = new Array<Extra>();
        var i:Int;
        var guy:Guy;
        var extra:Extra;

        for(i in 0...crowd_count){
            extra = new Extra();
            crowd.push(extra);
            Lib.current.addChild(extra);
            extra.x = Lib.current.stage.stageWidth / crowd_count *i + (Lib.current.stage.stageWidth / crowd_count / 2);
            extra.y = Lib.current.stage.stageHeight - extra.RADIUS*1.5;
        }

        for(i in 0...house_of_pain_member_count){
            guy = new Guy();
            house_of_pain.push(guy);
            Lib.current.addChild(guy);
            guy.x = Lib.current.stage.stageWidth / house_of_pain_member_count *i + (Lib.current.stage.stageWidth / house_of_pain_member_count / 2);
            guy.y = Lib.current.stage.stageHeight - guy.RADIUS;
        }

        transform = new SoundTransform();
        loadSong(PATH_TO_MP3);
        spectrum = new ByteArray();

        bl = Std.int(256/house_of_pain_member_count);
        cl = Std.int(256/crowd_count);
        Lib.current.addEventListener( Event.ENTER_FRAME, loop );
    }

    private inline function loop( _ ): Void
    {
        SoundMixer.computeSpectrum(spectrum,fft,stretch);
        var i:Int;
        var j:Int = 0;
        var cj:Int = 0;
        var audio_level:Float = 0;
        var caudio_level:Float = 0;

        var s:Float = 0;
        var f:Float = 0;
        var cf:Float = 0;
        var avg:Float;
        for(i in 0...255){ // 512 for both channels, 256-1 cause  256/3 * 3 = 255
            s = spectrum.readFloat();
            f += s;
            cf += s;
            if(i % bl == 0){

                audio_level = f / bl;

                // lew end of spectrum gets lower threshold
                if(j == 0 && audio_level > threshold*.04){
                    house_of_pain[0].jumpAround(audio_level*40);
                }else if(audio_level > threshold){
                    house_of_pain[j].jumpAround(audio_level);
                }
                j++;
                f = 0;
            }

            if(i % cl == 0 && cj < crowd_count){
                caudio_level = cf / cl;
                // lew end of spectrum gets lower threshold
                if(cj == 0 && caudio_level > threshold*.05){
                    crowd[0].jumpAround(caudio_level*6);
                }else if(cj < crowd_count/3 && caudio_level > threshold){
                    crowd[cj].jumpAround(caudio_level);
                }else if(caudio_level > threshold){
                    crowd[cj].jumpAround(caudio_level*.5);
                }
                cj++;
                cf = 0;

            }

        }
        getDown();
    }

    private inline function getDown(  ): Void
    {
        var guy:Guy;
        var extra:Extra;
        for(guy in house_of_pain){
            if(!guy.onTheFloor()){
                guy.y+=gravity;
            }
            if(!guy.handDown()){
                guy.right_hand.y+=gravity;
            }
        }
        for(extra in crowd){
            if(!extra.onTheFloor()){
                extra.y+=gravity;
            }
            if(!extra.handDown()){
                extra.right_hand.y+=gravity;
            }
        }
    }

    private inline function loadSong(file:String):Void
    {
        sound = new Sound();
        var req:URLRequest = new URLRequest(file);
        var context:SoundLoaderContext = new SoundLoaderContext(8000,true);
        sound.addEventListener(IOErrorEvent.IO_ERROR, io_error);
        sound.addEventListener(Event.COMPLETE, soundLoaded);
        sound.load(req,context);
    }

    private inline function soundLoaded(_):Void
    {
        play_but = new PlayPauseBut(sound);
        Lib.current.addChild(play_but);
    }

    private inline function io_error(_):Void{ trace("can't load mp3"); }

    public static function main(): Void
    {
        var m: JumpAround = new JumpAround();
    }
}

ok it’s not the most elegant code…

Guy.hx

import flash.display.Sprite;
import flash.Lib;
class Guy extends Sprite{
    private static inline var HAND_RADIUS: Float = 12;
    private var COLOR: UInt;
    private var HAND_COLOR: UInt;
    public var RADIUS: Float;
    private static inline var JUMP_HEIGHT_MULTIPLIER: Float = 90;

    private var left_hand: Sprite;
    public var right_hand: Sprite;
    public function new(radius = 25, color = 0x359040, hand_color = 0xEFD9C8){
        super();
        RADIUS = radius;
        COLOR =  color;
        HAND_COLOR = hand_color; // forgot how to override

        graphics.beginFill(this.COLOR);
        graphics.drawCircle(0,0,RADIUS);
        graphics.endFill();

        left_hand = new Sprite();
        left_hand.graphics.beginFill(HAND_COLOR);
        left_hand.graphics.drawCircle(RADIUS,HAND_RADIUS,HAND_RADIUS);
        left_hand.graphics.endFill();
        addChild(left_hand);

        right_hand = new Sprite();
        right_hand.graphics.beginFill(HAND_COLOR);
        right_hand.graphics.drawCircle(-RADIUS,HAND_RADIUS,HAND_RADIUS);
        right_hand.graphics.endFill();
        addChild(right_hand);
    }

    public inline function onTheFloor(  ): Bool
    {
        if(y >= Lib.current.stage.stageHeight - RADIUS){
            right_hand.y = 0;
            return true;
        }else{
            return false;
        }
    }

    public inline function handDown(  ): Bool
    {
        if(right_hand.y >= 0){
            return true;
        }else{
            return false;
        }
    }

    public inline function jumpAround( _height:Float ): Void
    {
        if(onTheFloor()){
            y-= _height * JUMP_HEIGHT_MULTIPLIER;
            right_hand.y-= _height * HAND_RADIUS*10;
        }
    }
}

Extra.hx

class Extra extends Guy{ // not sure why anymore

    public function new(){
        super(35, 0x157020, 0xCFB9A8);
        scaleX = scaleY = .6;
    }
}

PlayPauseBut.hx

import com.remixtechnology.ShadowedTextField;
import flash.events.MouseEvent;
import flash.display.Sprite;
import flash.media.Sound;
import flash.media.SoundMixer;
import flash.media.SoundChannel;
class PlayPauseBut extends Sprite{
    private var start_txt: ShadowedTextField;
    private var stop_txt: ShadowedTextField;
    private var sound: Sound;
    private var playing: Bool;
    private var position: Float;
    private var channel: SoundChannel;
    public function new(_sound:Sound){
        sound = _sound;
        super();
        start_txt = new ShadowedTextField("Play");
        start_txt.setColor(0x7799CC);
        start_txt.setSize(24);
        stop_txt = new ShadowedTextField("Pause");
        stop_txt.setColor(0x888888);

        addChild(start_txt);

        addEventListener(MouseEvent.CLICK, click_start);
        position = 0;
    }
    private inline function click_start( _ ): Void
    {
        if(!playing){
            removeChild(start_txt);
            addChild(stop_txt);
            channel = sound.play(position);
            playing = true;
        }else{
            removeChild(stop_txt);
            addChild(start_txt);
            position = channel.position;
            channel.stop();
            playing = false;
        }
    }
    private inline function click_stop( _ ): Void
    {

    }
}

com.remixtechnology.ShadowedTextField

package com.remixtechnology;

import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
import flash.text.AntiAliasType;

class ShadowedTextField extends Sprite
{

    private var fgColor:UInt;
    private var bgColor:UInt;
    private var xoff:Float;
    private var yoff:Float;

    private var tfFore:TextField;
    private var tfBack:TextField;
    private var fmtFore:TextFormat;
    private var fmtBack:TextFormat;

    private var size:Int;
    private var font:String;

    public function new( ?text:String )
    {
        bgColor = 0x000000;
        fgColor = 0xFFCC00;
        xoff = 1;
        yoff = 1;
        size = 14;
        font = 'Verdana';

        mouseChildren = false;
        mouseEnabled = false;

        tfBack = new TextField();
        tfBack.autoSize = TextFieldAutoSize.LEFT;
        tfBack.selectable = false;
        tfBack.x = xoff;
        tfBack.y = yoff;
        addChild(tfBack);

        tfFore = new TextField();
        tfFore.autoSize = TextFieldAutoSize.LEFT;
        tfFore.selectable = false;
        addChild(tfFore);

        fmtFore = new TextFormat();
        fmtFore.color = fgColor;
        fmtFore.size = size;
        fmtFore.font = font;
        fmtFore.bold = true;

        fmtBack = new TextFormat();
        fmtBack.color = bgColor;
        fmtBack.size = size;
        fmtBack.font = font;
        fmtBack.bold = true;

        if(text != null) setText(text);

        super();
    }

    public inline function setText(n:String):Void
    {
        tfFore.text = n;
        tfFore.setTextFormat(fmtFore);

        tfBack.text = n;
        tfBack.setTextFormat(fmtBack);
    }

    public inline function appendText(n:String):Void
    {
        tfFore.appendText("\n" + n);
        tfFore.setTextFormat(fmtFore);

        tfBack.appendText("\n" + n);
        tfBack.setTextFormat(fmtBack);
    }

    public inline function setColor(color:UInt):Void
    {
        fmtFore.color = color;
        tfFore.setTextFormat(fmtFore);
    }

    public inline function setShadowColor(color:UInt):Void
    {
        fmtBack.color = color;
        tfBack.setTextFormat(fmtBack);
    }

    public inline function setSize(size:Int):Void{
        fmtFore.size = size;
        tfFore.setTextFormat(fmtFore);

        fmtBack.size = size;
        tfBack.setTextFormat(fmtBack);
    }

    public inline function bold(b:Bool):Void{
        if(b){
            fmtFore.bold = true;
            fmtBack.bold = true;
        }else{
            fmtFore.bold = false;
            fmtBack.bold = false;
        }
    }

}

Say Anything