14
ActionScript 3.0 programming Sound Thomas Lövgren Flash developer, designer & programmer [email protected] Introduction to Umeå Institute of Design, 2011-09-20

Flash/ActionScript 3.0 programming - interactiondesign.seactionscript_course_ixd2...The Sound class is used to load, play and manage basic sound functionalities Some useful properties,

  • Upload
    others

  • View
    12

  • Download
    0

Embed Size (px)

Citation preview

ActionScript 3.0 programming

Sound

Thomas Lövgren

Flash developer, designer & programmer

[email protected]

Introduction to

Umeå Institute of Design, 2011-09-20

Lecture outline

In this lecture we’ll discuss and practice the following topics:

Programming introduction, examples & exercises:

• AS3 Sound Architecture

• Loading and playing Internal and External sound

• Sound-related classes

• Coding a basic Sound Player

• More sound-functions & features

• Microphone

• Sound Visualization

Sound

Introduction to ActionScript 3.0 Sound

In AS3 there are a lot of useful classes and methods for different kinds of sound

functionality

• Sound class: Is used to load, play and manage basic sound properties

• SoundChannel class: Creates separate channel for each new sound played (“channel” does

not refer to left or right channel of stereo)

• SoundMixer class: A central mixer object through which all sound channels are mixed, can

be used to stop all sounds in an application

• SoundTransform class: Is used to control the volume, and panning between left and right

stereo channels of a source

• Microphone class: This class/object let us control the gain, sampling rate and other

properties of a Microphone

AS3 Sound Architecture

Sound

In Flash there are basically two different ways of working with sound

• Internal (sound in library)

• External Sound (loading a sound)

It’s recommended that we use external sounds: By loading a sound into the application, we

can keep the file-size down

Examples of how to use the different types:

• External sound: Load a sound file (MP3) into the application, for example:

Background music, mp3 player, sound visualization

• Internal sound: Attach sound from Library (linkage), for example:

Small sounds (file size), sound effects for a game, button click-sounds

Internal and External Sound

Sound

For playing a sound from library – first we need to import the sound to Flash, then set up

the Linkage property in library:

1. Right-click on the sound in library

2. Set up Linkage (from the Properties panel in CS5/CS5.5)

3. Check the box “Export for ActionScript”

//create a new "library" sound & channel object

var library_sound:explode_sound = new explode_sound(); //sound object

library_sound.play(); //play sound

Internal Sound: Sound in library/attach a sound

Sound

The Sound class is used to load, play and manage basic sound functionalities

Some useful properties, methods & events for the Sound class:

• load, play, complete, bytesLoaded, bytesTotal, length

//create a new sound object

var my_sound:Sound = new Sound();

Sound class (1/2)

Sound

Here is an example of how to load and play an external MP3 sound, and play it by using the

Sound class

var my_sound:Sound = new Sound(); //create a new sound object

my_sound.load(new URLRequest("David Gray - Sail Away.mp3")); //load sound

my_sound.play(); //play sound

Note! It’s recommended that we also use Events/functions to check if the loading is complete

Sound class (2/2) Load & play: External Sound

Sound

The SoundChannel class, can be used to create a separate channel for each new sound

played, stopping a sound and/or get sound-peaks

An application can have multiple sound channels that are mixed together

Some useful properties, methods & events for this class:

• stop, soundComplete, leftPeak, rightPeak, position

var my_sound:Sound = new Sound(); //create sound object

var my_channel:SoundChannel = new SoundChannel(); //create channel

my_sound.load(new URLRequest("David Gray - Sail Away.mp3")); //load sound

my_channel = my_sound.play(); //play sound through channel

//stopp the sound

my_channel.stop();

Note! The SoundChannel class is mandatory for stopping a sound!

SoundChannel class

Sound

If we want a sound to repeat when finished, we can add a listener to the Sound/channel

object with the event SOUND_COMPLETE

//add listener to channel object, soundcomplete event, function call

my_channel.addEventListener(Event.SOUND_COMPLETE, soundComplete);

//handler/function for repeating the sound

function soundComplete(event:Event):void{

my_channel = my_sound.play(); //play sound again

}

Sound Complete

Sound

There are a couple of basic sound features that can be useful; for example if we want to

pause and play a sound we can write like:

//declare variable, get the sound position

var pausePos:Number = sound_channel.position;

sound_channel.stop();

...and at a later point we can create a function that start’s the sound at that position by writing:

sound_channel = my_sound.play(pausePos);

Pause & play sound

Sound

The SoundTransform class, allows us to adjust the sound volume, and/or panning between

left and right channel of a source

Some of the properties and methods for this class:

• volume, pan

//add eventlistener to slider-mc, loop event, call function

volume_mc.slider_mc.addEventListener(Event.ENTER_FRAME, adjustVolume);

//function that adjust the volume by dragging a slider

function adjustVolume(event:Event):void{

var vol:Number = volume_mc.slider_mc.x / 100;

var st:SoundTransform = new SoundTransform(vol);

}

//more code…

SoundTransform class

Sound

The SoundMixer class creates a central mixer - changes to the mixer will affect all sounds

Some of the properties and methods for this class:

• stopAll, computeSpectrum, bufferTime etc

//add eventlister to button, mouse-event, function call

stop_all_btn.addEventListener(MouseEvent.MOUSE_DOWN, stopAllSound);

//function that stops all sounds

function stopAllSound(event:MouseEvent):void{

SoundMixer.stopAll();

}

SoundMixer class

Sound

The Microphone class allows us to connect a microphone, access and code specific features

By using the activityLevel method; it’s possible to for example animate the “activity” directly

from the microphone (see below)

//create a new microphone object

var my_mic:Microphone = Microphone.getMicrophone();

Security.showSettings(SecurityPanel.MICROPHONE);

my_mic.setLoopBack(true);

my_mic.setUseEchoSuppression(true);

//add eventListener, loop-event, function call

stage.addEventListener(Event.ENTER_FRAME, onLoop);

//function that displays the activities from the microphone

function onLoop(event:Event){

line_mc.width = my_mic.activityLevel * 5;

activity_dyn.text = "Activity: " + String(my_mic.activityLevel) + "%";

width_dyn.text = "Line_mc Width: " + String(line_mc.width) + "px";

trace(my_mic.activityLevel);

}

Microphone

Sound

By using the SoundMixer class and the computeSpectrum method; we can create

interesting and fancy animations synchronized with the sound source

Sound visualization

Sound

http://www.christeso.com/labs/actionscript-3-sound-visualization-generative-drawing.asp