In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

Sound.extract() on Streaming Audio over URL

Forked from Audio Color Cubes Visualiser.
Check out the function onSampleData() to see how to extract audio from a streaming source Sound to write into another (empty) Sound stream.
/**
 * Copyright excessdenied ( http://wonderfl.net/user/excessdenied )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/rB6M
 */

// forked from mi_ku's Audio Color Cubes Visualiser
package
{
    import flash.text.TextField;
    import flash.utils.ByteArray;
    import flash.display.*;
    import flash.events.*;
    import flash.media.*;
    import flash.net.URLRequest;
    
    import org.papervision3d.cameras.*;
    import org.papervision3d.objects.DisplayObject3D;
    import org.papervision3d.objects.primitives.*;
    import org.papervision3d.render.*;
    import org.papervision3d.scenes.*;
    import org.papervision3d.view.Viewport3D;
    import org.papervision3d.view.stats.StatsView;
    import org.papervision3d.materials.*;
    import org.papervision3d.materials.utils.*;
    import org.papervision3d.core.math.*;

    [SWF(width=465, height=465, frameRate=30, backgroundColor=0x000000)]
    public class Main extends Sprite
    {
        private var outSound:Sound;
        private var textfield:TextField;
        
        public function Main()
        {        
            camera = new Camera3D( 35, 2, 5000, false, false );
            renderer = new BasicRenderEngine();
            scene = new Scene3D();
            viewport = new Viewport3D( 465, 465 );
            addChild( viewport );
            addChild( new StatsView(renderer ) );

            boxArray = [];
            var size:Number = 50;
            var initialPower:Number = 10;

             boxInRow = 32;
            for( var iy:int = 0; iy < boxInRow; iy++ )
            {
                for( var ix:int = 0; ix < boxInRow; ix++ )
                {
                    var colorMats:Array = [];
                    for( var j:int = 0; j < 6; j++ )
                    {
                            colorMats.push( new ColorMaterial( 0x00a0d0 ) );
                    }
                    colorMats[0].name = "left";
                    colorMats[1].name = "right";
                    colorMats[2].name = "top";
                    colorMats[3].name = "bottom";
                    colorMats[4].name = "front";
                    colorMats[5].name = "back";

                        var cube:Cube = new Cube( new MaterialsList( colorMats ), size, size, initialPower );
                        boxArray.push( cube );
                        cube.x = ix * size - boxInRow * size * 0.5 + size * 0.5;
                        cube.z = iy * size - boxInRow * size * 0.5 + size * 0.5;
                        cube.y = 0;
                        cube.scaleY = 1.0;
                        scene.addChild( cube );
                }
            }
            
            addEventListener( Event.ENTER_FRAME, loop );
            var url:URLRequest = new URLRequest( "http://scfire-ntc-aa03.stream.aol.com:80/stream/1025" );
            sound = new Sound( url );
            //soundChannel = sound.play();
            outSound = new Sound();
            outSound.addEventListener(SampleDataEvent.SAMPLE_DATA, onSampleData);
            soundChannel = outSound.play();
            mouseYs = stage.stageHeight * 0.5;
        }
        
        protected function onSampleData(e:SampleDataEvent):void {
            var dataIn:ByteArray = new ByteArray();
            sound.extract(dataIn, 2100);
            dataIn.position = 0;
            //inittrace(stage);
            //trace(dataIn.bytesAvailable);
            
            if (dataIn.bytesAvailable >= 2100*2) {
                /*while (dataIn.bytesAvailable > 0) {
                    var n:Number = dataIn.readFloat();
                    //e.data.writeFloat(n);
                    e.data.writeFloat(n);
                }*/
                e.data.writeBytes(dataIn);
            } else {
                for (var i:int=0; i<2100*2; i++) {
                    e.data.writeFloat(0);
                 }
            }

            /*for ( var c:int=0; c<8192; c++) {
                if (dataIn.bytesAvailable > 0) {
                    var n:Number = dataIn.readFloat();
                    e.data.writeFloat(n);
                    //e.data.writeFloat(n);
                }
                else {
                    e.data.writeFloat(0);
                    e.data.writeFloat(0);
                    //e.data.writeFloat(Math.sin((Number(c+e.position)/Math.PI/2))*0.25);
                    //e.data.writeFloat(Math.sin((Number(c+e.position)/Math.PI/2))*0.25);
                }
            }*/
        }


        private var rot:Number = 0.0;
        private var mouseYs:Number;
        private function loop( event:Event ):void
        {
                //mouseYs = mouseY;
            rot += ( mouseX - stage.stageWidth * 0.5 ) * 0.0003;
            camera.x = 2000 * Math.sin( rot );
            camera.z = 2000 * Math.cos( rot );
            camera.y = mouseYs / stage.stageHeight * 3000;

            camera.lookAt( DisplayObject3D.ZERO );

            var dt:Number = 0.05;
            var randX:int = Math.random()* (boxInRow - 2) + 1;
            var randY:int = Math.random()* (boxInRow - 2) + 1;
            var cubex:Cube = boxArray[ randX + boxInRow * randY ] as Cube;
            cubex.scaleY = soundChannel.leftPeak * 100;
            hueShift += soundChannel.rightPeak * 0.05;
              if ( hueShift > 6.0 ) hueShift -= 6.0;
            
            for( var iy:int = 1; iy < boxInRow - 1; iy++ )
            {
                for( var ix:int = 1; ix < boxInRow - 1; ix++ )
                {
                    var cube:Cube = boxArray[ ix + boxInRow * iy ] as Cube;
                    var cube1:Cube = boxArray[ ix + boxInRow * ( iy + 1 ) ] as Cube;
                    var cube2:Cube = boxArray[ ix + boxInRow * ( iy - 1 ) ] as Cube;
                    var cube3:Cube = boxArray[ ix + 1 + boxInRow * iy ] as Cube;
                    var cube4:Cube = boxArray[ ix - 1 + boxInRow * iy ] as Cube;
                    var s1:Number = cube1.scaleY;
                    var s2:Number = cube2.scaleY;
                    var s3:Number = cube3.scaleY;
                    var s4:Number = cube4.scaleY;
                    var newScale:Number = ( s1 + s2 + s3 + s4 + cube.scaleY * 6 ) * 0.1;
                    cube.scaleY = newScale;
                    cube.y = newScale * 0.5 * 10;
                    var hue:Number = newScale / 100 * 6.0 + hueShift;
                    if ( hue > 6.0 ) hue -= 6.0;
                    var newColor:uint = HSVtoRGB( hue, soundChannel.rightPeak * 0.4 + 0.6, 0.9 );
                        var mat1:ColorMaterial = cube.materials.getMaterialByName( "top" ) as ColorMaterial;
                        var mat2:ColorMaterial = cube.materials.getMaterialByName( "bottom" ) as ColorMaterial;
                        var mat3:ColorMaterial = cube.materials.getMaterialByName( "left" ) as ColorMaterial;
                        var mat4:ColorMaterial = cube.materials.getMaterialByName( "right" ) as ColorMaterial;
                        var mat5:ColorMaterial = cube.materials.getMaterialByName( "front" ) as ColorMaterial;
                        var mat6:ColorMaterial = cube.materials.getMaterialByName( "back" ) as ColorMaterial;
                        mat1.fillColor = newColor;
                        mat2.fillColor = newColor;
                        mat3.fillColor = newColor;
                        mat4.fillColor = newColor;
                        mat5.fillColor = newColor;
                        mat6.fillColor = newColor;

                }
            }
            
            renderer.renderScene( scene, camera, viewport );
        }
        
        // helper functions rewriten from my C++ code :)
        private function makeColor( r:Number, g:Number, b:Number ):uint
        {
            var ri:uint = uint(r * 255.0);
            var gi:uint = uint(g * 255.0);
            var bi:uint = uint(b * 255.0);
            return ( ( ri & 0x0000FF ) | ( ( gi & 0x0000FF ) << 8 ) | ( ( bi & 0x0000FF ) << 16 ) );
        }
        // hue => [0.0;6.0] sat => [0.0;1.0] val [0.0;1.0]
        private function HSVtoRGB( hue:Number, sat:Number, val:Number ):uint
        {
            var I:int = Math.floor( hue );
            var F:Number = hue - I; 
            if ( !( I & 1 ) )
            {
                F = 1 - F;
            }
            var M:Number = val * ( 1.0 - sat     );
            var N:Number = val * ( 1.0 - sat * F );
            var V:Number = val;
            switch( I )
            {
            case 6:
            case 0:
                return makeColor( V, N, M );
            case 1:
                return makeColor( N, V, M );
            case 2:
                return makeColor( M, V, N );
            case 3:
                return makeColor( M, N, V );
            case 4:
                return makeColor( N, M, V );
            case 5:
                return makeColor( V, M, N );
            default:
                return makeColor( 0.0, 0.0, 0.0 );
            }
        }

        private var hueShift:Number = 0.0;
        private var boxInRow:int = 10;
        private var boxArray:Array;
        private var camera:Camera3D;
        private var renderer:BasicRenderEngine;
        private var scene:Scene3D;
        private var viewport:Viewport3D;
        private var sound:Sound;
        private var soundChannel:SoundChannel;
    }
}
import flash.utils.Timer;

import flash.display.Sprite;
import flash.display.Stage;
import flash.text.TextField;
import flash.text.TextFormat;


function inittrace(s:Stage):void
{
    WTrace.initTrace(s);
}

//global trace function
var trace:Function;

//wtreace class
class WTrace
{
        private static var FONT:String = "Fixedsys";
        private static var SIZE:Number = 12;
        private static var TextFields:Array = [];
        private static var trace_stage:Stage;
        
        public static function initTrace(stg:Stage):void
        {
            trace_stage = stg;
            trace = wtrace;
        }
        
        private static function scrollup():void
        {
            // maximum number of lines: 100
            if (TextFields.length > 100) 
            {
                var removeme:TextField = TextFields.shift();
                trace_stage.removeChild(removeme);
                removeme = null;
            }
            for(var x:Number=0;x<TextFields.length;x++)
            {
                (TextFields[x] as TextField).y -= SIZE*1.2;
            }
        }
    
        public static function wtrace(... args):void
        {
        
            var s:String="";
            var tracefield:TextField;
            
            for (var i:int;i < args.length;i++)
            {
                // imitating flash:
                // putting a space between the parameters
                if (i != 0) s+=" ";
                s+=args[i].toString();
            }
            

            tracefield= new TextField();
            tracefield.autoSize = "left";
            tracefield.background = true;
            tracefield.text = s;
            tracefield.y = trace_stage.stageHeight - 20;

            var tf:TextFormat = new TextFormat(FONT, SIZE);
            tracefield.setTextFormat(tf);
            trace_stage.addChild(tracefield);
            scrollup();                      
            TextFields.push(tracefield);
        }
}