Array

Modifying Sound

Must Read

Admin
Admin
Just post!

The SoundTransform class is used to control volume and panning for the SoundChannel and the SoundMixer.

Controlling Volume

The SoundTransform object can be applied to the SoundChannel in two ways. In this example, it is passed as a parameter when the channel is first created:

[code]

var sound:Sound = new Sound();
var volume:Number = 0.75;
var soundTransform:SoundTransform = new SoundTransform(volume);
var soundChannel:SoundChannel = sound.play(0, 0, soundTransform);

[/code]

If you change the volume property, the SoundTransform object needs to be reapplied:

[code]

transform.volume = 0.50;
soundChannel.soundTransform = transform;

[/code]

If you want to change the volume over time, use a Timer or an Enter Frame event. In this example, we bring the volume from silence to full volume, and then back to silence, and so forth. The volume gets reset on the upper and lower bounds because, due to float errors, it may not end up exactly on zero or one:

[code]

import flash.media.SoundTransform;
import flash.net.URLRequest;
import flash.media.SoundChannel;
import flash.events.Event;
var volume:Number = 0;
var direction:int = 1;
var transform:SoundTransform;
transform = new SoundTransform();
var sound:Sound = new Sound();
sound.load(new URLRequest(“mySound.mp3”));
var soundChannel:SoundChannel = sound.play();
stage.addEventListener(Event.ENTER_FRAME, changeVolume);
function changeVolume(event:Event):void {
volume += 0.01*direction;
// when reaching upper volume bounds, change direction
if (volume > 1) {
volume = 1.0;
direction *= -1;
// when reaching lower volume bounds, change direction
} else if (volume < 0) {
volume = 0;
direction *= -1;
}
transform.volume = volume;
soundChannel.soundTransform = transform;
}

[/code]

To change volume globally, use the SoundMixer. In the following example, we mute all the channels at once. Make sure not to confuse the syntax of the SoundTransform class and the soundTransform property which has a lowercase s:

[code]

var globalTransform:SoundTransform = new SoundTransform();
globalTransform.volume = 0;
SoundMixer.soundTransform = globalTransform;

[/code]

Panning

Panning is the position of the audio signal between the right and left channels in a stereo sound field. Pan has a value from ‒1 all the way to the left channel to 1 all the way to the right channel.

The external speaker on Android devices is mono. Make sure to inform your users to use their headphones, which are stereo, if you are using this technique.

The pan value is passed as a second parameter when creating a SoundTransform object:

[code]

var soundTransform:SoundTransform = new SoundTransform(1, -1);

[/code]

The object can be applied to one sound alone via SoundChannel:

[code]

var channel:SoundChannel = sound.play(0, 1, soundTransform);

[/code]

You can also pan all the sounds via SoundMixer:

[code]

import flash.media.SoundMixer;
SoundMixer.soundTransform = new SoundTransform(1, -1);

[/code]

A common technique to pan back and forth between channels is to use a Math.sin function, which returns a value between ‒1 and 1:

[code]

var panPhase:Number = 0;
var transformObject:SoundTransform;
var channel:SoundChannel
transformObject = new SoundTransform();
function onEnterFrame(event:Event):void {
transformObject.pan = Math.sin(panPhase);
channel.soundTransform = transformObject;
panPhase += 0.05;
}

[/code]

Previous articleID3 Tags
Next articleRaw Data and the Sound Spectrum
- Advertisement -

Latest News

Elevate Your Bentley Experience: The Bespoke Elegance of Bentayga EWB by Mulliner

Bentley Motors redefines the essence of bespoke luxury with the introduction of the Bentayga EWB's groundbreaking two-tone customization option—a...
- Advertisement -

More Articles Like This

- Advertisement -