PageRenderTime 57ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/Onyx-VJ 4.5.0/Onyx-VJ-Plugins/src/plugins/visualizer/WaveVisualizer.as

http://onyx-vj.googlecode.com/
ActionScript | 640 lines | 370 code | 43 blank | 227 comment | 20 complexity | 740e3b1bba17ee3521208fff5fac2d78 MD5 | raw file
  1. /**
  2. * Copyright bbbluevelvet ( http://wonderfl.net/user/bbbluevelvet )
  3. * MIT License ( http://www.opensource.org/licenses/mit-license.php )
  4. * Downloaded from: http://wonderfl.net/c/eMKK
  5. */
  6. // forked from ProjectNya's WaveParticle
  7. ////////////////////////////////////////////////////////////////////////////////
  8. // WaveParticle
  9. //
  10. // [AS3.0] WaveParticleに挑戦! (3)
  11. // http://www.project-nya.jp/modules/weblog/details.php?blog_id=1564
  12. // [AS3.0] FontLoaderクラスに挑戦! (1)
  13. // http://www.project-nya.jp/modules/weblog/details.php?blog_id=1337
  14. //
  15. // http://clockmaker.jp/blog/2011/11/starling-framework-sample/
  16. ////////////////////////////////////////////////////////////////////////////////
  17. package plugins.visualizer {
  18. import flash.display.BlendMode;
  19. import flash.display.Sprite;
  20. import flash.display.StageAlign;
  21. import flash.display.StageScaleMode;
  22. import flash.events.Event;
  23. import flash.events.ProgressEvent;
  24. import flash.geom.Rectangle;
  25. import onyx.core.*;
  26. import onyx.events.InteractionEvent;
  27. import onyx.parameter.*;
  28. import onyx.plugin.*;
  29. public final class WaveVisualizer extends Visualizer
  30. {
  31. //private var loader:FontLoader;
  32. //private var background:Background;
  33. private var se:SoundEffect;
  34. private var container:Sprite;
  35. private var wave:Wave;
  36. private var light:Light;
  37. public function WaveVisualizer() {
  38. Console.output('WaveParticle from bbbluevelvet ( http://wonderfl.net/user/bbbluevelvet )');
  39. Console.output('Adapted by Bruce LANE (http://www.batchass.fr)');
  40. //background = new Background();
  41. //addChild(background);
  42. //
  43. container = new Sprite();
  44. //addChild(container);
  45. //
  46. wave = new Wave(new Rectangle(0, 0, 465, 300));
  47. container.addChild(wave);
  48. wave.y = 182;
  49. //
  50. light = new Light(new Rectangle(0, 0, 465, 465));
  51. container.addChild(light);
  52. light.y = 0;
  53. //
  54. resize();
  55. }
  56. override public function render(info:RenderInfo):void
  57. {
  58. var array:Array = SpectrumAnalyzer.getSpectrum(true);
  59. var i:int = 0;
  60. var arraytotal:Number = 0;
  61. for ( i = 0; i < array.length; i++ )
  62. {
  63. arraytotal += array[i];
  64. }
  65. //Console.output('WaveParticle wave.tightness:'+arraytotal);
  66. wave.tightness = SpectrumAnalyzer.leftPeak + SpectrumAnalyzer.rightPeak;//arraytotal/3;
  67. wave.segments = arraytotal+1;
  68. info.render( container );
  69. }
  70. private function resize(evt:Event = null):void
  71. {
  72. var sw:uint = DISPLAY_WIDTH;
  73. var sh:uint = DISPLAY_HEIGHT;
  74. var cx:uint = uint(sw/2);
  75. var cy:uint = uint(sh/2);
  76. /*background.width = sw;
  77. background.height = sh;*/
  78. if (wave) {
  79. wave.scaleX = sw/465;
  80. wave.y = cy - 50;
  81. wave.start();
  82. }
  83. if (light) {
  84. light.resize(new Rectangle(0, 0, sw, sh));
  85. light.start();
  86. }
  87. }
  88. }
  89. }
  90. //////////////////////////////////////////////////
  91. // Waveクラス
  92. //////////////////////////////////////////////////
  93. import flash.display.Sprite;
  94. import flash.display.BitmapData;
  95. import flash.display.Bitmap;
  96. import flash.events.Event;
  97. import flash.geom.Rectangle;
  98. import flash.geom.Point;
  99. import flash.geom.ColorTransform;
  100. import flash.filters.BlurFilter;
  101. import flash.display.BlendMode;
  102. import frocessing.math.PerlinNoise;
  103. class Wave extends Sprite {
  104. private var rect:Rectangle;
  105. private var container:Sprite;
  106. private var bitmapData:BitmapData;
  107. private var bitmap:Bitmap;
  108. private var points:Array;
  109. private var _segments:uint = 5;
  110. private var ratio:Number = 1/_segments;
  111. private var color:uint = 0xFFFFFF;
  112. private var perlin:PerlinNoise;
  113. private var t:Number = 0;
  114. private var c:uint = 0;
  115. private var _tightness:uint = 40;
  116. private var colorTrans:ColorTransform;
  117. private var blur:BlurFilter;
  118. private var point:Point = new Point();
  119. public function Wave(r:Rectangle) {
  120. rect = r;
  121. init();
  122. }
  123. public function get segments():uint
  124. {
  125. return _segments;
  126. }
  127. public function set segments(value:uint):void
  128. {
  129. if ( value > 0 ) _segments = value;
  130. }
  131. public function get tightness():uint
  132. {
  133. return _tightness;
  134. }
  135. public function set tightness(value:uint):void
  136. {
  137. _tightness = value;
  138. }
  139. private function init():void {
  140. bitmapData = new BitmapData(rect.width, rect.height, true, 0x00FFFFFF);
  141. bitmap = new Bitmap(bitmapData);
  142. addChild(bitmap);
  143. container = new Sprite();
  144. addChild(container);
  145. container.alpha = 0.4;
  146. container.filters = [new BlurFilter(2, 2, 3)];
  147. perlin = new PerlinNoise();
  148. colorTrans = new ColorTransform(1, 1, 1, 0.6, 0, 0, 0, 0);
  149. blur = new BlurFilter(8, 8, 3);
  150. blendMode = BlendMode.HARDLIGHT;
  151. }
  152. public function start():void {
  153. addEventListener(Event.ENTER_FRAME, update, false, 0, true);
  154. }
  155. private function update(evt:Event):void {
  156. container.graphics.clear();
  157. for (var n:uint = 0; n < 6; n++) {
  158. var offset:Number = (n < 3) ? n*0.25 : (n + 1)*0.25;
  159. setup(offset);
  160. draw();
  161. }
  162. bitmapData.lock();
  163. bitmapData.draw(container, null, colorTrans, BlendMode.LAYER, rect, true);
  164. bitmapData.applyFilter(bitmapData, rect, point, blur);
  165. bitmapData.unlock();
  166. }
  167. private function setup(offset:Number):void {
  168. points = new Array();
  169. points.push(new Point(- rect.width*ratio, rect.height*0.5));
  170. for (var n:uint = 1; n <= segments + 1; n++) {
  171. var px:Number = n*rect.width*ratio;
  172. var py:Number = (perlin.noise(n*0.25, t + offset)*0.8 + 0.1)*rect.height;
  173. points.push(new Point(px - rect.width*ratio, py));
  174. }
  175. t += 0.002;
  176. points.push(new Point(rect.width*(1 + ratio), rect.height*0.5));
  177. points.unshift(points[0]);
  178. points.push(points[points.length - 1]);
  179. }
  180. private function draw():void {
  181. container.graphics.lineStyle(0, color, 0.5);
  182. container.graphics.moveTo(points[0].x, points[0].y);
  183. for (var p:uint = 0; p < points.length - 3; p++) {
  184. var p0:Point = points[p];
  185. var p1:Point = points[p + 1];
  186. var p2:Point = points[p + 2];
  187. var p3:Point = points[p + 3];
  188. for (var s:uint = 1; s < tightness + 1; s++) {
  189. var px:Number = spline(p0.x, p1.x, p2.x, p3.x, s/tightness);
  190. var py:Number = spline(p0.y, p1.y, p2.y, p3.y, s/tightness);
  191. container.graphics.lineTo(px, py);
  192. }
  193. }
  194. }
  195. private function spline(p0:Number, p1:Number, p2:Number, p3:Number, t:Number):Number {
  196. var v0:Number = (p2 - p0) * 0.5;
  197. var v1:Number = (p3 - p1) * 0.5;
  198. var t2:Number = t * t;
  199. var t3:Number = t2 * t;
  200. return (2 * p1 - 2 * p2 + v0 + v1) * t3 + ( -3 * p1 + 3 * p2 - 2 * v0 - v1) * t2 + v0 * t + p1;
  201. }
  202. }
  203. //////////////////////////////////////////////////
  204. // Lightクラス
  205. //////////////////////////////////////////////////
  206. import flash.display.Sprite;
  207. import flash.events.Event;
  208. import flash.geom.Rectangle;
  209. import flash.utils.Timer;
  210. import flash.events.TimerEvent;
  211. import flash.filters.BlurFilter;
  212. import flash.display.BlendMode;
  213. class Light extends Sprite {
  214. private var rect:Rectangle;
  215. private var container:Sprite;
  216. private var particles:Array;
  217. private var timer:Timer;
  218. private static var interval:uint = 50;
  219. public function Light(r:Rectangle) {
  220. rect = r;
  221. init();
  222. }
  223. private function init():void {
  224. particles = new Array();
  225. container = new Sprite();
  226. addChild(container);
  227. container.filters = [new BlurFilter(4, 4, 3)];
  228. blendMode = BlendMode.ADD;
  229. }
  230. public function start():void {
  231. create();
  232. timer = new Timer(interval);
  233. timer.addEventListener(TimerEvent.TIMER, create, false, 0, true);
  234. timer.start();
  235. addEventListener(Event.ENTER_FRAME, update, false, 0, true);
  236. }
  237. private function create(evt:TimerEvent = null):void {
  238. var radius:uint = uint(Math.random()*6 + 2);
  239. var particle:Particle = new Particle(radius);
  240. particle.x = (Math.random()*0.8 + 0.1)*rect.width;
  241. particle.y = (Math.random()*0.3 + 0.4)*rect.height;
  242. particle.initialize(uint(Math.random()*20) + 100);
  243. particle.vx = (Math.random() - 0.5)*2;
  244. particle.vy = Math.random()*5 - 1;
  245. particle.acceleration = Math.random()*0.01 + 0.996;
  246. container.addChild(particle);
  247. particles.push(particle);
  248. }
  249. private function update(evt:Event):void {
  250. for (var n:String in particles) {
  251. var particle:Particle = particles[n];
  252. if (particle) particle.update();
  253. if (particle.life < 0 || particle.y < 0 || particle.y > rect.height) {
  254. if (container.contains(particle)) container.removeChild(particle);
  255. particle = null;
  256. delete particles[n];
  257. }
  258. }
  259. }
  260. public function resize(r:Rectangle):void {
  261. rect = r;
  262. }
  263. }
  264. //////////////////////////////////////////////////
  265. // Particleクラス
  266. //////////////////////////////////////////////////
  267. import flash.display.Sprite;
  268. import flash.geom.Matrix;
  269. import flash.display.GradientType;
  270. import flash.display.SpreadMethod;
  271. import flash.display.InterpolationMethod;
  272. class Particle extends Sprite {
  273. private var radius:uint;
  274. private static var colors:Array = [0xFFFFFF, 0xFFFFFF, 0xFFFFFF];
  275. private static var alphas:Array = [1, 0.8, 0];
  276. private static var ratios:Array = [0, 153, 255];
  277. private var matrix:Matrix;
  278. public var vx:Number = 0;
  279. public var vy:Number = 0;
  280. public var acceleration:Number = 1;
  281. private var max:uint = 100;
  282. public var life:int = max;
  283. private var _scale:Number = 1;
  284. public function Particle(r:uint) {
  285. radius = r;
  286. init();
  287. }
  288. public function init():void {
  289. draw();
  290. }
  291. public function initialize(m:uint):void {
  292. max = m;
  293. life = max;
  294. }
  295. public function update():void {
  296. life --;
  297. x += vx;
  298. y -= vy;
  299. vy *= acceleration;
  300. scale = life/max;
  301. }
  302. public function get scale():Number {
  303. return _scale;
  304. }
  305. public function set scale(value:Number):void {
  306. _scale = value;
  307. scaleX = scaleY = _scale;
  308. alpha = _scale;
  309. }
  310. private function draw():void {
  311. matrix = new Matrix();
  312. matrix.createGradientBox(radius*2, radius*2, 0, - radius, - radius);
  313. graphics.beginGradientFill(GradientType.RADIAL, colors, alphas, ratios, matrix, SpreadMethod.PAD, InterpolationMethod.RGB, 0);
  314. graphics.drawCircle(0, 0, radius);
  315. graphics.endFill();
  316. }
  317. }
  318. //////////////////////////////////////////////////
  319. // SoundEffectクラス
  320. //////////////////////////////////////////////////
  321. import flash.events.EventDispatcher;
  322. import flash.events.Event;
  323. import flash.events.ProgressEvent;
  324. import flash.media.Sound;
  325. import flash.media.SoundChannel;
  326. import flash.media.SoundTransform;
  327. import flash.net.URLRequest;
  328. import flash.events.IOErrorEvent;
  329. class SoundEffect extends EventDispatcher {
  330. public var id:String;
  331. private var sound:Sound;
  332. private var channel:SoundChannel;
  333. private var level:Number;
  334. private var _volume:Number = 1;
  335. private var looping:Boolean = false;
  336. public var initialized:Boolean = false;
  337. public var playing:Boolean = false;
  338. public function SoundEffect() {
  339. }
  340. public function init(Snd:Class, lv:Number = 1):void {
  341. sound = new Snd();
  342. level = lv;
  343. }
  344. public function load(filePath:String, lv:Number = 1):void {
  345. sound = new Sound();
  346. sound.addEventListener(IOErrorEvent.IO_ERROR, ioerror, false, 0, true);
  347. sound.addEventListener(ProgressEvent.PROGRESS, progress, false, 0, true);
  348. sound.addEventListener(Event.COMPLETE, initialize, false, 0, true);
  349. try {
  350. sound.load(new URLRequest(filePath));
  351. } catch (err:Error) {
  352. trace(err.message);
  353. }
  354. level = lv;
  355. }
  356. private function ioerror(evt:IOErrorEvent):void {
  357. trace(evt.text);
  358. }
  359. private function progress(evt:ProgressEvent):void {
  360. dispatchEvent(evt);
  361. }
  362. private function initialize(evt:Event):void {
  363. initialized = true;
  364. channel = sound.play();
  365. channel.stop();
  366. dispatchEvent(evt);
  367. }
  368. public function play(loop:Boolean = false):void {
  369. playing = true;
  370. if (channel) channel.stop();
  371. looping = loop;
  372. channel = sound.play();
  373. var transform:SoundTransform = channel.soundTransform;
  374. transform.volume = level*volume;
  375. channel.soundTransform = transform;
  376. if (looping) {
  377. channel.addEventListener(Event.SOUND_COMPLETE, complete, false, 0, true);
  378. }
  379. }
  380. public function stop():void {
  381. playing = false;
  382. if (channel) {
  383. channel.stop();
  384. channel.removeEventListener(Event.SOUND_COMPLETE, complete);
  385. }
  386. }
  387. public function get volume():Number {
  388. return _volume;
  389. }
  390. public function set volume(value:Number):void {
  391. _volume = value;
  392. if (channel) {
  393. var transform:SoundTransform = channel.soundTransform;
  394. transform.volume = level*_volume;
  395. channel.soundTransform = transform;
  396. }
  397. }
  398. private function complete(evt:Event):void {
  399. channel.removeEventListener(Event.SOUND_COMPLETE, complete);
  400. if (looping) {
  401. channel = sound.play(0);
  402. channel.addEventListener(Event.SOUND_COMPLETE, complete, false, 0, true);
  403. var transform:SoundTransform = channel.soundTransform;
  404. transform.volume = level*volume;
  405. channel.soundTransform = transform;
  406. } else {
  407. playing = false;
  408. }
  409. }
  410. }
  411. //////////////////////////////////////////////////
  412. // FontLoaderクラス
  413. //////////////////////////////////////////////////
  414. /*import flash.events.EventDispatcher;
  415. import flash.display.Loader;
  416. import flash.display.LoaderInfo;
  417. import flash.net.URLRequest;
  418. import flash.text.Font;
  419. import flash.events.Event;
  420. import flash.events.ProgressEvent;
  421. import flash.events.IOErrorEvent;
  422. import flash.events.HTTPStatusEvent;
  423. import flash.events.SecurityErrorEvent;
  424. import flash.system.ApplicationDomain;
  425. import flash.system.SecurityDomain;
  426. import flash.system.LoaderContext;
  427. import flash.utils.getDefinitionByName;
  428. class FontLoader extends EventDispatcher {
  429. public var id:uint;
  430. private var loader:Loader;
  431. private var info:LoaderInfo;
  432. private var _className:String;
  433. private var _font:Font;
  434. private var _fontName:String;
  435. private var embeded:Boolean = false;
  436. public static const IO_ERROR:String = IOErrorEvent.IO_ERROR;
  437. public static const HTTP_STATUS:String = HTTPStatusEvent.HTTP_STATUS;
  438. public static const SECURITY_ERROR:String = SecurityErrorEvent.SECURITY_ERROR;
  439. public static const INIT:String = Event.INIT;
  440. public static const COMPLETE:String = Event.COMPLETE;
  441. public function FontLoader() {
  442. loader = new Loader();
  443. info = loader.contentLoaderInfo;
  444. }
  445. public function load(file:String, name:String, e:Boolean = false):void {
  446. _className = name;
  447. embeded = e;
  448. info.addEventListener(ProgressEvent.PROGRESS, progress, false, 0, true);
  449. info.addEventListener(IOErrorEvent.IO_ERROR, ioerror, false, 0, true);
  450. info.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpstatus, false, 0, true);
  451. info.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityerror, false, 0, true);
  452. info.addEventListener(Event.INIT, initialize, false, 0, true);
  453. info.addEventListener(Event.COMPLETE, complete, false, 0, true);
  454. try {
  455. var context:LoaderContext = new LoaderContext();
  456. context.applicationDomain = ApplicationDomain.currentDomain;
  457. context.securityDomain = SecurityDomain.currentDomain;
  458. loader.load(new URLRequest(file), context);
  459. } catch (err:Error) {
  460. trace(err.message);
  461. }
  462. }
  463. public function unload():void {
  464. loader.unload();
  465. }
  466. private function progress(evt:ProgressEvent):void {
  467. dispatchEvent(evt);
  468. }
  469. private function ioerror(evt:IOErrorEvent):void {
  470. loader.unload();
  471. dispatchEvent(new Event(FontLoader.IO_ERROR));
  472. }
  473. private function httpstatus(evt:HTTPStatusEvent):void {
  474. dispatchEvent(new Event(FontLoader.HTTP_STATUS));
  475. }
  476. private function securityerror(evt:SecurityErrorEvent):void {
  477. dispatchEvent(new Event(FontLoader.SECURITY_ERROR));
  478. }
  479. private function initialize(evt:Event):void {
  480. dispatchEvent(new Event(FontLoader.INIT));
  481. }
  482. private function complete(evt:Event):void {
  483. info.removeEventListener(IOErrorEvent.IO_ERROR, ioerror);
  484. info.removeEventListener(HTTPStatusEvent.HTTP_STATUS, httpstatus);
  485. info.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, securityerror);
  486. info.removeEventListener(Event.INIT, initialize);
  487. info.removeEventListener(Event.COMPLETE, complete);
  488. var FontClass:Class = Class(ApplicationDomain.currentDomain.getDefinition(className));
  489. if (!embeded) {
  490. Font.registerFont(FontClass);
  491. _font = Font(new FontClass());
  492. } else {
  493. var document:Object = new FontClass();
  494. _font = document.font;
  495. }
  496. _fontName = _font.fontName;
  497. dispatchEvent(new Event(FontLoader.COMPLETE));
  498. }
  499. public function get className():String {
  500. return _className;
  501. }
  502. public function get font():Font {
  503. return _font;
  504. }
  505. public function get fontName():String {
  506. return _fontName;
  507. }
  508. }*/
  509. //////////////////////////////////////////////////
  510. // Labelクラス
  511. //////////////////////////////////////////////////
  512. /*import flash.display.Sprite;
  513. import flash.text.TextField;
  514. import flash.text.TextFieldType;
  515. import flash.text.TextFieldAutoSize;
  516. import flash.text.AntiAliasType;
  517. import flash.text.TextFormat;
  518. import flash.text.TextFormatAlign;
  519. class Label extends Sprite {
  520. private var txt:TextField;
  521. private static var fontType:String = "Myriad Pro Semibold";
  522. private var _width:uint = 20;
  523. private var _height:uint = 20;
  524. private var size:uint = 12;
  525. public static const LEFT:String = TextFormatAlign.LEFT;
  526. public static const CENTER:String = TextFormatAlign.CENTER;
  527. public static const RIGHT:String = TextFormatAlign.RIGHT;
  528. public function Label(w:uint, h:uint, s:uint = 12, align:String = LEFT) {
  529. _width = w;
  530. _height = h;
  531. size = s;
  532. draw(align);
  533. }
  534. private function draw(align:String):void {
  535. txt = new TextField();
  536. addChild(txt);
  537. txt.width = _width;
  538. txt.height = _height;
  539. txt.autoSize = align;
  540. txt.type = TextFieldType.DYNAMIC;
  541. txt.selectable = false;
  542. txt.embedFonts = true;
  543. txt.antiAliasType = AntiAliasType.ADVANCED;
  544. var tf:TextFormat = new TextFormat();
  545. tf.font = fontType;
  546. tf.size = size;
  547. tf.align = align;
  548. txt.defaultTextFormat = tf;
  549. textColor = 0x000000;
  550. }
  551. public function set text(param:String):void {
  552. txt.text = param;
  553. }
  554. public function set textColor(param:uint):void {
  555. txt.textColor = param;
  556. }
  557. }
  558. //////////////////////////////////////////////////
  559. // Backgroundクラス
  560. //////////////////////////////////////////////////
  561. import flash.display.Shape;
  562. import flash.geom.Rectangle;
  563. import flash.geom.Matrix;
  564. import flash.display.GradientType;
  565. import flash.display.SpreadMethod;
  566. import flash.display.InterpolationMethod;
  567. class Background extends Shape {
  568. private static var colors:Array = [0xE0F5FA, 0x067AC2, 0x0D1944];
  569. private static var alphas:Array = [1, 1, 1];
  570. private static var ratios:Array = [0, 153, 255];
  571. public function Background() {
  572. draw();
  573. }
  574. private function draw():void {
  575. var matrix:Matrix = new Matrix();
  576. matrix.createGradientBox(1600, 1600, 0, - 560, - 800);
  577. graphics.beginGradientFill(GradientType.RADIAL, colors, alphas, ratios, matrix, SpreadMethod.PAD, InterpolationMethod.RGB, 0);
  578. graphics.drawRect(0, 0, 800, 600);
  579. graphics.endFill();
  580. }
  581. }
  582. */