How to Make a Sound in AS3

Making your own sounds with ActionScript saves you the effort of recording sounds and saves you the expense of buying sounds for your Flash movie. With ActionScripts sound objects, methods and properties, you can even design your sounds for your specific needs. Creating sounds is relatively easy with ActionScript. Creating specific types of sounds can be more challenging. However, if you're mathematically inclined and have a musical bent, creating your own sounds will give you an avenue to further develop your mathematical and musical composition skills.

Things You'll Need

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

Instructions

    • 1

      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.

    • 2

      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();"

    • 3

      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();

      }"

    • 4

      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)

      };"

    • 5

      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)

      };"

Learnify Hub © www.0685.com All Rights Reserved