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 2

Get Adobe Flash player
by makc3d 23 May 2011
  • Forked from makc3d's AudioTool test
  • Diff: 34
  • Related works: 3
  • Talk

    wonderwhyer at 23 May 2011 20:32
    Hehe, cool, music service you can stream from :)
    wonderwhyer at 07 Jun 2011 23:59
    Hmm BTW where did you read about their API?
    makc3d at 08 Jun 2011 12:57
    Joa shared google doc he made for apex vj. There's really nothing much beyond what you see here atm. No paging support, no searching support, nothing. You can only query for additional info about these tracks, such as waveform or album cover. Joa said he will be changing this eventually.
    wonderwhyer at 08 Jun 2011 16:11
    Yeah, and seems their own page works like that, am thinking on making a similar interface to one they have on their page with this. Cool thing to base audio visualization experiments on :)
    makc3d at 08 Jun 2011 22:26
    actually I was thinking that too :) I mean, to make an swf that loads tracks from a number of sources, like beatport/audiotool/soundcloud/hdd/whatever. then you load this swf into Loader, and use computeSpectrum. this way, you could update the code in every and all audioviz codes instantly.
    wonderwhyer at 09 Jun 2011 00:36
    Kind of thinking about such thing since December, kind of two layered system, player interface/skins/various sources to load sound data from in player class + interface that describes visualizers.
    makc3d at 09 Jun 2011 02:23
    right, but with computeSpectrum you dont need any of that. it works on whatever sound is played. even from videos.
    wonderwhyer at 09 Jun 2011 03:03
    It does not work for microphone as far as I can say and ability to use microphone as input has lost of fun possibilities :) Like you can play guitar yourself in fort of the screen :) Also have you seen how my musical atom and how it works? I think it is a good idea to abstract source of date from visual analyzer so that if what SoundMixer does will not be enough at later stage. I could also provide more complex data input then just single wave/spectrum input. Like visualizer could ask to get a future data, not current SoundMixer one. Or ask for only some parts of frequencies divided to separate channels. But I did not dig in to it that deep so far, mostly I want it to do so because Microphone input does not go trough SoundMixer and I want all possible data sources to look the same for visualizers.
    Embed
/**
 * Copyright makc3d ( http://wonderfl.net/user/makc3d )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/cK39
 */

// forked from makc3d's AudioTool test
// forked from makc3d's BeatPort player
package {
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	/**
	 * Audiotool Player v2,
	 * this time API-based.
	 */
	public class Audiotool extends Sprite {
		private var player:AudioToolPlayer
		public function Audiotool () {
			AudioToolPlayer (player = new AudioToolPlayer).play ();
			stage.addEventListener (MouseEvent.CLICK, nextTrack);
		}
		private function nextTrack (e:MouseEvent):void {
			player.next ();
		}
	}
}

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;

class AudioToolPlayer {
	public function next ():void {
		if (channel) {
			channel.stop ();
			playNextTrack (new Event ("whatever"))
		}
	}

	/**
	 * Plays audiotool tracks in listing order.
	 */
	public function play ():void {
		loadTracks ();
	}
	
	protected function loadTracks ():void {
		var loader:URLLoader = new URLLoader;
		loader.dataFormat = URLLoaderDataFormat.BINARY;
		subscribeLoader (loader, getTracksInPage, onIOFailure1);
		loader.load (new URLRequest ("http://api.audiotool.com/documents/relevant"));
	}
	

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

	private var tracks:Array = [], sound:Sound, channel:SoundChannel;
	private var context:SoundLoaderContext = new SoundLoaderContext (10, true);

	private function getTracksInPage (e:Event):void {
		var loader:URLLoader = URLLoader (e.target);
		unsubscribeLoader (loader, getTracksInPage, onIOFailure1);

		var result:XML = XML (loader.data);
		for each (var key:XML in result.document.publicKey) {
			tracks.push (makeUrl (key));
		}
		
		playNextTrack ();
	}

	private function playNextTrack (e:Event = null):void {
		if (e != null) {
			unsubscribeSoundStuff (); channel = null;
		}

		var url:String = tracks.pop ();
		tracks.unshift (url);

		trace ("playing:", url);

		channel = Sound (sound = new Sound (
			new URLRequest (url), context
		)).play ();

		subscribeSoundStuff ();
	}

	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 subscribeSoundStuff ():void {
		sound.addEventListener (IOErrorEvent.IO_ERROR, onIOFailure3);
		channel.addEventListener (Event.SOUND_COMPLETE, playNextTrack);
	}

	private function unsubscribeSoundStuff ():void {
		sound.removeEventListener (IOErrorEvent.IO_ERROR, onIOFailure3);
		channel.removeEventListener (Event.SOUND_COMPLETE, playNextTrack);
	}

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

	private function onIOFailure3 (e:IOErrorEvent):void {
		// assumes sound's ioError comes before channel's soundComplete
		unsubscribeSoundStuff ();
		playNextTrack ();
	}
}