Using Flash Player 10 to produce Dynamic Musical Notes

May 20th, 2008

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.

Entry Filed under: actionscript, as3, flash

1 Comment Add your own

  • 1. rao  |  October 25th, 2009 at 9:59 am

    please update the code to new SoundAPI

Leave a Comment

Required

Required, hidden

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Trackback this post  |  Subscribe to the comments via RSS Feed