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

AudioTool test with interface and access to spectrum

It throws and error on trying to access SoundMixer.computeSpectrum 
Will try to play around it later, not sure if we do or do not have needed policies to access sound data. 
In Flash there is difference between being able to play sound and being able to access its data stream. Sadly...

Update:
Found reason why it was not possible to get access to sound spectrum data.
Turns out that AudioTool uses Amazon servers to store music. And it uses redirect when you try to get that music trough their server link. 
Thing is that Flash requests crossdomain.xml from original url 
http://api.audiotool.com/crossdomain.xml
and does not request one from redirected url.
And it is a redirected url that is used by computeSpectrum to check if we are allowed to access that data.
Luckely here is same policy file on Amazon server 
http://audiotool.s3.amazonaws.com/crossdomain.xml

So added few changes mostly on 225-232 to make it load a policy file and it works like a charm now :)
Get Adobe Flash player
by wonderwhyer 11 Jun 2011
  • Forked from makc3d's AudioTool test 2
  • Diff: 218
  • Related works: 3
  • Talk

    wonderwhyer at 11 Jun 2011 03:22
    BTW there are more issues with computeSpectrum. Thing is that it tries to access sounds from all instances of Flash content all over the browser. So if you have YouTube open in other tab or something you will get errors... So its better to try and use Sound.extract instead.
    Embed
/**
 * Copyright wonderwhyer ( http://wonderfl.net/user/wonderwhyer )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/kR9H
 */

// forked from makc3d's AudioTool test 2
// forked from makc3d's AudioTool test
// forked from makc3d's BeatPort player
package {
    import flash.display.Sprite;
    import flash.events.MouseEvent;
    import flash.events.Event;
    import flash.media.SoundMixer;
    import flash.utils.ByteArray;
    import com.bit101.components.*;
    import flash.media.Sound;
    import flash.media.SoundChannel;
    import flash.events.IOErrorEvent;
    import flash.media.SoundTransform;


    /**
     * Audiotool Player v2,
     * this time API-based.
     */
    public class Audiotool extends Sprite {
        private var soundSources:AudioToolAPI;
        public var categories:Panel;
        public var time:Panel;
        public var list:com.bit101.components.List;
        
        public var currentSound:Sound;
        public var currentChannel:SoundChannel;
        
        public var PlayPauseButton:PushButton;
        public var Volume:Knob;
        public var st:SoundTransform = new SoundTransform();
        public var pos:HUISlider;
        
        
        public function Audiotool () {
            soundSources = new AudioToolAPI;
            
            soundSources.loadTracks();
            soundSources.addEventListener(Event.COMPLETE,listLoaded);
            soundSources.addEventListener(Event.SOUND_COMPLETE,soundComplete);
            addEventListener(Event.ENTER_FRAME,frame);
            
            PlayPauseButton = new PushButton(this,5,5,"play",PlayPause);
            PlayPauseButton.enabled = false;
            
            Volume = new Knob(this,PlayPauseButton.x,PlayPauseButton.y+PlayPauseButton.height+5,"volume",volumeChange);
            Volume.mode = "horizontal";
            Volume.value = 100;
            Volume.x = PlayPauseButton.x+(PlayPauseButton.width-Volume.width)/2;
            
                        
            list = new com.bit101.components.List(this,5,Volume.y+80+5);
            list.width = 440;
            list.height = 450-list.y-5;
            list.addEventListener(Event.SELECT,select);

            //throw(new Error(Volume.y+":"+Volume.height));
            
            pos = new HUISlider(this,PlayPauseButton.x+PlayPauseButton.width+5,5,"",posChange);
            pos.width=450-pos.x-5;
            

        }
        
        public function posChange(e:Event):void
        {
            if(PlayPauseButton.label == "Pause")
            {
                playSound(currentSound,pos.value*1000);
            }
        }
        
        public function volumeChange(e:Event):void
        {
            st.volume = Volume.value/100;
            if(currentChannel)
                currentChannel.soundTransform = st;
        }
        
        public function PlayPause(e:Event):void
        {
            trace(currentSound.url);
            if(PlayPauseButton.label == "Play")
            {
                playSound(currentSound,pos.value*1000);
            }
            else
            {
                currentChannel.stop();
                PlayPauseButton.label = "Play";
            }
        }
        
        public function select(e:Event):void
        {
            playSound(soundSources.getSound(list.selectedItem.label));
        }
        
        public function playSound(sound:Sound,position:Number = 0):void
        {
            
            PlayPauseButton.label = "Pause";
            if(currentChannel)
            {
                currentChannel.stop();
                currentChannel.removeEventListener(Event.SOUND_COMPLETE,soundComplete);
            }
            if(currentSound)
            {
                currentSound.removeEventListener(IOErrorEvent.IO_ERROR,soundError);
            }
            
            currentSound = sound;
            currentSound.addEventListener(IOErrorEvent.IO_ERROR,soundError);
            currentChannel = currentSound.play(position,0,st);
            currentChannel.addEventListener(Event.SOUND_COMPLETE,soundComplete);
        }
        
        public function PauseSound():void
        {
            currentChannel.stop();
        }
        
        public function soundError(e:Event):void
        {
            soundComplete(e);
        }
        
        public function soundComplete(e:Event):void
        {
            list.selectedIndex = list.selectedIndex == list.items.length - 1?0:list.selectedIndex+1;
        }
        
        public function listLoaded(e:Event):void
        {
            list.removeAll();
            for each(var key:String in soundSources.tracks){
                list.addItem({label:key})
            }
            list.selectedIndex = 0;
            PlayPauseButton.enabled=true;
        }
        
        private function frame(e:Event):void
        {
            if(currentChannel &&(PlayPauseButton.label == "Pause"))
            {
                pos.maximum = currentSound.length/1000;
                pos.value = currentChannel.position/1000;
                pos.label = Math.round(pos.maximum).toString();
            }
            var ba:ByteArray = new ByteArray();
            SoundMixer.computeSpectrum(ba,true);
            graphics.clear();
            graphics.lineStyle(1,0xcccccc);
            graphics.beginFill(0xcccccc);
            graphics.moveTo(pos.x+15,Volume.y+Volume.height-5);
            for(var i:uint=0;i<256;i++)
            {
                var val:Number = ba.readFloat();
                graphics.lineTo(pos.x+15+i,-val*(Volume.height-10)+Volume.y+Volume.height-5);
            }
            graphics.lineTo(pos.x+15+255,Volume.y+Volume.height-5);
        }
    }
}

import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.media.SoundLoaderContext;
import flash.net.URLLoader;
import flash.net.URLLoaderDataFormat;
import flash.net.URLRequest;
import flash.events.EventDispatcher;
import flash.events.ProgressEvent;
import flash.system.Security;

class AudioToolAPI extends EventDispatcher{


    public  function loadTracks ():void {
        var loader:URLLoader = new URLLoader;
        loader.dataFormat = URLLoaderDataFormat.BINARY;
        subscribeLoader (loader, tracksLoaded, onIOFailure1);
        loader.load (new URLRequest ("http://api.audiotool.com/documents/relevant"));
        //Security.loadPolicyFile("http://audiotool.s3.amazonaws.com/crossdomain.xml");
    }
    

    private function makeUrl (key:String):String {
        return "http://api.audiotool.com/play/" + key + "/s3.mp3";
    }

    private var context:SoundLoaderContext = new SoundLoaderContext (10, true);
    public var tracks:Array;
    
    private var crossDomains:Object = {};
    
    private function tracksLoaded (e:Event):void {
        var loader:URLLoader = URLLoader (e.target);
        unsubscribeLoader (loader, tracksLoaded, onIOFailure1);

        var result:XML = XML (loader.data);
        tracks = [];
        for each (var key:XML in result.document.publicKey) {
            tracks.push(key);
        }
        dispatchEvent(new Event(Event.COMPLETE));
    }
    
    public function getSound(key:String):Sound
    {
        var sound:Sound = new Sound (
            new URLRequest(
            makeUrl(key)), context
        );
        
        sound.addEventListener(ProgressEvent.PROGRESS,progress);
        return sound;
    }
    
    private function progress(e:Event):void
    {
        var s:Sound = e.currentTarget as Sound;
        var url:String = s.url;
        url = url.split("/").slice(0,3).join("/")+"/crossdomain.xml";
        Security.loadPolicyFile(url);
        s.removeEventListener(ProgressEvent.PROGRESS,progress);
    }

    private function subscribeLoader (loader:URLLoader, onComplete:Function, onIOFailure:Function):void {
        loader.addEventListener (Event.COMPLETE, onComplete);
        loader.addEventListener (IOErrorEvent.IO_ERROR, onIOFailure);
    }

    private function unsubscribeLoader (loader:URLLoader, onComplete:Function, onIOFailure:Function):void {
        loader.removeEventListener (Event.COMPLETE, onComplete);
        loader.removeEventListener (IOErrorEvent.IO_ERROR, onIOFailure);
    }

    private function onIOFailure1 (e:IOErrorEvent):void {
        unsubscribeLoader (URLLoader (e.target), tracksLoaded, onIOFailure1);
       loadTracks();
    }
}