﻿//****************************************************************************
//Copyright © 2005-2009. Adobe Macromedia Software LLC. All rights reserved.
//The following Code is subject to all restrictions 
//contained in the End User License Agreement accompanying
//this product.
//****************************************************************************
package com.adobe.captivate.flash
{
	import flash.display.*;
	import flash.events.*;
	import flash.media.*;
	import flash.utils.*;

	public class rdSound extends Sound
	{
		private var m_linkageID:String;
		private var m_soundChannel:SoundChannel;
		private var m_soundRef:Sound;
		private var m_fadeInFrames:Number;
		private var m_fadeOutFrames:Number;
		private var m_totalFrames:Number;
		private var m_playInfinitely:Boolean = false;
		private var m_timerMC:MovieClip;
		private var m_frameCount:int;
		
		function rdSound()
		{
			m_linkageID = "";
		}
		
		function attachSound(id:String):void
		{
			m_linkageID = id;
			var classDefinition:Object = getDefinitionByName(id);
			m_soundRef = Sound(new classDefinition());
		}
		
		function pause():void
		{
			stopSound();
		}
		
		function playSound():void
		{
		}
		
		function stopSound():void
		{
			if (m_soundChannel)
				m_soundChannel.stop();
			soundComplete(null);
		}
		
		public function startSound(fadeIn:Number = 0, fadeOut:Number = 0, fps:Number = 30):void
		{
			m_soundChannel = m_soundRef.play();
			initialize(fadeIn, fadeOut, fps);
		}
		
		public function playForever(fadeIn:Number = 0, fadeOut:Number = 0, fps:Number = 30):void
		{
			m_soundChannel = m_soundRef.play(0,9999);
			m_playInfinitely = true;
			initialize(fadeIn, fadeOut, fps);
		}
		
		private function initialize(fadeIn:Number, fadeOut:Number, fps:Number)
		{
			if(fps > 0)
			{
				m_fadeInFrames = fadeIn*fps;
				m_fadeOutFrames = fadeOut*fps;
				m_totalFrames = int(m_soundRef.length/1000)*fps;
				handleFade();
			}
		}
		
		private function handleFade():void
		{
			if(m_fadeInFrames > 0 || m_fadeOutFrames > 0)
			{
				m_timerMC = new MovieClip();
				m_timerMC.addEventListener(Event.ENTER_FRAME, enterframe);
				m_frameCount = 0;
				m_soundChannel.addEventListener(Event.SOUND_COMPLETE, soundComplete);
			}
		}
		
		private function soundComplete(aevent:Event):void
		{
			if(m_timerMC)
			{
				m_timerMC.removeEventListener(Event.ENTER_FRAME, enterframe);
				m_timerMC = null;
			}
		}
		
		private function enterframe(aevent:Event):void
		{
			m_frameCount++;
			if(m_fadeInFrames > 0 && m_frameCount < m_fadeInFrames)
			{
				setVolume(m_frameCount/m_fadeInFrames);
			}
			if(!m_playInfinitely && m_fadeOutFrames > 0)
			{
				var fadeOutLimit:Number = m_totalFrames - m_fadeOutFrames;
				if(m_frameCount > fadeOutLimit)
				{
					var posInSpan:Number = m_frameCount - fadeOutLimit;
					setVolume(1 - posInSpan/m_fadeOutFrames);
				}
			}
		}
		
		/*
		*	@vol: from 0 to 1
		*/
		private function setVolume(vol:Number):void
		{
			if(vol < 0)
				vol = 0;
			if(vol > 1)
				vol = 1;
			var aTransform:SoundTransform = m_soundChannel.soundTransform;			
			aTransform.volume = vol;
			m_soundChannel.soundTransform = aTransform;
		}
	}

}
