Archive for May, 2008

Flash Player 10 API online no zip file

Thanks to Alex Bustin. He decided to take the bandwidth hit and upload the Flash Player 10 API to his site.

No you don’t have to download it. Also, Matt Chotin said that some new properties do not code complete currently, so don’t think you did something wrong. It’s Adobe not you.

Add comment May 22nd, 2008

Using Flash Player 10 to produce Dynamic Musical Notes

image of swf flash player 10 guitar tunerSo after 13 year of off and on drumming I have learned a thing or two about music. Not much about tuning a guitar. Thanks to a class that I got a D in, Musical Acoustics, I know every note produces a frequency and a wavelength. With that you can now you Flash to produce a frequency and that is what I did, the six notes from left to right are the notes of a regularly tuned guitar.

Since I lost my Musical Acoustics book from UNT I had to look it up to find the Frequency of All Notes

and that pick the ones I wanted starting w/ E4.

So if you don’t have a guitar tuner…here you go.

View Code:

[as]

package
{
import flash.display.*;
import flash.events.*;
import flash.media.Sound;
import flash.text.TextField;

public class GuitarTuner extends Sprite
{
private var _key1:Sprite;
private var _key2:Sprite;
private var _key3:Sprite;
private var _key4:Sprite;
private var _key5:Sprite;
private var _key6:Sprite;

private var _sound:Sound;
private var _note:Number = 0;
private var noise:Number = 0;
private var text:TextField = new TextField();

//Constants
private static const E4:String = “E4″;
private static const A4:String = “A4″;
private static const D4:String = “D5″;
private static const G5:String = “G5″;
private static const B5:String = “B5″;
private static const E6:String = “E6″;

public function GuitarTuner()
{
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
_key1 = makeKey();
_key1.name = GuitarTuner.E4;
_key1.addEventListener(MouseEvent.CLICK, onClick, false, 0, true);
addChild(_key1);

_key2 = makeKey();
_key2.name = GuitarTuner.A4;
_key2.addEventListener(MouseEvent.CLICK, onClick, false, 0, true);
addChild(_key2);
_key2.x = spaceFromSibling(_key1, _key2);

_key3 = makeKey();
_key3.name = GuitarTuner.D4;
_key3.addEventListener(MouseEvent.CLICK, onClick, false, 0, true);
addChild(_key3);
_key3.x = spaceFromSibling(_key2, _key3);

_key4 = makeKey();
_key4.name = GuitarTuner.G5;
_key4.addEventListener(MouseEvent.CLICK, onClick, false, 0, true);
addChild(_key4);
_key4.x = spaceFromSibling(_key3, _key4);

_key5 = makeKey();
_key5.name = GuitarTuner.B5;
_key5.addEventListener(MouseEvent.CLICK, onClick, false, 0, true);
addChild(_key5);
_key5.x = spaceFromSibling(_key4, _key5);

_key6 = makeKey();
_key6.name = GuitarTuner.E6;
_key6.addEventListener(MouseEvent.CLICK, onClick, false, 0, true);
addChild(_key6);
_key6.x = spaceFromSibling(_key5, _key6);

addChild(text);
text.x = 300;

}
private function onCallback(e:SamplesCallbackEvent):void
{
for(var i:uint=0; i<512; i++)
{
noise += _note/ 44100;

var sample:Number = noise * Math.PI * 2;
_sound.samplesCallbackData.writeFloat(Math.sin(sample));
_sound.samplesCallbackData.writeFloat(Math.sin(sample));
}
}

private function onClick(evt:MouseEvent):void
{

_sound = new Sound();
_sound.addEventListener(Event.SAMPLES_CALLBACK, onCallback);
_sound.play();
var sprite:Sprite = Sprite(evt.currentTarget);
switch(sprite.name)
{
case “midC”:

_note = 277.18;

break;

case GuitarTuner.E4:

_note = 329.63;

break;

case GuitarTuner.A4:

_note = 440.00;

break;

case GuitarTuner.D4:
_note = 587.33;
break;

case GuitarTuner.G5:
_note = 783.99;
break;

case GuitarTuner.B5:
_note = 987.77;
break;

case GuitarTuner.E6:
_note = 1318.51;
break;
}

text.text = String(_note);

}

private function makeKey():Sprite
{
var key:Sprite = new Sprite()
key.graphics.lineStyle(1, 0×000000);
key.graphics.beginFill(0xFFFFFF, 1);
key.graphics.drawRect(0, 0, 20, 50);
key.graphics.endFill();
return key;
}

public function spaceFromSibling(sibling:DisplayObject, target:DisplayObject,
coordinate:String = “x”, distance:Number = 0):Number
{
var value:int;

switch(coordinate)
{
case “x”:
value = sibling.x + sibling.width + distance;
break;

case “y”:
value = sibling.y + sibling.height + distance;

break;
}

return Math.floor(value);
}
}
}[/as]

If this code doesn’t make sense check out Lee Brimelow’s tutorial on Dynamic Sound.

1 comment May 20th, 2008

Flash Player 10 Dynamic Sound Generation

I miss writing classes w/ 50 or so lines of code and here is one. This is a take off of Lee Brimelow’s Example of Dynamic Sound Generation. I’m using the BitmapData to create a tone.

[as]

package
{
import flash.display.*;
import flash.events.*;
import flash.media.*;
import flash.net.URLRequest;
import flash.system.Capabilities;
import flash.text.TextField;

public class DynamicSound extends Sprite
{
private var sound:Sound;
private var noise:Number = 0;
private var _loader:Loader;
private var _bm:Bitmap;
private var _verText:TextField;
public function DynamicSound():void
{
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
_verText = new TextField();
_verText.text = Capabilities.playerType + ” (” + Capabilities.version + “)”;
_loader = new Loader();
_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete, false, 0, true);
_loader.load(new URLRequest(”http://www.scarlet.nl/~ivo/photo_ASTRO3.JPEG”));

}

private function onComplete(evt:Event):void
{
_bm = Bitmap(_loader.content);
addChild(_bm);
addChild(_verText);
sound = new Sound();
sound.addEventListener(Event.SAMPLES_CALLBACK, onCallback);
sound.play();
}
private function onCallback(e:SamplesCallbackEvent):void
{
for(var i:uint=0; i<512; i++)
{
noise += _bm.bitmapData.getPixel(mouseX, mouseY)/ 44100;
var sample:Number = noise * Math.PI * 2;
sound.samplesCallbackData.writeFloat(Math.sin(sample));

sound.samplesCallbackData.writeFloat(Math.sin(sample));
}
}
}
}

[/as]

Quick and Dirty!

1 comment May 20th, 2008

How to load Pixel Bender in Flash Player 10

Hey so I spent most of the weekend trying to get Senocular’s tutorial on loading .pbj into flash player 10.

For some reason it wasn’t working for me so I decided to download Pixel Bender Preview Release and copy the file’s source code and export it (File > Export Pixel Bender…for Flash) and it worked so I wanted to post it. Just in case some one had the same problem.

AS FILE:

[as]package {
import flash.display.*;
import flash.events.*;
import flash.net.URLLoader;
import flash.net.URLLoaderDataFormat;
import flash.net.URLRequest;
import flash.utils.ByteArray;
import flash.utils.getTimer;

public class astro2 extends Sprite
{

// image for shader fill
private var _bitmap:Bitmap;

// shader fill shader; it will use
// the image as an input
private var _shader:Shader;

// loader to load pixel bender bytecode
// for shader instance
private var _shaderLoader:URLLoader

private var _loader:Loader

public function astro2()
{
//load image
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
_loader = new Loader();
_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onImgLoad, false, 0, true);
_loader.load(new URLRequest(”http://bp2.blogger.com/_Y8m29ZLX5ag/RarR3rcAVwI/AAAAAAAAAL4/dtA7xOMrI4g/s400/ASTRO_jpg.jpg”));

}

//once loaded load Pixel Bender File
private function onImgLoad(evt:Event):void
{
_bitmap = Bitmap(_loader.content);

_shader = new Shader();
_shaderLoader = new URLLoader();

_shaderLoader.dataFormat = URLLoaderDataFormat.BINARY;
_shaderLoader.addEventListener(Event.COMPLETE, shaderLoaded);
_shaderLoader.load(new URLRequest(”test.pbj”));
}

// shaderLoader complete handler
private function shaderLoaded(event:Event):void {

// set up shader

_shader.byteCode = _shaderLoader.data as ByteArray; // error if invalid
_shader.data.src.input = _bitmap.bitmapData; // image input
_shader.data.size.value = [100]; // constant size
_shader.data.fade.value = [.5]; // constant fade value

// draw the shader every frame
addEventListener(Event.ENTER_FRAME, drawKaleidoscope);

}

// enterframe handler
private function drawKaleidoscope(event:Event):void {
// base position on mouse location
_shader.data.position.value = [mouseX, mouseY];
// rotate constantly over time
_shader.data.angle.value = [getTimer()/500];

// draw a rectangle with the shader fill
graphics.clear();
graphics.beginShaderFill(_shader);
graphics.drawRect(0,0,300,250);
}
}
}[/as]

and the Pixel Bender code:

kernel Kaleidoscope
< namespace : “Petri Leskinen”;
version : 1;
vendor : “”;
description : “kaleidoscope -effect, rectangular”;
>
{
parameter float4 fadeColor
<
minValue: float4(0,0,0,0);
maxValue: float4(1,1,1,1);
defaultValue: float4(0,0,0,1);
description: “Fade color”;
>;
parameter float2 position
<
minValue: float2(0,0);
maxValue: float2(2880,2880);
defaultValue: float2(250,280);
description: “Center point”;
>;

parameter float size
<
minValue: float(1);
maxValue: float(500);
defaultValue: float(75);
description: “Size”;
>;

parameter float fade
<
minValue: float(0.5);
maxValue: float(1.0);
defaultValue: float(1.0);
description: “Fade”;
>;

parameter float angle
<
minValue: float(-3.14159);
maxValue: float(3.14159);
defaultValue: float(0.5);
description: “Rotation angle”;
>;

input image4 src;
output pixel4 dst;

void evaluatePixel()
{
float sina = sin(angle);
float cosa = cos(angle);

float2 newC = position – size/2.0* float2(cosa+sina, cosa-sina);

// mx matrix for rotation and scaling
// mxR reverse transformation
float2×2 mx = float2×2( cosa,sina,-sina,cosa) /size;
float2×2 mxR = float2×2( cosa,-sina,sina,cosa) *size;

// coordinate p1 on the new coordinate system,
// on the center area 0.0 < p1.x < 1.0 , 0.0 < p1.y < 1.0
float2 p1 = mx * (outCoord() -newC);

// p2 integer part, p1 only decimal part,
// which maps every pixel to the center area
float2 p2 = floor(p1);
p1 = fract(p1); // p1 -= p2;

// p3 dislocation for mirroring,
// mirrored if integer part odd
float2 p3 = 1.0-2.0*p1;
p1.x += mod (p2.x,2.0) > 0.0 ? p3.x : 0.0 ;
p1.y += mod (p2.y,2.0) > 0.0 ? p3.y : 0.0 ;

// reverting to the main coordinate system, sampling and mixing the pixel
p1 = newC + mxR*p1;
dst = mix( fadeColor, sampleLinear(src,p1) ,
pow ( fade,abs(p2.x)+abs(p2.y)) );

}
}

Add comment May 19th, 2008

How to compile and examples for Flash Player 10 or ASTRO BETA

As many of you know ASTRO is in Public beta.

Download browser plugin

Here are some links on how to compile:

Text instruction – by Mike Chanmbers

Video Instruction – by Lee Brimelow

Flash Magizine Article

A promising sound example:

flash produced sound example – by Keith Peters

Here is my “3D” example, quick and dirty:

[as]

package {
import flash.display.Loader;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Vector3D;
import flash.net.URLRequest;

public class ASTRO extends Sprite
{

private var _loader:Loader;
private var mc:MovieClip;
public function ASTRO()
{

stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.EXACT_FIT;
_loader = new Loader()
_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadComplete, false, 0, true);
_loader.load(new URLRequest(”record.gif”));

}

private function loadComplete(evt:Event):void
{

mc = new MovieClip();
mc.rotationY = 0;
mc.rotationZ = 0;

stage.addEventListener(MouseEvent.MOUSE_MOVE, onMove, false, 0, true);
mc.opaqueBackground = true;
_loader.x = – (_loader.width * 0.5);
_loader.y = – (_loader.height * 0.5);
mc.addChild(_loader);
mc.x = (_loader.width * 0.5);
mc.y = (_loader.height * 0.5);
addChild(mc);

}

private function onMove(evt:MouseEvent):void
{
mc.rotationZ = stage.mouseY;
mc.rotationX = stage.mouseX

}
}
}
[/as]

Also here are some classes that I saw through code completion. I don’t really know what to do with them yet. Maybe someone else will and will let me know (I have too much work and GTAing to do :) ).

  • flash.text.engine Package (over a dozen classes)
  • __AS3__.vec.Vector (that is wierd)
  • flash.display.GraphicsBitmapFill
  • flash.display.GraphicsPathCommand
  • flash.display.TriangleCulling (that sounds interesting)
  • flash.geom.Vector3D
  • flash.filters.ShaderFilter

Happy Coding!

1 comment May 16th, 2008


    Blog Calendar

    May 2008
    M T W T F S S
    « Apr   Jun »
     1234
    567891011
    12131415161718
    19202122232425
    262728293031  

    Posts by Month

    Posts by Category