import
flash.display.Bitmap;
import
flash.display.BitmapData;
import
flash.text.TextField;
import
flash.events.MouseEvent;
import
flash.text.TextFormat;
import
flash.text.TextFormatAlign;
import
flash.text.TextFieldAutoSize;
import
flash.media.Sound;
import
flash.events.SampleDataEvent;
var
mySound:Sound =
new
Sound();
var
t:
Number
=
0
;
var
amplitude:
Number
=
0.25
;
var
graph_amplitude:
Number
=
50
/amplitude;
var
bmpDat:BitmapData =
new
BitmapData(
400
,
200
,
false
,
0x0
);
var
bmp:Bitmap =
new
Bitmap(bmpDat);
bmp.y=
50
;
addChild(bmp);
var
tfield:TextField =
new
TextField();
tfield.text =
"화면을 누르면 시작."
;
tfield.x =
120
;
tfield.y =
100
;
tfield.autoSize = TextFieldAutoSize.LEFT;
tfield.selectable =
false
;
var
tformat:TextFormat = tfield.getTextFormat();
tformat.color =
0x33cc33
;
tformat.align = TextFormatAlign.CENTER;
tfield.setTextFormat(tformat);
addChild(tfield);
stage.addEventListener(MouseEvent.CLICK, mouse_h);
function
sineWaveGenerator(event:SampleDataEvent):
void
{
t+=
2
;
bmpDat.fillRect(
new
Rectangle(
0
,
0
,
400
,
200
),
0x0
);
var
wave:
Number
;
for
(
var
c:
int
=
0
; c<
2048
; c++ ) {
wave = getWave(c+event.position,t);
event.data.writeFloat(wave);
event.data.writeFloat(wave);
if
(c<
800
){
bmpDat.setPixel(c,wave*graph_amplitude+
100
,
0x00ff00
);
}
}
bmp.bitmapData = bmpDat;
}
function
getWave(p:
Number
,t:
Number
):
Number
{
return
(Math.sin(
0.420
*(
Number
(p+t)/Math.PI/
2
))+Math.sin(
0.420
*(
Number
(p)/Math.PI/
2
)))*amplitude;
}
function
mouse_h(evt:MouseEvent):
void
{
mySound.addEventListener(SampleDataEvent.SAMPLE_DATA,sineWaveGenerator);
mySound.play();
removeChild(tfield);
}