Start your Flash program. Select "New" from the "File" menu on the main Flash menu bar to create a Flash ActionScript 3 program. Select the "Actions" option from the "Window" menu on the Flash main menu bar to open the "Actions" editor to enter the piano keyboard code into Flash.
Declare a sound object named "pianoSound" in which to store piano key sounds. Type the code "var pianoSound:Sound = new Sound(); " as the first line of code in your "Actions" editor to code this declaration.
Declare a "KeyboardEvent" listener that will detect when the "spacebar" key on the keyboard is pressed. Name this event listener's function, "pressedKey" and code this function such that it will declare a "SampleDataEvent" listener. Code the "SampleDataEvent" event listener such that it will call a sound synthesis function (named "pianoSoundmake") and will play the sound data returned from the "pianoSoundmake" function. Type the code, listed below, beginning at the next line in the Action's editor to enter the code that accomplishes this.
stage.addEventListener (KeyboardEvent.KEY_DOWN, pressedKey);
function pressedKey(event: KeyboardEvent):void
{
if(event.keyCode == 32) {pianoSound.addEventListener(SampleDataEvent.SAMPLE_DATA, pianoSoundmake);pianoSound.play(); }
}
Generate the piano sound data with a "for loop" in the "pianoSoundmake" function that adds a sound data point 8,192 times to meet the sound buffering requirements of the Flash sound system. Use the "position" property to start the loop over at 8,192 increments to continue to supply sound data to the "pianoSound" sound object.
Type the code, listed below, starting at the next line in the Action's editor to accomplish this.
function pianoSoundmake(event:SampleDataEvent):void
{
for (var time:int = event.position; time < event.position + 8192; time++)
}
Add the code in the for loop to write the data into the pianoSound's data property called "data." with Flash's Math.sin() function. Code the "pianoSoundmake" function such that it will generate the piano sound data with a test frequency of 1,000 cycles per second. Normalize the sampling rate used in Math.sin statement by dividing the frequency by the Flash sound sampling rate, 44,100 cycles per second. Define the sine function so that it fits the standard equation of a sine wave, sine (2*PI*frequency+time). Type the code "event.data.writeFloat(0.5*Math.sin(2*Math.PI *(time/44100)*1000));" in the "pianoSoundmake" function body to add the sine wave sound to the "pianoSound" object as shown below.
pianoSoundmake(event:SampleDataEvent):void
{
for (var time:int = event.position; time < event.position + 8192; time++)
event.data.writeFloat(0.5*Math.sin(2*Math.PI *(time/44100)*6000));
}
Review your code for functionality and syntax errors. Select the "blue check" icon at the top of the Action's editor to have Flash check the code for syntax errors. Correct any errors reported. Copy and paste the code below if you have not already typed in the code in the previous steps.
var pianoSound:Sound = new Sound();
stage.addEventListener (KeyboardEvent.KEY_DOWN, pressedKey);
function pressedKey(event: KeyboardEvent):void
{
if(event.keyCode == 32) {pianoSound.addEventListener(SampleDataEvent.SAMPLE_DATA, pianoSoundmake);pianoSound.play(); }
}
function pianoSoundmake(event:SampleDataEvent):void
{
for (var time:int = event.position; time < event.position + 8192; time++)
event.data.writeFloat(0.5*Math.sin(2*Math.PI *(time/44100)*1000));
}
Select the "TestMovie" option from the "Control" menu in the main Flash menu bar to play and test the movie. Position your mouse over the screen and press the space bar key to hear the piano sound.