Guest
Public paste!

Hype/Papervision - document class

By: a guest | Mar 29th, 2010 | Syntax: ActionScript 3 | Size: 9.29 KB | Hits: 848 | Expires: Never
Copy text to clipboard
  1. package {
  2.         import __AS3__.vec.Vector;
  3.        
  4.         import com.almerblanklabs.view.ApplicationView;
  5.         import com.lifeztream.debug.FPS;
  6.        
  7.         import flash.display.SimpleButton;
  8.         import flash.display.Sprite;
  9.         import flash.events.Event;
  10.         import flash.events.IOErrorEvent;
  11.         import flash.events.MouseEvent;
  12.         import flash.events.ProgressEvent;
  13.         import flash.external.ExternalInterface;
  14.         import flash.media.Sound;
  15.         import flash.media.SoundChannel;
  16.         import flash.media.SoundLoaderContext;
  17.         import flash.media.SoundTransform;
  18.         import flash.net.URLRequest;
  19.         import flash.system.LoaderContext;
  20.         import flash.system.Security;
  21.         import flash.text.TextField;
  22.         import flash.text.TextFieldAutoSize;
  23.         import flash.text.TextFormat;
  24.         import flash.text.TextFormatAlign;
  25.        
  26.         import flash.utils.setTimeout;
  27.        
  28.         import hype.framework.behavior.SimpleBehavior;
  29.         import hype.framework.core.TimeType;
  30.         import hype.framework.rhythm.SimpleRhythm;
  31.         import hype.framework.sound.SoundAnalyzer;
  32.        
  33.         import org.papervision3d.objects.primitives.Sphere;
  34.        
  35.         [SWF(width='580', height='580', backgroundColor='#000000')]
  36.        
  37.         /**
  38.          *
  39.          * @author Nolan Butcher
  40.          *
  41.          * DISCLAIMER : This code sample is by no means an optimized example. It is a PROTOTYPE.
  42.          *                              Please keep this in mind when looking through this code.
  43.          *
  44.          */    
  45.        
  46.         public class Paper_Hype_Lab extends Sprite
  47.         {
  48.                 private static const ARTIST : String = "Trioptic of the Sound Scientists";
  49.                 private static const AUDIO_PATH : String = 'http://flexgraphix.com/blog/wp-content/uploads/2009/11/';
  50.                 private static const AUDIO_FILES : Array = [
  51.                                                                                                                 {file:'evil_violin.mp3', title:'Evil Violin(Rich \'n Stingy)'},
  52.                                                                                                                 {file:'am_i_going_to_die.mp3', title:'Am I Going to Die'},
  53.                                                                                                                 {file:'going_pro.mp3', title:'Going Pro'},
  54.                                                                                                                 {file:'life_is_easily_taken.mp3', title:'Life is Easily Taken'},
  55.                                                                                                                 {file:'bubachu.mp3', title:'Bubachu'},
  56.                                                                                                                 {file:'midi_in-out.mp3', title:'Midi In/Out'},
  57.                                                                                                                 {file:'may_6.mp3', title:'May 6'},
  58.                                                                                                                 {file:'slow_descent.mp3', title:'Slow Descent'},
  59.                                                                                                                 {file:'nocens_sonitus.mp3', title:'Nocens Sonitus'},
  60.                                                                                                                 {file:'remember.mp3', title:'Remember'},
  61.                                                                                                                 {file:'soulful_east_coast.mp3', title:'Soulful East Coast'},
  62.                                                                                                                 {file:'sunrise_on_the_highway.mp3', title:'Sunrise on the Highway'},
  63.                                                                                                                 {file:'the_sad_truth_of_it_all.mp3', title:'The Sad Truth of it All'},
  64.                                                                                                                 {file:'blueberry_haze.mp3', title:'Blueberry Haze'}
  65.                                                                                                         ];
  66.                
  67.                
  68.                
  69.                 private static const VOLUME : Number = 1;
  70.                
  71.                 private var _view : ApplicationView;
  72.                 private var _sphere : Sphere;
  73.                 private var _music : Sound;
  74.                 private var _sndChannel : SoundChannel;
  75.                 private var _soundAnalyzer : SoundAnalyzer;
  76.                 private var _freq : Vector.<Number> = new Vector.<Number>;
  77.                 private var _rythm : SimpleRhythm;
  78.                 private var _fps : FPS;
  79.                 private var _audioIndex : int = 1;
  80.                
  81.                 private var _button : Sprite;
  82.                 private var _hitArea : Sprite;
  83.                
  84.                 private var _textField : TextField;
  85.                 private var _songText : TextField;
  86.                
  87.                 private var _rotationX : Number = 0.3;
  88.                 private var _rotationY : Number = 0.3;
  89.                 private var _cameraPitch : Number = 90;
  90.                 private var _cameraYaw : Number = 270;
  91.                
  92.                 private var _xDist : Number;
  93.                 private var _yDist : Number;
  94.                
  95.                 private var _mouseDown : Boolean = false;
  96.                
  97.                 public function Paper_Hype_Lab()
  98.                 {
  99.                         Security.allowDomain('*');
  100.                        
  101.                         stage.frameRate = 60;
  102.  
  103.                         _sphere = new Sphere(null, 300, 24, 14);
  104.                        
  105.                         _view = new ApplicationView();                 
  106.                         _view.object3D = _sphere;
  107.                         _view.startRendering();
  108.                        
  109.                         addChild(_view);
  110.                        
  111.                         _initRythm();
  112.                         _initTextField();
  113.                         _loadSound();
  114.                         _initUI();
  115.                        
  116.                         // Get the FPS widget @ www.lifeztream.com
  117.                         _fps = new FPS();
  118.                         addChild(_fps);
  119.                        
  120.                 }
  121.                
  122.                 private function _handleMouseEvents(evt:MouseEvent):void
  123.                 {
  124.                         switch(String(evt.type))
  125.                         {
  126.                                 case MouseEvent.MOUSE_DOWN:
  127.                                         _mouseDown = true;
  128.                                 break;
  129.                                 case MouseEvent.MOUSE_UP:
  130.                                         _mouseDown = false;
  131.                                 break;
  132.                         }
  133.                 }
  134.                
  135.                 private function _loadSound():void
  136.                 {
  137.                         var context : SoundLoaderContext = new SoundLoaderContext(1000, true);
  138.                         var req : URLRequest = new URLRequest();
  139.                                 req.url = AUDIO_PATH + AUDIO_FILES[_audioIndex].file;
  140.                        
  141.                         if (_rythm && _rythm.isRunning) _pauseRythm();
  142.                         if (_sndChannel)
  143.                         {
  144.                                 _sndChannel.stop();
  145.                                 try {
  146.                                         _music.close();
  147.                                 } catch(e:Error) {
  148.                                         trace ('cannot close sound stream');
  149.                                 }
  150.                         }              
  151.                         _music = new Sound(req, context);
  152.                         _music.addEventListener(IOErrorEvent.IO_ERROR, _handleIOErrorEvents, false, 0, true);
  153.                        
  154.                         _sndChannel = _music.play(0, 1, new SoundTransform(VOLUME));
  155.                         _sndChannel.addEventListener(Event.SOUND_COMPLETE, _onSoundDone, false, 0, true);
  156.                        
  157.                         _soundAnalyzer = new SoundAnalyzer();
  158.                         _soundAnalyzer.start();
  159.                        
  160.                         _updateSongText();
  161.                         if (_rythm) _initRythm();
  162.                        
  163.                 }
  164.                
  165.                 private function _onSoundDone(evt:Event):void
  166.                 {
  167.                         _button.mouseEnabled = false;
  168.                         setTimeout(_nextSong, 1000);
  169.                 }
  170.                
  171.                 private function _nextSong():void
  172.                 {
  173.                         _button.mouseEnabled = true;
  174.                         _button.dispatchEvent(new MouseEvent(MouseEvent.CLICK));
  175.                 }
  176.                
  177.                 private function _handleIOErrorEvents(evt:IOErrorEvent):void
  178.                 {
  179.                         if (ExternalInterface.available)
  180.                         {
  181.                                 ExternalInterface.call("window.alert('" + AUDIO_FILES[_audioIndex].file + " cannot be loaded')");
  182.                         }
  183.                 }
  184.                
  185.                 private function _initUI():void
  186.                 {
  187.                         _hitArea = new Sprite();
  188.                         _hitArea.graphics.beginFill(0x000,0);
  189.                         _hitArea.graphics.drawRect(0,0,580,580);
  190.                         _hitArea.graphics.endFill();
  191.                         _hitArea.addEventListener(MouseEvent.MOUSE_DOWN, _handleMouseEvents, false, 0, true);
  192.                         _hitArea.addEventListener(MouseEvent.MOUSE_UP, _handleMouseEvents, false, 0, true);
  193.                         addChild(_hitArea);
  194.                        
  195.                         _button = new Sprite();
  196.                         _button.graphics.beginFill(0x666666);
  197.                         _button.graphics.drawRect(0,0,100,30);
  198.                         _button.graphics.endFill();
  199.                         _button.x = 476;
  200.                         _button.y = 4;
  201.                         _button.buttonMode = true;
  202.                         _button.mouseChildren = false;
  203.                         _button.useHandCursor = true;
  204.                         _button.addEventListener(MouseEvent.CLICK, _handleButtonEvents, false, 0, true);
  205.                        
  206.                        
  207.                         var buttonFormat : TextFormat = new TextFormat(null, 14, 0xFFFFFF, true);
  208.                         var buttonText : TextField = new TextField();
  209.                                
  210.                                 buttonText.defaultTextFormat = buttonFormat;
  211.                                 buttonText.text = "NEXT RIFF";
  212.                                 buttonText.height = 18;
  213.                                 buttonText.y = (_button.height - buttonText.textHeight) * .5;
  214.                                 buttonText.x = (_button.width - buttonText.textWidth) * .5;
  215.                        
  216.                         addChild(_button);
  217.                        
  218.                         _button.addChild(buttonText);
  219.                 }
  220.                
  221.                 private function _pauseRythm():void
  222.                 {
  223.                         _rythm.stop();
  224.                 }
  225.                
  226.                 private function _resumeRythm():void
  227.                 {
  228.                         _rythm.start();
  229.                 }
  230.                
  231.                 private function _initRythm():void
  232.                 {
  233.                         _rythm = new SimpleRhythm(_trackRythm);
  234.                         _rythm.start(TimeType.ENTER_FRAME);
  235.                 }
  236.                
  237.                 private function _initTextField():void
  238.                 {
  239.                         var textFormat : TextFormat = new TextFormat();
  240.                                 textFormat.color = 0x666666;
  241.                                 textFormat.align = TextFormatAlign.CENTER;
  242.                                 textFormat.size = 14;
  243.                        
  244.                        
  245.                        
  246.                        
  247.                         _songText = new TextField();
  248.                         _songText.defaultTextFormat = textFormat;
  249.                         _songText.width = 1;
  250.                         _songText.text = "-- :: --";
  251.                         _songText.autoSize = TextFieldAutoSize.CENTER;
  252.                         _songText.y = (stage.stageHeight - _songText.textHeight) - 4;
  253.                         _songText.x = (stage.stageWidth - _songText.width) * 0.5;
  254.                         addChild(_songText);
  255.                        
  256.                         var artistText : TextField = new TextField();
  257.                                 artistText.defaultTextFormat = textFormat;
  258.                                 artistText.width = 1;
  259.                                 artistText.text = "-- Artist :: " + ARTIST + " --";
  260.                                 artistText.x = (stage.stageWidth - artistText.width) * 0.5;
  261.                                 artistText.y = (_songText.y - artistText.textHeight) - 4;
  262.                                 artistText.autoSize = TextFieldAutoSize.CENTER;
  263.                                
  264.                         addChild(artistText);
  265.                        
  266.                         _textField = new TextField();
  267.                         _textField.width = 1;
  268.                         _textField.defaultTextFormat = textFormat;
  269.                         _textField.text = "-- Click and drag to rotate sphere --";
  270.                         _textField.x = (stage.stageWidth - _textField.width) * 0.5;
  271.                         _textField.y = artistText.y - _textField.textHeight - 5;
  272.                         _textField.autoSize = TextFieldAutoSize.CENTER;
  273.                        
  274.                         addChild(_textField);
  275.                        
  276.                        
  277.                 }
  278.                
  279.                 private function _updateSongText():void
  280.                 {
  281.                         _songText.text = "-- Song :: " + AUDIO_FILES[_audioIndex].title + " --";
  282.                         _songText.x = (stage.stageWidth - _songText.width) * 0.5;
  283.                 }
  284.                
  285.                 private function _handleButtonEvents(evt:MouseEvent):void
  286.                 {
  287.                         _sndChannel.removeEventListener(Event.SOUND_COMPLETE, _onSoundDone);
  288.                         if (_audioIndex < AUDIO_FILES.length-2)
  289.                         {
  290.                                 _audioIndex++;
  291.                         } else {
  292.                                 _audioIndex = 0;
  293.                         }
  294.                        
  295.                         _loadSound();
  296.                 }
  297.                
  298.                 private function _trackRythm(rythm : SimpleRhythm):void
  299.                 {
  300.                         for (var i:int = 1;i < _sphere.geometry.vertices.length + 1;++i)
  301.                         {
  302.                                 _freq[i-1] = _soundAnalyzer.getFrequencyIndex(i, 400, 100);
  303.                         }
  304.                        
  305.                         _view.moveParticles(_freq);
  306.                        
  307.                        
  308.                         // Rotates camera around sphere depending on mouse position
  309.                         if (_mouseDown)
  310.                         {
  311.                                 _xDist = mouseX - stage.stageWidth * 0.5;
  312.                                 _yDist = mouseY - stage.stageHeight * 0.5;
  313.                                
  314.                                 _cameraPitch = _yDist * _rotationX + 90;
  315.                                 _cameraYaw = _xDist * _rotationY + 270;
  316.                                
  317.                                 _view.camera.orbit(_cameraPitch, _cameraYaw);
  318.                         }
  319.                        
  320.                 }
  321.         }
  322. }