PageRenderTime 367ms CodeModel.GetById 161ms app.highlight 9ms RepoModel.GetById 195ms app.codeStats 0ms

/flash/MP3FileReferenceLoaderLib/src/org/audiofx/mp3/MP3FileReferenceLoader.as

http://echo-nest-remix.googlecode.com/
ActionScript | 149 lines | 93 code | 15 blank | 41 comment | 6 complexity | 657440cfac2c2b5ebc819cce51a6820b MD5 | raw file
  1/*
  2Copyright (c) 2008 Christopher Martin-Sperry (audiofx.org@gmail.com)
  3
  4Permission is hereby granted, free of charge, to any person obtaining a copy
  5of this software and associated documentation files (the "Software"), to deal
  6in the Software without restriction, including without limitation the rights
  7to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8copies of the Software, and to permit persons to whom the Software is
  9furnished to do so, subject to the following conditions:
 10
 11The above copyright notice and this permission notice shall be included in
 12all copies or substantial portions of the Software.
 13
 14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 15IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 16FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 17AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 18LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 19OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 20THE SOFTWARE.
 21*/
 22
 23package org.audiofx.mp3
 24{
 25	import flash.display.Loader;
 26	import flash.display.LoaderInfo;
 27	import flash.events.Event;
 28	import flash.events.EventDispatcher;
 29	import flash.media.Sound;
 30	import flash.net.FileReference;
 31	import flash.utils.ByteArray;
 32	import flash.utils.Endian;
 33	
 34	/**
 35	 * Dispatched when the MP3 data is loaded
 36	* @eventType org.audiofx.mp3.MP3SoundEvent.COMPLETE 
 37	* 
 38	*/
 39	[Event(name="complete", type="org.audiofx.mp3.MP3SoundEvent")]
 40	/**
 41	 * Class for loading MP3 files from a FileReference
 42	 * @author spender
 43	 * @see flash.net.FileReference
 44	 */
 45	public class MP3FileReferenceLoader extends EventDispatcher
 46	{
 47		private var mp3Parser:MP3Parser;
 48		
 49		/**
 50		 * Constructs an new MP3FileReferenceLoader instance 
 51		 * 
 52		 */
 53		public function MP3FileReferenceLoader()
 54		{
 55			mp3Parser=new MP3Parser();
 56			mp3Parser.addEventListener(Event.COMPLETE,parserCompleteHandler);
 57			
 58		}
 59		/**
 60		 * Once a FileReference instance has been obtained, and the user has browsed to a file, call getSound to start loading the MP3 data.
 61		 * When the data is ready, an <code>MP3SoundEvent.COMPLETE</code> event is emitted.
 62		 * @param fr A reference to a local file.
 63		 * @see MP3SoundEvent
 64		 */
 65		public function getSound(fr:FileReference):void
 66		{
 67			
 68			mp3Parser.loadFileRef(fr);
 69		}
 70		private function parserCompleteHandler(ev:Event):void
 71		{
 72			var parser:MP3Parser=ev.currentTarget as MP3Parser;
 73			generateSound(parser);
 74		}
 75		private function generateSound(mp3Source:MP3Parser):Boolean
 76		{
 77			var swfBytes:ByteArray=new ByteArray();
 78			swfBytes.endian=Endian.LITTLE_ENDIAN;
 79			for(var i:uint=0;i<SoundClassSwfByteCode.soundClassSwfBytes1.length;++i)
 80			{
 81				swfBytes.writeByte(SoundClassSwfByteCode.soundClassSwfBytes1[i]);
 82			}
 83			var swfSizePosition:uint=swfBytes.position;
 84			swfBytes.writeInt(0); //swf size will go here
 85			for(i=0;i<SoundClassSwfByteCode.soundClassSwfBytes2.length;++i)
 86			{
 87				swfBytes.writeByte(SoundClassSwfByteCode.soundClassSwfBytes2[i]);
 88			}
 89			var audioSizePosition:uint=swfBytes.position;
 90			swfBytes.writeInt(0); //audiodatasize+7 to go here
 91			swfBytes.writeByte(1);
 92			swfBytes.writeByte(0);
 93			mp3Source.writeSwfFormatByte(swfBytes);
 94			
 95			var sampleSizePosition:uint=swfBytes.position;
 96			swfBytes.writeInt(0); //number of samples goes here
 97			
 98			swfBytes.writeByte(0); //seeksamples
 99			swfBytes.writeByte(0);
100						
101			var frameCount:uint=0;
102			
103			var byteCount:uint=0; //this includes the seeksamples written earlier
104						
105			for(;;)
106			{
107			
108				var seg:ByteArraySegment=mp3Source.getNextFrame();
109				if(seg==null)break;
110				swfBytes.writeBytes(seg.byteArray,seg.start,seg.length);
111				byteCount+=seg.length;
112				frameCount++;
113			}
114			if(byteCount==0)
115			{
116				return false;
117			}
118			byteCount+=2;
119
120			var currentPos:uint=swfBytes.position;
121			swfBytes.position=audioSizePosition;
122			swfBytes.writeInt(byteCount+7);
123			swfBytes.position=sampleSizePosition;
124			swfBytes.writeInt(frameCount*1152);
125			swfBytes.position=currentPos;
126			for(i=0;i<SoundClassSwfByteCode.soundClassSwfBytes3.length;++i)
127			{
128				swfBytes.writeByte(SoundClassSwfByteCode.soundClassSwfBytes3[i]);
129			}
130			swfBytes.position=swfSizePosition;
131			swfBytes.writeInt(swfBytes.length);
132			swfBytes.position=0;
133			var swfBytesLoader:Loader=new Loader();
134			swfBytesLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,swfCreated);
135			swfBytesLoader.loadBytes(swfBytes);
136			return true;
137		}
138		private function swfCreated(ev:Event):void
139		{
140
141			var loaderInfo:LoaderInfo=ev.currentTarget as LoaderInfo;
142			var soundClass:Class=loaderInfo.applicationDomain.getDefinition("SoundClass") as Class;
143			var sound:Sound=new soundClass();
144			dispatchEvent(new MP3SoundEvent(MP3SoundEvent.COMPLETE,sound));
145			
146		}
147
148	}
149}