Working with Dynamic Sound - Page 2
by paviter singh (sonoport) and kirupa |  20 October 2010

  Have questions? Discuss this Flash / ActionScript tutorial with others on the forums.

In the previous page, you got your project up and running and learned about what code you currently have to work with. In this page, let's actually add the sound and look at ways at making it sound different.

Adding Sound
Now that we've looked at the code responsible for moving our spaceship around the screen, let's start adding sound! The first thing we are going to do is add a import statement to allow us to use the Sonoport library where a lot of the functionality we will use lives.

Towards the top of the code file, you will see some import statements:

import flash.display.*;
import flash.events.*;

Go ahead and add the import statement for the Sonoport library to it:

import flash.display.*;
import flash.events.*;
import com.sonoport.*;

Adding an import statement won't really result in any changes to your application right now, but you'll see shortly that you can now use types that live inside this library.

Ok, now let's actually get to adding the sound. First, add a variable called snd whose type is SpaceShipSoundAmbience towards the top where your other variables are declared:

private const wSWF:Number = 750;
private const hSWF:Number = 400;
 
private var targetX:Number = wSWF/2;
private var targetY:Number = hSWF/2;
 
private var ship:Sprite;
private var snd:SpaceShipAmbience;

Once you have declared your snd variable, it is time to initialize it. In your init function, copy and paste the two non-grayed out lines that you see below:

private function init():void
{
 
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
 
 
// Creating a new Spacehip object
ship = new Spaceship();
addChild(ship);
 
snd = new SpaceShipAmbience();
snd.play();
 
stage.addEventListener(Event.ENTER_FRAME,onEnter);
stage.addEventListener(MouseEvent.MOUSE_MOVE, onMove);
 
}

Once you have added the above two lines of code, test your application by pressing Ctrl + Enter (Cmd + Return). You will now see that a slight buzzing sound can be heard through your speakers. In addition to the spaceship sound, you might also hear a drip sound regularly. That’s our watermark :-) To disable it, you will have to add the following line of code:

snd.setKey("[email protected]:SpaceShipAmbience:all:1:0:F6A6A1CE");

Insert that code in-between the two lines you just added in the above section:

snd = new SpaceShipAmbience();
snd.setKey("[email protected]:SpaceShipAmbience:all:1:0:F6A6A1CE");
snd.play();

Once you have added this line, your sound should work. While the sound works, it sounds pretty static - it just plays with no real variation to it. Let's change that next.

Making the Sound Dynamic
What we want to do next is adjust the sound based on where the spaceship actually is. For example, if the spaceship is on the left side of your screen, you would probably want to hear the sound coming from your left speakers. If the spaceship is on the right side of your screen, it would be good to hear the sound coming from your right speakers.

This will require a few more lines of code. Each sound model has a unique set of dynamic parameters that can be changed using the setParam() function. For what we want to do, we need to change the panning of the sound, and it is based on the x-position of our spaceship.

Inside the onEnter function, add the following two lines towards the bottom of this function:

var pan:Number = (ship.x-wSWF/2) / (wSWF/2);
snd.setParam(BaseSound.PAN, pan );

My onEnter function currently looks as follows:

private function onEnter(event:Event):void
{
//calculation for interpolating the ship's position
ship.x = (0.90*ship.x + 0.1*targetX)-1;
ship.y = (0.90*ship.y + 0.1*targetY)-1;
 
var dx:Number = Math.abs(targetX - ship.x);
var dy:Number = Math.abs(targetY - ship.y);
//calculating the distance that the ship has travelled
var distance:Number = Math.sqrt(dx * dx + dy * dy);
 
if (distance >= 450)
{
distance = 450;
}
 
//scaling the ship based on its y-position
ship.scaleX = ship.scaleY = ship.y * 0.5 * 0.01;
 
// Dynamic panning
var pan:Number = (ship.x-wSWF/2) / (wSWF/2);
snd.setParam(BaseSound.PAN, pan );
}

If you run your application, you should now be able to tell that the sound moves between your left and right speakers depending on where on the screen your spaceship is.

This is done by first getting normalized values between -1 and 1, and that is what the pan variable determines and stores. The setParam function that lives in your snd SpaceShipAmbience object takes the BaseSound.PAN property and the value of pan as determined one line earlier. When these values hit the snd object, the currently playing sound is altered to match the specified pan to give you the result that you hear.

Ok, now that you are more familiar with dynamically changing our sound model, let us do something more exciting...such as changing the frequency! To change the SpaceShipAmbience’s frequency, add the following line of code towards the bottom of your onEnter function:

snd.setParam(SpaceShipAmbience.FREQ, 50+distance);

Test your application to see it at work. Now, notice that I am using the setParam method again to specify the property and the value that property needs to have. This time, the property is SpaceShipAmbience.FREQ, and the value is based on the total distance the spaceship has traveled.

Since we are having so much fun, let's make some changes to the Gain and Harmonics to make our spaceship sound even more realistic. In the onEnter function, go ahead and tack on the following lines of code:

//calculating gain level based on ship's y-position
var gainLevel:Number = 1-((hSWF - ship.y) / hSWF);
snd.setParam(BaseSound.GAIN, gainLevel);
 
//changing the sound model's harmonics value
var harVal:Number = ship.y / 7 ;
 
//do check for harmonics max level
if(harVal >= 50)
{
harVal = 50;
}
snd.setParam(SpaceShipAmbience.HARMONICS, harVal);

This code is also pretty straightforward. It just does more things depending on where on the screen your spaceship actually is. The BaseSound.GAIN and SpaceShipAmbience.HARMONICS properties are in the crosshairs this time.

You may be wondering how to know which property to use. The Sonoport API docs will help you out with that. For example, the SpaceShipAmbience page describes the harmonics and frequency properties.

Conclusion
If anything, hopefully this tutorial helped you to learn how easy it is to enhance your applications by simply improving how you work with sound. As you can see, the Sonoport library makes it fairly easy to work with a variety of sounds and tweak their respective parameters without having to break a sweat.

For any questions or comments, either visit Sonoport's web site or post on the forums here.

Paviter Singh (Sonoport)
Kirupa

 

1 | 2




SUPPORTERS:

kirupa.com's fast and reliable hosting provided by Media Temple.