| 1 | /** |
|---|
| 2 | * This plugin renders some cool visualizations in the display, great for audio playback. |
|---|
| 3 | * All visualization code is developed by Antti Kupila (www.anttikupila.com). |
|---|
| 4 | **/ |
|---|
| 5 | |
|---|
| 6 | package com.jeroenwijering.plugins { |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | import com.anttikupila.revolt.presets.*; |
|---|
| 10 | import com.jeroenwijering.events.*; |
|---|
| 11 | import com.jeroenwijering.utils.Randomizer; |
|---|
| 12 | |
|---|
| 13 | import flash.media.*; |
|---|
| 14 | import flash.display.*; |
|---|
| 15 | import flash.net.*; |
|---|
| 16 | import flash.events.*; |
|---|
| 17 | import flash.utils.*; |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | public class Revolt extends MovieClip implements PluginInterface { |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | /** Configuration data of the plugin. **/ |
|---|
| 24 | public var config:Object = { |
|---|
| 25 | gain:1, |
|---|
| 26 | simple:false, |
|---|
| 27 | sound:false, |
|---|
| 28 | timeout:10 |
|---|
| 29 | }; |
|---|
| 30 | /** Clip in which the visuals are shown. **/ |
|---|
| 31 | public var clip:MovieClip; |
|---|
| 32 | /** Clip in which the visuals are shown. **/ |
|---|
| 33 | private var visuals:Sprite; |
|---|
| 34 | /** List of visualization presets. **/ |
|---|
| 35 | private var presets:Array; |
|---|
| 36 | /** Randomizer for the preset. **/ |
|---|
| 37 | private var randomizer:Randomizer; |
|---|
| 38 | /** Reference to the View of the player. **/ |
|---|
| 39 | private var view:AbstractView; |
|---|
| 40 | /** Matrix in which the bitmapdata is loaded. **/ |
|---|
| 41 | private var bitmap:BitmapData; |
|---|
| 42 | /** Bytearray in which the bitmapdata is loaded. **/ |
|---|
| 43 | private var array:ByteArray; |
|---|
| 44 | /** ID for the timeout between preset switches. **/ |
|---|
| 45 | private var timeout:Number; |
|---|
| 46 | /** Currently active preset. **/ |
|---|
| 47 | private var current:Preset; |
|---|
| 48 | |
|---|
| 49 | |
|---|
| 50 | /** Setup all presets and the click. **/ |
|---|
| 51 | public function Revolt() { |
|---|
| 52 | clip = this; |
|---|
| 53 | presets = new Array( |
|---|
| 54 | new LineFourier(), |
|---|
| 55 | new Explosion(), |
|---|
| 56 | new LineSmooth(), |
|---|
| 57 | new LineWorm(), |
|---|
| 58 | new Tunnel() |
|---|
| 59 | ); |
|---|
| 60 | randomizer = new Randomizer(presets.length); |
|---|
| 61 | }; |
|---|
| 62 | |
|---|
| 63 | |
|---|
| 64 | /** Connect the plugin to the player and setup the bitmap. **/ |
|---|
| 65 | public function initializePlugin(vie:AbstractView):void { |
|---|
| 66 | view = vie; |
|---|
| 67 | view.addControllerListener(ControllerEvent.RESIZE,resizeHandler); |
|---|
| 68 | view.addModelListener(ModelEvent.STATE,stateHandler); |
|---|
| 69 | if(config['width']) { |
|---|
| 70 | bitmap = new BitmapData(config['width'],config['height'],false,0x000000); |
|---|
| 71 | } else { |
|---|
| 72 | bitmap = new BitmapData(view.config['width'],view.config['height'],false,0x000000); |
|---|
| 73 | } |
|---|
| 74 | array = new ByteArray(); |
|---|
| 75 | visuals = new Sprite(); |
|---|
| 76 | clip.addChild(visuals); |
|---|
| 77 | visuals.addEventListener(MouseEvent.CLICK,clickHandler); |
|---|
| 78 | visuals.buttonMode = true; |
|---|
| 79 | visuals.mouseChildren = false; |
|---|
| 80 | var pic:Bitmap = new Bitmap(bitmap); |
|---|
| 81 | pic.smoothing = true; |
|---|
| 82 | visuals.addChild(pic); |
|---|
| 83 | if(config['simple'] == true) { |
|---|
| 84 | current = new LineNoFourier(view.config['lightcolor']); |
|---|
| 85 | } else { |
|---|
| 86 | next(); |
|---|
| 87 | } |
|---|
| 88 | resizeHandler(); |
|---|
| 89 | }; |
|---|
| 90 | |
|---|
| 91 | |
|---|
| 92 | /** When clicking, send an event for the simple setting, or switch visualizers. **/ |
|---|
| 93 | private function clickHandler(evt:MouseEvent):void { |
|---|
| 94 | view.sendEvent(view.config['displayclick']); |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | |
|---|
| 98 | /** Compute a new soundspectrum bitmap. **/ |
|---|
| 99 | private function compute(ev:Event):void { |
|---|
| 100 | SoundMixer.computeSpectrum(array,current.fourier,0); |
|---|
| 101 | var soundArray:Array = new Array(); |
|---|
| 102 | for (var i:uint = 0; i < 512; i++) { |
|---|
| 103 | soundArray.push(array.readFloat()*config['gain']); |
|---|
| 104 | } |
|---|
| 105 | current.applyGfx(bitmap,soundArray); |
|---|
| 106 | }; |
|---|
| 107 | |
|---|
| 108 | |
|---|
| 109 | /** Flip to the next preset. **/ |
|---|
| 110 | private function next(evt:Event=null):void { |
|---|
| 111 | clearTimeout(timeout); |
|---|
| 112 | if(config['simple'] != true) { |
|---|
| 113 | current = presets[randomizer.pick()]; |
|---|
| 114 | view.sendEvent('TRACE'," REVOLT: "+current.toString() + ' preset'); |
|---|
| 115 | timeout = setTimeout(next,config['timeout']*1000); |
|---|
| 116 | } |
|---|
| 117 | }; |
|---|
| 118 | |
|---|
| 119 | |
|---|
| 120 | /** Resize the visualizer to the display. **/ |
|---|
| 121 | private function resizeHandler(evt:ControllerEvent=null) { |
|---|
| 122 | if(config['width']) { |
|---|
| 123 | clip.x = config['x']; |
|---|
| 124 | clip.y = config['y']; |
|---|
| 125 | clip.width = config['width']; |
|---|
| 126 | clip.height = config['height']; |
|---|
| 127 | clip.visible = config['visible']; |
|---|
| 128 | } else { |
|---|
| 129 | clip.width = view.config['width']; |
|---|
| 130 | clip.height = view.config['height']; |
|---|
| 131 | } |
|---|
| 132 | }; |
|---|
| 133 | |
|---|
| 134 | |
|---|
| 135 | /** Only show the visualizer when content is playing. **/ |
|---|
| 136 | private function stateHandler(evt:ModelEvent=null) { |
|---|
| 137 | removeEventListener(Event.ENTER_FRAME,compute); |
|---|
| 138 | clearTimeout(timeout); |
|---|
| 139 | switch(view.config['state']) { |
|---|
| 140 | case ModelStates.PAUSED: |
|---|
| 141 | case ModelStates.PLAYING: |
|---|
| 142 | var typ = view.playlist[view.config['item']]['type']; |
|---|
| 143 | if(config['sound'] != true || typ != 'sound') { |
|---|
| 144 | addEventListener(Event.ENTER_FRAME,compute); |
|---|
| 145 | if(config['simple'] != true) { |
|---|
| 146 | timeout = setTimeout(next,config['timeout']*1000); |
|---|
| 147 | } |
|---|
| 148 | } |
|---|
| 149 | break; |
|---|
| 150 | } |
|---|
| 151 | }; |
|---|
| 152 | |
|---|
| 153 | |
|---|
| 154 | } |
|---|
| 155 | |
|---|
| 156 | |
|---|
| 157 | } |
|---|