PageRenderTime 685ms CodeModel.GetById 474ms app.highlight 10ms RepoModel.GetById 120ms app.codeStats 0ms

/flash/echo-nest-flash-api/src/com/ryanberdeen/echonest/api/v3/track/utils/TrackLoader.as

http://echo-nest-remix.googlecode.com/
ActionScript | 129 lines | 106 code | 23 blank | 0 comment | 2 complexity | 562b4201dc1333708f6250bb20b3d049 MD5 | raw file
  1package com.ryanberdeen.echonest.api.v3.track.utils {
  2  import com.ryanberdeen.echonest.api.v3.EchoNestError;
  3  import com.ryanberdeen.echonest.api.v3.EchoNestErrorEvent;
  4  import com.ryanberdeen.echonest.api.v3.track.AnalysisEvent;
  5  import com.ryanberdeen.echonest.api.v3.track.AnalysisLoader;
  6  import com.ryanberdeen.echonest.api.v3.track.TrackApi;
  7  import com.ryanberdeen.utils.MD5Calculator;
  8
  9  import flash.events.Event;
 10  import flash.events.EventDispatcher;
 11  import flash.events.IOErrorEvent;
 12  import flash.events.SecurityErrorEvent;
 13  import flash.media.Sound;
 14  import flash.net.FileReference;
 15
 16  import org.audiofx.mp3.MP3FileReferenceLoader;
 17  import org.audiofx.mp3.MP3SoundEvent;
 18
 19  public class TrackLoader extends EventDispatcher {
 20    private var trackApi:TrackApi;
 21
 22    private var _fileReference:FileReference;
 23    private var mp3Loader:MP3FileReferenceLoader;
 24    private var _md5Calculator:MD5Calculator;
 25    private var _analysisLoader:AnalysisLoader;
 26
 27    private var _alwaysUpload:Boolean;
 28
 29    private var _sound:Sound;
 30
 31    private var _md5:String;
 32    private var _id:String;
 33
 34    public function TrackLoader(trackApi:TrackApi) {
 35      this.trackApi = trackApi;
 36      _fileReference = new FileReference();
 37      _md5Calculator = new MD5Calculator();
 38      _analysisLoader = new AnalysisLoader(trackApi);
 39      _alwaysUpload = false;
 40    }
 41
 42    public function set alwaysUpload(alwaysUpload:Boolean):void {
 43      _alwaysUpload = alwaysUpload;
 44    }
 45
 46    public function get fileReference():FileReference {
 47      return _fileReference;
 48    }
 49
 50    public function get md5Calculator():MD5Calculator {
 51      return _md5Calculator;
 52    }
 53
 54    public function get analysisLoader():AnalysisLoader {
 55      return _analysisLoader;
 56    }
 57
 58    public function get sound():Sound {
 59      return _sound;
 60    }
 61
 62    public function load(...properties):void {
 63      _analysisLoader.properties = properties;
 64      _fileReference.addEventListener(Event.SELECT, fileReferenceSelectHandler);
 65      _fileReference.browse();
 66    }
 67
 68    private function fileReferenceSelectHandler(e:Event):void {
 69      dispatchEvent(new TrackLoaderEvent(TrackLoaderEvent.LOADING_SOUND));
 70      mp3Loader = new MP3FileReferenceLoader();
 71      mp3Loader.addEventListener(Event.COMPLETE, soundLoadCompleteHandler);
 72      mp3Loader.getSound(_fileReference);
 73    }
 74
 75    private function soundLoadCompleteHandler(e:MP3SoundEvent):void {
 76      _sound = e.sound;
 77      dispatchEvent(new TrackLoaderEvent(TrackLoaderEvent.SOUND_LOADED));
 78      mp3Loader.removeEventListener(Event.COMPLETE, soundLoadCompleteHandler);
 79
 80      if (_alwaysUpload) {
 81        uploadFile();
 82      }
 83      else {
 84        calculateMd5();
 85      }
 86    }
 87
 88    private function calculateMd5():void {
 89      dispatchEvent(new TrackLoaderEvent(TrackLoaderEvent.CALCULATING_MD5));
 90      _md5Calculator.addEventListener(Event.COMPLETE, md5CompleteHandler);
 91      _md5Calculator.calculate(_fileReference.data);
 92    }
 93
 94    private function md5CompleteHandler(e:Event):void {
 95      dispatchEvent(new TrackLoaderEvent(TrackLoaderEvent.MD5_CALCULATED));
 96      _md5 = _md5Calculator.md5;
 97      _md5Calculator = null;
 98
 99      dispatchEvent(new TrackLoaderEvent(TrackLoaderEvent.CHECKING_ANALYSIS));
100      loadAnalysis({md5: _md5});
101    }
102
103    private function uploadFile():void {
104      dispatchEvent(new TrackLoaderEvent(TrackLoaderEvent.UPLOADING_FILE));
105      trackApi.uploadFileReference({file: _fileReference, wait: "N"}, {
106        onResponse: function(track:Object):void {
107          _id = track.id;
108          dispatchEvent(new TrackLoaderEvent(TrackLoaderEvent.FILE_UPLOADED));
109          dispatchEvent(new TrackLoaderEvent(TrackLoaderEvent.LOADING_ANALYSIS));
110          loadAnalysis({id: _id});
111        },
112        onEchoNestError: function(error:EchoNestError):void {
113          dispatchEvent(error.createEvent());
114        },
115        onError: dispatchEvent
116      });
117    }
118
119    private function loadAnalysis(parameters:Object):void {
120      _analysisLoader.addEventListener(AnalysisEvent.UNKNOWN, analysisUnknownHandler);
121      _analysisLoader.load(parameters);
122    }
123
124    private function analysisUnknownHandler(e:Event):void {
125      uploadFile();
126      dispatchEvent(e);
127    }
128  }
129}