Worker+Sound / forked from: ActionScript Worker test
WORKER上でおとはなるのか?!テスト
/**
* Copyright watanabe ( http://wonderfl.net/user/watanabe )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/wgvi
*/
// forked from bkzen's forked from: ActionScript Worker test
//WORKER上でおとはなるのか?!テスト
package {
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.events.Event;
import flash.geom.Point;
import flash.system.Capabilities;
import flash.text.TextField;
import flash.display.Sprite;
import flash.system.MessageChannel;
import flash.system.Worker;
import flash.system.WorkerDomain;
import flash.utils.ByteArray;
import flash.display.Sprite;
import flash.events.*;
import flash.media.Sound;
import flash.utils.ByteArray;
import flash.display.Sprite;
public class FlashTest extends Sprite {
private var worker:Worker;
private var messageChannel:MessageChannel;
private var bmd:BitmapData;
private var bmp:Bitmap;
private var bytes: ByteArray;
private static const SAMPLING:int = 8192/2; // バッファ
private static const FREQUENCY:int = 440; // 周波数
private static const SAMPLING_RATE:int = 44100; // サンプリングレート
private static const PI:Number = Math.PI * 2; // 1周期
private static const VOLUME:Number = 0.1; // ボリューム
public function FlashTest() {
// write as3 code here..
var version: Array = Capabilities.version.split(" ")[1].split(",");
if (!Worker.isSupported || !(version[0] >= 11 && version[1] >= 5) )
{
var txt:TextField = new TextField();
txt.autoSize = "left";
txt.text = "Not support";
addChild(txt);
return;
}
bmd = new BitmapData(1024, 1024, true, 0);
if (Worker.current.isPrimordial)
{
txt = new TextField();
txt.autoSize = "left";
bmp = new Bitmap(bmd, "auto", true);
bytes = bmd.getPixels(bmd.rect);
bytes.shareable = true;
worker = WorkerDomain.current.createWorker(loaderInfo.bytes);
messageChannel = worker.createMessageChannel(Worker.current);
worker.setSharedProperty("_test_channel", messageChannel);
worker.setSharedProperty("bytes", bytes);
worker.start();
txt.text = messageChannel.receive(true);
addChild(bmp);
addChild(txt);
addEventListener(Event.ENTER_FRAME, mainLoop);
}
else
{
messageChannel = Worker.current.getSharedProperty("_test_channel");
bytes = Worker.current.getSharedProperty("bytes");
messageChannel.send("hogemoja");
//音を鳴らしちゃうよ
var sound:Sound = new Sound;
sound.addEventListener(SampleDataEvent.SAMPLE_DATA, onSampleData);
sound.play();
//addEventListener(Event.ENTER_FRAME, workersLoop);
}
}
private function onSampleData(e:SampleDataEvent):void
{
var data:ByteArray = e.data;
var pos:Number = e.position;
var w:Number = (PI / (SAMPLING_RATE / FREQUENCY*(Math.random()*0.15) ));
var phase:Number = (pos * w) % PI;
for(var i:int = 0 ; i<SAMPLING ; i++)
{
var d:Number = VOLUME * Math.sin(phase);
phase = (phase + w) % PI;
data.writeFloat(d);
data.writeFloat(d);
}
}
private function mainLoop(e: Event): void
{
bmd.lock();
bytes.position = 0;
bmd.setPixels(bmd.rect, bytes);
bmd.unlock();
}
private function workersLoop(e:Event):void
{
var offsets: Array = [], n: int = 12;
for (var i:int = 0; i < n; i++)
{
offsets[i] = new Point();
}
for (i = 0; i < 3; i++ )
{
bmd.perlinNoise(10, 10, n, Math.random() * 0xFFFFFF, true, true, 7, false, offsets);
}
bytes.position = 0;
bmd.copyPixelsToByteArray(bmd.rect, bytes);
}
}
}