Sound In Image
read more on
http://blog.yoz.sk/2011/10/a-sound-in-image-experiment/
/**
* Copyright jozefchutka ( http://wonderfl.net/user/jozefchutka )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/2rEF
*/
/* read more on
http://blog.yoz.sk/2011/10/a-sound-in-image-experiment/
*/
package
{
import flash.system.LoaderContext;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Loader;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.SampleDataEvent;
import flash.media.Sound;
import flash.media.SoundLoaderContext;
import flash.net.FileReference;
import flash.net.URLRequest;
import flash.utils.ByteArray;
[SWF(backgroundColor="#000000")]
public class TestAS extends Sprite
{
public function TestAS():void
{
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
//var context:SoundLoaderContext = new SoundLoaderContext(1000, true);
//var request:URLRequest = new URLRequest("assets/spring.mp3");
//var sound:Sound = new Sound(request, context);
//sound.addEventListener(Event.COMPLETE, onSoundComplete);
var loader:Loader = new Loader;
loader.load(
new URLRequest('http://blog.yoz.sk/examples/soundInImage/soundInImage.png'),
new LoaderContext(true)
);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, function(event:Event):void
{
playBitmapData(Bitmap(loader.content).bitmapData);
});
addChild(loader);
}
/*
private function onSoundComplete(event:Event):void
{
var sound:Sound = Sound(event.currentTarget);
var bytes:ByteArray = new ByteArray;
var totalSamples:Number = sound.length * 44.1;
sound.extract(bytes, 4800 * 44.1, 44.1 * 1000);
var bitmap:Bitmap = new Bitmap;
bitmap.bitmapData = imageVectorToBitmapData(bytesToImageVector(bytes), 465);
addChild(bitmap);
stage.addEventListener(MouseEvent.CLICK, function(event:Event):void
{
saveBitmapData(bitmap.bitmapData);
});
playBitmapData(bitmap.bitmapData);
}
private static function bytesToImageVector(bytes:ByteArray):Vector.<uint>
{
var length:uint = bytes.length >> 3;
var data:Vector.<uint> = new Vector.<uint>();
bytes.position = 0;
for(var i:int = 0; i < length; i++)
{
var l:uint = (bytes.readFloat() * .5 + .5) * uint.MAX_VALUE;
var r:uint = (bytes.readFloat() * .5 + .5) * uint.MAX_VALUE;
data.push(l & 0xffff0000 | r >> 16 & 0xffff);
}
return data;
}
private static function imageVectorToBitmapData(data:Vector.<uint>, width:uint):BitmapData
{
var height:uint = Math.ceil(data.length / width);
trace("image size:", width, height);
var bitmapData:BitmapData = new BitmapData(width, height, true);
data.length = width * height;
bitmapData.setVector(bitmapData.rect, data);
return bitmapData;
}
*/
private static function playBitmapData(bitmapData:BitmapData):void
{
var sound:Sound = new Sound;
var data:Vector.<uint> = bitmapData.getVector(bitmapData.rect);
sound.addEventListener(SampleDataEvent.SAMPLE_DATA, function(event:SampleDataEvent):void
{
var start:uint = event.position;
if(start >= data.length)
return;
var end:uint = Math.min(start + 8192, data.length);
for(var i:uint = start; i < end; i++)
{
var l:uint = data[i] & 0xffff0000 | 0xffff;
var r:uint = data[i] & 0xffff << 16;
event.data.writeFloat(l / uint.MAX_VALUE * 2 - 1);
event.data.writeFloat(r / uint.MAX_VALUE * 2 - 1);
}
});
sound.play();
}
/*
private function saveBitmapData(bitmapData:BitmapData):void
{
var png:ByteArray = PNG24Encoder.encode(bitmapData);
var fileReference:FileReference = new FileReference;
fileReference.save(png, "soundInImage.png");
}
*/
}
}