PageRenderTime 59ms CodeModel.GetById 56ms app.highlight 1ms RepoModel.GetById 1ms app.codeStats 0ms

/flash/soundtouch-as3/examples/player/stretch.mxml

http://echo-nest-remix.googlecode.com/
Macromedia eXtensible Markup Language | 114 lines | 93 code | 21 blank | 0 comment | 0 complexity | d6b9920d38947d2396fd38615f042357 MD5 | raw file
  1<?xml version="1.0" encoding="utf-8"?>
  2
  3<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
  4  <mx:Panel title="TD Stretch Demo" width="100%" paddingTop="10" paddingRight="10" paddingBottom="10" paddingLeft="10">
  5    <mx:HBox paddingBottom="10">
  6      <mx:Button id="browseButton" label="Choose a songÂ…" click="chooseFile();"/>
  7    </mx:HBox>
  8    <mx:VBox width="100%">
  9      <mx:Label text="Tempo Adjustment"/>
 10      <mx:HSlider id="tempoSlider" minimum="0" maximum="2" value="1" snapInterval="0.01" liveDragging="false" change="updateTempo();" width="100%"/>
 11    </mx:VBox>
 12    <mx:Button id="playButton" label="Play" enabled="false" click="togglePlayPause();"/>
 13  </mx:Panel>
 14
 15  <mx:Script><![CDATA[
 16    import com.ryanberdeen.audio.SampleSourcePlayer;
 17    import com.ryanberdeen.audio.SoundSampleSource;
 18    import com.ryanberdeen.soundtouch.StretchSS;
 19
 20    import flash.events.Event;
 21    import flash.media.Sound;
 22    import flash.net.FileReference;
 23
 24    import mx.collections.ArrayCollection;
 25
 26    import org.audiofx.mp3.MP3FileReferenceLoader;
 27    import org.audiofx.mp3.MP3SoundEvent;
 28
 29    private var fileReference:FileReference;
 30    private var mp3Loader:MP3FileReferenceLoader;
 31    private var sound:Sound;
 32
 33    private var player:SampleSourcePlayer;
 34    private var playing:Boolean;
 35
 36    private var stretchSs:StretchSS;
 37
 38    private function reset():void {
 39      resetPlayer();
 40    }
 41
 42    private function resetPlayer():void {
 43      if (player != null) {
 44        player.stop();
 45
 46        player = null;
 47      }
 48    }
 49
 50    private function chooseFile():void {
 51      reset();
 52      fileReference = new FileReference();
 53      fileReference.addEventListener(Event.SELECT, function(e:Event):void {
 54        loadSound();
 55      });
 56      fileReference.browse();
 57    }
 58
 59    private function loadSound():void {
 60      mp3Loader = new MP3FileReferenceLoader();
 61      mp3Loader.addEventListener(Event.COMPLETE, handleSoundLoadComplete);
 62      mp3Loader.getSound(fileReference);
 63    }
 64
 65    private function handleSoundLoadComplete(e:MP3SoundEvent):void {
 66      mp3Loader.removeEventListener(Event.COMPLETE, handleSoundLoadComplete);
 67      sound = e.sound;
 68
 69      preparePlayer();
 70    }
 71
 72    private function preparePlayer():void {
 73      resetPlayer();
 74      player = new SampleSourcePlayer();
 75      player.addEventListener(Event.SOUND_COMPLETE, playerSoundCompleteHandler);
 76      stretchSs = new StretchSS(sound, tempoSlider.value);
 77      player.sampleSource = stretchSs;
 78      playButton.enabled = true;
 79      playButton.label = "Play";
 80      playing = false;
 81    }
 82
 83    private function playerSoundCompleteHandler(e:Event):void {
 84      preparePlayer();
 85    }
 86
 87    private function togglePlayPause():void {
 88      if (!playing) {
 89        play();
 90      }
 91      else {
 92        pause();
 93      }
 94    }
 95
 96    private function play():void {
 97      player.start();
 98      playButton.label = "Pause";
 99      playing = true;
100    }
101
102    private function pause():void {
103      player.stop();
104      playButton.label = "Play";
105      playing = false;
106    }
107
108    private function updateTempo():void {
109      if (player != null) {
110        preparePlayer();
111      }
112    }
113  ]]></mx:Script>
114</mx:Application>