Jul i Sonofonhuset

This was a project where I took part as game design and programming intern in Titoonic a/s in late 2008. The production time was one month. My main tasks on the project was game design, sound design, and sound programming (where the sound engine was committed to the internal AS3 Framework).

Game Play

Example Code

An example from the manager class which was an adaptation of Matt Przybylski's code http://www.reintroducing.com:

/**
 * Fades the sound to the specified volume over the specified amount of time.
 * @param name: The string identifier of the sound
 * @param targVolume = 0: The target volume to fade to, between 0 and 1
 * @param fadeLength = 1: The time to fade over, in seconds
 */
public function fadeSound(name:String, targVolume:Number = 0, fadeLength:Number = 1, removeOnSilence:Boolean = false):void {
	var fadeChannel:SoundChannel = this._soundsDict[name].channel;
 
	//init SoundShortcuts in Tweener
	SoundShortcuts.init( );
	Tweener.addTween( fadeChannel, {_sound_volume:targVolume, time:fadeLength} );
 
	if( removeOnSilence && getSoundVolume( name ) <= 0) {
		this.removeSound( name );
	}
}

The sound object class

package {
import flash.display.DisplayObject;
import flash.events.Event;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.media.SoundTransform;    
 
/**
 * This class is used by the SoundManager class to instantiate a sound 
 * and its data such as name, volume, panning, etc.
 * See SoundManager for more info.
 */
public class SoundObject 
{
    public var name:String;
    public var volume:Number = 1;
    public var pan:Number = 0;
    public var startTime:Number = 0;
    public var loops:int = 0;
    public var volumeGroup:String;
    public var loadCategory:String;
    public var metaData:String;
    public var playCategory:String;
    public var playSolo:Boolean;
    public var parentDisplayObject:DisplayObject;
    public var sound:Sound;
    //important to declare or else it is not defined until the sound plays
    public var channel:SoundChannel = new SoundChannel;
    public var position:int = 0;
    public var paused:Boolean = true;
    public var pausedByAll:Boolean = false;
    public var isPlaying:Boolean = false;
    public var isMuted:Boolean = false;
    // if wearing headphones, most panning is too apparent. 
    // zOffset simulates the distance from the listener to the screen 
    private var zOffset:Number = 0.75;
 
    /**
     * Constructor
     */
    public function SoundObject() 
    {
    }
 
    /**
     * Play this sound. Prevents dublets of sounds with the solo flag. 
     * Prevents playback of new sounds in mute mode.
     * @param _startTime is the time that the clip has to start in seconds.
     * @param _loops is the amount of times the clip has to loop.
     * @param _volume is the volume of the clip
     * @param _pan is the left/right panning of the clip.
     */
    public function play(_startTime:Number, _loops:int, _volume:Number, _pan:Number):void 
    {    
        if(isPlaying && playSolo) return;        
 
        if( paused ) _startTime = startTime;
        else _startTime = 0;
 
        pan = _pan;
        if(isMuted) volume = 0;
        else volume = _volume;
 
        loops = _loops;
 
        paused = false;
        isPlaying = true;
 
        channel = sound.play(startTime, loops);
        setVol(volume);
        setPan(pan);
        channel.addEventListener(Event.SOUND_COMPLETE, sndComplete);
    }
 
    /**
     * Fires when the sound clip ends
     * @param event
     */
    private function sndComplete(event:Event):void 
    {
        channel.removeEventListener(Event.SOUND_COMPLETE, sndComplete);
        isPlaying = false;
    }
 
    /**
     * Stops the sound and resets startTime
     */
    public function stop():void 
    {
        paused = true;
        isPlaying = false;
        channel.stop();
        position = 0;
    }
 
    /**
     * Stops the sound and saves startTime
     */
    public function pause():void 
    {
        paused = true;
        isPlaying = false;
        position = channel.position;
        channel.stop();
    }
 
    /**
     * Sets the sound's volume to the passed Number between 0.0 and 1.0.
     * @param volume is a Number between 0.0 - 1.0
     */
    public function setVol(volume:Number):void 
    {
        // prevent overdrive
        if (volume < 0) volume = 0;
        else if (volume > 1) volume = 1;
 
        //set the volume
        var transform:SoundTransform = channel.soundTransform;
        transform.volume = volume;
        channel.soundTransform = transform;
    }
 
    /**
     * Sets the sound's panning to the passed Number 
     * between -1.0 (left) and 1.0 (right).
     * @param pan is a Number between -1.0 and 1.0
     */
    public function setPan(pan:Number):void 
    {
        // prevent off limit values
        if (pan < -1) pan = -1;
        else if (pan > 1) pan = 1;
 
        //offset z to simulate user's distance from the screen
        pan *= zOffset;
        //set the panning
        var transform:SoundTransform = channel.soundTransform;
        transform.pan = pan;
        channel.soundTransform = transform;
    }
}
}
 
Back to top
sonofon.txt · Last modified: 2009/08/26 08:20 by Nicolaj Schweitz