How to Make a Flash Piano Keyboard

Written by mark stansberry
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to Make a Flash Piano Keyboard
Turn your computer keyboard into a piano with Adobe Flash software. (Ryan McVay/Photodisc/Getty Images)

Turning your computer into a piano can not only save you the expense of buying a piano, but can also build your music composition skills. Designing a full-featured Flash piano keyboard will let you not only play piano notes with your computer keyboard, but will also require you to study the intricacies of piano sounds. Making a working piano keyboard in Flash requires that you not only know how to code Flash's sound and keyboard commands, but also incorporate realistic piano sounds into your Flash program.

Skill level:
Easy

Other People Are Reading

Things you need

  • Adobe Flash Professional (CS3, CS4 or CS5 versions)

Show MoreHide

Instructions

  1. 1

    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.

  2. 2

    Declare a sound object named "pianoSound" in which to store piano key sounds. Type the code "var piano Sound:Sound = new Sound(); " as the first line of code in your "Actions" editor to code this declaration.

  3. 3

    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, pressed Key);

    function pressed Key(event: KeyboardEvent):void

    {

    if(event.keyCode == 32) {pianoSound.addEventListener(SampleDataEvent.SAMPLE_DATA, pianoSoundmake);pianoSound.play(); }

    }

  4. 4

    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++)

    }

  5. 5

    Add the code in the for loop to write the data into the piano Sound'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. Normalise 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 (2PIfrequency+time). Type the code "event.data.writeFloat(0.5Math.sin(2Math.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.5Math.sin(2Math.PI (time/44100)6000));

    }

  6. 6

    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 piano Sound:Sound = new Sound();

    stage.addEventListener (KeyboardEvent.KEY_DOWN, pressed Key);

    function pressed Key(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.5Math.sin(2Math.PI (time/44100)1000));

    }

  7. 7

    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.

Tips and warnings

  • Consider recording piano notes from a real piano and saving them as MP3 files if you want to eliminate the need to write music synthesis code. In this case, you will load piano key sounds into your Flash piano keyboard instead of synthesising them.
  • Be prepared to work a long time if you want to synthesise your piano sounds so that your flash piano will sound like a concert hall piano. Music professionals spend years attempting to model notes from piano keys to make better and better musical synthesizers. Modelling piano keys requires advanced knowledge of mathematical subjects like Fourier analysis and an understanding of physics as it relates to acoustics.

Don't Miss

Filter:
  • All types
  • Articles
  • Slideshows
  • Videos
Sort:
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the eHow.co.uk site, you consent to the use of cookies. For more information, please see our Cookie policy.