I have a question regarding encryption, once again this relates to reading serial numbers from the aux:SerialNumber namespace.
When I open my file in AS3 from photos of the Canon EOS D1000, it can't read this bit in the ByteArray var i:int = p.search('<x:xmpmeta xmlns:x="\'dobe:ns:meta/\''); because well its encrypted.
What makes this interesting is, if I open the file in Photoshop then save it and close it the XMP data is correct and my app can read it.
My question would be how would I decrypt the XMP data from within AS3? Is there a way to do it. Or another way to read it?
This is the method I use:
private function getXMP(ba:ByteArray):XML
{
var LP:ByteArray = new ByteArray();
var PACKET:ByteArray = new ByteArray();
var l:int;
ba.readBytes(LP, 2, 2);
//http://www.adobe.com/devnet/xmp.html read part 3: Storage in Files.
//that will explain the -2 -29 and other things you see here.
l = LP.readInt() - 2 -29;
ba.readBytes(PACKET, 33, l);
var p:String = trim(""+PACKET);
var i:int = p.search('<x:xmpmeta xmlns:x="adobe:ns:meta/"');
//Delete all in front of the XMP XML
p = p.substr(i);
//For some reason this left some rubbish in front, so I'll hardcode it out for now
var ar:Array = p.split('<');
var s:String = "";
var q:int;
var j:int = ar.length;
for(q=1;q<j;q++)
{
s += '<'+ar[q];
}
i = s.search('</x:xmpmeta>');
i += ('</x:xmpmeta>').length;
s = s.slice(0,i);
//Delete all behind the XMP XML
//trace(s);
return XML(s);
}
Related
I would like to know is there a way we can detect audio frequency from microphone in html 5 web audio. I wish to make an online guitar tuner, and I need to have the audio frequency in hertz, from the sound input. I've seen some EQ and filters effects, but I didn't see anything about frequency recognition.
EDIT:
I found this: http://www.smartjava.org/content/exploring-html5-web-audio-visualizing-sound
The 2nd point (analyser node) is really interesting. I seen his source code, but I can't figure how to connect the analyser to the microphone input.
He calls a playSound() function when the mp3 file starts to play, and there he draws his canvas. But I do not have a playSound() like function...
I wrote a web audio library which, among other things, can detect frequency from mic input. Check it out at https://github.com/rserota/wad#pitch-detection
var voice = new Wad({source : 'mic' });
var tuner = new Wad.Poly();
tuner.add(voice);
voice.play();
tuner.updatePitch() // The tuner is now calculating the pitch and note name of its input 60 times per second. These values are stored in tuner.pitch and tuner.noteName.
var logPitch = function(){
console.log(tuner.pitch, tuner.noteName)
requestAnimationFrame(logPitch)
};
logPitch();
// If you sing into your microphone, your pitch will be logged to the console in real time.
tuner.stopUpdatingPitch(); // Stop calculating the pitch if you don't need to know it anymore.
You should be able to use BiquadFilterNode.
Example code from the link:
var audioCtx = new AudioContext();
var biquadFilter = audioCtx.createBiquadFilter();
biquadfilter.getFrequencyResponse(myFrequencyArray,magResponseOutput,phaseResponseOutput);
You can use the following code to get the frequencies from the mic.
navigator.mediaDevices.getUserMedia({audio:true}).then(function(localStream){
var audioContext = new(window.AudioContext || window.webkitAudioContext)();
var input = audioContext.createMediaStreamSource(localStream);
var analyser = audioContext.createAnalyser();
var scriptProcessor = audioContext.createScriptProcessor();
// Some analyser setup
analyser.smoothingTimeConstant = 0;
analyser.fftSize = 64;
input.connect(analyser);
analyser.connect(scriptProcessor);
scriptProcessor.connect(audioContext.destination);
var getAverageVolume = function( array){
var length = array.length;
var values = 0;
var i = 0;
for (; i < length; i++) {
values += array[i];
}
return values / length;
};
var onAudio = function(){
var tempArray = new window.Uint8Array(analyser.frequencyBinCount);
analyser.getByteFrequencyData(tempArray);
var latestFrequency = (getAverageVolume(tempArray));
//use latestFrequency
};
scriptProcessor.onaudioprocess = onAudio;
})
.catch(function(){
//Handle error
});
I'm using Web Audio for various purposes and while samples loaded via URL and oscillators are working and playing properly, building a custom source buffer is not. I have tried to load my own AudioBuffer into an AudioBufferSourceNode using the code below and through the Chrome-NetBeans debugger I can see that it's loading the buffer with data and no errors are flagged, but when start is called, no sound is ever produced. Note that I'm just filling the buffer with noise, but I plan to fill it with my own custom wave data. I realize it's likely that I'm filling the buffer with the wrong data type, but I have been unable to find any documentation or examples regarding the proper way of doing it. Any help would be appreciated.
var audioContext = new (window.AudioContext || window.webkitAudioContext)();
var frameCount = 2000;
var sampleRate = 4000;
var myBuffer = audioContext.createBuffer(2, frameCount, sampleRate);
// FILL WITH WHITE NOISE
for (var i = 0; i < frameCount; i++) {
myBuffer[i] = Math.random() * 2 - 1;
}
sourceNode = audioContext.createBufferSource();
sourceNode.buffer = myBuffer;
sourceNode.connect(audioContext.destination);
sourceNode.start(0);
This will synth your noise inside a callback method which is called everytime you have rendered yet another BUFF_SIZE number of samples
var BUFF_SIZE = 2048; // spec allows, yet do not go below 1024
var audio_context = new AudioContext();
var gain_node = audio_context.createGain();
gain_node.connect( audio_context.destination );
var source_node = audio_context.createScriptProcessor(BUFF_SIZE, 1, 1);
source_node.onaudioprocess = (function() {
return function(event) {
var synth_buff = event.outputBuffer.getChannelData(0); // mono for now
// FILL WITH WHITE NOISE
for (var i = 0, buff_size = synth_buff.length; i < buff_size; i++) {
synth_buff[i] = Math.random() * 2 - 1;
}
};
}());
source_node.connect(gain_node);
I'm using fzip to zip the contents of a folder. The folder contains 4 files of 500MB, the problem is that this uses all of my computers memory. Is there a way to prevent this? And lock the memory at 20% or something?
this is my code:
public function packageFileParser(filesToZip:Array):void {
for (var i:uint = 0; i < filesToZip.length; i++) {
if (filesToZip[i]["isDirectory"] == true) {
packageFileParser(filesToZip[i].getDirectoryListing());
} else {
if (filesToZip[i]["isHidden"] == false) {
var byteLoader:UrlURLLoader = new UrlURLLoader();
byteLoader.dataFormat = URLLoaderDataFormat.BINARY;
byteLoader.addEventListener (flash.events.Event.COMPLETE, urlLoaderCompleteHandler);
var fileRequest:URLRequest = new URLRequest ("/"+filesToZip[i]["nativePath"]);
byteLoader.load (fileRequest);
}
}
}
}
private function urlLoaderCompleteHandler(event:flash.events.Event):void {
var saveZip : Function = function(zip : FZip) : void {
var out : ByteArray = new ByteArray();
zip.serialize(out);
var fs : FileStream = new FileStream;
targetFile = File.desktopDirectory.resolvePath(zipName);
fs.open(targetFile, FileMode.WRITE);
fs.writeBytes(out);
out.clear();
fs.close();
};
var fullpath:String = event.target.urlRequest.url;
var filename:String = fullpath.substr(fullpath.lastIndexOf("/")+1,fullpath.length);
var filepath:String = fullpath.substr(0,fullpath.lastIndexOf("/")+1);
filepath = filepath.split("/Users/Thibaut/Desktop/testfolder").join("");
zip.addFile(filepath+filename, event.target.data);
saveZip(zip);
}
AFAIK, FZip library creates entire archive in memory and then saves it to disk. There's nothing you can do except to change library. Try airxzip from coltware, it served me well (I think there was no big memory loads even on large archives, because they were flushed to disk while adding files to them.)
Also, if you're using AIR capabilities (FileStream class), you should add "air" tag to the question, it changes a lot compared to plain AS3.
Is there a simple example to read/write metadata of e.g. a local .jpg?
XMPCore ActionScript Reference
Unfortunately I was not able to apply the instructions from adobe.
A simple example will help me a lot!
Thanks
I've written a small snippet on the matter. this snippet is far from being proper tested, and is most definite not written in a clear and coherent way. But for now it seems to work. I'll update as I work on it.
From http://snipplr.com/view/51037/xmp-metadata-from-jpg/
private function init(event:Event):void
{
var ldr:Loader = new Loader();
ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, imgLoaded);
var s:String = "link/to/asset.jpg";
ldr.load(new URLRequest(s));
}
private function imgLoaded(e:Event):void{
var info:LoaderInfo = e.target as LoaderInfo;
var xmpXML:XML = getXMP(info.bytes);
//trace(xmpXML);
var meta:XMPMeta = new XMPMeta(xmpXML);
}
private function trim(s:String):String{
return s.replace( /^([\s|\t|\n]+)?(.*)([\s|\t|\n]+)?$/gm, "$2" );
}
private function getXMP(ba:ByteArray):XML{
var LP:ByteArray = new ByteArray();
var PACKET:ByteArray = new ByteArray();
var l:int;
ba.readBytes(LP, 2, 2);
/*
http://www.adobe.com/devnet/xmp.html
read part 3: Storage in Files.
that will explain the -2 -29 and other things you see here.
*/
l = LP.readInt() - 2 -29;
ba.readBytes(PACKET, 33, l);
var p:String = trim(""+PACKET);
var i:int = p.search('<x:xmpmeta xmlns:x="adobe:ns:meta/"');
/* Delete all in front of the XMP XML */
p = p.substr(i);
/*
For some reason this left some rubbish in front, so I'll hardcode it out for now
TODO clean up
*/
var ar:Array = p.split('<');
var s:String = "";
var q:int;
var j:int = ar.length;
for(q=1;q<j;q++){
s += '<'+ar[q];
}
i = s.search('</x:xmpmeta>');
i += ('</x:xmpmeta>').length;
s = s.slice(0,i);
/* Delete all behind the XMP XML */
return XML(s);
}
I know how to get Flash CS4 symbols into Flash Builder via .swc.
The class names become available in the main class, but I can only instantiate those one by one, writing each name into the code.
How can I loop through the .swc and load its assets in an array without mentioning their name, then obtain and use these names for instantiation? ideally, something like (half-assed pseudocode):
the_instances: = new Array
for(i=0; i<the_SWC.length; i++)
{
tmp = new eval( the_SWC[i].name + '\(\)' )
the_instances.push( tmp )
}
or anything else to get those names in a loop.
You have at least 2 options.
Option1:
Given the fact that a SWC file is a zip file that contains a swf with the embedded assets and an xml file describing the contents, you can load the swc as a zip, get the xml and parse it.
var swcLoader:URLLoader = new URLLoader(new URLRequest('assets/assetsLib.swc'));
swcLoader.dataFormat = URLLoaderDataFormat.BINARY;
swcLoader.addEventListener(Event.COMPLETE, swcLoaded);
function swcLoaded(event:Event):void{
var zipFile:ZipFile = new ZipFile(event.target.data);
for(var i:int = 0; i < zipFile.entries.length; i++) {
var entry:ZipEntry = zipFile.entries[i];
if(entry.name == 'catalog.xml'){
var data:ByteArray = zipFile.getInput(entry);
var list:XML = new XML(zipFile.getInput(entry));
var nodes:XMLList = list.children();
for (var j:int = 0; j < nodes.length(); j++) {
if (nodes[j].name().localName == "libraries") {
var libraries:XML = nodes[j];
var libList:XMLList = libraries.children();
for(var k:int = 0 ; k < libList.length(); k++){
var library:XML = libList[k];
var classList:XMLList = library.children();
for(var l:int = 0 ; l < classList.length(); l++){
var classDef:XML = classList[l];
trace('class name: ' + classDef.#name);
//var LibClass:Class = this.loaderInfo.applicationDomain.getDefinition(classDef.#name) as Class;
}
}
}
}
}
}
}
I am using the nochump library.
Option2:
Since you only need the class names to make your like easier and you mentioned using Flash CS4(which makes me assume you have access to the .fla file generating the swc), you can write a simple jsfl script that will write that line of code for you.
var doc = fl.getDocumentDOM();
var libItems = doc.library.items;
var libItemsNum = libItems.length;
var classesString = 'var '+doc.name.substr(0,doc.name.length-4)+'Classes = [';
var classesNum = 0;
var classes = [];
fl.outputPanel.clear();
for(var i = 0 ; i < libItemsNum; i++){
if(libItems[i].linkageExportForAS){
classes[classesNum] = libItems[i].linkageClassName;
classesNum++;
}
}
for(i = 0; i < classesNum; i++){
if(i < classesNum-1) classesString += '"'+classes[i]+'",';
else classesString += '"'+classes[i]+'"];';
}
fl.clipCopyString(classesString);
fl.trace(classesString);
All you need to do is:
File > New > Flash Javascript File and paste the code.
Save it in the Commands folder with a descriptive name, like: listExportClasses.
Since it's in the Commands menu, if you use this often enough you could add a keyboard shortcut.
What the command will is generate an array with the name of the fla file and contain the exported classes' names and conveniently place it in your clipboard.
e.g
var assetsLibClasses = ["Start1","Start2","Start3","Button","ColorBox","GameBackground","MenuBackground"];