SoundManager (3)
/**
* Copyright ProjectNya ( http://wonderfl.net/user/ProjectNya )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/nOR1
*/
// forked from Event's Instrument
////////////////////////////////////////////////////////////////////////////////
// SoundManager (3)
////////////////////////////////////////////////////////////////////////////////
package {
import flash.display.Sprite;
import flash.display.Shape;
import flash.media.SoundMixer;
import flash.utils.ByteArray;
import flash.events.Event;
import flash.events.MouseEvent;
/*
* ※お題のプログラムで使用している音源の、当イベント以外での使用を禁じます。
* それ以外の音源の使用は自由ですので、他に使用した音源がありましたら、権利をご確認の上各自ご用意下さい。
* なお、当イベントでは、音源のアップロード場所として、こえ部( http://koebu.com/ )のご利用を推奨いたします。
* * mp3 files used in the code are not allowed to use outside wonderfl.net/jsdo.it
* You're free to use your own sound source for JAM, if you have rights to use it.
* We recommend soundcloud.com to upload music.
* http://assets.wonderfl.net/sounds/event/jam/drop1.mp3
* http://assets.wonderfl.net/sounds/event/jam/drop2.mp3
* http://assets.wonderfl.net/sounds/event/jam/drop3.mp3
* http://assets.wonderfl.net/sounds/event/jam/drop4.mp3
* http://assets.wonderfl.net/sounds/event/jam/drop5.mp3
* http://assets.wonderfl.net/sounds/event/jam/drop6.mp3
* http://assets.wonderfl.net/sounds/event/jam/drop7.mp3
* http://assets.wonderfl.net/sounds/event/jam/drop9.mp3
* http://assets.wonderfl.net/sounds/event/jam/drop9.mp3
* http://assets.wonderfl.net/sounds/event/jam/drop10.mp3
*/
[SWF(backgroundColor="#000000", width="465", height="465", frameRate="30")]
public class Main extends Sprite {
private var manager:SoundManager;
private static var max:uint = 10;
private var sources:Array;
private var completed:uint = 0;
private var menus:Array;
private var lights:Array;
private static var cols:uint = 16;
private static var rows:uint = 16;
private var byteArray:ByteArray;
private static var channels:uint = 256;
private var factors:uint = 0;
public function Main() {
//Wonderfl.capture_delay(1);
init();
}
private function init():void {
graphics.beginFill(0x000000);
graphics.drawRect(0, 0, 465, 465);
graphics.endFill();
//
intialize();
manager = new SoundManager();
sources = new Array();
menus = new Array();
for (var n:uint = 0; n < max; n++) {
sources.push("http://assets.wonderfl.net/sounds/event/jam/drop" + (n + 1) + ".mp3");
var menu:ActionBtn = new ActionBtn();
menu.x = 52 + 90*(n%5);
menu.y = 400 + 42*uint(n/5);
addChild(menu);
var name:String = "sound"+(n+1)
menu.init({id: n, label: name, width: 80});
menu.enabled = false;
menus.push(menu);
}
load();
//
byteArray = new ByteArray();
}
private function intialize():void {
lights = new Array();
for (var c:uint = 0; c < 2; c++) {
lights[c] = new Array();
for (var n:uint = 0; n < cols*rows; n++) {
var light:Light = new Light();
if (c == 0) {
light.x = 225 - 14*(n%cols);
} else {
light.x = 243 + 14*(n%cols);
}
light.y = 363 - 340/(rows - 1)*uint(n/cols);
addChild(light);
light.blink(0);
lights[c].push(light);
}
}
}
private function load():void {
manager.addEventListener(Event.COMPLETE, complete, false, 0, true);
for (var n:uint = 0; n < max; n++) {
manager.load(sources[n], "snd" + n);
}
}
private function complete(evt:Event):void {
completed ++;
if (completed > max - 1) {
evt.target.removeEventListener(Event.COMPLETE, complete);
setup();
}
}
private function setup():void {
for (var n:uint = 0; n < max; n++) {
var menu:ActionBtn = menus[n];
menu.enabled = true;
menu.addEventListener(MouseEvent.CLICK, select, false, 0, true);
}
addEventListener(Event.ENTER_FRAME, update, false, 0, true);
}
private function select(evt:MouseEvent):void {
var id:uint = evt.target.id;
manager.play("snd" + id);
}
private function update(evt:Event):void {
SoundMixer.computeSpectrum(byteArray, true, factors);
for (var c:uint = 0; c < 2; c++) {
for (var n:uint = 0; n < channels; n++) {
var light:Light = lights[c][n];
var p:Number = byteArray.readFloat();
light.blink(p*10);
}
}
}
}
}
//////////////////////////////////////////////////
// SoundManagerクラス
//////////////////////////////////////////////////
import flash.events.EventDispatcher;
import flash.events.Event;
class SoundManager extends EventDispatcher {
private var sounds:Object;
private var levels:Object;
public function SoundManager() {
init();
}
private function init():void {
sounds = new Object();
levels = new Object();
}
public function load(file:String, id:String, level:Number = 1):void {
var se:SoundEffect = new SoundEffect();
se.addEventListener(Event.COMPLETE, complete, false, 0, true);
se.load(file, id);
sounds[id] = se;
levels[id] = level;
}
public function play(id:String):void {
var se:SoundEffect = sounds[id];
var volume:Number = levels[id];
se.play(id, volume);
}
private function complete(evt:Event):void {
dispatchEvent(new Event(Event.COMPLETE));
}
}
//////////////////////////////////////////////////
// SoundEffectクラス
//////////////////////////////////////////////////
import flash.events.EventDispatcher;
import flash.events.Event;
import flash.events.ProgressEvent;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.media.SoundTransform;
import flash.events.ProgressEvent;
import flash.net.URLRequest;
import flash.media.SoundLoaderContext;
class SoundEffect extends EventDispatcher {
private static var soundList:Object;
private var sound:Sound;
private var channel:SoundChannel;
private static var initialized:Boolean = false;
private var volume:Number;
private var looping:Boolean = false;
public function SoundEffect() {
if (!initialized) initialize();
}
private static function initialize():void {
initialized = true;
soundList = new Object();
}
public function init(Snd:Class, id:String):void {
var snd:Sound = new Snd();
soundList[id] = snd;
}
public function load(file:String, id:String):void {
var snd:Sound = new Sound();
snd.load(new URLRequest(file), new SoundLoaderContext(10, true));
snd.addEventListener(ProgressEvent.PROGRESS, progress, false, 0, true);
snd.addEventListener(Event.COMPLETE, loaded, false, 0, true);
soundList[id] = snd;
}
public function play(id:String, vol:Number, loop:Boolean = false):void {
if (channel != null) channel.stop();
sound = soundList[id];
volume = vol;
looping = loop;
channel = sound.play();
var transform:SoundTransform = channel.soundTransform;
transform.volume = volume;
channel.soundTransform = transform;
if (looping) {
channel.addEventListener(Event.SOUND_COMPLETE, complete, false, 0, true);
}
}
public function stop():void {
if (channel != null) {
channel.stop();
channel.removeEventListener(Event.SOUND_COMPLETE, complete);
}
}
private function progress(evt:ProgressEvent):void {
dispatchEvent(evt);
}
private function loaded(evt:Event):void {
dispatchEvent(evt);
}
private function complete(evt:Event):void {
channel.removeEventListener(Event.SOUND_COMPLETE, complete);
if (looping) {
channel = sound.play(0);
channel.addEventListener(Event.SOUND_COMPLETE, complete, false, 0, true);
var transform:SoundTransform = channel.soundTransform;
transform.volume = volume;
channel.soundTransform = transform;
}
}
}
//////////////////////////////////////////////////
// ActionBtnクラス
//////////////////////////////////////////////////
import flash.display.Sprite;
import flash.display.Shape;
import flash.text.TextField;
import flash.text.TextFieldType;
import flash.text.AntiAliasType;
import flash.text.TextFormat;
import flash.text.TextFormatAlign;
import flash.filters.DropShadowFilter;
import flash.geom.Matrix;
import flash.display.GradientType;
import flash.display.SpreadMethod;
import flash.display.InterpolationMethod;
import flash.geom.ColorTransform;
import flash.events.Event;
import flash.events.MouseEvent;
class ActionBtn extends Sprite {
public var id:uint;
private var shade:Shape;
private var base:Shape;
private var front:Shape;
private var light:Shape;
private var overlay:Shape;
private var txt:TextField;
private var label:String = "";
private static var fontType:String = "_ゴシック";
private var _width:uint = 60;
private static var _height:uint = 32;
private static var corner:uint = 6;
private static var tHeight:uint = 17;
private static var bColor:uint = 0xFFFFFF;
private static var sColor:uint = 0x000000;
private static var baseColor:uint = 0x0066CC;
private static var lightColor:uint = 0x00FFFF;
private static var overColor:uint = 0x3333FF;
private static var tColor:uint = 0xFFFFFF;
private static var defaultColorTrans:ColorTransform;
private static var disableColor:uint = 0xCCCCCC;
private static var disableColorTrans:ColorTransform;
private var mode:int;
private var _selected:Boolean = false;
private var _enabled:Boolean = true;
public function ActionBtn() {
}
public function init(option:Object):void {
if (option.id != undefined) id = option.id;
if (option.label != undefined) label = option.label;
if (option.width != undefined) _width = option.width;
draw();
}
private function draw():void {
defaultColorTrans = new ColorTransform();
disableColorTrans = new ColorTransform();
disableColorTrans.color = disableColor;
shade = new Shape();
base = new Shape();
front = new Shape();
light = new Shape();
overlay = new Shape();
txt = new TextField();
addChild(shade);
addChild(base);
addChild(front);
addChild(light);
addChild(overlay);
addChild(txt);
createBase(shade, _width, _height, corner, bColor);
shade.filters = [new DropShadowFilter(1, 90, sColor, 0.4, 4, 4, 2, 3, false, false)];
createBase(base, _width-2, _height-2, corner, baseColor);
createFront(front, _width, _height, corner);
createLight(light, _width-2, _height-2, corner);
createOverlay(overlay, _width-2, _height-2, corner);
txt.x = -_width/2;
txt.y = -tHeight + _height/4;
txt.width = _width;
txt.height = tHeight;
txt.type = TextFieldType.DYNAMIC;
txt.selectable = false;
//txt.embedFonts = true;
//txt.antiAliasType = AntiAliasType.ADVANCED;
var tf:TextFormat = new TextFormat();
tf.font = fontType;
tf.size = 10;
tf.align = TextFormatAlign.CENTER;
txt.defaultTextFormat = tf;
txt.textColor = tColor;
txt.text = label;
txt.filters = [new DropShadowFilter(0, 90, sColor, 0.5, 2, 2, 2, 3, false, false)];
light.alpha = 0;
enabled = true;
mouseChildren = false;
}
private function rollOver(evt:MouseEvent = null):void {
mode = 1;
addEventListener(Event.ENTER_FRAME, action, false, 0, true);
}
private function rollOut(evt:MouseEvent = null):void {
mode = -1;
addEventListener(Event.ENTER_FRAME, action, false, 0, true);
}
private function action(evt:Event):void {
light.alpha += 0.1*mode;
if (light.alpha < 0) {
light.alpha = 0;
removeEventListener(Event.ENTER_FRAME, action);
}
if (light.alpha > 1) {
light.alpha = 1;
removeEventListener(Event.ENTER_FRAME, action);
}
}
private function _up():void {
base.transform.colorTransform = defaultColorTrans;
front.transform.colorTransform = defaultColorTrans;
}
private function _down():void {
base.transform.colorTransform = defaultColorTrans;
front.transform.colorTransform = defaultColorTrans;
light.alpha = 1;
}
private function _disable():void {
base.transform.colorTransform = disableColorTrans;
front.transform.colorTransform = disableColorTrans;
}
public function get selected():Boolean {
return _selected;
}
public function set selected(param:Boolean):void {
_selected = param;
enabled = !_selected;
if (_selected) {
_down();
} else {
_up();
rollOut();
}
}
public function get enabled():Boolean {
return _enabled;
}
public function set enabled(param:Boolean):void {
_enabled = param;
buttonMode = _enabled;
mouseEnabled = _enabled;
useHandCursor = _enabled;
if (_enabled) {
_up();
addEventListener(MouseEvent.MOUSE_OVER, rollOver, false, 0, true);
addEventListener(MouseEvent.MOUSE_OUT, rollOut, false, 0, true);
} else {
_disable();
removeEventListener(MouseEvent.MOUSE_OVER, rollOver);
removeEventListener(MouseEvent.MOUSE_OUT, rollOut);
}
}
private function createBase(target:Shape, w:uint, h:uint, c:uint, color:uint, alpha:Number = 1):void {
target.graphics.beginFill(color, alpha);
target.graphics.drawRoundRect(-w/2, -h/2, w, h, c*2);
target.graphics.endFill();
}
private function createFront(target:Shape, w:uint, h:uint, c:uint):void {
var colors:Array = [baseColor, baseColor];
var alphas:Array = [0, 0.5];
var ratios:Array = [0, 255];
var matrix:Matrix = new Matrix();
matrix.createGradientBox(w, h, 0.5*Math.PI, -w/2, -h/2);
target.graphics.beginGradientFill(GradientType.LINEAR, colors, alphas, ratios, matrix, SpreadMethod.PAD, InterpolationMethod.RGB, 0);
target.graphics.drawRoundRect(-w/2, -h/2, w, h, c*2);
target.graphics.endFill();
}
private function createLight(target:Shape, w:uint, h:uint, c:uint):void {
var colors:Array = [lightColor, overColor];
var alphas:Array = [1, 0];
var ratios:Array = [40, 160];
var matrix:Matrix = new Matrix();
matrix.createGradientBox(w*2, h*3, 0, -w, -h*0.9);
target.graphics.beginGradientFill(GradientType.RADIAL, colors, alphas, ratios, matrix, SpreadMethod.PAD, InterpolationMethod.RGB, 0);
target.graphics.drawRoundRect(-w/2, -h/2, w, h, c*2);
target.graphics.endFill();
}
private function createOverlay(target:Shape, w:uint, h:uint, c:uint):void {
target.graphics.beginFill(bColor, 0.3);
target.graphics.drawEllipse(-w*1.125, -h*4.375, w*2.25, h*4.375);
target.graphics.endFill();
var _mask:Shape = new Shape();
_mask.graphics.beginFill(sColor);
_mask.graphics.drawRoundRect(-w/2, -h/2, w, h, c*2);
_mask.graphics.endFill();
addChild(_mask);
target.mask = _mask;
}
}
//////////////////////////////////////////////////
// Lightクラス
//////////////////////////////////////////////////
import flash.display.Sprite;
import flash.display.Shape;
import flash.events.Event;
import flash.geom.Matrix;
import flash.display.GradientType;
import flash.geom.ColorTransform;
class Light extends Sprite {
private var base:Sprite;
private static var center:Number = 4;
private static var radius:Number = 10;
private static var color:uint = 0xFFFFFF;
private var amplitude:Number = 0;
private var speed:Number;
private static var radian:Number = Math.PI/180;
private static var color1:uint = 0xFFFFFF;
private static var color2:uint = 0x00CCFF;
private var colorTrans:ColorTransform;
public function Light() {
base = new Sprite();
addChild(base);
draw();
colorTrans = new ColorTransform(0, 0, 0, 1, 0, 0, 0, 0);
}
public function blink(p:Number):void {
setRGB(p);
base.transform.colorTransform = colorTrans;
}
private function setRGB(percent:Number):void {
var r1:int = (color1 >> 16 & 0xFF);
var g1:int = (color1 >> 8 & 0xFF);
var b1:int = (color1 & 0xFF);
var r2:int = (color2 >> 16 & 0xFF);
var g2:int = (color2 >> 8 & 0xFF);
var b2:int = (color2 & 0xFF);
//colorTrans.alphaMultiplier = 0.75 + 0.5*percent;
colorTrans.alphaMultiplier = 0.1 + 0.9*percent;
colorTrans.redOffset = (r1 + r2)*0.5 + (r2 - r1)*0.5*percent;
colorTrans.greenOffset = (g1 + g2)*0.5 + (g2 - g1)*0.5*percent;
colorTrans.blueOffset = (b1 + b2)*0.5 + (b2 - b1)*0.5*percent;
}
private function draw():void {
base.graphics.beginFill(color);
base.graphics.drawCircle(0, 0, center);
base.graphics.endFill();
var colors:Array = [color, color];
var alphas:Array = [0.6, 0];
var ratios:Array = [0, 255];
var matrix:Matrix = new Matrix();
matrix.createGradientBox(radius*2, radius*2, 0, -radius, -radius);
base.graphics.beginGradientFill(GradientType.RADIAL, colors, alphas, ratios, matrix);
base.graphics.drawCircle(0, 0, radius);
base.graphics.endFill();
}
}