Manipulating MP3 speed and direction
Here is one of those examples that I did just for the hell of it. I often wondered how hard it would be to play an MP3 in reverse. It turns out that it is really easy with Flash Player 10. Now my implementation of reverse is pretty weak and I’m sure guys like Andre Michelle could make something that sounded perfect. Click the button below to try it out. You can reverse direction and speed it up.
You surely heard the jerkiness and little glitches and that could surely be corrected with more work. All of this is possible thanks to the new Sound.extract() method. This allows you to grab the bytes from a loaded sound and then manipulate them before passing them to the sound card. You tell the method how many sound samples you want to extract and what position to start from in the file. It is this position property that allows you to move in any direction in the file. Subtracting a number makes it go in reverse and adding to it moves the music forward.
Now one thing that screwed me up at first is the fact that the extract method deals with samples, not bytes. A sample is 8 total bytes of audio data with 4 bytes being for each channel. Another thing that is tricky is getting the total number of samples in a file. I used the method of multiplying 44100 by the number of seconds in the file. Obviously it will only work on files set at that sample rate and it isn’t foolproof. But this method could be useful to allow people to scrub back and forth through an audio file.
The code is shown below and you can also download the FLA. I created in Flash but you could easily dump this into Flash Builder as well. I’d be interested to hear if someone gets it sounding totally smooth. The real trick would be extract a chunk of samples and then reverse the bytes of the samples. I tried that but couldn’t figure it out.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | var ex:Sound; var ns:Sound; var position:int; var max:int; var speed:int = 4096; c.b1.addEventListener(MouseEvent.CLICK, function(){speed=-8192;}); c.b2.addEventListener(MouseEvent.CLICK, function(){speed=-4096;}); c.b3.addEventListener(MouseEvent.CLICK, function(){speed=4096;}); c.b4.addEventListener(MouseEvent.CLICK, function(){speed=8192;}); lclip.visible = false; c.visible = false; loadmusic.addEventListener(MouseEvent.CLICK, init); function init(e:Event):void { position = 0; loadmusic.visible = false; lclip.visible = true; ex = new Sound(new URLRequest("col.mp3")); ex.addEventListener(Event.COMPLETE, onComplete); ns = new Sound(); ns.addEventListener(SampleDataEvent.SAMPLE_DATA, onData); } function onComplete(e:Event):void { lclip.visible = false; c.visible = true; max = 44100 * (ex.length/1000) - 4096; position = max; ns.play(); } function onData(e:SampleDataEvent):void { var bytes:ByteArray = new ByteArray(); ex.extract(bytes, 4096, position); e.data.writeBytes(bytes); position += speed; if(position < 0) position = max; else if(position > max) position = 0; } |














