Start the Flash program. Click "New" from the "File" menu on the main Flash menu bar. Click the file type "Flash File (ActionScript 3.0)" from the New Document dialog box that appears. Click "OK" to close this dialog box.
Select the "Actions" option from the Window menu on the main Flash menu bar to open the Actions editor. Position your mouse cursor on the first line of the Actions editor. Click your mouse button and type the code below (without the quotation marks) to declare a sound object:
"var mySound:Sound = new Sound();"
Type the code below (again, without the quotation marks), starting on the next line in the Actions editor, to instruct Flash to start sampling sound data and playing the sound data when the user presses the spacebar key (event.keyCode =32) on the keyboard:
"stage.addEventListener (KeyboardEvent.KEY_DOWN, pressedKey)
function pressedKey(event: KeyboardEvent):void
{ if(event.keyCode == 32) {mySound.addEventListener(SampleDataEvent.SAMPLE_DATA,soundGenerator)}
mySound.play();
}"
Type the code below (without quotation marks), starting on the next line of the Actions editor, to define the function that generates the sound data with the cosine wave function for a time equal to 100,000 sound samples.
"function soundGenerator(event:SampleDataEvent):void {
for ( var t:int=0; t<8192; t = t + 1 ) {
event.data.writeFloat(0.25*Math.cos((Number(t+event.position)/Math.PI)));
event.data.writeFloat(0.25*Math.cos((Number(t+event.position)/Math.PI)));
}
if (event.position>= 100000) {mySound.removeEventListener(SampleDataEvent.SAMPLE_DATA,soundGenerator);}
trace(event.position)
};"
Review the code you typed in, as listed below, for syntax errors and correct as necessary. Copy and paste the code below (without the quotation marks) into the Actions editor if you did not type the code in the previous steps and you want to ensure that your code has no errors.
"var mySound:Sound = new Sound();
stage.addEventListener (KeyboardEvent.KEY_DOWN, pressedKey)
function pressedKey(event: KeyboardEvent):void
{
if(event.keyCode == 32) {mySound.addEventListener(SampleDataEvent.SAMPLE_DATA,soundGenerator)}
mySound.play();
}
function soundGenerator(event:SampleDataEvent):void {
for ( var t:int=0; t<8192; t = t + 1 ) {
event.data.writeFloat(0.25*Math.cos((Number(t+event.position)/Math.PI)));
event.data.writeFloat(0.25*Math.cos((Number(t+event.position)/Math.PI)));
}
if (event.position>= 100000) {mySound.removeEventListener(SampleDataEvent.SAMPLE_DATA,soundGenerator);}
trace(event.position)
};"