flash on 2015-4-18
L : Load
Enter / Space : Play
P : Pause
/**
* Copyright 127.0.0.1 ( http://wonderfl.net/user/127.0.0.1 )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/y1le
*/
package {
import flash.display.Sprite;
public class FlashTest extends Sprite {
import flash.display.BitmapData;
import flash.display.Bitmap;
import flash.utils.ByteArray;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.net.FileReference;
import flash.events.KeyboardEvent;
import flash.events.Event;
import flash.net.FileFilter;
import flash.media.SoundMixer;
import flash.geom.ColorTransform;
import flash.geom.Rectangle;
private const bmpdat:BitmapData = new BitmapData(465,465,false,0x000000);
private const bmp:Bitmap = new Bitmap(bmpdat);
private const color:ColorTransform = new ColorTransform(1,1,1,0.99,0,0,0,0);
private const rect:Rectangle = new Rectangle(0,0,465,465);
private const bytedat:ByteArray = new ByteArray();
private const vectordat:Vector.<int> = new Vector.<int>(512);
private const vectordat_x:Vector.<int> = new Vector.<int>(512);
private const vectordat_y:Vector.<int> = new Vector.<int>(512);
private var i:int;
private const file:FileReference = new FileReference();
private const filter0:FileFilter = new FileFilter("MP3(*.mp3)","*.mp3");
private const filter1:FileFilter = new FileFilter("All(*.*)","*.*");
private var loaded:Boolean;
private var position:int;
private var sound:Sound = new Sound();
private var soundch:SoundChannel = new SoundChannel();
public function FlashTest() {
addChild(bmp);
stage.addEventListener(KeyboardEvent.KEY_DOWN,key_down);
stage.addEventListener(Event.ENTER_FRAME,enter_frame);
soundch.addEventListener(Event.SOUND_COMPLETE,sound_complete);
}
private function key_down(e:KeyboardEvent):void {
switch (e.keyCode) {
case 76 :
// L : Load
file.browse([filter0,filter1]);
file.addEventListener(Event.SELECT,file_load);
break;
case 13 :
case 32 :
// ENTER, SPACE : Play
soundch.stop();
soundch = sound.play(position);
break;
case 80 :
// P : Pause
soundch.stop();
position = soundch.position;
break;
}
}
private function file_load(e:Event):void {
file.load();
file.addEventListener(Event.COMPLETE,file_load_complete);
}
private function file_load_complete(e:Event):void {
loaded = true;
position = 0;
sound = new Sound();
sound.loadCompressedDataFromByteArray(file.data,file.data.length);
soundch.stop();
soundch = sound.play();
}
private function enter_frame(e:Event):void {
if (loaded) {
bmpdat.lock();
bmpdat.colorTransform(rect,color);
position = soundch.position;
SoundMixer.computeSpectrum(bytedat);
for (i = 0; i < 512; i = i + 1) {
vectordat[i] = bytedat.readFloat() * 256;
vectordat_x[i] = vectordat[i] * Math.sin(i * Math.PI / 256);
vectordat_y[i] = vectordat[i] * Math.cos(i * Math.PI / 256);
bmpdat.setPixel32(232 + vectordat_x[i],232 + vectordat_y[i],0xFFFFFF);
}
bmpdat.unlock();
}
}
private function sound_complete(e:Event):void {
position = 0;
}
}
}